1 /*
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 "drmhwc"
18
19 #include "VSyncWorker.h"
20
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23
24 #include <cstdlib>
25 #include <cstring>
26 #include <ctime>
27
28 #include "drm/ResourceManager.h"
29 #include "utils/log.h"
30
31 namespace android {
32
CreateInstance(std::shared_ptr<DrmDisplayPipeline> & pipe)33 auto VSyncWorker::CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe)
34 -> std::unique_ptr<VSyncWorker> {
35 auto vsw = std::unique_ptr<VSyncWorker>(new VSyncWorker());
36
37 if (pipe) {
38 vsw->high_crtc_ = pipe->crtc->Get()->GetIndexInResArray()
39 << DRM_VBLANK_HIGH_CRTC_SHIFT;
40 vsw->drm_fd_ = pipe->device->GetFd();
41 }
42
43 vsw->vswt_ = std::thread(&VSyncWorker::ThreadFn, vsw.get());
44
45 return vsw;
46 }
47
~VSyncWorker()48 VSyncWorker::~VSyncWorker() {
49 StopThread();
50
51 vswt_.join();
52 }
53
UpdateVSyncControl()54 void VSyncWorker::UpdateVSyncControl() {
55 {
56 const std::lock_guard<std::mutex> lock(mutex_);
57 enabled_ = ShouldEnable();
58 }
59
60 cv_.notify_all();
61 }
62
SetVsyncPeriodNs(uint32_t vsync_period_ns)63 void VSyncWorker::SetVsyncPeriodNs(uint32_t vsync_period_ns) {
64 const std::lock_guard<std::mutex> lock(mutex_);
65 vsync_period_ns_ = vsync_period_ns;
66 last_timestamp_ = std::nullopt;
67 }
68
SetVsyncTimestampTracking(bool enabled)69 void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
70 {
71 const std::lock_guard<std::mutex> lock(mutex_);
72 enable_vsync_timestamps_ = enabled;
73 if (enabled) {
74 // Reset the freshness flag to ensure that only a fresh timestamp is
75 // returned from GetLastVsyncTimestamp.
76 last_timestamp_is_fresh_ = false;
77 }
78 }
79 UpdateVSyncControl();
80 }
81
GetLastVsyncTimestamp()82 uint32_t VSyncWorker::GetLastVsyncTimestamp() {
83 const std::lock_guard<std::mutex> lock(mutex_);
84 return last_timestamp_is_fresh_ ? last_timestamp_.value_or(0) : 0;
85 }
86
GetNextVsyncTimestamp(int64_t time)87 int64_t VSyncWorker::GetNextVsyncTimestamp(int64_t time) {
88 const std::lock_guard<std::mutex> lock(mutex_);
89 return GetPhasedVSync(vsync_period_ns_, time);
90 }
91
SetTimestampCallback(std::optional<VsyncTimestampCallback> && callback)92 void VSyncWorker::SetTimestampCallback(
93 std::optional<VsyncTimestampCallback> &&callback) {
94 {
95 const std::lock_guard<std::mutex> lock(mutex_);
96 callback_ = std::move(callback);
97 }
98 UpdateVSyncControl();
99 }
100
StopThread()101 void VSyncWorker::StopThread() {
102 {
103 const std::lock_guard<std::mutex> lock(mutex_);
104 thread_exit_ = true;
105 enabled_ = false;
106 }
107
108 cv_.notify_all();
109 }
110
ShouldEnable() const111 bool VSyncWorker::ShouldEnable() const {
112 return enable_vsync_timestamps_ || callback_.has_value();
113 };
114
115 /*
116 * Returns the timestamp of the next vsync in phase with last_timestamp_.
117 * For example:
118 * last_timestamp_ = 137
119 * frame_ns = 50
120 * current = 683
121 *
122 * ret = (50 * ((683 - 137)/50 + 1)) + 137
123 * ret = 687
124 *
125 * Thus, we must sleep until timestamp 687 to maintain phase with the last
126 * timestamp.
127 */
GetPhasedVSync(int64_t frame_ns,int64_t current) const128 int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
129 if (!last_timestamp_.has_value())
130 return current + frame_ns;
131
132 return (frame_ns * ((current - *last_timestamp_) / frame_ns + 1)) +
133 *last_timestamp_;
134 }
135
136 static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
137
SyntheticWaitVBlank(int64_t * timestamp)138 int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
139 int64_t phased_timestamp = 0;
140 {
141 std::lock_guard<std::mutex> lock(mutex_);
142 int64_t time_now = ResourceManager::GetTimeMonotonicNs();
143 phased_timestamp = GetPhasedVSync(vsync_period_ns_, time_now);
144 }
145
146 struct timespec vsync {};
147 vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
148 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
149
150 int ret = 0;
151 do {
152 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
153 } while (ret == EINTR);
154 if (ret != 0)
155 return ret;
156
157 *timestamp = phased_timestamp;
158 return 0;
159 }
160
ThreadFn()161 void VSyncWorker::ThreadFn() {
162 int ret = 0;
163
164 for (;;) {
165 {
166 std::unique_lock<std::mutex> lock(mutex_);
167 // Thread safety analysis doesn't understand std::unique_lock.
168 #pragma clang diagnostic push
169 #pragma clang diagnostic ignored "-Wthread-safety-analysis"
170 if (thread_exit_)
171 break;
172
173 if (!enabled_)
174 cv_.wait(lock);
175
176 if (!enabled_)
177 continue;
178 #pragma clang diagnostic pop
179 }
180
181 ret = -EAGAIN;
182 int64_t timestamp = 0;
183 drmVBlank vblank{};
184
185 if (drm_fd_) {
186 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
187 (high_crtc_ &
188 DRM_VBLANK_HIGH_CRTC_MASK));
189 vblank.request.sequence = 1;
190
191 ret = drmWaitVBlank(*drm_fd_, &vblank);
192 if (ret == -EINTR)
193 continue;
194 }
195
196 if (ret != 0) {
197 ret = SyntheticWaitVBlank(×tamp);
198 if (ret != 0)
199 continue;
200 } else {
201 constexpr int kUsToNsMul = 1000;
202 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
203 (int64_t)vblank.reply.tval_usec * kUsToNsMul;
204 }
205
206 std::optional<VsyncTimestampCallback> vsync_callback;
207 int64_t vsync_period_ns = 0;
208
209 {
210 const std::lock_guard<std::mutex> lock(mutex_);
211 if (!enabled_)
212 continue;
213 if (enable_vsync_timestamps_) {
214 last_timestamp_is_fresh_ = true;
215 }
216 vsync_callback = callback_;
217 vsync_period_ns = vsync_period_ns_;
218 last_timestamp_ = timestamp;
219 }
220
221 if (vsync_callback) {
222 vsync_callback.value()(timestamp, vsync_period_ns);
223 }
224 }
225
226 ALOGI("VSyncWorker thread exit");
227 }
228 } // namespace android
229