blob: 55dbd262819ba8b9e523dd3ff2d940695081f6e4 [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
Sean Paul4057be32015-05-13 06:23:09 -070019#include "vsyncworker.h"
Sean Paul4057be32015-05-13 06:23:09 -070020
Sean Paul4057be32015-05-13 06:23:09 -070021#include <stdlib.h>
22#include <time.h>
23#include <xf86drm.h>
24#include <xf86drmMode.h>
25
Sean Paulf72cccd2018-08-27 13:59:08 -040026#include <log/log.h>
Sean Paul4057be32015-05-13 06:23:09 -070027
28namespace android {
29
30VSyncWorker::VSyncWorker()
31 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
32 drm_(NULL),
Sean Paul4057be32015-05-13 06:23:09 -070033 display_(-1),
Alexandru Gheorghe976f69a2018-04-11 16:22:12 +010034 enabled_(false),
Sean Paul4057be32015-05-13 06:23:09 -070035 last_timestamp_(-1) {
36}
37
38VSyncWorker::~VSyncWorker() {
39}
40
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010041int VSyncWorker::Init(DrmDevice *drm, int display) {
Sean Paul4057be32015-05-13 06:23:09 -070042 drm_ = drm;
43 display_ = display;
44
45 return InitWorker();
46}
47
Adrian Salidofa37f672017-02-16 10:29:46 -080048void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
49 Lock();
Sean Paula5df1de2016-02-10 10:00:08 -080050 callback_ = callback;
Adrian Salidofa37f672017-02-16 10:29:46 -080051 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070052}
53
Adrian Salidofa37f672017-02-16 10:29:46 -080054void VSyncWorker::VSyncControl(bool enabled) {
55 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070056 enabled_ = enabled;
57 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080058 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070059
Adrian Salidofa37f672017-02-16 10:29:46 -080060 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070061}
62
63/*
64 * Returns the timestamp of the next vsync in phase with last_timestamp_.
65 * For example:
66 * last_timestamp_ = 137
67 * frame_ns = 50
68 * current = 683
69 *
70 * ret = (50 * ((683 - 137)/50 + 1)) + 137
71 * ret = 687
72 *
73 * Thus, we must sleep until timestamp 687 to maintain phase with the last
74 * timestamp.
75 */
76int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
77 if (last_timestamp_ < 0)
78 return current + frame_ns;
79
80 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
81 last_timestamp_;
82}
83
84static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
85
86int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
87 struct timespec vsync;
88 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
89
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070090 float refresh = 60.0f; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070091 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070092 if (conn && conn->active_mode().v_refresh() != 0.0f)
Sean Paul4057be32015-05-13 06:23:09 -070093 refresh = conn->active_mode().v_refresh();
94 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070095 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
96 conn ? conn->active_mode().v_refresh() : 0.0f);
Sean Paul4057be32015-05-13 06:23:09 -070097
Sean Paulf72cccd2018-08-27 13:59:08 -040098 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh,
99 vsync.tv_sec * kOneSecondNs +
100 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -0700101 vsync.tv_sec = phased_timestamp / kOneSecondNs;
102 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
103 do {
104 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
105 } while (ret == -1 && errno == EINTR);
106 if (ret)
107 return ret;
108
109 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
110 return 0;
111}
112
113void VSyncWorker::Routine() {
Adrian Salidofa37f672017-02-16 10:29:46 -0800114 int ret;
Sean Paul4057be32015-05-13 06:23:09 -0700115
Adrian Salidofa37f672017-02-16 10:29:46 -0800116 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700117 if (!enabled_) {
118 ret = WaitForSignalOrExitLocked();
119 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100120 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700121 return;
122 }
123 }
124
Sean Paul4057be32015-05-13 06:23:09 -0700125 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800126 std::shared_ptr<VsyncCallback> callback(callback_);
Adrian Salidofa37f672017-02-16 10:29:46 -0800127 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700128
Sean Paul4057be32015-05-13 06:23:09 -0700129 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
130 if (!crtc) {
131 ALOGE("Failed to get crtc for display");
132 return;
133 }
134 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
135
136 drmVBlank vblank;
137 memset(&vblank, 0, sizeof(vblank));
138 vblank.request.type = (drmVBlankSeqType)(
139 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
140 vblank.request.sequence = 1;
141
142 int64_t timestamp;
143 ret = drmWaitVBlank(drm_->fd(), &vblank);
144 if (ret == -EINTR) {
145 return;
146 } else if (ret) {
147 ret = SyntheticWaitVBlank(&timestamp);
148 if (ret)
149 return;
150 } else {
151 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
152 (int64_t)vblank.reply.tval_usec * 1000;
153 }
154
155 /*
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200156 * VSync could be disabled during routine execution so it could potentially
157 * lead to crash since callback's inner hook could be invalid anymore. We have
158 * no control over lifetime of this hook, therefore we can't rely that it'll
159 * be valid after vsync disabling.
160 *
161 * Blocking VSyncControl to wait until routine
162 * will finish execution is logically correct way to fix this issue, but it
163 * creates visible lags and stutters, so we have to resort to other ways of
164 * mitigating this issue.
165 *
166 * Doing check before attempt to invoke callback drastically shortens the
167 * window when such situation could happen and that allows us to practically
168 * avoid this issue.
169 *
170 * Please note that issue described below is different one and it is related
171 * to RegisterCallback, not to disabling vsync via VSyncControl.
172 */
173 if (!enabled_)
174 return;
175 /*
Sean Paula5df1de2016-02-10 10:00:08 -0800176 * There's a race here where a change in callback_ will not take effect until
Sean Paul4057be32015-05-13 06:23:09 -0700177 * the next subsequent requested vsync. This is unavoidable since we can't
178 * call the vsync hook while holding the thread lock.
179 *
Sean Paula5df1de2016-02-10 10:00:08 -0800180 * We could shorten the race window by caching callback_ right before calling
181 * the hook. However, in practice, callback_ is only updated once, so it's not
Sean Paul4057be32015-05-13 06:23:09 -0700182 * worth the overhead.
183 */
Sean Paula5df1de2016-02-10 10:00:08 -0800184 if (callback)
185 callback->Callback(display, timestamp);
Sean Paul4057be32015-05-13 06:23:09 -0700186 last_timestamp_ = timestamp;
187}
Sean Paulf72cccd2018-08-27 13:59:08 -0400188} // namespace android