blob: 9d271dc8421e39c3af3d02b892be3366f7936264 [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>
Leon Scroggins III07738132023-04-24 11:43:53 -040024#include <log/log_main.h>
Dominik Laskowski62eff352021-12-06 09:59:41 -080025
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026#include <scheduler/TimeKeeper.h>
27
Alec Mouri9b133ca2023-11-14 19:00:01 +000028#include <common/FlagManager.h>
Kevin DuBoise4f27a82019-11-12 11:41:41 -080029#include "VSyncDispatchTimerQueue.h"
Kevin DuBois305bef12019-10-09 13:23:27 -070030#include "VSyncTracker.h"
31
Leon Scroggins III07738132023-04-24 11:43:53 -040032#undef LOG_TAG
33#define LOG_TAG "VSyncDispatch"
34
Kevin DuBois305bef12019-10-09 13:23:27 -070035namespace android::scheduler {
Dominik Laskowski62eff352021-12-06 09:59:41 -080036
Ady Abraham5e7371c2020-03-24 14:47:24 -070037using base::StringAppendF;
Kevin DuBois305bef12019-10-09 13:23:27 -070038
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070039namespace {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080040
Ady Abrahamb0d3d982023-10-30 11:29:51 -070041nsecs_t getExpectedCallbackTime(nsecs_t nextVsyncTime,
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070042 const VSyncDispatch::ScheduleTiming& timing) {
Ady Abrahamb0d3d982023-10-30 11:29:51 -070043 return nextVsyncTime - timing.readyDuration - timing.workDuration;
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070044}
45
46nsecs_t getExpectedCallbackTime(VSyncTracker& tracker, nsecs_t now,
47 const VSyncDispatch::ScheduleTiming& timing) {
48 const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
49 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
Ady Abrahamb0d3d982023-10-30 11:29:51 -070050 return getExpectedCallbackTime(nextVsyncTime, timing);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070051}
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080052
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070053} // namespace
54
Kevin DuBoise4f27a82019-11-12 11:41:41 -080055VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070056VSyncTracker::~VSyncTracker() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070057
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080058VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string name,
59 VSyncDispatch::Callback callback,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080060 nsecs_t minVsyncDistance)
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080061 : mName(std::move(name)),
62 mCallback(std::move(callback)),
Kevin DuBoisc94ca832019-11-26 12:56:24 -080063 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070064
Kevin DuBoise4f27a82019-11-12 11:41:41 -080065std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070066 return mLastDispatchTime;
67}
68
Kevin DuBoise4f27a82019-11-12 11:41:41 -080069std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070070 return mName;
71}
72
Kevin DuBoise4f27a82019-11-12 11:41:41 -080073std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070074 if (!mArmedInfo) {
75 return {};
76 }
77 return {mArmedInfo->mActualWakeupTime};
78}
79
Ady Abraham9c53ee72020-07-22 21:16:18 -070080std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const {
81 if (!mArmedInfo) {
82 return {};
83 }
84 return {mArmedInfo->mActualReadyTime};
85}
86
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080087std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
88 if (!mArmedInfo) {
89 return {};
90 }
91 return {mArmedInfo->mActualVsyncTime};
92}
93
Ady Abraham9c53ee72020-07-22 21:16:18 -070094ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing,
Kevin DuBois2311b1a2019-11-18 16:19:08 -080095 VSyncTracker& tracker, nsecs_t now) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070096 auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(
97 std::max(timing.earliestVsync, now + timing.workDuration + timing.readyDuration));
Ady Abraham69b9e622021-07-19 12:24:31 -070098 auto nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -080099
100 bool const wouldSkipAVsyncTarget =
101 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
Ady Abraham69b9e622021-07-19 12:24:31 -0700102 bool const wouldSkipAWakeup =
103 mArmedInfo && ((nextWakeupTime > (mArmedInfo->mActualWakeupTime + mMinVsyncDistance)));
Ady Abrahamd6d80162023-10-23 12:57:41 -0700104 if (FlagManager::getInstance().dont_skip_on_early()) {
Ady Abraham529bd9f2023-10-05 14:55:30 -0700105 if (wouldSkipAVsyncTarget || wouldSkipAWakeup) {
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700106 nextVsyncTime = mArmedInfo->mActualVsyncTime;
107 } else {
108 nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700109 }
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700110 nextWakeupTime = std::max(now, nextVsyncTime - timing.workDuration - timing.readyDuration);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700111 } else {
112 if (wouldSkipAVsyncTarget && wouldSkipAWakeup) {
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700113 return getExpectedCallbackTime(nextVsyncTime, timing);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700114 }
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700115 nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime);
116 nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800117 }
118
Ady Abraham9c53ee72020-07-22 21:16:18 -0700119 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
120 mScheduleTiming = timing;
121 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700122 return nextWakeupTime;
Kevin DuBois305bef12019-10-09 13:23:27 -0700123}
124
Ady Abraham9c53ee72020-07-22 21:16:18 -0700125void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
126 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700127}
128
129bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
130 return mWorkloadUpdateInfo.has_value();
131}
132
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700133nsecs_t VSyncDispatchTimerQueueEntry::adjustVsyncIfNeeded(VSyncTracker& tracker,
134 nsecs_t nextVsyncTime) const {
135 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
136 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
137 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
138 const nsecs_t currentPeriod = tracker.currentPeriod();
139 bool const nextVsyncTooClose = mLastDispatchTime &&
140 (nextVsyncTime - *mLastDispatchTime + mMinVsyncDistance) <= currentPeriod;
141 if (alreadyDispatchedForVsync) {
142 return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
143 }
144
145 if (nextVsyncTooClose) {
146 return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + currentPeriod);
147 }
148
149 return nextVsyncTime;
150}
151
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800152void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700153 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700154 return;
155 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700156
157 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700158 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700159 mWorkloadUpdateInfo.reset();
160 }
161
Ady Abraham9c53ee72020-07-22 21:16:18 -0700162 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
163 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
164
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700165 const auto nextVsyncTime =
166 adjustVsyncIfNeeded(tracker, /*nextVsyncTime*/
167 tracker.nextAnticipatedVSyncTimeFrom(earliestVsync));
Ady Abraham9c53ee72020-07-22 21:16:18 -0700168 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
169 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
170
171 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700172}
173
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800174void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700175 mArmedInfo.reset();
176}
177
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800178nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700179 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
180 disarm();
181 return *mLastDispatchTime;
182}
183
Ady Abraham9c53ee72020-07-22 21:16:18 -0700184void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
185 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700186 {
187 std::lock_guard<std::mutex> lk(mRunningMutex);
188 mRunning = true;
189 }
190
Ady Abraham9c53ee72020-07-22 21:16:18 -0700191 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700192
193 std::lock_guard<std::mutex> lk(mRunningMutex);
194 mRunning = false;
195 mCv.notify_all();
196}
197
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800198void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700199 std::unique_lock<std::mutex> lk(mRunningMutex);
200 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
201}
202
Ady Abraham5e7371c2020-03-24 14:47:24 -0700203void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
204 std::lock_guard<std::mutex> lk(mRunningMutex);
205 std::string armedInfo;
206 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700207 StringAppendF(&armedInfo,
208 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700209 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700210 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700211 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
212 }
213
214 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
215 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700216 StringAppendF(&result,
217 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms earliestVsync: %.2fms relative "
218 "to now\n",
219 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
220 (mScheduleTiming.earliestVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700221
222 if (mLastDispatchTime) {
223 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
224 (systemTime() - *mLastDispatchTime) / 1e6f);
225 } else {
226 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
227 }
228}
229
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800230VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Leon Scroggins III67388622023-02-06 20:36:20 -0500231 VsyncSchedule::TrackerPtr tracker,
232 nsecs_t timerSlack, nsecs_t minVsyncDistance)
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800233 : mTimeKeeper(std::move(tk)),
Leon Scroggins III67388622023-02-06 20:36:20 -0500234 mTracker(std::move(tracker)),
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800235 mTimerSlack(timerSlack),
236 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700237
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800238VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700239 std::lock_guard lock(mMutex);
en.liu07e0e292023-07-05 19:01:52 +0800240 mRunning = false;
Kevin DuBois305bef12019-10-09 13:23:27 -0700241 cancelTimer();
Leon Scroggins III07738132023-04-24 11:43:53 -0400242 for (auto& [_, entry] : mCallbacks) {
243 ALOGE("Forgot to unregister a callback on VSyncDispatch!");
244 entry->ensureNotRunning();
245 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700246}
247
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800248void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700249 mIntendedWakeupTime = kInvalidTime;
250 mTimeKeeper->alarmCancel();
251}
252
Ady Abrahamb491c902020-08-15 15:47:56 -0700253void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700254 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700255 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
256 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700257 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700258}
259
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800260void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700261 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
262}
263
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800264void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
265 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700266 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800267 std::optional<nsecs_t> targetVsync;
268 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700269 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
270 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700271 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700272 continue;
273 }
274
275 if (it != skipUpdateIt) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500276 callback->update(*mTracker, now);
Kevin DuBois305bef12019-10-09 13:23:27 -0700277 }
278 auto const wakeupTime = *callback->wakeupTime();
Dominik Laskowski62eff352021-12-06 09:59:41 -0800279 if (!min || *min > wakeupTime) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800280 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700281 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800282 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700283 }
284 }
285
Dominik Laskowski62eff352021-12-06 09:59:41 -0800286 if (min && min < mIntendedWakeupTime) {
287 if (ATRACE_ENABLED() && nextWakeupName && targetVsync) {
288 ftl::Concat trace(ftl::truncated<5>(*nextWakeupName), " alarm in ", ns2us(*min - now),
289 "us; VSYNC in ", ns2us(*targetVsync - now), "us");
290 ATRACE_NAME(trace.c_str());
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800291 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700292 setTimer(*min, now);
293 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800294 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700295 cancelTimer();
296 }
297}
298
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800299void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700300 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800301 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800302 nsecs_t vsyncTimestamp;
303 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700304 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700305 };
306 std::vector<Invocation> invocations;
307 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700308 std::lock_guard lock(mMutex);
en.liu07e0e292023-07-05 19:01:52 +0800309 if (!mRunning) {
310 ALOGD("TimerQueue is not running. Skipping callback.");
311 return;
312 }
Kevin DuBoisf9477832020-07-16 10:21:36 -0700313 auto const now = mTimeKeeper->now();
314 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700315 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
316 auto& callback = it->second;
317 auto const wakeupTime = callback->wakeupTime();
318 if (!wakeupTime) {
319 continue;
320 }
321
Ady Abraham9c53ee72020-07-22 21:16:18 -0700322 auto const readyTime = callback->readyTime();
323
Kevin DuBoisf9477832020-07-16 10:21:36 -0700324 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
325 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700326 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700327 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
328 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700329 }
330 }
331
332 mIntendedWakeupTime = kInvalidTime;
333 rearmTimer(mTimeKeeper->now());
334 }
335
336 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700337 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
338 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700339 }
340}
341
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800342VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800343 Callback callback, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700344 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700345 return CallbackToken{
346 mCallbacks
347 .emplace(++mCallbackToken,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800348 std::make_shared<VSyncDispatchTimerQueueEntry>(std::move(callbackName),
349 std::move(callback),
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800350 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700351 .first->first};
352}
353
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800354void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
355 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700356 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700357 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700358 auto it = mCallbacks.find(token);
359 if (it != mCallbacks.end()) {
360 entry = it->second;
361 mCallbacks.erase(it);
362 }
363 }
364
365 if (entry) {
366 entry->ensureNotRunning();
367 }
368}
369
Ady Abraham9c53ee72020-07-22 21:16:18 -0700370ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
371 ScheduleTiming scheduleTiming) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800372 std::lock_guard lock(mMutex);
373 return scheduleLocked(token, scheduleTiming);
374}
Kevin DuBois305bef12019-10-09 13:23:27 -0700375
Ady Abraham011f8ba2022-11-22 15:09:07 -0800376ScheduleResult VSyncDispatchTimerQueue::scheduleLocked(CallbackToken token,
377 ScheduleTiming scheduleTiming) {
378 auto it = mCallbacks.find(token);
379 if (it == mCallbacks.end()) {
380 return {};
381 }
382 auto& callback = it->second;
383 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700384
Ady Abraham011f8ba2022-11-22 15:09:07 -0800385 /* If the timer thread will run soon, we'll apply this work update via the callback
386 * timer recalculation to avoid cancelling a callback that is about to fire. */
387 auto const rearmImminent = now > mIntendedWakeupTime;
388 if (CC_UNLIKELY(rearmImminent)) {
389 callback->addPendingWorkloadUpdate(scheduleTiming);
Leon Scroggins III67388622023-02-06 20:36:20 -0500390 return getExpectedCallbackTime(*mTracker, now, scheduleTiming);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800391 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700392
Leon Scroggins III67388622023-02-06 20:36:20 -0500393 const ScheduleResult result = callback->schedule(scheduleTiming, *mTracker, now);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800394 if (!result.has_value()) {
395 return {};
396 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700397
Ady Abraham011f8ba2022-11-22 15:09:07 -0800398 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
399 rearmTimerSkippingUpdateFor(now, it);
Kevin DuBois305bef12019-10-09 13:23:27 -0700400 }
401
402 return result;
403}
404
Ady Abraham011f8ba2022-11-22 15:09:07 -0800405ScheduleResult VSyncDispatchTimerQueue::update(CallbackToken token, ScheduleTiming scheduleTiming) {
406 std::lock_guard lock(mMutex);
407 const auto it = mCallbacks.find(token);
408 if (it == mCallbacks.end()) {
409 return {};
410 }
411
412 auto& callback = it->second;
413 if (!callback->targetVsync().has_value()) {
414 return {};
415 }
416
417 return scheduleLocked(token, scheduleTiming);
418}
419
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800420CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700421 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700422
423 auto it = mCallbacks.find(token);
424 if (it == mCallbacks.end()) {
425 return CancelResult::Error;
426 }
427 auto& callback = it->second;
428
Kevin DuBoisb340b732020-06-16 09:07:35 -0700429 auto const wakeupTime = callback->wakeupTime();
430 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700431 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700432
433 if (*wakeupTime == mIntendedWakeupTime) {
434 mIntendedWakeupTime = kInvalidTime;
435 rearmTimer(mTimeKeeper->now());
436 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700437 return CancelResult::Cancelled;
438 }
439 return CancelResult::TooLate;
440}
441
Ady Abraham5e7371c2020-03-24 14:47:24 -0700442void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700443 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700444 StringAppendF(&result, "\tTimer:\n");
445 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700446 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
447 mMinVsyncDistance / 1e6f);
448 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700449 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
450 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
451 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
452 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700453 StringAppendF(&result, "\tCallbacks:\n");
454 for (const auto& [token, entry] : mCallbacks) {
455 entry->dump(result);
456 }
457}
458
Leon Scroggins III67388622023-02-06 20:36:20 -0500459VSyncCallbackRegistration::VSyncCallbackRegistration(std::shared_ptr<VSyncDispatch> dispatch,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800460 VSyncDispatch::Callback callback,
461 std::string callbackName)
Leon Scroggins III67388622023-02-06 20:36:20 -0500462 : mDispatch(std::move(dispatch)),
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400463 mToken(mDispatch->registerCallback(std::move(callback), std::move(callbackName))) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700464
465VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400466 : mDispatch(std::move(other.mDispatch)), mToken(std::exchange(other.mToken, std::nullopt)) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700467
468VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400469 if (this == &other) return *this;
470 if (mToken) {
471 mDispatch->unregisterCallback(*mToken);
472 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700473 mDispatch = std::move(other.mDispatch);
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400474 mToken = std::exchange(other.mToken, std::nullopt);
Kevin DuBois305bef12019-10-09 13:23:27 -0700475 return *this;
476}
477
478VSyncCallbackRegistration::~VSyncCallbackRegistration() {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400479 if (mToken) mDispatch->unregisterCallback(*mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700480}
481
Ady Abraham9c53ee72020-07-22 21:16:18 -0700482ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400483 if (!mToken) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700484 return std::nullopt;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800485 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400486 return mDispatch->schedule(*mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700487}
488
Ady Abraham011f8ba2022-11-22 15:09:07 -0800489ScheduleResult VSyncCallbackRegistration::update(VSyncDispatch::ScheduleTiming scheduleTiming) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400490 if (!mToken) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800491 return std::nullopt;
492 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400493 return mDispatch->update(*mToken, scheduleTiming);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800494}
495
Kevin DuBois305bef12019-10-09 13:23:27 -0700496CancelResult VSyncCallbackRegistration::cancel() {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400497 if (!mToken) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800498 return CancelResult::Error;
499 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400500 return mDispatch->cancel(*mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700501}
502
503} // namespace android::scheduler