blob: e9f4f9c09026bf73e9adeff2aaf2233fddd6bb2d [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
Sean Paul4057be32015-05-13 06:23:09 -070021#include <xf86drm.h>
22#include <xf86drmMode.h>
23
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cstdlib>
25#include <ctime>
26
Roman Stratiienkod518a052021-02-25 19:15:14 +020027#include "utils/log.h"
28
Sean Paul4057be32015-05-13 06:23:09 -070029namespace android {
30
31VSyncWorker::VSyncWorker()
32 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020033 drm_(nullptr),
Sean Paul4057be32015-05-13 06:23:09 -070034 display_(-1),
Alexandru Gheorghe976f69a2018-04-11 16:22:12 +010035 enabled_(false),
Sean Paul4057be32015-05-13 06:23:09 -070036 last_timestamp_(-1) {
37}
38
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010039int VSyncWorker::Init(DrmDevice *drm, int display) {
Sean Paul4057be32015-05-13 06:23:09 -070040 drm_ = drm;
41 display_ = display;
42
43 return InitWorker();
44}
45
Adrian Salidofa37f672017-02-16 10:29:46 -080046void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
47 Lock();
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020048 callback_ = std::move(callback);
Adrian Salidofa37f672017-02-16 10:29:46 -080049 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070050}
51
Roman Stratiienko23701092020-09-26 02:08:41 +030052void VSyncWorker::RegisterClientCallback(hwc2_callback_data_t data,
53 hwc2_function_pointer_t hook) {
54 Lock();
55 vsync_callback_data_ = data;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020056 vsync_callback_hook_ = (HWC2_PFN_VSYNC)hook;
Roman Stratiienko23701092020-09-26 02:08:41 +030057 Unlock();
58}
59
Adrian Salidofa37f672017-02-16 10:29:46 -080060void VSyncWorker::VSyncControl(bool enabled) {
61 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070062 enabled_ = enabled;
63 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080064 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070065
Adrian Salidofa37f672017-02-16 10:29:46 -080066 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070067}
68
69/*
70 * Returns the timestamp of the next vsync in phase with last_timestamp_.
71 * For example:
72 * last_timestamp_ = 137
73 * frame_ns = 50
74 * current = 683
75 *
76 * ret = (50 * ((683 - 137)/50 + 1)) + 137
77 * ret = 687
78 *
79 * Thus, we must sleep until timestamp 687 to maintain phase with the last
80 * timestamp.
81 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020082int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -070083 if (last_timestamp_ < 0)
84 return current + frame_ns;
85
86 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
87 last_timestamp_;
88}
89
90static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
91
92int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020093 struct timespec vsync {};
Sean Paul4057be32015-05-13 06:23:09 -070094 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020095 if (ret)
96 return ret;
Sean Paul4057be32015-05-13 06:23:09 -070097
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020098 float refresh = 60.0F; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070099 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200100 if (conn && conn->active_mode().v_refresh() != 0.0F)
Sean Paul4057be32015-05-13 06:23:09 -0700101 refresh = conn->active_mode().v_refresh();
102 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700103 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200104 conn ? conn->active_mode().v_refresh() : 0.0F);
Sean Paul4057be32015-05-13 06:23:09 -0700105
Sean Paulf72cccd2018-08-27 13:59:08 -0400106 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh,
107 vsync.tv_sec * kOneSecondNs +
108 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -0700109 vsync.tv_sec = phased_timestamp / kOneSecondNs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200110 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Sean Paul4057be32015-05-13 06:23:09 -0700111 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200112 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Sean Paul4057be32015-05-13 06:23:09 -0700113 } while (ret == -1 && errno == EINTR);
114 if (ret)
115 return ret;
116
117 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
118 return 0;
119}
120
121void VSyncWorker::Routine() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200122 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700123
Adrian Salidofa37f672017-02-16 10:29:46 -0800124 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700125 if (!enabled_) {
126 ret = WaitForSignalOrExitLocked();
127 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100128 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700129 return;
130 }
131 }
132
Sean Paul4057be32015-05-13 06:23:09 -0700133 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800134 std::shared_ptr<VsyncCallback> callback(callback_);
Adrian Salidofa37f672017-02-16 10:29:46 -0800135 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700136
Sean Paul4057be32015-05-13 06:23:09 -0700137 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
138 if (!crtc) {
139 ALOGE("Failed to get crtc for display");
140 return;
141 }
142 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
143
144 drmVBlank vblank;
145 memset(&vblank, 0, sizeof(vblank));
146 vblank.request.type = (drmVBlankSeqType)(
147 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
148 vblank.request.sequence = 1;
149
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200150 int64_t timestamp = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700151 ret = drmWaitVBlank(drm_->fd(), &vblank);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200152 if (ret == -EINTR)
Sean Paul4057be32015-05-13 06:23:09 -0700153 return;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200154
155 if (ret) {
Sean Paul4057be32015-05-13 06:23:09 -0700156 ret = SyntheticWaitVBlank(&timestamp);
157 if (ret)
158 return;
159 } else {
160 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
161 (int64_t)vblank.reply.tval_usec * 1000;
162 }
163
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200164 if (!enabled_)
165 return;
Roman Stratiienko23701092020-09-26 02:08:41 +0300166
Sean Paula5df1de2016-02-10 10:00:08 -0800167 if (callback)
168 callback->Callback(display, timestamp);
Roman Stratiienko23701092020-09-26 02:08:41 +0300169
170 Lock();
171 if (enabled_ && vsync_callback_hook_ && vsync_callback_data_)
172 vsync_callback_hook_(vsync_callback_data_, display, timestamp);
173 Unlock();
174
Sean Paul4057be32015-05-13 06:23:09 -0700175 last_timestamp_ = timestamp;
176}
Sean Paulf72cccd2018-08-27 13:59:08 -0400177} // namespace android