blob: 3ad16fe72ebc7cdd27c2c26ad899d7eba0258518 [file] [log] [blame]
Sean Paul4057be32015-05-13 06:23:09 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "hwc-vsync-worker"
18
19#include "drmresources.h"
20#include "vsyncworker.h"
21#include "worker.h"
22
23#include <map>
24#include <stdlib.h>
25#include <time.h>
26#include <xf86drm.h>
27#include <xf86drmMode.h>
28
29#include <cutils/log.h>
30#include <hardware/hardware.h>
31
32namespace android {
33
34VSyncWorker::VSyncWorker()
35 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
36 drm_(NULL),
Sean Paul4057be32015-05-13 06:23:09 -070037 display_(-1),
38 last_timestamp_(-1) {
39}
40
41VSyncWorker::~VSyncWorker() {
42}
43
44int VSyncWorker::Init(DrmResources *drm, int display) {
45 drm_ = drm;
46 display_ = display;
47
48 return InitWorker();
49}
50
Adrian Salidofa37f672017-02-16 10:29:46 -080051void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
52 Lock();
Sean Paula5df1de2016-02-10 10:00:08 -080053 callback_ = callback;
Adrian Salidofa37f672017-02-16 10:29:46 -080054 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070055}
56
Adrian Salidofa37f672017-02-16 10:29:46 -080057void VSyncWorker::VSyncControl(bool enabled) {
58 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070059 enabled_ = enabled;
60 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080061 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070062
Adrian Salidofa37f672017-02-16 10:29:46 -080063 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070064}
65
66/*
67 * Returns the timestamp of the next vsync in phase with last_timestamp_.
68 * For example:
69 * last_timestamp_ = 137
70 * frame_ns = 50
71 * current = 683
72 *
73 * ret = (50 * ((683 - 137)/50 + 1)) + 137
74 * ret = 687
75 *
76 * Thus, we must sleep until timestamp 687 to maintain phase with the last
77 * timestamp.
78 */
79int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
80 if (last_timestamp_ < 0)
81 return current + frame_ns;
82
83 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
84 last_timestamp_;
85}
86
87static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
88
89int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
90 struct timespec vsync;
91 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
92
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070093 float refresh = 60.0f; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070094 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070095 if (conn && conn->active_mode().v_refresh() != 0.0f)
Sean Paul4057be32015-05-13 06:23:09 -070096 refresh = conn->active_mode().v_refresh();
97 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070098 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
99 conn ? conn->active_mode().v_refresh() : 0.0f);
Sean Paul4057be32015-05-13 06:23:09 -0700100
101 int64_t phased_timestamp = GetPhasedVSync(
102 kOneSecondNs / refresh, vsync.tv_sec * kOneSecondNs + vsync.tv_nsec);
103 vsync.tv_sec = phased_timestamp / kOneSecondNs;
104 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
105 do {
106 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
107 } while (ret == -1 && errno == EINTR);
108 if (ret)
109 return ret;
110
111 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
112 return 0;
113}
114
115void VSyncWorker::Routine() {
Adrian Salidofa37f672017-02-16 10:29:46 -0800116 int ret;
Sean Paul4057be32015-05-13 06:23:09 -0700117
Adrian Salidofa37f672017-02-16 10:29:46 -0800118 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700119 if (!enabled_) {
120 ret = WaitForSignalOrExitLocked();
121 if (ret == -EINTR) {
122 return;
123 }
124 }
125
126 bool enabled = enabled_;
127 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800128 std::shared_ptr<VsyncCallback> callback(callback_);
Adrian Salidofa37f672017-02-16 10:29:46 -0800129 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700130
131 if (!enabled)
132 return;
133
134 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
135 if (!crtc) {
136 ALOGE("Failed to get crtc for display");
137 return;
138 }
139 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
140
141 drmVBlank vblank;
142 memset(&vblank, 0, sizeof(vblank));
143 vblank.request.type = (drmVBlankSeqType)(
144 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
145 vblank.request.sequence = 1;
146
147 int64_t timestamp;
148 ret = drmWaitVBlank(drm_->fd(), &vblank);
149 if (ret == -EINTR) {
150 return;
151 } else if (ret) {
152 ret = SyntheticWaitVBlank(&timestamp);
153 if (ret)
154 return;
155 } else {
156 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
157 (int64_t)vblank.reply.tval_usec * 1000;
158 }
159
160 /*
Sean Paula5df1de2016-02-10 10:00:08 -0800161 * There's a race here where a change in callback_ will not take effect until
Sean Paul4057be32015-05-13 06:23:09 -0700162 * the next subsequent requested vsync. This is unavoidable since we can't
163 * call the vsync hook while holding the thread lock.
164 *
Sean Paula5df1de2016-02-10 10:00:08 -0800165 * We could shorten the race window by caching callback_ right before calling
166 * the hook. However, in practice, callback_ is only updated once, so it's not
Sean Paul4057be32015-05-13 06:23:09 -0700167 * worth the overhead.
168 */
Sean Paula5df1de2016-02-10 10:00:08 -0800169 if (callback)
170 callback->Callback(display, timestamp);
Sean Paul4057be32015-05-13 06:23:09 -0700171 last_timestamp_ = timestamp;
172}
173}