blob: 25eeeab746b6587ef4cb4941743c45db38f777aa [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>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020025#include <cstring>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <ctime>
27
Roman Stratiienkod518a052021-02-25 19:15:14 +020028#include "utils/log.h"
29
Sean Paul4057be32015-05-13 06:23:09 -070030namespace android {
31
32VSyncWorker::VSyncWorker()
33 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020034 drm_(nullptr),
Sean Paul4057be32015-05-13 06:23:09 -070035 display_(-1),
Alexandru Gheorghe976f69a2018-04-11 16:22:12 +010036 enabled_(false),
Sean Paul4057be32015-05-13 06:23:09 -070037 last_timestamp_(-1) {
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();
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020049 callback_ = std::move(callback);
Adrian Salidofa37f672017-02-16 10:29:46 -080050 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070051}
52
Roman Stratiienko23701092020-09-26 02:08:41 +030053void VSyncWorker::RegisterClientCallback(hwc2_callback_data_t data,
54 hwc2_function_pointer_t hook) {
55 Lock();
56 vsync_callback_data_ = data;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020057 vsync_callback_hook_ = (HWC2_PFN_VSYNC)hook;
Roman Stratiienko23701092020-09-26 02:08:41 +030058 Unlock();
59}
60
Adrian Salidofa37f672017-02-16 10:29:46 -080061void VSyncWorker::VSyncControl(bool enabled) {
62 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070063 enabled_ = enabled;
64 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080065 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070066
Adrian Salidofa37f672017-02-16 10:29:46 -080067 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070068}
69
70/*
71 * Returns the timestamp of the next vsync in phase with last_timestamp_.
72 * For example:
73 * last_timestamp_ = 137
74 * frame_ns = 50
75 * current = 683
76 *
77 * ret = (50 * ((683 - 137)/50 + 1)) + 137
78 * ret = 687
79 *
80 * Thus, we must sleep until timestamp 687 to maintain phase with the last
81 * timestamp.
82 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020083int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -070084 if (last_timestamp_ < 0)
85 return current + frame_ns;
86
87 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
88 last_timestamp_;
89}
90
91static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
92
93int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020094 struct timespec vsync {};
Sean Paul4057be32015-05-13 06:23:09 -070095 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020096 if (ret)
97 return ret;
Sean Paul4057be32015-05-13 06:23:09 -070098
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020099 float refresh = 60.0F; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -0700100 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200101 if (conn && conn->active_mode().v_refresh() != 0.0F)
Sean Paul4057be32015-05-13 06:23:09 -0700102 refresh = conn->active_mode().v_refresh();
103 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700104 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200105 conn ? conn->active_mode().v_refresh() : 0.0F);
Sean Paul4057be32015-05-13 06:23:09 -0700106
Sean Paulf72cccd2018-08-27 13:59:08 -0400107 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh,
108 vsync.tv_sec * kOneSecondNs +
109 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -0700110 vsync.tv_sec = phased_timestamp / kOneSecondNs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Sean Paul4057be32015-05-13 06:23:09 -0700112 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200113 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Sean Paul4057be32015-05-13 06:23:09 -0700114 } while (ret == -1 && errno == EINTR);
115 if (ret)
116 return ret;
117
118 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
119 return 0;
120}
121
122void VSyncWorker::Routine() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200123 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700124
Adrian Salidofa37f672017-02-16 10:29:46 -0800125 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700126 if (!enabled_) {
127 ret = WaitForSignalOrExitLocked();
128 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100129 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700130 return;
131 }
132 }
133
Sean Paul4057be32015-05-13 06:23:09 -0700134 int display = display_;
Sean Paula5df1de2016-02-10 10:00:08 -0800135 std::shared_ptr<VsyncCallback> callback(callback_);
Adrian Salidofa37f672017-02-16 10:29:46 -0800136 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700137
Sean Paul4057be32015-05-13 06:23:09 -0700138 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
139 if (!crtc) {
140 ALOGE("Failed to get crtc for display");
141 return;
142 }
143 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
144
145 drmVBlank vblank;
146 memset(&vblank, 0, sizeof(vblank));
147 vblank.request.type = (drmVBlankSeqType)(
148 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
149 vblank.request.sequence = 1;
150
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200151 int64_t timestamp = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700152 ret = drmWaitVBlank(drm_->fd(), &vblank);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200153 if (ret == -EINTR)
Sean Paul4057be32015-05-13 06:23:09 -0700154 return;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200155
156 if (ret) {
Sean Paul4057be32015-05-13 06:23:09 -0700157 ret = SyntheticWaitVBlank(&timestamp);
158 if (ret)
159 return;
160 } else {
161 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
162 (int64_t)vblank.reply.tval_usec * 1000;
163 }
164
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200165 if (!enabled_)
166 return;
Roman Stratiienko23701092020-09-26 02:08:41 +0300167
Sean Paula5df1de2016-02-10 10:00:08 -0800168 if (callback)
169 callback->Callback(display, timestamp);
Roman Stratiienko23701092020-09-26 02:08:41 +0300170
171 Lock();
172 if (enabled_ && vsync_callback_hook_ && vsync_callback_data_)
173 vsync_callback_hook_(vsync_callback_data_, display, timestamp);
174 Unlock();
175
Sean Paul4057be32015-05-13 06:23:09 -0700176 last_timestamp_ = timestamp;
177}
Sean Paulf72cccd2018-08-27 13:59:08 -0400178} // namespace android