blob: b805bf68e4f515189b00a016011943aa4dc6d97e [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));
Ady Abraham69b9e622021-07-19 12:24:31 -070087 auto nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -080088
89 bool const wouldSkipAVsyncTarget =
90 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
Ady Abraham69b9e622021-07-19 12:24:31 -070091 bool const wouldSkipAWakeup =
92 mArmedInfo && ((nextWakeupTime > (mArmedInfo->mActualWakeupTime + mMinVsyncDistance)));
93 if (wouldSkipAVsyncTarget && wouldSkipAWakeup) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070094 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBoisc94ca832019-11-26 12:56:24 -080095 }
96
97 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
98 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
99 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
100 if (alreadyDispatchedForVsync) {
101 nextVsyncTime =
102 tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
Ady Abraham69b9e622021-07-19 12:24:31 -0700103 nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800104 }
105
Ady Abraham9c53ee72020-07-22 21:16:18 -0700106 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
107 mScheduleTiming = timing;
108 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700109 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBois305bef12019-10-09 13:23:27 -0700110}
111
Ady Abraham9c53ee72020-07-22 21:16:18 -0700112void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
113 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700114}
115
116bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
117 return mWorkloadUpdateInfo.has_value();
118}
119
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800120void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700121 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700122 return;
123 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700124
125 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700126 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700127 mWorkloadUpdateInfo.reset();
128 }
129
Ady Abraham9c53ee72020-07-22 21:16:18 -0700130 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
131 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
132
133 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(earliestVsync);
134 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
135 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
136
137 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700138}
139
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800140void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700141 mArmedInfo.reset();
142}
143
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800144nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700145 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
146 disarm();
147 return *mLastDispatchTime;
148}
149
Ady Abraham9c53ee72020-07-22 21:16:18 -0700150void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
151 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700152 {
153 std::lock_guard<std::mutex> lk(mRunningMutex);
154 mRunning = true;
155 }
156
Ady Abraham9c53ee72020-07-22 21:16:18 -0700157 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700158
159 std::lock_guard<std::mutex> lk(mRunningMutex);
160 mRunning = false;
161 mCv.notify_all();
162}
163
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800164void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700165 std::unique_lock<std::mutex> lk(mRunningMutex);
166 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
167}
168
Ady Abraham5e7371c2020-03-24 14:47:24 -0700169void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
170 std::lock_guard<std::mutex> lk(mRunningMutex);
171 std::string armedInfo;
172 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700173 StringAppendF(&armedInfo,
174 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700175 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700176 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700177 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
178 }
179
180 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
181 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700182 StringAppendF(&result,
183 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms earliestVsync: %.2fms relative "
184 "to now\n",
185 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
186 (mScheduleTiming.earliestVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700187
188 if (mLastDispatchTime) {
189 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
190 (systemTime() - *mLastDispatchTime) / 1e6f);
191 } else {
192 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
193 }
194}
195
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800196VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800197 VSyncTracker& tracker, nsecs_t timerSlack,
198 nsecs_t minVsyncDistance)
199 : mTimeKeeper(std::move(tk)),
200 mTracker(tracker),
201 mTimerSlack(timerSlack),
202 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700203
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800204VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700205 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700206 cancelTimer();
207}
208
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800209void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700210 mIntendedWakeupTime = kInvalidTime;
211 mTimeKeeper->alarmCancel();
212}
213
Ady Abrahamb491c902020-08-15 15:47:56 -0700214void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700215 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700216 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
217 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700218 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700219}
220
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800221void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700222 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
223}
224
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800225void VSyncDispatchTimerQueue::TraceBuffer::note(std::string_view name, nsecs_t alarmIn,
226 nsecs_t vsFor) {
227 if (ATRACE_ENABLED()) {
228 snprintf(str_buffer.data(), str_buffer.size(), "%.4s%s%" PRId64 "%s%" PRId64,
229 name.substr(0, kMaxNamePrint).data(), kTraceNamePrefix, alarmIn,
230 kTraceNameSeparator, vsFor);
231 }
232 ATRACE_NAME(str_buffer.data());
233}
234
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800235void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
236 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700237 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800238 std::optional<nsecs_t> targetVsync;
239 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700240 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
241 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700242 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700243 continue;
244 }
245
246 if (it != skipUpdateIt) {
247 callback->update(mTracker, now);
248 }
249 auto const wakeupTime = *callback->wakeupTime();
250 if (!min || (min && *min > wakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800251 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700252 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800253 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700254 }
255 }
256
257 if (min && (min < mIntendedWakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800258 if (targetVsync && nextWakeupName) {
259 mTraceBuffer.note(*nextWakeupName, *min - now, *targetVsync - now);
260 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700261 setTimer(*min, now);
262 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800263 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700264 cancelTimer();
265 }
266}
267
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800268void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700269 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800270 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800271 nsecs_t vsyncTimestamp;
272 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700273 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700274 };
275 std::vector<Invocation> invocations;
276 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700277 std::lock_guard lock(mMutex);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700278 auto const now = mTimeKeeper->now();
279 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700280 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
281 auto& callback = it->second;
282 auto const wakeupTime = callback->wakeupTime();
283 if (!wakeupTime) {
284 continue;
285 }
286
Ady Abraham9c53ee72020-07-22 21:16:18 -0700287 auto const readyTime = callback->readyTime();
288
Kevin DuBoisf9477832020-07-16 10:21:36 -0700289 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
290 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700291 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700292 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
293 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700294 }
295 }
296
297 mIntendedWakeupTime = kInvalidTime;
298 rearmTimer(mTimeKeeper->now());
299 }
300
301 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700302 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
303 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700304 }
305}
306
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800307VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800308 Callback const& callbackFn, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700309 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700310 return CallbackToken{
311 mCallbacks
312 .emplace(++mCallbackToken,
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800313 std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800314 callbackFn,
315 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700316 .first->first};
317}
318
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800319void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
320 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700321 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700322 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700323 auto it = mCallbacks.find(token);
324 if (it != mCallbacks.end()) {
325 entry = it->second;
326 mCallbacks.erase(it);
327 }
328 }
329
330 if (entry) {
331 entry->ensureNotRunning();
332 }
333}
334
Ady Abraham9c53ee72020-07-22 21:16:18 -0700335ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
336 ScheduleTiming scheduleTiming) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700337 ScheduleResult result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700338 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700339 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700340
341 auto it = mCallbacks.find(token);
342 if (it == mCallbacks.end()) {
343 return result;
344 }
345 auto& callback = it->second;
Kevin DuBois305bef12019-10-09 13:23:27 -0700346 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700347
348 /* If the timer thread will run soon, we'll apply this work update via the callback
349 * timer recalculation to avoid cancelling a callback that is about to fire. */
350 auto const rearmImminent = now > mIntendedWakeupTime;
351 if (CC_UNLIKELY(rearmImminent)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700352 callback->addPendingWorkloadUpdate(scheduleTiming);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700353 return getExpectedCallbackTime(mTracker, now, scheduleTiming);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700354 }
355
Ady Abraham9c53ee72020-07-22 21:16:18 -0700356 result = callback->schedule(scheduleTiming, mTracker, now);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700357 if (!result.has_value()) {
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800358 return result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700359 }
360
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800361 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700362 rearmTimerSkippingUpdateFor(now, it);
363 }
364 }
365
366 return result;
367}
368
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800369CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700370 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700371
372 auto it = mCallbacks.find(token);
373 if (it == mCallbacks.end()) {
374 return CancelResult::Error;
375 }
376 auto& callback = it->second;
377
Kevin DuBoisb340b732020-06-16 09:07:35 -0700378 auto const wakeupTime = callback->wakeupTime();
379 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700380 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700381
382 if (*wakeupTime == mIntendedWakeupTime) {
383 mIntendedWakeupTime = kInvalidTime;
384 rearmTimer(mTimeKeeper->now());
385 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700386 return CancelResult::Cancelled;
387 }
388 return CancelResult::TooLate;
389}
390
Ady Abraham5e7371c2020-03-24 14:47:24 -0700391void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700392 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700393 StringAppendF(&result, "\tTimer:\n");
394 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700395 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
396 mMinVsyncDistance / 1e6f);
397 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700398 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
399 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
400 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
401 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700402 StringAppendF(&result, "\tCallbacks:\n");
403 for (const auto& [token, entry] : mCallbacks) {
404 entry->dump(result);
405 }
406}
407
Kevin DuBois305bef12019-10-09 13:23:27 -0700408VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -0800409 VSyncDispatch::Callback const& callbackFn,
Kevin DuBois305bef12019-10-09 13:23:27 -0700410 std::string const& callbackName)
411 : mDispatch(dispatch),
412 mToken(dispatch.registerCallback(callbackFn, callbackName)),
413 mValidToken(true) {}
414
415VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
416 : mDispatch(other.mDispatch),
417 mToken(std::move(other.mToken)),
418 mValidToken(std::move(other.mValidToken)) {
419 other.mValidToken = false;
420}
421
422VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
423 mDispatch = std::move(other.mDispatch);
424 mToken = std::move(other.mToken);
425 mValidToken = std::move(other.mValidToken);
426 other.mValidToken = false;
427 return *this;
428}
429
430VSyncCallbackRegistration::~VSyncCallbackRegistration() {
431 if (mValidToken) mDispatch.get().unregisterCallback(mToken);
432}
433
Ady Abraham9c53ee72020-07-22 21:16:18 -0700434ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800435 if (!mValidToken) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700436 return std::nullopt;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800437 }
Ady Abraham9c53ee72020-07-22 21:16:18 -0700438 return mDispatch.get().schedule(mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700439}
440
441CancelResult VSyncCallbackRegistration::cancel() {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800442 if (!mValidToken) {
443 return CancelResult::Error;
444 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700445 return mDispatch.get().cancel(mToken);
446}
447
448} // namespace android::scheduler