Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 1 | /* |
| 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 Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 18 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 22 | #include <common/trace.h> |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 23 | #include <ftl/concat.h> |
Leon Scroggins III | 0773813 | 2023-04-24 11:43:53 -0400 | [diff] [blame] | 24 | #include <log/log_main.h> |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 25 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 26 | #include <scheduler/TimeKeeper.h> |
| 27 | |
Alec Mouri | 9b133ca | 2023-11-14 19:00:01 +0000 | [diff] [blame] | 28 | #include <common/FlagManager.h> |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 29 | #include "VSyncDispatchTimerQueue.h" |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 30 | #include "VSyncTracker.h" |
| 31 | |
Leon Scroggins III | 0773813 | 2023-04-24 11:43:53 -0400 | [diff] [blame] | 32 | #undef LOG_TAG |
| 33 | #define LOG_TAG "VSyncDispatch" |
| 34 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 35 | namespace android::scheduler { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 36 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 37 | using base::StringAppendF; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 38 | |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 39 | namespace { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 40 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 41 | ScheduleResult getExpectedCallbackTime(nsecs_t nextVsyncTime, |
| 42 | const VSyncDispatch::ScheduleTiming& timing) { |
| 43 | return {TimePoint::fromNs(nextVsyncTime - timing.readyDuration - timing.workDuration), |
| 44 | TimePoint::fromNs(nextVsyncTime)}; |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 47 | void traceEntry(const VSyncDispatchTimerQueueEntry& entry, nsecs_t now) { |
Vishnu Nair | 2665ca9 | 2024-07-09 22:08:15 +0000 | [diff] [blame^] | 48 | if (!SFTRACE_ENABLED() || !entry.wakeupTime().has_value() || !entry.targetVsync().has_value()) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
| 52 | ftl::Concat trace(ftl::truncated<5>(entry.name()), " alarm in ", |
| 53 | ns2us(*entry.wakeupTime() - now), "us; VSYNC in ", |
| 54 | ns2us(*entry.targetVsync() - now), "us"); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 55 | SFTRACE_FORMAT_INSTANT(trace.c_str()); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 58 | } // namespace |
| 59 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 60 | VSyncDispatch::~VSyncDispatch() = default; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 61 | VSyncTracker::~VSyncTracker() = default; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 62 | |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 63 | VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string name, |
| 64 | VSyncDispatch::Callback callback, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 65 | nsecs_t minVsyncDistance) |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 66 | : mName(std::move(name)), |
| 67 | mCallback(std::move(callback)), |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 68 | mMinVsyncDistance(minVsyncDistance) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 69 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 70 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 71 | return mLastDispatchTime; |
| 72 | } |
| 73 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 74 | std::string_view VSyncDispatchTimerQueueEntry::name() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 75 | return mName; |
| 76 | } |
| 77 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 78 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 79 | if (!mArmedInfo) { |
| 80 | return {}; |
| 81 | } |
| 82 | return {mArmedInfo->mActualWakeupTime}; |
| 83 | } |
| 84 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 85 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::readyTime() const { |
| 86 | if (!mArmedInfo) { |
| 87 | return {}; |
| 88 | } |
| 89 | return {mArmedInfo->mActualReadyTime}; |
| 90 | } |
| 91 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 92 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const { |
| 93 | if (!mArmedInfo) { |
| 94 | return {}; |
| 95 | } |
| 96 | return {mArmedInfo->mActualVsyncTime}; |
| 97 | } |
| 98 | |
ramindani | 558f4a9 | 2024-02-16 15:49:23 -0800 | [diff] [blame] | 99 | ScheduleResult VSyncDispatchTimerQueueEntry::schedule(VSyncDispatch::ScheduleTiming timing, |
| 100 | VSyncTracker& tracker, nsecs_t now) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 101 | SFTRACE_NAME("VSyncDispatchTimerQueueEntry::schedule"); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 102 | auto nextVsyncTime = |
| 103 | tracker.nextAnticipatedVSyncTimeFrom(std::max(timing.lastVsync, |
| 104 | now + timing.workDuration + |
| 105 | timing.readyDuration), |
| 106 | timing.lastVsync); |
Ady Abraham | 69b9e62 | 2021-07-19 12:24:31 -0700 | [diff] [blame] | 107 | auto nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 108 | |
| 109 | bool const wouldSkipAVsyncTarget = |
| 110 | mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance)); |
Ady Abraham | 69b9e62 | 2021-07-19 12:24:31 -0700 | [diff] [blame] | 111 | bool const wouldSkipAWakeup = |
| 112 | mArmedInfo && ((nextWakeupTime > (mArmedInfo->mActualWakeupTime + mMinVsyncDistance))); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 113 | SFTRACE_FORMAT_INSTANT("%s: wouldSkipAVsyncTarget=%d wouldSkipAWakeup=%d", mName.c_str(), |
| 114 | wouldSkipAVsyncTarget, wouldSkipAWakeup); |
Ady Abraham | bf55489 | 2024-02-14 18:18:21 +0000 | [diff] [blame] | 115 | if (FlagManager::getInstance().dont_skip_on_early_ro()) { |
Ady Abraham | 529bd9f | 2023-10-05 14:55:30 -0700 | [diff] [blame] | 116 | if (wouldSkipAVsyncTarget || wouldSkipAWakeup) { |
Ady Abraham | b0d3d98 | 2023-10-30 11:29:51 -0700 | [diff] [blame] | 117 | nextVsyncTime = mArmedInfo->mActualVsyncTime; |
| 118 | } else { |
| 119 | nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime); |
Ady Abraham | 529bd9f | 2023-10-05 14:55:30 -0700 | [diff] [blame] | 120 | } |
Ady Abraham | b0d3d98 | 2023-10-30 11:29:51 -0700 | [diff] [blame] | 121 | nextWakeupTime = std::max(now, nextVsyncTime - timing.workDuration - timing.readyDuration); |
Ady Abraham | 529bd9f | 2023-10-05 14:55:30 -0700 | [diff] [blame] | 122 | } else { |
| 123 | if (wouldSkipAVsyncTarget && wouldSkipAWakeup) { |
Ady Abraham | b0d3d98 | 2023-10-30 11:29:51 -0700 | [diff] [blame] | 124 | return getExpectedCallbackTime(nextVsyncTime, timing); |
Ady Abraham | 529bd9f | 2023-10-05 14:55:30 -0700 | [diff] [blame] | 125 | } |
Ady Abraham | b0d3d98 | 2023-10-30 11:29:51 -0700 | [diff] [blame] | 126 | nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime); |
| 127 | nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 130 | auto const nextReadyTime = nextVsyncTime - timing.readyDuration; |
| 131 | mScheduleTiming = timing; |
| 132 | mArmedInfo = {nextWakeupTime, nextVsyncTime, nextReadyTime}; |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 133 | return ScheduleResult{TimePoint::fromNs(nextWakeupTime), TimePoint::fromNs(nextVsyncTime)}; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 136 | ScheduleResult VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate( |
Ady Abraham | da8af4c | 2024-02-14 00:24:34 +0000 | [diff] [blame] | 137 | VSyncTracker& tracker, nsecs_t now, VSyncDispatch::ScheduleTiming timing) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 138 | mWorkloadUpdateInfo = timing; |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 139 | const auto armedInfo = getArmedInfo(tracker, now, timing, mArmedInfo); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 140 | return {TimePoint::fromNs(armedInfo.mActualWakeupTime), |
| 141 | TimePoint::fromNs(armedInfo.mActualVsyncTime)}; |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const { |
| 145 | return mWorkloadUpdateInfo.has_value(); |
| 146 | } |
| 147 | |
Ady Abraham | 3fcfd8b | 2022-07-12 12:31:00 -0700 | [diff] [blame] | 148 | nsecs_t VSyncDispatchTimerQueueEntry::adjustVsyncIfNeeded(VSyncTracker& tracker, |
| 149 | nsecs_t nextVsyncTime) const { |
| 150 | bool const alreadyDispatchedForVsync = mLastDispatchTime && |
| 151 | ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime && |
| 152 | (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime); |
| 153 | const nsecs_t currentPeriod = tracker.currentPeriod(); |
| 154 | bool const nextVsyncTooClose = mLastDispatchTime && |
| 155 | (nextVsyncTime - *mLastDispatchTime + mMinVsyncDistance) <= currentPeriod; |
| 156 | if (alreadyDispatchedForVsync) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 157 | SFTRACE_FORMAT_INSTANT("alreadyDispatchedForVsync"); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 158 | return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance, |
| 159 | *mLastDispatchTime); |
Ady Abraham | 3fcfd8b | 2022-07-12 12:31:00 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (nextVsyncTooClose) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 163 | SFTRACE_FORMAT_INSTANT("nextVsyncTooClose"); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 164 | return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + currentPeriod, |
| 165 | *mLastDispatchTime + currentPeriod); |
Ady Abraham | 3fcfd8b | 2022-07-12 12:31:00 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return nextVsyncTime; |
| 169 | } |
| 170 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 171 | auto VSyncDispatchTimerQueueEntry::getArmedInfo(VSyncTracker& tracker, nsecs_t now, |
| 172 | VSyncDispatch::ScheduleTiming timing, |
| 173 | std::optional<ArmingInfo> armedInfo) const |
| 174 | -> ArmingInfo { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 175 | SFTRACE_NAME("VSyncDispatchTimerQueueEntry::getArmedInfo"); |
Ady Abraham | da8af4c | 2024-02-14 00:24:34 +0000 | [diff] [blame] | 176 | const auto earliestReadyBy = now + timing.workDuration + timing.readyDuration; |
| 177 | const auto earliestVsync = std::max(earliestReadyBy, timing.lastVsync); |
| 178 | |
| 179 | const auto nextVsyncTime = |
| 180 | adjustVsyncIfNeeded(tracker, /*nextVsyncTime*/ |
| 181 | tracker.nextAnticipatedVSyncTimeFrom(earliestVsync, |
| 182 | timing.lastVsync)); |
| 183 | const auto nextReadyTime = nextVsyncTime - timing.readyDuration; |
| 184 | const auto nextWakeupTime = nextReadyTime - timing.workDuration; |
| 185 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 186 | if (FlagManager::getInstance().dont_skip_on_early_ro()) { |
| 187 | bool const wouldSkipAVsyncTarget = |
| 188 | armedInfo && (nextVsyncTime > (armedInfo->mActualVsyncTime + mMinVsyncDistance)); |
| 189 | bool const wouldSkipAWakeup = |
| 190 | armedInfo && (nextWakeupTime > (armedInfo->mActualWakeupTime + mMinVsyncDistance)); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 191 | SFTRACE_FORMAT_INSTANT("%s: wouldSkipAVsyncTarget=%d wouldSkipAWakeup=%d", mName.c_str(), |
| 192 | wouldSkipAVsyncTarget, wouldSkipAWakeup); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 193 | if (wouldSkipAVsyncTarget || wouldSkipAWakeup) { |
| 194 | return *armedInfo; |
| 195 | } |
Ady Abraham | da8af4c | 2024-02-14 00:24:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return ArmingInfo{nextWakeupTime, nextVsyncTime, nextReadyTime}; |
| 199 | } |
| 200 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 201 | void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 202 | SFTRACE_NAME("VSyncDispatchTimerQueueEntry::update"); |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 203 | if (!mArmedInfo && !mWorkloadUpdateInfo) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 206 | |
| 207 | if (mWorkloadUpdateInfo) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 208 | const auto workDelta = mWorkloadUpdateInfo->workDuration - mScheduleTiming.workDuration; |
| 209 | const auto readyDelta = mWorkloadUpdateInfo->readyDuration - mScheduleTiming.readyDuration; |
| 210 | const auto lastVsyncDelta = mWorkloadUpdateInfo->lastVsync - mScheduleTiming.lastVsync; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 211 | SFTRACE_FORMAT_INSTANT("Workload updated workDelta=%" PRId64 " readyDelta=%" PRId64 |
| 212 | " lastVsyncDelta=%" PRId64, |
| 213 | workDelta, readyDelta, lastVsyncDelta); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 214 | mScheduleTiming = *mWorkloadUpdateInfo; |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 215 | mWorkloadUpdateInfo.reset(); |
| 216 | } |
| 217 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 218 | mArmedInfo = getArmedInfo(tracker, now, mScheduleTiming, mArmedInfo); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 221 | void VSyncDispatchTimerQueueEntry::disarm() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 222 | mArmedInfo.reset(); |
| 223 | } |
| 224 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 225 | nsecs_t VSyncDispatchTimerQueueEntry::executing() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 226 | mLastDispatchTime = mArmedInfo->mActualVsyncTime; |
| 227 | disarm(); |
| 228 | return *mLastDispatchTime; |
| 229 | } |
| 230 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 231 | void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp, |
| 232 | nsecs_t deadlineTimestamp) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 233 | { |
| 234 | std::lock_guard<std::mutex> lk(mRunningMutex); |
| 235 | mRunning = true; |
| 236 | } |
| 237 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 238 | mCallback(vsyncTimestamp, wakeupTimestamp, deadlineTimestamp); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 239 | |
| 240 | std::lock_guard<std::mutex> lk(mRunningMutex); |
| 241 | mRunning = false; |
| 242 | mCv.notify_all(); |
| 243 | } |
| 244 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 245 | void VSyncDispatchTimerQueueEntry::ensureNotRunning() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 246 | std::unique_lock<std::mutex> lk(mRunningMutex); |
| 247 | mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; }); |
| 248 | } |
| 249 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 250 | void VSyncDispatchTimerQueueEntry::dump(std::string& result) const { |
| 251 | std::lock_guard<std::mutex> lk(mRunningMutex); |
| 252 | std::string armedInfo; |
| 253 | if (mArmedInfo) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 254 | StringAppendF(&armedInfo, |
| 255 | "[wake up in %.2fms deadline in %.2fms for vsync %.2fms from now]", |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 256 | (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f, |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 257 | (mArmedInfo->mActualReadyTime - systemTime()) / 1e6f, |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 258 | (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f); |
| 259 | } |
| 260 | |
| 261 | StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(), |
| 262 | mRunning ? "(in callback function)" : "", armedInfo.c_str()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 263 | StringAppendF(&result, |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 264 | "\t\t\tworkDuration: %.2fms readyDuration: %.2fms lastVsync: %.2fms relative " |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 265 | "to now\n", |
| 266 | mScheduleTiming.workDuration / 1e6f, mScheduleTiming.readyDuration / 1e6f, |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 267 | (mScheduleTiming.lastVsync - systemTime()) / 1e6f); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 268 | |
| 269 | if (mLastDispatchTime) { |
| 270 | StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n", |
| 271 | (systemTime() - *mLastDispatchTime) / 1e6f); |
| 272 | } else { |
| 273 | StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n"); |
| 274 | } |
| 275 | } |
| 276 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 277 | VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk, |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 278 | VsyncSchedule::TrackerPtr tracker, |
| 279 | nsecs_t timerSlack, nsecs_t minVsyncDistance) |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 280 | : mTimeKeeper(std::move(tk)), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 281 | mTracker(std::move(tracker)), |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 282 | mTimerSlack(timerSlack), |
| 283 | mMinVsyncDistance(minVsyncDistance) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 284 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 285 | VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 286 | std::lock_guard lock(mMutex); |
en.liu | 07e0e29 | 2023-07-05 19:01:52 +0800 | [diff] [blame] | 287 | mRunning = false; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 288 | cancelTimer(); |
Leon Scroggins III | 0773813 | 2023-04-24 11:43:53 -0400 | [diff] [blame] | 289 | for (auto& [_, entry] : mCallbacks) { |
| 290 | ALOGE("Forgot to unregister a callback on VSyncDispatch!"); |
| 291 | entry->ensureNotRunning(); |
| 292 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 295 | void VSyncDispatchTimerQueue::cancelTimer() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 296 | mIntendedWakeupTime = kInvalidTime; |
| 297 | mTimeKeeper->alarmCancel(); |
| 298 | } |
| 299 | |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 300 | void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t /*now*/) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 301 | mIntendedWakeupTime = targetTime; |
Ady Abraham | b491c90 | 2020-08-15 15:47:56 -0700 | [diff] [blame] | 302 | mTimeKeeper->alarmAt(std::bind(&VSyncDispatchTimerQueue::timerCallback, this), |
| 303 | mIntendedWakeupTime); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 304 | mLastTimerSchedule = mTimeKeeper->now(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 307 | void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) { |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 308 | rearmTimerSkippingUpdateFor(now, mCallbacks.cend()); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 311 | void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor( |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 312 | nsecs_t now, CallbackMap::const_iterator skipUpdateIt) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 313 | SFTRACE_CALL(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 314 | std::optional<nsecs_t> min; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 315 | std::optional<nsecs_t> targetVsync; |
| 316 | std::optional<std::string_view> nextWakeupName; |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 317 | for (auto it = mCallbacks.cbegin(); it != mCallbacks.cend(); ++it) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 318 | auto& callback = it->second; |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 319 | if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 320 | continue; |
| 321 | } |
| 322 | |
| 323 | if (it != skipUpdateIt) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 324 | callback->update(*mTracker, now); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 325 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 326 | |
| 327 | traceEntry(*callback, now); |
| 328 | |
| 329 | const auto wakeupTime = *callback->wakeupTime(); |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 330 | if (!min || *min > wakeupTime) { |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 331 | nextWakeupName = callback->name(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 332 | min = wakeupTime; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 333 | targetVsync = callback->targetVsync(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 337 | if (min && min < mIntendedWakeupTime) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 338 | setTimer(*min, now); |
| 339 | } else { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 340 | SFTRACE_NAME("cancel timer"); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 341 | cancelTimer(); |
| 342 | } |
| 343 | } |
| 344 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 345 | void VSyncDispatchTimerQueue::timerCallback() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 346 | SFTRACE_CALL(); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 347 | struct Invocation { |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 348 | std::shared_ptr<VSyncDispatchTimerQueueEntry> callback; |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 349 | nsecs_t vsyncTimestamp; |
| 350 | nsecs_t wakeupTimestamp; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 351 | nsecs_t deadlineTimestamp; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 352 | }; |
| 353 | std::vector<Invocation> invocations; |
| 354 | { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 355 | std::lock_guard lock(mMutex); |
en.liu | 07e0e29 | 2023-07-05 19:01:52 +0800 | [diff] [blame] | 356 | if (!mRunning) { |
| 357 | ALOGD("TimerQueue is not running. Skipping callback."); |
| 358 | return; |
| 359 | } |
Kevin DuBois | f947783 | 2020-07-16 10:21:36 -0700 | [diff] [blame] | 360 | auto const now = mTimeKeeper->now(); |
| 361 | mLastTimerCallback = now; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 362 | for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { |
| 363 | auto& callback = it->second; |
| 364 | auto const wakeupTime = callback->wakeupTime(); |
| 365 | if (!wakeupTime) { |
| 366 | continue; |
| 367 | } |
| 368 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 369 | traceEntry(*callback, now); |
Ady Abraham | 9633f8e | 2024-03-04 19:38:12 +0000 | [diff] [blame] | 370 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 371 | auto const readyTime = callback->readyTime(); |
Kevin DuBois | f947783 | 2020-07-16 10:21:36 -0700 | [diff] [blame] | 372 | auto const lagAllowance = std::max(now - mIntendedWakeupTime, static_cast<nsecs_t>(0)); |
| 373 | if (*wakeupTime < mIntendedWakeupTime + mTimerSlack + lagAllowance) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 374 | callback->executing(); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 375 | invocations.emplace_back(Invocation{callback, *callback->lastExecutedVsyncTarget(), |
| 376 | *wakeupTime, *readyTime}); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | mIntendedWakeupTime = kInvalidTime; |
| 381 | rearmTimer(mTimeKeeper->now()); |
| 382 | } |
| 383 | |
| 384 | for (auto const& invocation : invocations) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 385 | ftl::Concat trace(ftl::truncated<5>(invocation.callback->name())); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 386 | SFTRACE_FORMAT("%s: %s", __func__, trace.c_str()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 387 | invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp, |
| 388 | invocation.deadlineTimestamp); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 392 | VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback( |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 393 | Callback callback, std::string callbackName) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 394 | std::lock_guard lock(mMutex); |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 395 | return mCallbacks |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 396 | .try_emplace(++mCallbackToken, |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 397 | std::make_shared<VSyncDispatchTimerQueueEntry>(std::move(callbackName), |
| 398 | std::move(callback), |
| 399 | mMinVsyncDistance)) |
| 400 | .first->first; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 403 | void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) { |
| 404 | std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 405 | { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 406 | std::lock_guard lock(mMutex); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 407 | auto it = mCallbacks.find(token); |
| 408 | if (it != mCallbacks.end()) { |
| 409 | entry = it->second; |
Dominik Laskowski | 0f3e5b9 | 2023-11-17 18:03:41 -0500 | [diff] [blame] | 410 | mCallbacks.erase(it->first); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | if (entry) { |
| 415 | entry->ensureNotRunning(); |
| 416 | } |
| 417 | } |
| 418 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 419 | std::optional<ScheduleResult> VSyncDispatchTimerQueue::schedule(CallbackToken token, |
| 420 | ScheduleTiming scheduleTiming) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 421 | std::lock_guard lock(mMutex); |
| 422 | return scheduleLocked(token, scheduleTiming); |
| 423 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 424 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 425 | std::optional<ScheduleResult> VSyncDispatchTimerQueue::scheduleLocked( |
| 426 | CallbackToken token, ScheduleTiming scheduleTiming) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 427 | auto it = mCallbacks.find(token); |
| 428 | if (it == mCallbacks.end()) { |
| 429 | return {}; |
| 430 | } |
| 431 | auto& callback = it->second; |
| 432 | auto const now = mTimeKeeper->now(); |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 433 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 434 | /* If the timer thread will run soon, we'll apply this work update via the callback |
| 435 | * timer recalculation to avoid cancelling a callback that is about to fire. */ |
| 436 | auto const rearmImminent = now > mIntendedWakeupTime; |
| 437 | if (CC_UNLIKELY(rearmImminent)) { |
Ady Abraham | da8af4c | 2024-02-14 00:24:34 +0000 | [diff] [blame] | 438 | return callback->addPendingWorkloadUpdate(*mTracker, now, scheduleTiming); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 439 | } |
Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 440 | |
ramindani | 558f4a9 | 2024-02-16 15:49:23 -0800 | [diff] [blame] | 441 | const auto result = callback->schedule(scheduleTiming, *mTracker, now); |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 442 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 443 | if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) { |
| 444 | rearmTimerSkippingUpdateFor(now, it); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 445 | } |
| 446 | |
ramindani | 558f4a9 | 2024-02-16 15:49:23 -0800 | [diff] [blame] | 447 | return result; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 448 | } |
| 449 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 450 | std::optional<ScheduleResult> VSyncDispatchTimerQueue::update(CallbackToken token, |
| 451 | ScheduleTiming scheduleTiming) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 452 | std::lock_guard lock(mMutex); |
| 453 | const auto it = mCallbacks.find(token); |
| 454 | if (it == mCallbacks.end()) { |
| 455 | return {}; |
| 456 | } |
| 457 | |
| 458 | auto& callback = it->second; |
| 459 | if (!callback->targetVsync().has_value()) { |
| 460 | return {}; |
| 461 | } |
| 462 | |
| 463 | return scheduleLocked(token, scheduleTiming); |
| 464 | } |
| 465 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 466 | CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 467 | std::lock_guard lock(mMutex); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 468 | |
| 469 | auto it = mCallbacks.find(token); |
| 470 | if (it == mCallbacks.end()) { |
| 471 | return CancelResult::Error; |
| 472 | } |
| 473 | auto& callback = it->second; |
| 474 | |
Kevin DuBois | b340b73 | 2020-06-16 09:07:35 -0700 | [diff] [blame] | 475 | auto const wakeupTime = callback->wakeupTime(); |
| 476 | if (wakeupTime) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 477 | callback->disarm(); |
Kevin DuBois | b340b73 | 2020-06-16 09:07:35 -0700 | [diff] [blame] | 478 | |
| 479 | if (*wakeupTime == mIntendedWakeupTime) { |
| 480 | mIntendedWakeupTime = kInvalidTime; |
| 481 | rearmTimer(mTimeKeeper->now()); |
| 482 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 483 | return CancelResult::Cancelled; |
| 484 | } |
| 485 | return CancelResult::TooLate; |
| 486 | } |
| 487 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 488 | void VSyncDispatchTimerQueue::dump(std::string& result) const { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 489 | std::lock_guard lock(mMutex); |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 490 | StringAppendF(&result, "\tTimer:\n"); |
| 491 | mTimeKeeper->dump(result); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 492 | StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f, |
| 493 | mMinVsyncDistance / 1e6f); |
| 494 | StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n", |
Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 495 | (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f); |
| 496 | StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n", |
| 497 | (mTimeKeeper->now() - mLastTimerCallback) / 1e6f, |
| 498 | (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 499 | StringAppendF(&result, "\tCallbacks:\n"); |
| 500 | for (const auto& [token, entry] : mCallbacks) { |
| 501 | entry->dump(result); |
| 502 | } |
| 503 | } |
| 504 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 505 | VSyncCallbackRegistration::VSyncCallbackRegistration(std::shared_ptr<VSyncDispatch> dispatch, |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 506 | VSyncDispatch::Callback callback, |
| 507 | std::string callbackName) |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 508 | : mDispatch(std::move(dispatch)), |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 509 | mToken(mDispatch->registerCallback(std::move(callback), std::move(callbackName))) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 510 | |
| 511 | VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other) |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 512 | : mDispatch(std::move(other.mDispatch)), mToken(std::exchange(other.mToken, std::nullopt)) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 513 | |
| 514 | VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) { |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 515 | if (this == &other) return *this; |
| 516 | if (mToken) { |
| 517 | mDispatch->unregisterCallback(*mToken); |
| 518 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 519 | mDispatch = std::move(other.mDispatch); |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 520 | mToken = std::exchange(other.mToken, std::nullopt); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 521 | return *this; |
| 522 | } |
| 523 | |
| 524 | VSyncCallbackRegistration::~VSyncCallbackRegistration() { |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 525 | if (mToken) mDispatch->unregisterCallback(*mToken); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 526 | } |
| 527 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 528 | std::optional<ScheduleResult> VSyncCallbackRegistration::schedule( |
| 529 | VSyncDispatch::ScheduleTiming scheduleTiming) { |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 530 | if (!mToken) { |
Ady Abraham | b5d3afa | 2021-05-07 11:22:23 -0700 | [diff] [blame] | 531 | return std::nullopt; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 532 | } |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 533 | return mDispatch->schedule(*mToken, scheduleTiming); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 534 | } |
| 535 | |
ramindani | 32a88b1 | 2024-01-31 18:45:30 -0800 | [diff] [blame] | 536 | std::optional<ScheduleResult> VSyncCallbackRegistration::update( |
| 537 | VSyncDispatch::ScheduleTiming scheduleTiming) { |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 538 | if (!mToken) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 539 | return std::nullopt; |
| 540 | } |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 541 | return mDispatch->update(*mToken, scheduleTiming); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 544 | CancelResult VSyncCallbackRegistration::cancel() { |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 545 | if (!mToken) { |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 546 | return CancelResult::Error; |
| 547 | } |
Leon Scroggins III | 6c440ae | 2023-04-21 15:01:03 -0400 | [diff] [blame] | 548 | return mDispatch->cancel(*mToken); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | } // namespace android::scheduler |