blob: 6dd6b02ff838c4e6ddf5c55313472de0677962d8 [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
Drew Davenport15016c42024-12-13 15:13:28 -070033auto VSyncWorker::CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe)
Drew Davenport30f4e9c2024-12-16 16:12:11 -070034 -> std::unique_ptr<VSyncWorker> {
35 auto vsw = std::unique_ptr<VSyncWorker>(new VSyncWorker());
Sean Paul4057be32015-05-13 06:23:09 -070036
Roman Stratiienko63762a92023-09-18 22:33:45 +030037 if (pipe) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020038 vsw->high_crtc_ = pipe->crtc->Get()->GetIndexInResArray()
39 << DRM_VBLANK_HIGH_CRTC_SHIFT;
Roman Stratiienko76892782023-01-16 17:15:53 +020040 vsw->drm_fd_ = pipe->device->GetFd();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020041 }
42
Drew Davenport30f4e9c2024-12-16 16:12:11 -070043 vsw->vswt_ = std::thread(&VSyncWorker::ThreadFn, vsw.get());
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020044
45 return vsw;
Sean Paul4057be32015-05-13 06:23:09 -070046}
47
Drew Davenport30f4e9c2024-12-16 16:12:11 -070048VSyncWorker::~VSyncWorker() {
49 StopThread();
50
51 vswt_.join();
52}
53
Drew Davenportb39a3f82024-12-13 15:04:56 -070054void VSyncWorker::UpdateVSyncControl() {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020055 {
56 const std::lock_guard<std::mutex> lock(mutex_);
Drew Davenportb39a3f82024-12-13 15:04:56 -070057 enabled_ = ShouldEnable();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020058 last_timestamp_ = -1;
59 }
Sean Paul4057be32015-05-13 06:23:09 -070060
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020061 cv_.notify_all();
62}
63
Drew Davenport59833182024-12-13 10:02:15 -070064void VSyncWorker::SetVsyncPeriodNs(uint32_t vsync_period_ns) {
65 const std::lock_guard<std::mutex> lock(mutex_);
66 vsync_period_ns_ = vsync_period_ns;
67}
68
Drew Davenport33121b72024-12-13 14:59:35 -070069void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
Drew Davenportb39a3f82024-12-13 15:04:56 -070070 {
71 const std::lock_guard<std::mutex> lock(mutex_);
72 enable_vsync_timestamps_ = enabled;
73 if (enabled) {
74 // Reset the last timestamp so the caller knows if a vsync timestamp is
75 // fresh or not.
76 last_vsync_timestamp_ = 0;
77 }
Drew Davenport33121b72024-12-13 14:59:35 -070078 }
Drew Davenportb39a3f82024-12-13 15:04:56 -070079 UpdateVSyncControl();
Drew Davenport33121b72024-12-13 14:59:35 -070080}
81
82uint32_t VSyncWorker::GetLastVsyncTimestamp() {
83 const std::lock_guard<std::mutex> lock(mutex_);
84 return last_vsync_timestamp_;
85}
86
Drew Davenport63a699e2024-12-13 15:00:00 -070087void VSyncWorker::SetTimestampCallback(
88 std::optional<VsyncTimestampCallback> &&callback) {
Drew Davenportb39a3f82024-12-13 15:04:56 -070089 {
90 const std::lock_guard<std::mutex> lock(mutex_);
91 callback_ = std::move(callback);
92 }
93 UpdateVSyncControl();
Drew Davenport63a699e2024-12-13 15:00:00 -070094}
95
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020096void VSyncWorker::StopThread() {
97 {
98 const std::lock_guard<std::mutex> lock(mutex_);
99 thread_exit_ = true;
100 enabled_ = false;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200101 }
102
103 cv_.notify_all();
Sean Paul4057be32015-05-13 06:23:09 -0700104}
105
Drew Davenportb39a3f82024-12-13 15:04:56 -0700106bool VSyncWorker::ShouldEnable() const {
107 return enable_vsync_timestamps_ || callback_.has_value();
108};
109
Sean Paul4057be32015-05-13 06:23:09 -0700110/*
111 * Returns the timestamp of the next vsync in phase with last_timestamp_.
112 * For example:
113 * last_timestamp_ = 137
114 * frame_ns = 50
115 * current = 683
116 *
117 * ret = (50 * ((683 - 137)/50 + 1)) + 137
118 * ret = 687
119 *
120 * Thus, we must sleep until timestamp 687 to maintain phase with the last
121 * timestamp.
122 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200123int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -0700124 if (last_timestamp_ < 0)
125 return current + frame_ns;
126
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200127 return (frame_ns * ((current - last_timestamp_) / frame_ns + 1)) +
Sean Paul4057be32015-05-13 06:23:09 -0700128 last_timestamp_;
129}
130
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200131static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
Sean Paul4057be32015-05-13 06:23:09 -0700132
133int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Drew Davenport6bfccb82024-12-19 13:44:47 -0700134 int64_t phased_timestamp = 0;
135 {
136 std::lock_guard<std::mutex> lock(mutex_);
137 int64_t time_now = ResourceManager::GetTimeMonotonicNs();
138 phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
139 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200140
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200141 struct timespec vsync {};
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200142 vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200143 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200144
145 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700146 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200147 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Keith Mok554743d2021-11-17 01:09:44 +0000148 } while (ret == EINTR);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200149 if (ret != 0)
Sean Paul4057be32015-05-13 06:23:09 -0700150 return ret;
151
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200152 *timestamp = phased_timestamp;
Sean Paul4057be32015-05-13 06:23:09 -0700153 return 0;
154}
155
Drew Davenport30f4e9c2024-12-16 16:12:11 -0700156void VSyncWorker::ThreadFn() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200157 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700158
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200159 for (;;) {
160 {
Drew Davenport30f4e9c2024-12-16 16:12:11 -0700161 std::unique_lock<std::mutex> lock(mutex_);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200162 if (thread_exit_)
163 break;
164
165 if (!enabled_)
Drew Davenport30f4e9c2024-12-16 16:12:11 -0700166 cv_.wait(lock);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200167
168 if (!enabled_)
169 continue;
Sean Paul4057be32015-05-13 06:23:09 -0700170 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200171
172 ret = -EAGAIN;
173 int64_t timestamp = 0;
174 drmVBlank vblank{};
175
176 if (drm_fd_) {
177 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
178 (high_crtc_ &
179 DRM_VBLANK_HIGH_CRTC_MASK));
180 vblank.request.sequence = 1;
181
Roman Stratiienko76892782023-01-16 17:15:53 +0200182 ret = drmWaitVBlank(*drm_fd_, &vblank);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200183 if (ret == -EINTR)
184 continue;
185 }
186
187 if (ret != 0) {
188 ret = SyntheticWaitVBlank(&timestamp);
189 if (ret != 0)
190 continue;
191 } else {
192 constexpr int kUsToNsMul = 1000;
193 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
194 (int64_t)vblank.reply.tval_usec * kUsToNsMul;
195 }
196
Drew Davenport63a699e2024-12-13 15:00:00 -0700197 std::optional<VsyncTimestampCallback> vsync_callback;
Drew Davenport6bfccb82024-12-19 13:44:47 -0700198 int64_t vsync_period_ns = 0;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200199
200 {
201 const std::lock_guard<std::mutex> lock(mutex_);
202 if (!enabled_)
203 continue;
Drew Davenport33121b72024-12-13 14:59:35 -0700204 if (enable_vsync_timestamps_) {
205 last_vsync_timestamp_ = timestamp;
206 }
Drew Davenport63a699e2024-12-13 15:00:00 -0700207 vsync_callback = callback_;
Drew Davenport6bfccb82024-12-19 13:44:47 -0700208 vsync_period_ns = vsync_period_ns_;
209 last_timestamp_ = timestamp;
Drew Davenport63a699e2024-12-13 15:00:00 -0700210 }
211
212 if (vsync_callback) {
Drew Davenport6bfccb82024-12-19 13:44:47 -0700213 vsync_callback.value()(timestamp, vsync_period_ns);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200214 }
Sean Paul4057be32015-05-13 06:23:09 -0700215 }
216
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200217 ALOGI("VSyncWorker thread exit");
Sean Paul4057be32015-05-13 06:23:09 -0700218}
Sean Paulf72cccd2018-08-27 13:59:08 -0400219} // namespace android