blob: 6123e5cb95eed651516617abfd4e062aa77e815a [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
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Sean Paul4057be32015-05-13 06:23:09 -070018
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 Stratiienkod2cc7382022-12-28 18:51:59 +020028#include "drm/ResourceManager.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020029#include "utils/log.h"
30
Sean Paul4057be32015-05-13 06:23:09 -070031namespace android {
32
Roman Stratiienko63762a92023-09-18 22:33:45 +030033auto VSyncWorker::CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe,
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020034 VSyncWorkerCallbacks &callbacks)
35 -> std::shared_ptr<VSyncWorker> {
36 auto vsw = std::shared_ptr<VSyncWorker>(new VSyncWorker());
Sean Paul4057be32015-05-13 06:23:09 -070037
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020038 vsw->callbacks_ = callbacks;
Sean Paul4057be32015-05-13 06:23:09 -070039
Roman Stratiienko63762a92023-09-18 22:33:45 +030040 if (pipe) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020041 vsw->high_crtc_ = pipe->crtc->Get()->GetIndexInResArray()
42 << DRM_VBLANK_HIGH_CRTC_SHIFT;
Roman Stratiienko76892782023-01-16 17:15:53 +020043 vsw->drm_fd_ = pipe->device->GetFd();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020044 }
45
46 std::thread(&VSyncWorker::ThreadFn, vsw.get(), vsw).detach();
47
48 return vsw;
Sean Paul4057be32015-05-13 06:23:09 -070049}
50
Adrian Salidofa37f672017-02-16 10:29:46 -080051void VSyncWorker::VSyncControl(bool enabled) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020052 {
53 const std::lock_guard<std::mutex> lock(mutex_);
54 enabled_ = enabled;
55 last_timestamp_ = -1;
56 }
Sean Paul4057be32015-05-13 06:23:09 -070057
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020058 cv_.notify_all();
59}
60
Drew Davenport59833182024-12-13 10:02:15 -070061void VSyncWorker::SetVsyncPeriodNs(uint32_t vsync_period_ns) {
62 const std::lock_guard<std::mutex> lock(mutex_);
63 vsync_period_ns_ = vsync_period_ns;
64}
65
Drew Davenport33121b72024-12-13 14:59:35 -070066void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
67 const std::lock_guard<std::mutex> lock(mutex_);
68 enable_vsync_timestamps_ = enabled;
69 if (enabled) {
70 // Reset the last timestamp so the caller knows if a vsync timestamp is
71 // fresh or not.
72 last_vsync_timestamp_ = 0;
73 }
74}
75
76uint32_t VSyncWorker::GetLastVsyncTimestamp() {
77 const std::lock_guard<std::mutex> lock(mutex_);
78 return last_vsync_timestamp_;
79}
80
Drew Davenport63a699e2024-12-13 15:00:00 -070081void VSyncWorker::SetTimestampCallback(
82 std::optional<VsyncTimestampCallback> &&callback) {
83 const std::lock_guard<std::mutex> lock(mutex_);
84 callback_ = std::move(callback);
85}
86
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020087void VSyncWorker::StopThread() {
88 {
89 const std::lock_guard<std::mutex> lock(mutex_);
90 thread_exit_ = true;
91 enabled_ = false;
92 callbacks_ = {};
93 }
94
95 cv_.notify_all();
Sean Paul4057be32015-05-13 06:23:09 -070096}
97
98/*
99 * Returns the timestamp of the next vsync in phase with last_timestamp_.
100 * For example:
101 * last_timestamp_ = 137
102 * frame_ns = 50
103 * current = 683
104 *
105 * ret = (50 * ((683 - 137)/50 + 1)) + 137
106 * ret = 687
107 *
108 * Thus, we must sleep until timestamp 687 to maintain phase with the last
109 * timestamp.
110 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -0700112 if (last_timestamp_ < 0)
113 return current + frame_ns;
114
115 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
116 last_timestamp_;
117}
118
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200119static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
Sean Paul4057be32015-05-13 06:23:09 -0700120
121int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200122 auto time_now = ResourceManager::GetTimeMonotonicNs();
123
Drew Davenportd387c842024-12-16 16:57:24 -0700124 auto phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200125 struct timespec vsync {};
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200126 vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200127 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200128
129 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700130 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200131 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Keith Mok554743d2021-11-17 01:09:44 +0000132 } while (ret == EINTR);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200133 if (ret != 0)
Sean Paul4057be32015-05-13 06:23:09 -0700134 return ret;
135
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200136 *timestamp = phased_timestamp;
Sean Paul4057be32015-05-13 06:23:09 -0700137 return 0;
138}
139
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200140void VSyncWorker::ThreadFn(const std::shared_ptr<VSyncWorker> &vsw) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200141 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700142
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200143 for (;;) {
144 {
145 std::unique_lock<std::mutex> lock(vsw->mutex_);
146 if (thread_exit_)
147 break;
148
149 if (!enabled_)
150 vsw->cv_.wait(lock);
151
152 if (!enabled_)
153 continue;
Sean Paul4057be32015-05-13 06:23:09 -0700154 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200155
156 ret = -EAGAIN;
157 int64_t timestamp = 0;
158 drmVBlank vblank{};
159
160 if (drm_fd_) {
161 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
162 (high_crtc_ &
163 DRM_VBLANK_HIGH_CRTC_MASK));
164 vblank.request.sequence = 1;
165
Roman Stratiienko76892782023-01-16 17:15:53 +0200166 ret = drmWaitVBlank(*drm_fd_, &vblank);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200167 if (ret == -EINTR)
168 continue;
169 }
170
171 if (ret != 0) {
172 ret = SyntheticWaitVBlank(&timestamp);
173 if (ret != 0)
174 continue;
175 } else {
176 constexpr int kUsToNsMul = 1000;
177 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
178 (int64_t)vblank.reply.tval_usec * kUsToNsMul;
179 }
180
181 decltype(callbacks_.out_event) callback;
Drew Davenport63a699e2024-12-13 15:00:00 -0700182 std::optional<VsyncTimestampCallback> vsync_callback;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200183
184 {
185 const std::lock_guard<std::mutex> lock(mutex_);
186 if (!enabled_)
187 continue;
188 callback = callbacks_.out_event;
Drew Davenport33121b72024-12-13 14:59:35 -0700189 if (enable_vsync_timestamps_) {
190 last_vsync_timestamp_ = timestamp;
191 }
Drew Davenport63a699e2024-12-13 15:00:00 -0700192 vsync_callback = callback_;
193 }
194
195 if (vsync_callback) {
196 vsync_callback.value()(timestamp, vsync_period_ns_);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200197 }
198
199 if (callback)
200 callback(timestamp);
201
202 last_timestamp_ = timestamp;
Sean Paul4057be32015-05-13 06:23:09 -0700203 }
204
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200205 ALOGI("VSyncWorker thread exit");
Sean Paul4057be32015-05-13 06:23:09 -0700206}
Sean Paulf72cccd2018-08-27 13:59:08 -0400207} // namespace android