blob: ac80da46f1409d49dd15dec1e1ad9b9ae2b41585 [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)
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020034 -> std::shared_ptr<VSyncWorker> {
35 auto vsw = std::shared_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
43 std::thread(&VSyncWorker::ThreadFn, vsw.get(), vsw).detach();
44
45 return vsw;
Sean Paul4057be32015-05-13 06:23:09 -070046}
47
Drew Davenportb39a3f82024-12-13 15:04:56 -070048void VSyncWorker::UpdateVSyncControl() {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020049 {
50 const std::lock_guard<std::mutex> lock(mutex_);
Drew Davenportb39a3f82024-12-13 15:04:56 -070051 enabled_ = ShouldEnable();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020052 last_timestamp_ = -1;
53 }
Sean Paul4057be32015-05-13 06:23:09 -070054
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020055 cv_.notify_all();
56}
57
Drew Davenport59833182024-12-13 10:02:15 -070058void VSyncWorker::SetVsyncPeriodNs(uint32_t vsync_period_ns) {
59 const std::lock_guard<std::mutex> lock(mutex_);
60 vsync_period_ns_ = vsync_period_ns;
61}
62
Drew Davenport33121b72024-12-13 14:59:35 -070063void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
Drew Davenportb39a3f82024-12-13 15:04:56 -070064 {
65 const std::lock_guard<std::mutex> lock(mutex_);
66 enable_vsync_timestamps_ = enabled;
67 if (enabled) {
68 // Reset the last timestamp so the caller knows if a vsync timestamp is
69 // fresh or not.
70 last_vsync_timestamp_ = 0;
71 }
Drew Davenport33121b72024-12-13 14:59:35 -070072 }
Drew Davenportb39a3f82024-12-13 15:04:56 -070073 UpdateVSyncControl();
Drew Davenport33121b72024-12-13 14:59:35 -070074}
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) {
Drew Davenportb39a3f82024-12-13 15:04:56 -070083 {
84 const std::lock_guard<std::mutex> lock(mutex_);
85 callback_ = std::move(callback);
86 }
87 UpdateVSyncControl();
Drew Davenport63a699e2024-12-13 15:00:00 -070088}
89
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020090void VSyncWorker::StopThread() {
91 {
92 const std::lock_guard<std::mutex> lock(mutex_);
93 thread_exit_ = true;
94 enabled_ = false;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020095 }
96
97 cv_.notify_all();
Sean Paul4057be32015-05-13 06:23:09 -070098}
99
Drew Davenportb39a3f82024-12-13 15:04:56 -0700100bool VSyncWorker::ShouldEnable() const {
101 return enable_vsync_timestamps_ || callback_.has_value();
102};
103
Sean Paul4057be32015-05-13 06:23:09 -0700104/*
105 * Returns the timestamp of the next vsync in phase with last_timestamp_.
106 * For example:
107 * last_timestamp_ = 137
108 * frame_ns = 50
109 * current = 683
110 *
111 * ret = (50 * ((683 - 137)/50 + 1)) + 137
112 * ret = 687
113 *
114 * Thus, we must sleep until timestamp 687 to maintain phase with the last
115 * timestamp.
116 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200117int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -0700118 if (last_timestamp_ < 0)
119 return current + frame_ns;
120
121 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
122 last_timestamp_;
123}
124
Roman Stratiienko780f7da2022-01-10 16:04:15 +0200125static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
Sean Paul4057be32015-05-13 06:23:09 -0700126
127int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200128 auto time_now = ResourceManager::GetTimeMonotonicNs();
129
Drew Davenportd387c842024-12-16 16:57:24 -0700130 auto phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200131 struct timespec vsync {};
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200132 vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200133 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200134
135 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700136 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200137 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Keith Mok554743d2021-11-17 01:09:44 +0000138 } while (ret == EINTR);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200139 if (ret != 0)
Sean Paul4057be32015-05-13 06:23:09 -0700140 return ret;
141
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200142 *timestamp = phased_timestamp;
Sean Paul4057be32015-05-13 06:23:09 -0700143 return 0;
144}
145
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200146void VSyncWorker::ThreadFn(const std::shared_ptr<VSyncWorker> &vsw) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200147 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700148
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200149 for (;;) {
150 {
151 std::unique_lock<std::mutex> lock(vsw->mutex_);
152 if (thread_exit_)
153 break;
154
155 if (!enabled_)
156 vsw->cv_.wait(lock);
157
158 if (!enabled_)
159 continue;
Sean Paul4057be32015-05-13 06:23:09 -0700160 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200161
162 ret = -EAGAIN;
163 int64_t timestamp = 0;
164 drmVBlank vblank{};
165
166 if (drm_fd_) {
167 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
168 (high_crtc_ &
169 DRM_VBLANK_HIGH_CRTC_MASK));
170 vblank.request.sequence = 1;
171
Roman Stratiienko76892782023-01-16 17:15:53 +0200172 ret = drmWaitVBlank(*drm_fd_, &vblank);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200173 if (ret == -EINTR)
174 continue;
175 }
176
177 if (ret != 0) {
178 ret = SyntheticWaitVBlank(&timestamp);
179 if (ret != 0)
180 continue;
181 } else {
182 constexpr int kUsToNsMul = 1000;
183 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
184 (int64_t)vblank.reply.tval_usec * kUsToNsMul;
185 }
186
Drew Davenport63a699e2024-12-13 15:00:00 -0700187 std::optional<VsyncTimestampCallback> vsync_callback;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200188
189 {
190 const std::lock_guard<std::mutex> lock(mutex_);
191 if (!enabled_)
192 continue;
Drew Davenport33121b72024-12-13 14:59:35 -0700193 if (enable_vsync_timestamps_) {
194 last_vsync_timestamp_ = timestamp;
195 }
Drew Davenport63a699e2024-12-13 15:00:00 -0700196 vsync_callback = callback_;
197 }
198
199 if (vsync_callback) {
200 vsync_callback.value()(timestamp, vsync_period_ns_);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200201 }
Roman Stratiienkod2cc7382022-12-28 18:51:59 +0200202 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