blob: 533ccafcc868f5feabbf00872d90de2b10ef3b9d [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) {
Ady Abraham4335afd2023-12-18 19:10:47 -080048 const auto nextVsyncTime =
49 tracker.nextAnticipatedVSyncTimeFrom(std::max(timing.lastVsync,
50 now + timing.workDuration +
51 timing.readyDuration),
52 timing.lastVsync);
Ady Abrahamb0d3d982023-10-30 11:29:51 -070053 return getExpectedCallbackTime(nextVsyncTime, timing);
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070054}
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080055
Ady Abrahamb5d3afa2021-05-07 11:22:23 -070056} // namespace
57
Kevin DuBoise4f27a82019-11-12 11:41:41 -080058VSyncDispatch::~VSyncDispatch() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070059VSyncTracker::~VSyncTracker() = default;
Kevin DuBois305bef12019-10-09 13:23:27 -070060
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080061VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string name,
62 VSyncDispatch::Callback callback,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080063 nsecs_t minVsyncDistance)
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080064 : mName(std::move(name)),
65 mCallback(std::move(callback)),
Kevin DuBoisc94ca832019-11-26 12:56:24 -080066 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -070067
Kevin DuBoise4f27a82019-11-12 11:41:41 -080068std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070069 return mLastDispatchTime;
70}
71
Kevin DuBoise4f27a82019-11-12 11:41:41 -080072std::string_view VSyncDispatchTimerQueueEntry::name() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070073 return mName;
74}
75
Kevin DuBoise4f27a82019-11-12 11:41:41 -080076std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const {
Kevin DuBois305bef12019-10-09 13:23:27 -070077 if (!mArmedInfo) {
78 return {};
79 }
80 return {mArmedInfo->mActualWakeupTime};
81}
82
Ady Abraham9c53ee72020-07-22 21:16:18 -070083std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const {
84 if (!mArmedInfo) {
85 return {};
86 }
87 return {mArmedInfo->mActualReadyTime};
88}
89
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080090std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const {
91 if (!mArmedInfo) {
92 return {};
93 }
94 return {mArmedInfo->mActualVsyncTime};
95}
96
Ady Abraham9c53ee72020-07-22 21:16:18 -070097ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing,
Kevin DuBois2311b1a2019-11-18 16:19:08 -080098 VSyncTracker& tracker, nsecs_t now) {
Ady Abraham4335afd2023-12-18 19:10:47 -080099 auto nextVsyncTime =
100 tracker.nextAnticipatedVSyncTimeFrom(std::max(timing.lastVsync,
101 now + timing.workDuration +
102 timing.readyDuration),
103 timing.lastVsync);
Ady Abraham69b9e622021-07-19 12:24:31 -0700104 auto nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800105
106 bool const wouldSkipAVsyncTarget =
107 mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance));
Ady Abraham69b9e622021-07-19 12:24:31 -0700108 bool const wouldSkipAWakeup =
109 mArmedInfo && ((nextWakeupTime > (mArmedInfo->mActualWakeupTime + mMinVsyncDistance)));
Ady Abrahamd6d80162023-10-23 12:57:41 -0700110 if (FlagManager::getInstance().dont_skip_on_early()) {
Ady Abraham529bd9f2023-10-05 14:55:30 -0700111 if (wouldSkipAVsyncTarget || wouldSkipAWakeup) {
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700112 nextVsyncTime = mArmedInfo->mActualVsyncTime;
113 } else {
114 nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700115 }
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700116 nextWakeupTime = std::max(now, nextVsyncTime - timing.workDuration - timing.readyDuration);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700117 } else {
118 if (wouldSkipAVsyncTarget && wouldSkipAWakeup) {
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700119 return getExpectedCallbackTime(nextVsyncTime, timing);
Ady Abraham529bd9f2023-10-05 14:55:30 -0700120 }
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700121 nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime);
122 nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800123 }
124
Ady Abraham9c53ee72020-07-22 21:16:18 -0700125 auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
126 mScheduleTiming = timing;
127 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Ady Abrahamb0d3d982023-10-30 11:29:51 -0700128 return nextWakeupTime;
Kevin DuBois305bef12019-10-09 13:23:27 -0700129}
130
Ady Abraham9c53ee72020-07-22 21:16:18 -0700131void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming timing) {
132 mWorkloadUpdateInfo = timing;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700133}
134
135bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const {
136 return mWorkloadUpdateInfo.has_value();
137}
138
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700139nsecs_t VSyncDispatchTimerQueueEntry::adjustVsyncIfNeeded(VSyncTracker& tracker,
140 nsecs_t nextVsyncTime) const {
141 bool const alreadyDispatchedForVsync = mLastDispatchTime &&
142 ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
143 (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
144 const nsecs_t currentPeriod = tracker.currentPeriod();
145 bool const nextVsyncTooClose = mLastDispatchTime &&
146 (nextVsyncTime - *mLastDispatchTime + mMinVsyncDistance) <= currentPeriod;
147 if (alreadyDispatchedForVsync) {
Ady Abraham4335afd2023-12-18 19:10:47 -0800148 return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance,
149 *mLastDispatchTime);
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700150 }
151
152 if (nextVsyncTooClose) {
Ady Abraham4335afd2023-12-18 19:10:47 -0800153 return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + currentPeriod,
154 *mLastDispatchTime + currentPeriod);
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700155 }
156
157 return nextVsyncTime;
158}
159
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800160void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700161 if (!mArmedInfo && !mWorkloadUpdateInfo) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700162 return;
163 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700164
165 if (mWorkloadUpdateInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700166 mScheduleTiming = *mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700167 mWorkloadUpdateInfo.reset();
168 }
169
Ady Abraham9c53ee72020-07-22 21:16:18 -0700170 const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
Ady Abraham4335afd2023-12-18 19:10:47 -0800171 const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.lastVsync);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700172
Ady Abraham3fcfd8b2022-07-12 12:31:00 -0700173 const auto nextVsyncTime =
174 adjustVsyncIfNeeded(tracker, /*nextVsyncTime*/
Ady Abraham4335afd2023-12-18 19:10:47 -0800175 tracker.nextAnticipatedVSyncTimeFrom(earliestVsync,
176 mScheduleTiming.lastVsync));
Ady Abraham9c53ee72020-07-22 21:16:18 -0700177 const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
178 const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;
179
180 mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime};
Kevin DuBois305bef12019-10-09 13:23:27 -0700181}
182
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800183void VSyncDispatchTimerQueueEntry::disarm() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700184 mArmedInfo.reset();
185}
186
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800187nsecs_t VSyncDispatchTimerQueueEntry::executing() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700188 mLastDispatchTime = mArmedInfo->mActualVsyncTime;
189 disarm();
190 return *mLastDispatchTime;
191}
192
Ady Abraham9c53ee72020-07-22 21:16:18 -0700193void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp,
194 nsecs_t deadlineTimestamp) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700195 {
196 std::lock_guard<std::mutex> lk(mRunningMutex);
197 mRunning = true;
198 }
199
Ady Abraham9c53ee72020-07-22 21:16:18 -0700200 mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700201
202 std::lock_guard<std::mutex> lk(mRunningMutex);
203 mRunning = false;
204 mCv.notify_all();
205}
206
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800207void VSyncDispatchTimerQueueEntry::ensureNotRunning() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700208 std::unique_lock<std::mutex> lk(mRunningMutex);
209 mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; });
210}
211
Ady Abraham5e7371c2020-03-24 14:47:24 -0700212void VSyncDispatchTimerQueueEntry::dump(std::string& result) const {
213 std::lock_guard<std::mutex> lk(mRunningMutex);
214 std::string armedInfo;
215 if (mArmedInfo) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700216 StringAppendF(&armedInfo,
217 "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]",
Ady Abraham5e7371c2020-03-24 14:47:24 -0700218 (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f,
Ady Abraham9c53ee72020-07-22 21:16:18 -0700219 (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f,
Ady Abraham5e7371c2020-03-24 14:47:24 -0700220 (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f);
221 }
222
223 StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(),
224 mRunning ? "(in callback function)" : "", armedInfo.c_str());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700225 StringAppendF(&result,
Ady Abraham4335afd2023-12-18 19:10:47 -0800226 "\t\t\tworkDuration: %.2fms readyDuration: %.2fms lastVsync: %.2fms relative "
Ady Abraham9c53ee72020-07-22 21:16:18 -0700227 "to now\n",
228 mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f,
Ady Abraham4335afd2023-12-18 19:10:47 -0800229 (mScheduleTiming.lastVsync - systemTime()) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700230
231 if (mLastDispatchTime) {
232 StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n",
233 (systemTime() - *mLastDispatchTime) / 1e6f);
234 } else {
235 StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n");
236 }
237}
238
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800239VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk,
Leon Scroggins III67388622023-02-06 20:36:20 -0500240 VsyncSchedule::TrackerPtr tracker,
241 nsecs_t timerSlack, nsecs_t minVsyncDistance)
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800242 : mTimeKeeper(std::move(tk)),
Leon Scroggins III67388622023-02-06 20:36:20 -0500243 mTracker(std::move(tracker)),
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800244 mTimerSlack(timerSlack),
245 mMinVsyncDistance(minVsyncDistance) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700246
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800247VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() {
Ady Abraham8cb21882020-08-26 18:22:05 -0700248 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700249 cancelTimer();
Leon Scroggins III07738132023-04-24 11:43:53 -0400250 for (auto& [_, entry] : mCallbacks) {
251 ALOGE("Forgot to unregister a callback on VSyncDispatch!");
252 entry->ensureNotRunning();
253 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700254}
255
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800256void VSyncDispatchTimerQueue::cancelTimer() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700257 mIntendedWakeupTime = kInvalidTime;
258 mTimeKeeper->alarmCancel();
259}
260
Ady Abrahamb491c902020-08-15 15:47:56 -0700261void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700262 mIntendedWakeupTime = targetTime;
Ady Abrahamb491c902020-08-15 15:47:56 -0700263 mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this),
264 mIntendedWakeupTime);
Ady Abraham75398722020-04-07 14:08:45 -0700265 mLastTimerSchedule = mTimeKeeper->now();
Kevin DuBois305bef12019-10-09 13:23:27 -0700266}
267
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800268void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700269 rearmTimerSkippingUpdateFor(now, mCallbacks.end());
270}
271
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800272void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor(
273 nsecs_t now, CallbackMap::iterator const& skipUpdateIt) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700274 std::optional<nsecs_t> min;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800275 std::optional<nsecs_t> targetVsync;
276 std::optional<std::string_view> nextWakeupName;
Kevin DuBois305bef12019-10-09 13:23:27 -0700277 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
278 auto& callback = it->second;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700279 if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700280 continue;
281 }
282
283 if (it != skipUpdateIt) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500284 callback->update(*mTracker, now);
Kevin DuBois305bef12019-10-09 13:23:27 -0700285 }
286 auto const wakeupTime = *callback->wakeupTime();
Dominik Laskowski62eff352021-12-06 09:59:41 -0800287 if (!min || *min > wakeupTime) {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800288 nextWakeupName = callback->name();
Kevin DuBois305bef12019-10-09 13:23:27 -0700289 min = wakeupTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800290 targetVsync = callback->targetVsync();
Kevin DuBois305bef12019-10-09 13:23:27 -0700291 }
292 }
293
Dominik Laskowski62eff352021-12-06 09:59:41 -0800294 if (min && min < mIntendedWakeupTime) {
295 if (ATRACE_ENABLED() && nextWakeupName && targetVsync) {
296 ftl::Concat trace(ftl::truncated<5>(*nextWakeupName), " alarm in ", ns2us(*min - now),
297 "us; VSYNC in ", ns2us(*targetVsync - now), "us");
298 ATRACE_NAME(trace.c_str());
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800299 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700300 setTimer(*min, now);
301 } else {
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800302 ATRACE_NAME("cancel timer");
Kevin DuBois305bef12019-10-09 13:23:27 -0700303 cancelTimer();
304 }
305}
306
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800307void VSyncDispatchTimerQueue::timerCallback() {
Kevin DuBois305bef12019-10-09 13:23:27 -0700308 struct Invocation {
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800309 std::shared_ptr<VSyncDispatchTimerQueueEntry> callback;
Kevin DuBois2968afc2020-01-14 09:48:50 -0800310 nsecs_t vsyncTimestamp;
311 nsecs_t wakeupTimestamp;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700312 nsecs_t deadlineTimestamp;
Kevin DuBois305bef12019-10-09 13:23:27 -0700313 };
314 std::vector<Invocation> invocations;
315 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700316 std::lock_guard lock(mMutex);
Kevin DuBoisf9477832020-07-16 10:21:36 -0700317 auto const now = mTimeKeeper->now();
318 mLastTimerCallback = now;
Kevin DuBois305bef12019-10-09 13:23:27 -0700319 for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
320 auto& callback = it->second;
321 auto const wakeupTime = callback->wakeupTime();
322 if (!wakeupTime) {
323 continue;
324 }
325
Ady Abraham9c53ee72020-07-22 21:16:18 -0700326 auto const readyTime = callback->readyTime();
327
Kevin DuBoisf9477832020-07-16 10:21:36 -0700328 auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0));
329 if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700330 callback->executing();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700331 invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(),
332 *wakeupTime, *readyTime});
Kevin DuBois305bef12019-10-09 13:23:27 -0700333 }
334 }
335
336 mIntendedWakeupTime = kInvalidTime;
337 rearmTimer(mTimeKeeper->now());
338 }
339
340 for (auto const& invocation : invocations) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700341 invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp,
342 invocation.deadlineTimestamp);
Kevin DuBois305bef12019-10-09 13:23:27 -0700343 }
344}
345
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800346VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback(
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800347 Callback callback, std::string callbackName) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700348 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700349 return CallbackToken{
350 mCallbacks
351 .emplace(++mCallbackToken,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800352 std::make_shared<VSyncDispatchTimerQueueEntry>(std::move(callbackName),
353 std::move(callback),
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800354 mMinVsyncDistance))
Kevin DuBois305bef12019-10-09 13:23:27 -0700355 .first->first};
356}
357
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800358void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) {
359 std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr;
Kevin DuBois305bef12019-10-09 13:23:27 -0700360 {
Ady Abraham8cb21882020-08-26 18:22:05 -0700361 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700362 auto it = mCallbacks.find(token);
363 if (it != mCallbacks.end()) {
364 entry = it->second;
365 mCallbacks.erase(it);
366 }
367 }
368
369 if (entry) {
370 entry->ensureNotRunning();
371 }
372}
373
Ady Abraham9c53ee72020-07-22 21:16:18 -0700374ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token,
375 ScheduleTiming scheduleTiming) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800376 std::lock_guard lock(mMutex);
377 return scheduleLocked(token, scheduleTiming);
378}
Kevin DuBois305bef12019-10-09 13:23:27 -0700379
Ady Abraham011f8ba2022-11-22 15:09:07 -0800380ScheduleResult VSyncDispatchTimerQueue::scheduleLocked(CallbackToken token,
381 ScheduleTiming scheduleTiming) {
382 auto it = mCallbacks.find(token);
383 if (it == mCallbacks.end()) {
384 return {};
385 }
386 auto& callback = it->second;
387 auto const now = mTimeKeeper->now();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700388
Ady Abraham011f8ba2022-11-22 15:09:07 -0800389 /* If the timer thread will run soon, we'll apply this work update via the callback
390 * timer recalculation to avoid cancelling a callback that is about to fire. */
391 auto const rearmImminent = now > mIntendedWakeupTime;
392 if (CC_UNLIKELY(rearmImminent)) {
393 callback->addPendingWorkloadUpdate(scheduleTiming);
Leon Scroggins III67388622023-02-06 20:36:20 -0500394 return getExpectedCallbackTime(*mTracker, now, scheduleTiming);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800395 }
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700396
Leon Scroggins III67388622023-02-06 20:36:20 -0500397 const ScheduleResult result = callback->schedule(scheduleTiming, *mTracker, now);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800398 if (!result.has_value()) {
399 return {};
400 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700401
Ady Abraham011f8ba2022-11-22 15:09:07 -0800402 if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) {
403 rearmTimerSkippingUpdateFor(now, it);
Kevin DuBois305bef12019-10-09 13:23:27 -0700404 }
405
406 return result;
407}
408
Ady Abraham011f8ba2022-11-22 15:09:07 -0800409ScheduleResult VSyncDispatchTimerQueue::update(CallbackToken token, ScheduleTiming scheduleTiming) {
410 std::lock_guard lock(mMutex);
411 const auto it = mCallbacks.find(token);
412 if (it == mCallbacks.end()) {
413 return {};
414 }
415
416 auto& callback = it->second;
417 if (!callback->targetVsync().has_value()) {
418 return {};
419 }
420
421 return scheduleLocked(token, scheduleTiming);
422}
423
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800424CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) {
Ady Abraham8cb21882020-08-26 18:22:05 -0700425 std::lock_guard lock(mMutex);
Kevin DuBois305bef12019-10-09 13:23:27 -0700426
427 auto it = mCallbacks.find(token);
428 if (it == mCallbacks.end()) {
429 return CancelResult::Error;
430 }
431 auto& callback = it->second;
432
Kevin DuBoisb340b732020-06-16 09:07:35 -0700433 auto const wakeupTime = callback->wakeupTime();
434 if (wakeupTime) {
Kevin DuBois305bef12019-10-09 13:23:27 -0700435 callback->disarm();
Kevin DuBoisb340b732020-06-16 09:07:35 -0700436
437 if (*wakeupTime == mIntendedWakeupTime) {
438 mIntendedWakeupTime = kInvalidTime;
439 rearmTimer(mTimeKeeper->now());
440 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700441 return CancelResult::Cancelled;
442 }
443 return CancelResult::TooLate;
444}
445
Ady Abraham5e7371c2020-03-24 14:47:24 -0700446void VSyncDispatchTimerQueue::dump(std::string& result) const {
Ady Abraham8cb21882020-08-26 18:22:05 -0700447 std::lock_guard lock(mMutex);
Ady Abraham75398722020-04-07 14:08:45 -0700448 StringAppendF(&result, "\tTimer:\n");
449 mTimeKeeper->dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700450 StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f,
451 mMinVsyncDistance / 1e6f);
452 StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n",
Ady Abraham75398722020-04-07 14:08:45 -0700453 (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f);
454 StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n",
455 (mTimeKeeper->now() - mLastTimerCallback) / 1e6f,
456 (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700457 StringAppendF(&result, "\tCallbacks:\n");
458 for (const auto& [token, entry] : mCallbacks) {
459 entry->dump(result);
460 }
461}
462
Leon Scroggins III67388622023-02-06 20:36:20 -0500463VSyncCallbackRegistration::VSyncCallbackRegistration(std::shared_ptr<VSyncDispatch> dispatch,
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800464 VSyncDispatch::Callback callback,
465 std::string callbackName)
Leon Scroggins III67388622023-02-06 20:36:20 -0500466 : mDispatch(std::move(dispatch)),
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400467 mToken(mDispatch->registerCallback(std::move(callback), std::move(callbackName))) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700468
469VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other)
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400470 : mDispatch(std::move(other.mDispatch)), mToken(std::exchange(other.mToken, std::nullopt)) {}
Kevin DuBois305bef12019-10-09 13:23:27 -0700471
472VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400473 if (this == &other) return *this;
474 if (mToken) {
475 mDispatch->unregisterCallback(*mToken);
476 }
Kevin DuBois305bef12019-10-09 13:23:27 -0700477 mDispatch = std::move(other.mDispatch);
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400478 mToken = std::exchange(other.mToken, std::nullopt);
Kevin DuBois305bef12019-10-09 13:23:27 -0700479 return *this;
480}
481
482VSyncCallbackRegistration::~VSyncCallbackRegistration() {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400483 if (mToken) mDispatch->unregisterCallback(*mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700484}
485
Ady Abraham9c53ee72020-07-22 21:16:18 -0700486ScheduleResult VSyncCallbackRegistration::schedule(VSyncDispatch::ScheduleTiming scheduleTiming) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400487 if (!mToken) {
Ady Abrahamb5d3afa2021-05-07 11:22:23 -0700488 return std::nullopt;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800489 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400490 return mDispatch->schedule(*mToken, scheduleTiming);
Kevin DuBois305bef12019-10-09 13:23:27 -0700491}
492
Ady Abraham011f8ba2022-11-22 15:09:07 -0800493ScheduleResult VSyncCallbackRegistration::update(VSyncDispatch::ScheduleTiming scheduleTiming) {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400494 if (!mToken) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800495 return std::nullopt;
496 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400497 return mDispatch->update(*mToken, scheduleTiming);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800498}
499
Kevin DuBois305bef12019-10-09 13:23:27 -0700500CancelResult VSyncCallbackRegistration::cancel() {
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400501 if (!mToken) {
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800502 return CancelResult::Error;
503 }
Leon Scroggins III6c440ae2023-04-21 15:01:03 -0400504 return mDispatch->cancel(*mToken);
Kevin DuBois305bef12019-10-09 13:23:27 -0700505}
506
507} // namespace android::scheduler