blob: dfbf8ce3d2ff5fd4bbbee398115849abebd4197d [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
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "VSyncWorker.h"
Sean Paul4057be32015-05-13 06:23:09 -070020
Roman Stratiienko13cc3662020-08-29 21:35:39 +030021#include <log/log.h>
Sean Paul4057be32015-05-13 06:23:09 -070022#include <stdlib.h>
23#include <time.h>
24#include <xf86drm.h>
25#include <xf86drmMode.h>
26
Sean Paul4057be32015-05-13 06:23:09 -070027namespace android {
28
29VSyncWorker::VSyncWorker()
30 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
31 drm_(NULL),
Sean Paul4057be32015-05-13 06:23:09 -070032 display_(-1),
Alexandru Gheorghe976f69a2018-04-11 16:22:12 +010033 enabled_(false),
Sean Paul4057be32015-05-13 06:23:09 -070034 last_timestamp_(-1) {
35}
36
37VSyncWorker::~VSyncWorker() {
38}
39
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010040int VSyncWorker::Init(DrmDevice *drm, int display) {
Sean Paul4057be32015-05-13 06:23:09 -070041 drm_ = drm;
42 display_ = display;
43
44 return InitWorker();
45}
46
Adrian Salidofa37f672017-02-16 10:29:46 -080047void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
48 Lock();
Sean Paula5df1de2016-02-10 10:00:08 -080049 callback_ = callback;
Adrian Salidofa37f672017-02-16 10:29:46 -080050 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070051}
52
Adrian Salidofa37f672017-02-16 10:29:46 -080053void VSyncWorker::VSyncControl(bool enabled) {
54 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070055 enabled_ = enabled;
56 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080057 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070058
Adrian Salidofa37f672017-02-16 10:29:46 -080059 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070060}
61
62/*
63 * Returns the timestamp of the next vsync in phase with last_timestamp_.
64 * For example:
65 * last_timestamp_ = 137
66 * frame_ns = 50
67 * current = 683
68 *
69 * ret = (50 * ((683 - 137)/50 + 1)) + 137
70 * ret = 687
71 *
72 * Thus, we must sleep until timestamp 687 to maintain phase with the last
73 * timestamp.
74 */
75int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
76 if (last_timestamp_ < 0)
77 return current + frame_ns;
78
79 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
80 last_timestamp_;
81}
82
83static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
84
85int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
86 struct timespec vsync;
87 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
88
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070089 float refresh = 60.0f; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070090 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070091 if (conn && conn->active_mode().v_refresh() != 0.0f)
Sean Paul4057be32015-05-13 06:23:09 -070092 refresh = conn->active_mode().v_refresh();
93 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070094 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
95 conn ? conn->active_mode().v_refresh() : 0.0f);
Sean Paul4057be32015-05-13 06:23:09 -070096
Sean Paulf72cccd2018-08-27 13:59:08 -040097 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh,
98 vsync.tv_sec * kOneSecondNs +
99 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -0700100 vsync.tv_sec = phased_timestamp / kOneSecondNs;
101 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
102 do {
103 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
104 } while (ret == -1 && errno == EINTR);
105 if (ret)
106 return ret;
107
108 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
109 return 0;
110}
111
112void VSyncWorker::Routine() {
Adrian Salidofa37f672017-02-16 10:29:46 -0800113 int ret;
Sean Paul4057be32015-05-13 06:23:09 -0700114
Adrian Salidofa37f672017-02-16 10:29:46 -0800115 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700116 if (!enabled_) {
117 ret = WaitForSignalOrExitLocked();
118 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100119 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700120 return;
121 }
122 }
123
Sean Paul4057be32015-05-13 06:23:09 -0700124 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800125 std::shared_ptr<VsyncCallback> callback(callback_);
Adrian Salidofa37f672017-02-16 10:29:46 -0800126 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700127
Sean Paul4057be32015-05-13 06:23:09 -0700128 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
129 if (!crtc) {
130 ALOGE("Failed to get crtc for display");
131 return;
132 }
133 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
134
135 drmVBlank vblank;
136 memset(&vblank, 0, sizeof(vblank));
137 vblank.request.type = (drmVBlankSeqType)(
138 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
139 vblank.request.sequence = 1;
140
141 int64_t timestamp;
142 ret = drmWaitVBlank(drm_->fd(), &vblank);
143 if (ret == -EINTR) {
144 return;
145 } else if (ret) {
146 ret = SyntheticWaitVBlank(&timestamp);
147 if (ret)
148 return;
149 } else {
150 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
151 (int64_t)vblank.reply.tval_usec * 1000;
152 }
153
154 /*
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200155 * VSync could be disabled during routine execution so it could potentially
156 * lead to crash since callback's inner hook could be invalid anymore. We have
157 * no control over lifetime of this hook, therefore we can't rely that it'll
158 * be valid after vsync disabling.
159 *
160 * Blocking VSyncControl to wait until routine
161 * will finish execution is logically correct way to fix this issue, but it
162 * creates visible lags and stutters, so we have to resort to other ways of
163 * mitigating this issue.
164 *
165 * Doing check before attempt to invoke callback drastically shortens the
166 * window when such situation could happen and that allows us to practically
167 * avoid this issue.
168 *
169 * Please note that issue described below is different one and it is related
170 * to RegisterCallback, not to disabling vsync via VSyncControl.
171 */
172 if (!enabled_)
173 return;
174 /*
Sean Paula5df1de2016-02-10 10:00:08 -0800175 * There's a race here where a change in callback_ will not take effect until
Sean Paul4057be32015-05-13 06:23:09 -0700176 * the next subsequent requested vsync. This is unavoidable since we can't
177 * call the vsync hook while holding the thread lock.
178 *
Sean Paula5df1de2016-02-10 10:00:08 -0800179 * We could shorten the race window by caching callback_ right before calling
180 * the hook. However, in practice, callback_ is only updated once, so it's not
Sean Paul4057be32015-05-13 06:23:09 -0700181 * worth the overhead.
182 */
Sean Paula5df1de2016-02-10 10:00:08 -0800183 if (callback)
184 callback->Callback(display, timestamp);
Sean Paul4057be32015-05-13 06:23:09 -0700185 last_timestamp_ = timestamp;
186}
Sean Paulf72cccd2018-08-27 13:59:08 -0400187} // namespace android