blob: 7f8882de33c649086da52375447e8ee6730b1fc1 [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 <stdlib.h>
22#include <time.h>
23#include <xf86drm.h>
24#include <xf86drmMode.h>
25
Roman Stratiienkod518a052021-02-25 19:15:14 +020026#include "utils/log.h"
27
Sean Paul4057be32015-05-13 06:23:09 -070028namespace 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
Roman Stratiienko23701092020-09-26 02:08:41 +030054void VSyncWorker::RegisterClientCallback(hwc2_callback_data_t data,
55 hwc2_function_pointer_t hook) {
56 Lock();
57 vsync_callback_data_ = data;
58 vsync_callback_hook_ = reinterpret_cast<HWC2_PFN_VSYNC>(hook);
59 Unlock();
60}
61
Adrian Salidofa37f672017-02-16 10:29:46 -080062void VSyncWorker::VSyncControl(bool enabled) {
63 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070064 enabled_ = enabled;
65 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080066 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070067
Adrian Salidofa37f672017-02-16 10:29:46 -080068 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070069}
70
71/*
72 * Returns the timestamp of the next vsync in phase with last_timestamp_.
73 * For example:
74 * last_timestamp_ = 137
75 * frame_ns = 50
76 * current = 683
77 *
78 * ret = (50 * ((683 - 137)/50 + 1)) + 137
79 * ret = 687
80 *
81 * Thus, we must sleep until timestamp 687 to maintain phase with the last
82 * timestamp.
83 */
84int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
85 if (last_timestamp_ < 0)
86 return current + frame_ns;
87
88 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
89 last_timestamp_;
90}
91
92static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
93
94int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
95 struct timespec vsync;
96 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
97
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070098 float refresh = 60.0f; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070099 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -0700100 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,
104 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;
110 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
111 do {
112 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
113 } 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() {
Adrian Salidofa37f672017-02-16 10:29:46 -0800122 int ret;
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
150 int64_t timestamp;
151 ret = drmWaitVBlank(drm_->fd(), &vblank);
152 if (ret == -EINTR) {
153 return;
154 } else if (ret) {
155 ret = SyntheticWaitVBlank(&timestamp);
156 if (ret)
157 return;
158 } else {
159 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
160 (int64_t)vblank.reply.tval_usec * 1000;
161 }
162
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200163 if (!enabled_)
164 return;
Roman Stratiienko23701092020-09-26 02:08:41 +0300165
Sean Paula5df1de2016-02-10 10:00:08 -0800166 if (callback)
167 callback->Callback(display, timestamp);
Roman Stratiienko23701092020-09-26 02:08:41 +0300168
169 Lock();
170 if (enabled_ && vsync_callback_hook_ && vsync_callback_data_)
171 vsync_callback_hook_(vsync_callback_data_, display, timestamp);
172 Unlock();
173
Sean Paul4057be32015-05-13 06:23:09 -0700174 last_timestamp_ = timestamp;
175}
Sean Paulf72cccd2018-08-27 13:59:08 -0400176} // namespace android