blob: 27f43115c19b5f321e3dfcc673146fe8bcabe1de [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
Dominik Laskowski62eff352021-12-06 09:59:41 -080018
Kevin DuBois305bef12019-10-09 13:23:27 -070019#include <vector>
20
Dominik Laskowski62eff352021-12-06 09:59:41 -080021#include <android-base/stringprintf.h>
22#include <ftl/concat.h>
23#include <utils/Trace.h>
24
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080025#include <scheduler/TimeKeeper.h>
26
Kevin DuBoise4f27a82019-11-12 11:41:41 -080027#include "VSyncDispatchTimerQueue.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070028#include "VSyncTracker.h"
29
30namespace android::scheduler {
Dominik Laskowski62eff352021-12-06 09:59:41 -080031
Ady Abraham5e7371c2020-03-24 14:47:24 -070032using base::StringAppendF;
Kevin DuBois305bef12019-10-09 13:23:27 -070033
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070034namespace {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080035
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070036nsecs_t getExpectedCallbackTime(nsecs_t nextVsyncTime,
37 const VSyncDispatch::ScheduleTiming& timing) {
38 return nextVsyncTime - timing.readyDuration - timing.workDuration;
39}
40
41nsecs_t getExpectedCallbackTime(VSyncTracker& tracker, nsecs_t now,
42 const VSyncDispatch::ScheduleTiming& timing) {
43 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
44 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
45 return getExpectedCallbackTime(nextVsyncTime, timing);
46}
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080047
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070048} // namespace
49
Kevin DuBoise4f27a82019-11-12 11:41:41 -080050VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070051VSyncTracker::~VSyncTracker() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070052
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080053VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string name,
54 VSyncDispatch::Callback callback,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080055 nsecs_t minVsyncDistance)
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080056 : mName(std::move(name)),
57 mCallback(std::move(callback)),
Kevin DuBoisc94ca832019-11-26 12:56:24 -080058 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070059
Kevin DuBoise4f27a82019-11-12 11:41:41 -080060std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070061 return mLastDispatchTime;
62}
63
Kevin DuBoise4f27a82019-11-12 11:41:41 -080064std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070065 return mName;
66}
67
Kevin DuBoise4f27a82019-11-12 11:41:41 -080068std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070069 if (!mArmedInfo) {
70 return {};
71 }
72 return {mArmedInfo->mActualWakeupTime};
73}
74
Ady Abraham9c53ee72020-07-22 21:16:18 -070075std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const {
76 if (!mArmedInfo) {
77 return {};
78 }
79 return {mArmedInfo->mActualReadyTime};
80}
81
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080082std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
83 if (!mArmedInfo) {
84 return {};
85 }
86 return {mArmedInfo->mActualVsyncTime};
87}
88
Ady Abraham9c53ee72020-07-22 21:16:18 -070089ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing,
Kevin DuBois2311b1a2019-11-18 16:19:08 -080090 VSyncTracker& tracker, nsecs_t now) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070091 auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
92 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
Ady Abraham69b9e622021-07-19 12:24:31 -070093 auto nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -080094
95 bool const wouldSkipAVsyncTarget =
96 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
Ady Abraham69b9e622021-07-19 12:24:31 -070097 bool const wouldSkipAWakeup =
98 mArmedInfo && ((nextWakeupTime > (mArmedInfo->mActualWakeupTime + mMinVsyncDistance)));
99 if (wouldSkipAVsyncTarget && wouldSkipAWakeup) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700100 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800101 }
102
103 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
104 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
105 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
106 if (alreadyDispatchedForVsync) {
107 nextVsyncTime =
108 tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
Ady Abraham69b9e622021-07-19 12:24:31 -0700109 nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800110 }
111
Ady Abraham9c53ee72020-07-22 21:16:18 -0700112 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
113 mScheduleTiming = timing;
114 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700115 return getExpectedCallbackTime(nextVsyncTime, timing);
Kevin DuBois305bef12019-10-09 13:23:27 -0700116}
117
Ady Abraham9c53ee72020-07-22 21:16:18 -0700118void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
119 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700120}
121
122bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
123 return mWorkloadUpdateInfo.has_value();
124}
125
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800126void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700127 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700128 return;
129 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700130
131 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700132 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700133 mWorkloadUpdateInfo.reset();
134 }
135
Ady Abraham9c53ee72020-07-22 21:16:18 -0700136 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
137 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
138
139 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(earliestVsync);
140 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
141 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
142
143 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700144}
145
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800146void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700147 mArmedInfo.reset();
148}
149
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800150nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700151 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
152 disarm();
153 return *mLastDispatchTime;
154}
155
Ady Abraham9c53ee72020-07-22 21:16:18 -0700156void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
157 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700158 {
159 std::lock_guard<std::mutex> lk(mRunningMutex);
160 mRunning = true;
161 }
162
Ady Abraham9c53ee72020-07-22 21:16:18 -0700163 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700164
165 std::lock_guard<std::mutex> lk(mRunningMutex);
166 mRunning = false;
167 mCv.notify_all();
168}
169
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800170void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700171 std::unique_lock<std::mutex> lk(mRunningMutex);
172 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
173}
174
Ady Abraham5e7371c2020-03-24 14:47:24 -0700175void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
176 std::lock_guard<std::mutex> lk(mRunningMutex);
177 std::string armedInfo;
178 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700179 StringAppendF(&armedInfo,
180 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700181 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700182 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700183 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
184 }
185
186 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
187 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700188 StringAppendF(&result,
189 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms earliestVsync: %.2fms relative "
190 "to now\n",
191 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
192 (mScheduleTiming.earliestVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700193
194 if (mLastDispatchTime) {
195 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
196 (systemTime() - *mLastDispatchTime) / 1e6f);
197 } else {
198 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
199 }
200}
201
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800202VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800203 VSyncTracker& tracker, nsecs_t timerSlack,
204 nsecs_t minVsyncDistance)
205 : mTimeKeeper(std::move(tk)),
206 mTracker(tracker),
207 mTimerSlack(timerSlack),
208 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700209
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800210VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700211 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700212 cancelTimer();
213}
214
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800215void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700216 mIntendedWakeupTime = kInvalidTime;
217 mTimeKeeper->alarmCancel();
218}
219
Ady Abrahamb491c902020-08-15 15:47:56 -0700220void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700221 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700222 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
223 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700224 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700225}
226
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800227void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700228 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
229}
230
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800231void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
232 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700233 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800234 std::optional<nsecs_t> targetVsync;
235 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700236 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
237 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700238 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700239 continue;
240 }
241
242 if (it != skipUpdateIt) {
243 callback->update(mTracker, now);
244 }
245 auto const wakeupTime = *callback->wakeupTime();
Dominik Laskowski62eff352021-12-06 09:59:41 -0800246 if (!min || *min > wakeupTime) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800247 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700248 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800249 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700250 }
251 }
252
Dominik Laskowski62eff352021-12-06 09:59:41 -0800253 if (min && min < mIntendedWakeupTime) {
254 if (ATRACE_ENABLED() && nextWakeupName && targetVsync) {
255 ftl::Concat trace(ftl::truncated<5>(*nextWakeupName), " alarm in ", ns2us(*min - now),
256 "us; VSYNC in ", ns2us(*targetVsync - now), "us");
257 ATRACE_NAME(trace.c_str());
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800258 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700259 setTimer(*min, now);
260 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800261 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700262 cancelTimer();
263 }
264}
265
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800266void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700267 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800268 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800269 nsecs_t vsyncTimestamp;
270 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700271 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700272 };
273 std::vector<Invocation> invocations;
274 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700275 std::lock_guard lock(mMutex);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700276 auto const now = mTimeKeeper->now();
277 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700278 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
279 auto& callback = it->second;
280 auto const wakeupTime = callback->wakeupTime();
281 if (!wakeupTime) {
282 continue;
283 }
284
Ady Abraham9c53ee72020-07-22 21:16:18 -0700285 auto const readyTime = callback->readyTime();
286
Kevin DuBoisf9477832020-07-16 10:21:36 -0700287 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
288 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700289 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700290 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
291 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700292 }
293 }
294
295 mIntendedWakeupTime = kInvalidTime;
296 rearmTimer(mTimeKeeper->now());
297 }
298
299 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700300 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
301 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700302 }
303}
304
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800305VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800306 Callback callback, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700307 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700308 return CallbackToken{
309 mCallbacks
310 .emplace(++mCallbackToken,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800311 std::make_shared<VSyncDispatchTimerQueueEntry>(std::move(callbackName),
312 std::move(callback),
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800313 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700314 .first->first};
315}
316
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800317void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
318 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700319 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700320 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700321 auto it = mCallbacks.find(token);
322 if (it != mCallbacks.end()) {
323 entry = it->second;
324 mCallbacks.erase(it);
325 }
326 }
327
328 if (entry) {
329 entry->ensureNotRunning();
330 }
331}
332
Ady Abraham9c53ee72020-07-22 21:16:18 -0700333ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
334 ScheduleTiming scheduleTiming) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700335 ScheduleResult result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700336 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700337 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700338
339 auto it = mCallbacks.find(token);
340 if (it == mCallbacks.end()) {
341 return result;
342 }
343 auto& callback = it->second;
Kevin DuBois305bef12019-10-09 13:23:27 -0700344 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700345
346 /* If the timer thread will run soon, we'll apply this work update via the callback
347 * timer recalculation to avoid cancelling a callback that is about to fire. */
348 auto const rearmImminent = now > mIntendedWakeupTime;
349 if (CC_UNLIKELY(rearmImminent)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700350 callback->addPendingWorkloadUpdate(scheduleTiming);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700351 return getExpectedCallbackTime(mTracker, now, scheduleTiming);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700352 }
353
Ady Abraham9c53ee72020-07-22 21:16:18 -0700354 result = callback->schedule(scheduleTiming, mTracker, now);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700355 if (!result.has_value()) {
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800356 return result;
Kevin DuBois305bef12019-10-09 13:23:27 -0700357 }
358
Kevin DuBois2311b1a2019-11-18 16:19:08 -0800359 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700360 rearmTimerSkippingUpdateFor(now, it);
361 }
362 }
363
364 return result;
365}
366
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800367CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700368 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700369
370 auto it = mCallbacks.find(token);
371 if (it == mCallbacks.end()) {
372 return CancelResult::Error;
373 }
374 auto& callback = it->second;
375
Kevin DuBoisb340b732020-06-16 09:07:35 -0700376 auto const wakeupTime = callback->wakeupTime();
377 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700378 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700379
380 if (*wakeupTime == mIntendedWakeupTime) {
381 mIntendedWakeupTime = kInvalidTime;
382 rearmTimer(mTimeKeeper->now());
383 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700384 return CancelResult::Cancelled;
385 }
386 return CancelResult::TooLate;
387}
388
Ady Abraham5e7371c2020-03-24 14:47:24 -0700389void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700390 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700391 StringAppendF(&result, "\tTimer:\n");
392 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700393 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
394 mMinVsyncDistance / 1e6f);
395 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700396 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
397 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
398 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
399 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700400 StringAppendF(&result, "\tCallbacks:\n");
401 for (const auto& [token, entry] : mCallbacks) {
402 entry->dump(result);
403 }
404}
405
Kevin DuBois305bef12019-10-09 13:23:27 -0700406VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800407 VSyncDispatch::Callback callback,
408 std::string callbackName)
Kevin DuBois305bef12019-10-09 13:23:27 -0700409 : mDispatch(dispatch),
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800410 mToken(dispatch.registerCallback(std::move(callback), std::move(callbackName))),
Kevin DuBois305bef12019-10-09 13:23:27 -0700411 mValidToken(true) {}
412
413VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
414 : mDispatch(other.mDispatch),
415 mToken(std::move(other.mToken)),
416 mValidToken(std::move(other.mValidToken)) {
417 other.mValidToken = false;
418}
419
420VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
421 mDispatch = std::move(other.mDispatch);
422 mToken = std::move(other.mToken);
423 mValidToken = std::move(other.mValidToken);
424 other.mValidToken = false;
425 return *this;
426}
427
428VSyncCallbackRegistration::~VSyncCallbackRegistration() {
429 if (mValidToken) mDispatch.get().unregisterCallback(mToken);
430}
431
Ady Abraham9c53ee72020-07-22 21:16:18 -0700432ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800433 if (!mValidToken) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700434 return std::nullopt;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800435 }
Ady Abraham9c53ee72020-07-22 21:16:18 -0700436 return mDispatch.get().schedule(mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700437}
438
439CancelResult VSyncCallbackRegistration::cancel() {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800440 if (!mValidToken) {
441 return CancelResult::Error;
442 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700443 return mDispatch.get().cancel(mToken);
444}
445
446} // namespace android::scheduler