blob: ca6ea27d141bb7984da9020f57856210682612c1 [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
Kevin DuBoise4f27a82019-11-12 11:41:41 -080029VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070030VSyncTracker::~VSyncTracker() = default;
31TimeKeeper::~TimeKeeper() = default;
32
Kevin DuBoise4f27a82019-11-12 11:41:41 -080033VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name,
Kevin DuBois2968afc2020-01-14 09:48:50 -080034 VSyncDispatch::Callback const& cb,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080035 nsecs_t minVsyncDistance)
36 : mName(name),
37 mCallback(cb),
Kevin DuBoisc94ca832019-11-26 12:56:24 -080038 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070039
Kevin DuBoise4f27a82019-11-12 11:41:41 -080040std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070041 return mLastDispatchTime;
42}
43
Kevin DuBoise4f27a82019-11-12 11:41:41 -080044std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070045 return mName;
46}
47
Kevin DuBoise4f27a82019-11-12 11:41:41 -080048std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070049 if (!mArmedInfo) {
50 return {};
51 }
52 return {mArmedInfo->mActualWakeupTime};
53}
54
Ady Abraham9c53ee72020-07-22 21:16:18 -070055std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const {
56 if (!mArmedInfo) {
57 return {};
58 }
59 return {mArmedInfo->mActualReadyTime};
60}
61
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080062std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
63 if (!mArmedInfo) {
64 return {};
65 }
66 return {mArmedInfo->mActualVsyncTime};
67}
68
Ady Abraham9c53ee72020-07-22 21:16:18 -070069ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing,
Kevin DuBois2311b1a2019-11-18 16:19:08 -080070 VSyncTracker& tracker, nsecs_t now) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070071 auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
72 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
Kevin DuBoisc94ca832019-11-26 12:56:24 -080073
74 bool const wouldSkipAVsyncTarget =
75 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
76 if (wouldSkipAVsyncTarget) {
77 return ScheduleResult::Scheduled;
78 }
79
80 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
81 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
82 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
83 if (alreadyDispatchedForVsync) {
84 nextVsyncTime =
85 tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
Kevin DuBois2311b1a2019-11-18 16:19:08 -080086 }
87
Ady Abraham9c53ee72020-07-22 21:16:18 -070088 auto const nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
89 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
90 mScheduleTiming = timing;
91 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBoisc94ca832019-11-26 12:56:24 -080092 return ScheduleResult::Scheduled;
Kevin DuBois305bef12019-10-09 13:23:27 -070093}
94
Ady Abraham9c53ee72020-07-22 21:16:18 -070095void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
96 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -070097}
98
99bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
100 return mWorkloadUpdateInfo.has_value();
101}
102
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800103void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700104 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700105 return;
106 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700107
108 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700110 mWorkloadUpdateInfo.reset();
111 }
112
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
114 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
115
116 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(earliestVsync);
117 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
118 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
119
120 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700121}
122
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800123void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700124 mArmedInfo.reset();
125}
126
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800127nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700128 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
129 disarm();
130 return *mLastDispatchTime;
131}
132
Ady Abraham9c53ee72020-07-22 21:16:18 -0700133void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
134 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700135 {
136 std::lock_guard<std::mutex> lk(mRunningMutex);
137 mRunning = true;
138 }
139
Ady Abraham9c53ee72020-07-22 21:16:18 -0700140 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700141
142 std::lock_guard<std::mutex> lk(mRunningMutex);
143 mRunning = false;
144 mCv.notify_all();
145}
146
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800147void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700148 std::unique_lock<std::mutex> lk(mRunningMutex);
149 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
150}
151
Ady Abraham5e7371c2020-03-24 14:47:24 -0700152void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
153 std::lock_guard<std::mutex> lk(mRunningMutex);
154 std::string armedInfo;
155 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700156 StringAppendF(&armedInfo,
157 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700158 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700159 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700160 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
161 }
162
163 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
164 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700165 StringAppendF(&result,
166 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms earliestVsync: %.2fms relative "
167 "to now\n",
168 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
169 (mScheduleTiming.earliestVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700170
171 if (mLastDispatchTime) {
172 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
173 (systemTime() - *mLastDispatchTime) / 1e6f);
174 } else {
175 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
176 }
177}
178
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800179VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800180 VSyncTracker& tracker, nsecs_t timerSlack,
181 nsecs_t minVsyncDistance)
182 : mTimeKeeper(std::move(tk)),
183 mTracker(tracker),
184 mTimerSlack(timerSlack),
185 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700186
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800187VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700188 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700189 cancelTimer();
190}
191
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800192void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700193 mIntendedWakeupTime = kInvalidTime;
194 mTimeKeeper->alarmCancel();
195}
196
Ady Abrahamb491c902020-08-15 15:47:56 -0700197void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700198 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700199 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
200 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700201 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700202}
203
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800204void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700205 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
206}
207
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800208void VSyncDispatchTimerQueue::TraceBuffer::note(std::string_view name, nsecs_t alarmIn,
209 nsecs_t vsFor) {
210 if (ATRACE_ENABLED()) {
211 snprintf(str_buffer.data(), str_buffer.size(), "%.4s%s%" PRId64 "%s%" PRId64,
212 name.substr(0, kMaxNamePrint).data(), kTraceNamePrefix, alarmIn,
213 kTraceNameSeparator, vsFor);
214 }
215 ATRACE_NAME(str_buffer.data());
216}
217
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800218void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
219 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700220 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800221 std::optional<nsecs_t> targetVsync;
222 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700223 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
224 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700225 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700226 continue;
227 }
228
229 if (it != skipUpdateIt) {
230 callback->update(mTracker, now);
231 }
232 auto const wakeupTime = *callback->wakeupTime();
233 if (!min || (min && *min > wakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800234 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700235 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800236 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700237 }
238 }
239
240 if (min && (min < mIntendedWakeupTime)) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800241 if (targetVsync && nextWakeupName) {
242 mTraceBuffer.note(*nextWakeupName, *min - now, *targetVsync - now);
243 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700244 setTimer(*min, now);
245 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800246 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700247 cancelTimer();
248 }
249}
250
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800251void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700252 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800253 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800254 nsecs_t vsyncTimestamp;
255 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700256 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700257 };
258 std::vector<Invocation> invocations;
259 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700260 std::lock_guard lock(mMutex);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700261 auto const now = mTimeKeeper->now();
262 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700263 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
264 auto& callback = it->second;
265 auto const wakeupTime = callback->wakeupTime();
266 if (!wakeupTime) {
267 continue;
268 }
269
Ady Abraham9c53ee72020-07-22 21:16:18 -0700270 auto const readyTime = callback->readyTime();
271
Kevin DuBoisf9477832020-07-16 10:21:36 -0700272 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
273 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700274 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700275 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
276 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700277 }
278 }
279
280 mIntendedWakeupTime = kInvalidTime;
281 rearmTimer(mTimeKeeper->now());
282 }
283
284 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700285 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
286 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700287 }
288}
289
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800290VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Kevin DuBois2968afc2020-01-14 09:48:50 -0800291 Callback const& callbackFn, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700292 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700293 return CallbackToken{
294 mCallbacks
295 .emplace(++mCallbackToken,
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800296 std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800297 callbackFn,
298 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700299 .first->first};
300}
301
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800302void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
303 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700304 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700305 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700306 auto it = mCallbacks.find(token);
307 if (it != mCallbacks.end()) {
308 entry = it->second;
309 mCallbacks.erase(it);
310 }
311 }
312
313 if (entry) {
314 entry->ensureNotRunning();
315 }
316}
317
Ady Abraham9c53ee72020-07-22 21:16:18 -0700318ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
319 ScheduleTiming scheduleTiming) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700320 auto result = ScheduleResult::Error;
321 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700322 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700323
324 auto it = mCallbacks.find(token);
325 if (it == mCallbacks.end()) {
326 return result;
327 }
328 auto& callback = it->second;
Kevin DuBois305bef12019-10-09 13:23:27 -0700329 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700330
331 /* If the timer thread will run soon, we'll apply this work update via the callback
332 * timer recalculation to avoid cancelling a callback that is about to fire. */
333 auto const rearmImminent = now > mIntendedWakeupTime;
334 if (CC_UNLIKELY(rearmImminent)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700335 callback->addPendingWorkloadUpdate(scheduleTiming);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700336 return ScheduleResult::Scheduled;
337 }
338
Ady Abraham9c53ee72020-07-22 21:16:18 -0700339 result = callback->schedule(scheduleTiming, mTracker, now);
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800340 if (result == ScheduleResult::CannotSchedule) {
341 return result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700342 }
343
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800344 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700345 rearmTimerSkippingUpdateFor(now, it);
346 }
347 }
348
349 return result;
350}
351
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800352CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700353 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700354
355 auto it = mCallbacks.find(token);
356 if (it == mCallbacks.end()) {
357 return CancelResult::Error;
358 }
359 auto& callback = it->second;
360
Kevin DuBoisb340b732020-06-16 09:07:35 -0700361 auto const wakeupTime = callback->wakeupTime();
362 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700363 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700364
365 if (*wakeupTime == mIntendedWakeupTime) {
366 mIntendedWakeupTime = kInvalidTime;
367 rearmTimer(mTimeKeeper->now());
368 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700369 return CancelResult::Cancelled;
370 }
371 return CancelResult::TooLate;
372}
373
Ady Abraham5e7371c2020-03-24 14:47:24 -0700374void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700375 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700376 StringAppendF(&result, "\tTimer:\n");
377 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700378 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
379 mMinVsyncDistance / 1e6f);
380 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700381 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
382 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
383 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
384 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700385 StringAppendF(&result, "\tCallbacks:\n");
386 for (const auto& [token, entry] : mCallbacks) {
387 entry->dump(result);
388 }
389}
390
Kevin DuBois305bef12019-10-09 13:23:27 -0700391VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -0800392 VSyncDispatch::Callback const& callbackFn,
Kevin DuBois305bef12019-10-09 13:23:27 -0700393 std::string const& callbackName)
394 : mDispatch(dispatch),
395 mToken(dispatch.registerCallback(callbackFn, callbackName)),
396 mValidToken(true) {}
397
398VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
399 : mDispatch(other.mDispatch),
400 mToken(std::move(other.mToken)),
401 mValidToken(std::move(other.mValidToken)) {
402 other.mValidToken = false;
403}
404
405VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
406 mDispatch = std::move(other.mDispatch);
407 mToken = std::move(other.mToken);
408 mValidToken = std::move(other.mValidToken);
409 other.mValidToken = false;
410 return *this;
411}
412
413VSyncCallbackRegistration::~VSyncCallbackRegistration() {
414 if (mValidToken) mDispatch.get().unregisterCallback(mToken);
415}
416
Ady Abraham9c53ee72020-07-22 21:16:18 -0700417ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800418 if (!mValidToken) {
419 return ScheduleResult::Error;
420 }
Ady Abraham9c53ee72020-07-22 21:16:18 -0700421 return mDispatch.get().schedule(mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700422}
423
424CancelResult VSyncCallbackRegistration::cancel() {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800425 if (!mValidToken) {
426 return CancelResult::Error;
427 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700428 return mDispatch.get().cancel(mToken);
429}
430
431} // namespace android::scheduler