blob: cc9c96b794ef2242302324a03632cbfce4d8ba34 [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
Sean Paula5df1de2016-02-10 10:00:08 -080051int VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
Sean Paul4057be32015-05-13 06:23:09 -070052 int ret = Lock();
53 if (ret) {
54 ALOGE("Failed to lock vsync worker lock %d\n", ret);
55 return ret;
56 }
57
Sean Paula5df1de2016-02-10 10:00:08 -080058 callback_ = callback;
Sean Paul4057be32015-05-13 06:23:09 -070059
60 ret = Unlock();
61 if (ret) {
62 ALOGE("Failed to unlock vsync worker lock %d\n", ret);
63 return ret;
64 }
65 return 0;
66}
67
68int VSyncWorker::VSyncControl(bool enabled) {
69 int ret = Lock();
70 if (ret) {
71 ALOGE("Failed to lock vsync worker lock %d\n", ret);
72 return ret;
73 }
74
75 enabled_ = enabled;
76 last_timestamp_ = -1;
77 int signal_ret = SignalLocked();
78
79 ret = Unlock();
80 if (ret) {
81 ALOGE("Failed to unlock vsync worker lock %d\n", ret);
82 return ret;
83 }
84
85 return signal_ret;
86}
87
88/*
89 * Returns the timestamp of the next vsync in phase with last_timestamp_.
90 * For example:
91 * last_timestamp_ = 137
92 * frame_ns = 50
93 * current = 683
94 *
95 * ret = (50 * ((683 - 137)/50 + 1)) + 137
96 * ret = 687
97 *
98 * Thus, we must sleep until timestamp 687 to maintain phase with the last
99 * timestamp.
100 */
101int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
102 if (last_timestamp_ < 0)
103 return current + frame_ns;
104
105 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
106 last_timestamp_;
107}
108
109static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
110
111int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
112 struct timespec vsync;
113 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
114
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700115 float refresh = 60.0f; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -0700116 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700117 if (conn && conn->active_mode().v_refresh() != 0.0f)
Sean Paul4057be32015-05-13 06:23:09 -0700118 refresh = conn->active_mode().v_refresh();
119 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700120 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
121 conn ? conn->active_mode().v_refresh() : 0.0f);
Sean Paul4057be32015-05-13 06:23:09 -0700122
123 int64_t phased_timestamp = GetPhasedVSync(
124 kOneSecondNs / refresh, vsync.tv_sec * kOneSecondNs + vsync.tv_nsec);
125 vsync.tv_sec = phased_timestamp / kOneSecondNs;
126 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
127 do {
128 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
129 } while (ret == -1 && errno == EINTR);
130 if (ret)
131 return ret;
132
133 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
134 return 0;
135}
136
137void VSyncWorker::Routine() {
138 int ret = Lock();
139 if (ret) {
140 ALOGE("Failed to lock worker %d", ret);
141 return;
142 }
143
144 if (!enabled_) {
145 ret = WaitForSignalOrExitLocked();
146 if (ret == -EINTR) {
147 return;
148 }
149 }
150
151 bool enabled = enabled_;
152 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800153 std::shared_ptr<VsyncCallback> callback(callback_);
Sean Paul4057be32015-05-13 06:23:09 -0700154
155 ret = Unlock();
156 if (ret) {
157 ALOGE("Failed to unlock worker %d", ret);
158 }
159
160 if (!enabled)
161 return;
162
163 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
164 if (!crtc) {
165 ALOGE("Failed to get crtc for display");
166 return;
167 }
168 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
169
170 drmVBlank vblank;
171 memset(&vblank, 0, sizeof(vblank));
172 vblank.request.type = (drmVBlankSeqType)(
173 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
174 vblank.request.sequence = 1;
175
176 int64_t timestamp;
177 ret = drmWaitVBlank(drm_->fd(), &vblank);
178 if (ret == -EINTR) {
179 return;
180 } else if (ret) {
181 ret = SyntheticWaitVBlank(&timestamp);
182 if (ret)
183 return;
184 } else {
185 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
186 (int64_t)vblank.reply.tval_usec * 1000;
187 }
188
189 /*
Sean Paula5df1de2016-02-10 10:00:08 -0800190 * There's a race here where a change in callback_ will not take effect until
Sean Paul4057be32015-05-13 06:23:09 -0700191 * the next subsequent requested vsync. This is unavoidable since we can't
192 * call the vsync hook while holding the thread lock.
193 *
Sean Paula5df1de2016-02-10 10:00:08 -0800194 * We could shorten the race window by caching callback_ right before calling
195 * the hook. However, in practice, callback_ is only updated once, so it's not
Sean Paul4057be32015-05-13 06:23:09 -0700196 * worth the overhead.
197 */
Sean Paula5df1de2016-02-10 10:00:08 -0800198 if (callback)
199 callback->Callback(display, timestamp);
Sean Paul4057be32015-05-13 06:23:09 -0700200 last_timestamp_ = timestamp;
201}
202}