blob: 28be962f2ca3cf0f5f3cd1dc43b6178a21528a24 [file] [log] [blame]
Kevin DuBois305bef12019-10-09 13:23:27 -07001/*
2 * Copyright 2019 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 ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abraham5e7371c2020-03-24 14:47:24 -070018#include <android-base/stringprintf.h>
Kevin DuBois305bef12019-10-09 13:23:27 -070019#include <utils/Trace.h>
20#include <vector>
21
22#include "TimeKeeper.h"
Kevin DuBoise4f27a82019-11-12 11:41:41 -080023#include "VSyncDispatchTimerQueue.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070024#include "VSyncTracker.h"
25
26namespace android::scheduler {
Ady Abraham5e7371c2020-03-24 14:47:24 -070027using base::StringAppendF;
Kevin DuBois305bef12019-10-09 13:23:27 -070028
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070029namespace {
30nsecs_t getExpectedCallbackTime(nsecs_t nextVsyncTime,
31 const VSyncDispatch::ScheduleTiming& timing) {
32 return nextVsyncTime - timing.readyDuration - timing.workDuration;
33}
34
35nsecs_t getExpectedCallbackTime(VSyncTracker& tracker, nsecs_t now,
36 const VSyncDispatch::ScheduleTiming& timing) {
37 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
38 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
39 return getExpectedCallbackTime(nextVsyncTime, timing);
40}
41} // namespace
42
Kevin DuBoise4f27a82019-11-12 11:41:41 -080043VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070044VSyncTracker::~VSyncTracker() = default;
45TimeKeeper::~TimeKeeper() = default;
46
Kevin DuBoise4f27a82019-11-12 11:41:41 -080047VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name,
Kevin DuBois2968afc2020-01-14 09:48:50 -080048 VSyncDispatch::Callback const& cb,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080049 nsecs_t minVsyncDistance)
50 : mName(name),
51 mCallback(cb),
Kevin DuBoisc94ca832019-11-26 12:56:24 -080052 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070053
Kevin DuBoise4f27a82019-11-12 11:41:41 -080054std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070055 return mLastDispatchTime;
56}
57
Kevin DuBoise4f27a82019-11-12 11:41:41 -080058std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070059 return mName;
60}
61
Kevin DuBoise4f27a82019-11-12 11:41:41 -080062std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070063 if (!mArmedInfo) {
64 return {};
65 }
66 return {mArmedInfo->mActualWakeupTime};
67}
68
Ady Abraham9c53ee72020-07-22 21:16:18 -070069std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const {
70 if (!mArmedInfo) {
71 return {};
72 }
73 return {mArmedInfo->mActualReadyTime};
74}
75
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080076std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
77 if (!mArmedInfo) {
78 return {};
79 }
80 return {mArmedInfo->mActualVsyncTime};
81}
82
Ady Abraham9c53ee72020-07-22 21:16:18 -070083ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing,
Kevin DuBois2311b1a2019-11-18 16:19:08 -080084 VSyncTracker& tracker, nsecs_t now) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070085 auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
86 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
Kevin DuBoisc94ca832019-11-26 12:56:24 -080087
88 bool const wouldSkipAVsyncTarget =
89 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
90 if (wouldSkipAVsyncTarget) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070091 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBoisc94ca832019-11-26 12:56:24 -080092 }
93
94 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
95 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
96 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
97 if (alreadyDispatchedForVsync) {
98 nextVsyncTime =
99 tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800100 }
101
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 auto const nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
103 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
104 mScheduleTiming = timing;
105 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700106 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBois305bef12019-10-09 13:23:27 -0700107}
108
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
110 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700111}
112
113bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
114 return mWorkloadUpdateInfo.has_value();
115}
116
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800117void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700118 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700119 return;
120 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700121
122 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700123 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700124 mWorkloadUpdateInfo.reset();
125 }
126
Ady Abraham9c53ee72020-07-22 21:16:18 -0700127 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
128 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
129
130 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(earliestVsync);
131 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
132 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
133
134 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700135}
136
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800137void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700138 mArmedInfo.reset();
139}
140
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800141nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700142 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
143 disarm();
144 return *mLastDispatchTime;
145}
146
Ady Abraham9c53ee72020-07-22 21:16:18 -0700147void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
148 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700149 {
150 std::lock_guard<std::mutex> lk(mRunningMutex);
151 mRunning = true;
152 }
153
Ady Abraham9c53ee72020-07-22 21:16:18 -0700154 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700155
156 std::lock_guard<std::mutex> lk(mRunningMutex);
157 mRunning = false;
158 mCv.notify_all();
159}
160
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800161void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700162 std::unique_lock<std::mutex> lk(mRunningMutex);
163 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
164}
165
Ady Abraham5e7371c2020-03-24 14:47:24 -0700166void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
167 std::lock_guard<std::mutex> lk(mRunningMutex);
168 std::string armedInfo;
169 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700170 StringAppendF(&armedInfo,
171 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700172 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700173 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700174 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
175 }
176
177 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
178 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700179 StringAppendF(&result,
180 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms earliestVsync: %.2fms relative "
181 "to now\n",
182 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
183 (mScheduleTiming.earliestVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700184
185 if (mLastDispatchTime) {
186 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
187 (systemTime() - *mLastDispatchTime) / 1e6f);
188 } else {
189 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
190 }
191}
192
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800193VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800194 VSyncTracker& tracker, nsecs_t timerSlack,
195 nsecs_t minVsyncDistance)
196 : mTimeKeeper(std::move(tk)),
197 mTracker(tracker),
198 mTimerSlack(timerSlack),
199 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700200
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800201VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700202 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700203 cancelTimer();
204}
205
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800206void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700207 mIntendedWakeupTime = kInvalidTime;
208 mTimeKeeper->alarmCancel();
209}
210
Ady Abrahamb491c902020-08-15 15:47:56 -0700211void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700212 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700213 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
214 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700215 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700216}
217
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800218void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700219 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
220}
221
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800222void VSyncDispatchTimerQueue::TraceBuffer::note(std::string_view name, nsecs_t alarmIn,
223 nsecs_t vsFor) {
224 if (ATRACE_ENABLED()) {
225 snprintf(str_buffer.data(), str_buffer.size(), "%.4s%s%" PRId64 "%s%" PRId64,
226 name.substr(0, kMaxNamePrint).data(), kTraceNamePrefix, alarmIn,
227 kTraceNameSeparator, vsFor);
228 }
229 ATRACE_NAME(str_buffer.data());
230}
231
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800232void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
233 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700234 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800235 std::optional<nsecs_t> targetVsync;
236 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700237 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
238 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700239 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700240 continue;
241 }
242
243 if (it != skipUpdateIt) {
244 callback->update(mTracker, now);
245 }
246 auto const wakeupTime = *callback->wakeupTime();
247 if (!min || (min && *min > wakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800248 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700249 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800250 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700251 }
252 }
253
254 if (min && (min < mIntendedWakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800255 if (targetVsync && nextWakeupName) {
256 mTraceBuffer.note(*nextWakeupName, *min - now, *targetVsync - now);
257 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700258 setTimer(*min, now);
259 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800260 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700261 cancelTimer();
262 }
263}
264
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800265void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700266 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800267 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800268 nsecs_t vsyncTimestamp;
269 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700270 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700271 };
272 std::vector<Invocation> invocations;
273 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700274 std::lock_guard lock(mMutex);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700275 auto const now = mTimeKeeper->now();
276 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700277 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
278 auto& callback = it->second;
279 auto const wakeupTime = callback->wakeupTime();
280 if (!wakeupTime) {
281 continue;
282 }
283
Ady Abraham9c53ee72020-07-22 21:16:18 -0700284 auto const readyTime = callback->readyTime();
285
Kevin DuBoisf9477832020-07-16 10:21:36 -0700286 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
287 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700288 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700289 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
290 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700291 }
292 }
293
294 mIntendedWakeupTime = kInvalidTime;
295 rearmTimer(mTimeKeeper->now());
296 }
297
298 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700299 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
300 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700301 }
302}
303
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800304VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800305 Callback const& callbackFn, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700306 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700307 return CallbackToken{
308 mCallbacks
309 .emplace(++mCallbackToken,
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800310 std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800311 callbackFn,
312 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700313 .first->first};
314}
315
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800316void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
317 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700318 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700319 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700320 auto it = mCallbacks.find(token);
321 if (it != mCallbacks.end()) {
322 entry = it->second;
323 mCallbacks.erase(it);
324 }
325 }
326
327 if (entry) {
328 entry->ensureNotRunning();
329 }
330}
331
Ady Abraham9c53ee72020-07-22 21:16:18 -0700332ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
333 ScheduleTiming scheduleTiming) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700334 ScheduleResult result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700335 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700336 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700337
338 auto it = mCallbacks.find(token);
339 if (it == mCallbacks.end()) {
340 return result;
341 }
342 auto& callback = it->second;
Kevin DuBois305bef12019-10-09 13:23:27 -0700343 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700344
345 /* If the timer thread will run soon, we'll apply this work update via the callback
346 * timer recalculation to avoid cancelling a callback that is about to fire. */
347 auto const rearmImminent = now > mIntendedWakeupTime;
348 if (CC_UNLIKELY(rearmImminent)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700349 callback->addPendingWorkloadUpdate(scheduleTiming);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700350 return getExpectedCallbackTime(mTracker, now, scheduleTiming);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700351 }
352
Ady Abraham9c53ee72020-07-22 21:16:18 -0700353 result = callback->schedule(scheduleTiming, mTracker, now);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700354 if (!result.has_value()) {
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800355 return result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700356 }
357
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800358 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700359 rearmTimerSkippingUpdateFor(now, it);
360 }
361 }
362
363 return result;
364}
365
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800366CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700367 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700368
369 auto it = mCallbacks.find(token);
370 if (it == mCallbacks.end()) {
371 return CancelResult::Error;
372 }
373 auto& callback = it->second;
374
Kevin DuBoisb340b732020-06-16 09:07:35 -0700375 auto const wakeupTime = callback->wakeupTime();
376 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700377 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700378
379 if (*wakeupTime == mIntendedWakeupTime) {
380 mIntendedWakeupTime = kInvalidTime;
381 rearmTimer(mTimeKeeper->now());
382 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700383 return CancelResult::Cancelled;
384 }
385 return CancelResult::TooLate;
386}
387
Ady Abraham5e7371c2020-03-24 14:47:24 -0700388void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700389 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700390 StringAppendF(&result, "\tTimer:\n");
391 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700392 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
393 mMinVsyncDistance / 1e6f);
394 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700395 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
396 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
397 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
398 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700399 StringAppendF(&result, "\tCallbacks:\n");
400 for (const auto& [token, entry] : mCallbacks) {
401 entry->dump(result);
402 }
403}
404
Kevin DuBois305bef12019-10-09 13:23:27 -0700405VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -0800406 VSyncDispatch::Callback const& callbackFn,
Kevin DuBois305bef12019-10-09 13:23:27 -0700407 std::string const& callbackName)
408 : mDispatch(dispatch),
409 mToken(dispatch.registerCallback(callbackFn, callbackName)),
410 mValidToken(true) {}
411
412VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
413 : mDispatch(other.mDispatch),
414 mToken(std::move(other.mToken)),
415 mValidToken(std::move(other.mValidToken)) {
416 other.mValidToken = false;
417}
418
419VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
420 mDispatch = std::move(other.mDispatch);
421 mToken = std::move(other.mToken);
422 mValidToken = std::move(other.mValidToken);
423 other.mValidToken = false;
424 return *this;
425}
426
427VSyncCallbackRegistration::~VSyncCallbackRegistration() {
428 if (mValidToken) mDispatch.get().unregisterCallback(mToken);
429}
430
Ady Abraham9c53ee72020-07-22 21:16:18 -0700431ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800432 if (!mValidToken) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700433 return std::nullopt;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800434 }
Ady Abraham9c53ee72020-07-22 21:16:18 -0700435 return mDispatch.get().schedule(mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700436}
437
438CancelResult VSyncCallbackRegistration::cancel() {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800439 if (!mValidToken) {
440 return CancelResult::Error;
441 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700442 return mDispatch.get().cancel(mToken);
443}
444
445} // namespace android::scheduler