| 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 | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 18 | #include <android-base/stringprintf.h> | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 19 | #include <utils/Trace.h> | 
|  | 20 | #include <vector> | 
|  | 21 |  | 
|  | 22 | #include "TimeKeeper.h" | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 23 | #include "VSyncDispatchTimerQueue.h" | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 24 | #include "VSyncTracker.h" | 
|  | 25 |  | 
|  | 26 | namespace android::scheduler { | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 27 | using base::StringAppendF; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 28 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 29 | VSyncDispatch::~VSyncDispatch() = default; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 30 | VSyncTracker::~VSyncTracker() = default; | 
|  | 31 | TimeKeeper::~TimeKeeper() = default; | 
|  | 32 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 33 | VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name, | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 34 | VSyncDispatch::Callback const& cb, | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 35 | nsecs_t minVsyncDistance) | 
|  | 36 | : mName(name), | 
|  | 37 | mCallback(cb), | 
|  | 38 | mWorkDuration(0), | 
|  | 39 | mEarliestVsync(0), | 
|  | 40 | mMinVsyncDistance(minVsyncDistance) {} | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 41 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 42 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 43 | return mLastDispatchTime; | 
|  | 44 | } | 
|  | 45 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 46 | std::string_view VSyncDispatchTimerQueueEntry::name() const { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 47 | return mName; | 
|  | 48 | } | 
|  | 49 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 50 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 51 | if (!mArmedInfo) { | 
|  | 52 | return {}; | 
|  | 53 | } | 
|  | 54 | return {mArmedInfo->mActualWakeupTime}; | 
|  | 55 | } | 
|  | 56 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 57 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::targetVsync() const { | 
|  | 58 | if (!mArmedInfo) { | 
|  | 59 | return {}; | 
|  | 60 | } | 
|  | 61 | return {mArmedInfo->mActualVsyncTime}; | 
|  | 62 | } | 
|  | 63 |  | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 64 | ScheduleResult VSyncDispatchTimerQueueEntry::schedule(nsecs_t workDuration, nsecs_t earliestVsync, | 
|  | 65 | VSyncTracker& tracker, nsecs_t now) { | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 66 | auto nextVsyncTime = | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 67 | tracker.nextAnticipatedVSyncTimeFrom(std::max(earliestVsync, now + workDuration)); | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 68 |  | 
|  | 69 | bool const wouldSkipAVsyncTarget = | 
|  | 70 | mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance)); | 
|  | 71 | if (wouldSkipAVsyncTarget) { | 
|  | 72 | return ScheduleResult::Scheduled; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | bool const alreadyDispatchedForVsync = mLastDispatchTime && | 
|  | 76 | ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime && | 
|  | 77 | (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime); | 
|  | 78 | if (alreadyDispatchedForVsync) { | 
|  | 79 | nextVsyncTime = | 
|  | 80 | tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance); | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | auto const nextWakeupTime = nextVsyncTime - workDuration; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 84 | mWorkDuration = workDuration; | 
|  | 85 | mEarliestVsync = earliestVsync; | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 86 | mArmedInfo = {nextWakeupTime, nextVsyncTime}; | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 87 | return ScheduleResult::Scheduled; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 90 | void VSyncDispatchTimerQueueEntry::addPendingWorkloadUpdate(nsecs_t workDuration, | 
|  | 91 | nsecs_t earliestVsync) { | 
|  | 92 | mWorkloadUpdateInfo = {.earliestVsync = earliestVsync, .duration = workDuration}; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | bool VSyncDispatchTimerQueueEntry::hasPendingWorkloadUpdate() const { | 
|  | 96 | return mWorkloadUpdateInfo.has_value(); | 
|  | 97 | } | 
|  | 98 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 99 | void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) { | 
| Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 100 | if (!mArmedInfo && !mWorkloadUpdateInfo) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 101 | return; | 
|  | 102 | } | 
| Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | if (mWorkloadUpdateInfo) { | 
|  | 105 | mEarliestVsync = mWorkloadUpdateInfo->earliestVsync; | 
|  | 106 | mWorkDuration = mWorkloadUpdateInfo->duration; | 
|  | 107 | mWorkloadUpdateInfo.reset(); | 
|  | 108 | } | 
|  | 109 |  | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 110 | auto const nextVsyncTime = | 
|  | 111 | tracker.nextAnticipatedVSyncTimeFrom(std::max(mEarliestVsync, now + mWorkDuration)); | 
|  | 112 | mArmedInfo = {nextVsyncTime - mWorkDuration, nextVsyncTime}; | 
|  | 113 | } | 
|  | 114 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 115 | void VSyncDispatchTimerQueueEntry::disarm() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 116 | mArmedInfo.reset(); | 
|  | 117 | } | 
|  | 118 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 119 | nsecs_t VSyncDispatchTimerQueueEntry::executing() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 120 | mLastDispatchTime = mArmedInfo->mActualVsyncTime; | 
|  | 121 | disarm(); | 
|  | 122 | return *mLastDispatchTime; | 
|  | 123 | } | 
|  | 124 |  | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 125 | void VSyncDispatchTimerQueueEntry::callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 126 | { | 
|  | 127 | std::lock_guard<std::mutex> lk(mRunningMutex); | 
|  | 128 | mRunning = true; | 
|  | 129 | } | 
|  | 130 |  | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 131 | mCallback(vsyncTimestamp, wakeupTimestamp); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 132 |  | 
|  | 133 | std::lock_guard<std::mutex> lk(mRunningMutex); | 
|  | 134 | mRunning = false; | 
|  | 135 | mCv.notify_all(); | 
|  | 136 | } | 
|  | 137 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 138 | void VSyncDispatchTimerQueueEntry::ensureNotRunning() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 139 | std::unique_lock<std::mutex> lk(mRunningMutex); | 
|  | 140 | mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; }); | 
|  | 141 | } | 
|  | 142 |  | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 143 | void VSyncDispatchTimerQueueEntry::dump(std::string& result) const { | 
|  | 144 | std::lock_guard<std::mutex> lk(mRunningMutex); | 
|  | 145 | std::string armedInfo; | 
|  | 146 | if (mArmedInfo) { | 
|  | 147 | StringAppendF(&armedInfo, "[wake up in %.2fms for vsync %.2fms from now]", | 
|  | 148 | (mArmedInfo->mActualWakeupTime - systemTime()) / 1e6f, | 
|  | 149 | (mArmedInfo->mActualVsyncTime - systemTime()) / 1e6f); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | StringAppendF(&result, "\t\t%s: %s %s\n", mName.c_str(), | 
|  | 153 | mRunning ? "(in callback function)" : "", armedInfo.c_str()); | 
|  | 154 | StringAppendF(&result, "\t\t\tmWorkDuration: %.2fms mEarliestVsync: %.2fms relative to now\n", | 
|  | 155 | mWorkDuration / 1e6f, (mEarliestVsync - systemTime()) / 1e6f); | 
|  | 156 |  | 
|  | 157 | if (mLastDispatchTime) { | 
|  | 158 | StringAppendF(&result, "\t\t\tmLastDispatchTime: %.2fms ago\n", | 
|  | 159 | (systemTime() - *mLastDispatchTime) / 1e6f); | 
|  | 160 | } else { | 
|  | 161 | StringAppendF(&result, "\t\t\tmLastDispatchTime unknown\n"); | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 165 | VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk, | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 166 | VSyncTracker& tracker, nsecs_t timerSlack, | 
|  | 167 | nsecs_t minVsyncDistance) | 
|  | 168 | : mTimeKeeper(std::move(tk)), | 
|  | 169 | mTracker(tracker), | 
|  | 170 | mTimerSlack(timerSlack), | 
|  | 171 | mMinVsyncDistance(minVsyncDistance) {} | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 172 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 173 | VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 174 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
|  | 175 | cancelTimer(); | 
|  | 176 | } | 
|  | 177 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 178 | void VSyncDispatchTimerQueue::cancelTimer() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 179 | mIntendedWakeupTime = kInvalidTime; | 
|  | 180 | mTimeKeeper->alarmCancel(); | 
|  | 181 | } | 
|  | 182 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 183 | void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t now) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 184 | mIntendedWakeupTime = targetTime; | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 185 | mTimeKeeper->alarmIn(std::bind(&VSyncDispatchTimerQueue::timerCallback, this), | 
|  | 186 | targetTime - now); | 
| Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 187 | mLastTimerSchedule = mTimeKeeper->now(); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 190 | void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 191 | rearmTimerSkippingUpdateFor(now, mCallbacks.end()); | 
|  | 192 | } | 
|  | 193 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 194 | void VSyncDispatchTimerQueue::TraceBuffer::note(std::string_view name, nsecs_t alarmIn, | 
|  | 195 | nsecs_t vsFor) { | 
|  | 196 | if (ATRACE_ENABLED()) { | 
|  | 197 | snprintf(str_buffer.data(), str_buffer.size(), "%.4s%s%" PRId64 "%s%" PRId64, | 
|  | 198 | name.substr(0, kMaxNamePrint).data(), kTraceNamePrefix, alarmIn, | 
|  | 199 | kTraceNameSeparator, vsFor); | 
|  | 200 | } | 
|  | 201 | ATRACE_NAME(str_buffer.data()); | 
|  | 202 | } | 
|  | 203 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 204 | void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor( | 
|  | 205 | nsecs_t now, CallbackMap::iterator const& skipUpdateIt) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 206 | std::optional<nsecs_t> min; | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 207 | std::optional<nsecs_t> targetVsync; | 
|  | 208 | std::optional<std::string_view> nextWakeupName; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 209 | for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { | 
|  | 210 | auto& callback = it->second; | 
| Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 211 | if (!callback->wakeupTime() && !callback->hasPendingWorkloadUpdate()) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 212 | continue; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | if (it != skipUpdateIt) { | 
|  | 216 | callback->update(mTracker, now); | 
|  | 217 | } | 
|  | 218 | auto const wakeupTime = *callback->wakeupTime(); | 
|  | 219 | if (!min || (min && *min > wakeupTime)) { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 220 | nextWakeupName = callback->name(); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 221 | min = wakeupTime; | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 222 | targetVsync = callback->targetVsync(); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 223 | } | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | if (min && (min < mIntendedWakeupTime)) { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 227 | if (targetVsync && nextWakeupName) { | 
|  | 228 | mTraceBuffer.note(*nextWakeupName, *min - now, *targetVsync - now); | 
|  | 229 | } | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 230 | setTimer(*min, now); | 
|  | 231 | } else { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 232 | ATRACE_NAME("cancel timer"); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 233 | cancelTimer(); | 
|  | 234 | } | 
|  | 235 | } | 
|  | 236 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 237 | void VSyncDispatchTimerQueue::timerCallback() { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 238 | struct Invocation { | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 239 | std::shared_ptr<VSyncDispatchTimerQueueEntry> callback; | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 240 | nsecs_t vsyncTimestamp; | 
|  | 241 | nsecs_t wakeupTimestamp; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 242 | }; | 
|  | 243 | std::vector<Invocation> invocations; | 
|  | 244 | { | 
|  | 245 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
| Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 246 | mLastTimerCallback = mTimeKeeper->now(); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 247 | for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { | 
|  | 248 | auto& callback = it->second; | 
|  | 249 | auto const wakeupTime = callback->wakeupTime(); | 
|  | 250 | if (!wakeupTime) { | 
|  | 251 | continue; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | if (*wakeupTime < mIntendedWakeupTime + mTimerSlack) { | 
|  | 255 | callback->executing(); | 
|  | 256 | invocations.emplace_back( | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 257 | Invocation{callback, *callback->lastExecutedVsyncTarget(), *wakeupTime}); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 258 | } | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | mIntendedWakeupTime = kInvalidTime; | 
|  | 262 | rearmTimer(mTimeKeeper->now()); | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | for (auto const& invocation : invocations) { | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 266 | invocation.callback->callback(invocation.vsyncTimestamp, invocation.wakeupTimestamp); | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 267 | } | 
|  | 268 | } | 
|  | 269 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 270 | VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback( | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 271 | Callback const& callbackFn, std::string callbackName) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 272 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
|  | 273 | return CallbackToken{ | 
|  | 274 | mCallbacks | 
|  | 275 | .emplace(++mCallbackToken, | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 276 | std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName, | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 277 | callbackFn, | 
|  | 278 | mMinVsyncDistance)) | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 279 | .first->first}; | 
|  | 280 | } | 
|  | 281 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 282 | void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) { | 
|  | 283 | std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 284 | { | 
|  | 285 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
|  | 286 | auto it = mCallbacks.find(token); | 
|  | 287 | if (it != mCallbacks.end()) { | 
|  | 288 | entry = it->second; | 
|  | 289 | mCallbacks.erase(it); | 
|  | 290 | } | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | if (entry) { | 
|  | 294 | entry->ensureNotRunning(); | 
|  | 295 | } | 
|  | 296 | } | 
|  | 297 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 298 | ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token, nsecs_t workDuration, | 
|  | 299 | nsecs_t earliestVsync) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 300 | auto result = ScheduleResult::Error; | 
|  | 301 | { | 
|  | 302 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
|  | 303 |  | 
|  | 304 | auto it = mCallbacks.find(token); | 
|  | 305 | if (it == mCallbacks.end()) { | 
|  | 306 | return result; | 
|  | 307 | } | 
|  | 308 | auto& callback = it->second; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 309 | auto const now = mTimeKeeper->now(); | 
| Kevin DuBois | 5c18c1c | 2020-05-27 15:50:50 -0700 | [diff] [blame] | 310 |  | 
|  | 311 | /* If the timer thread will run soon, we'll apply this work update via the callback | 
|  | 312 | * timer recalculation to avoid cancelling a callback that is about to fire. */ | 
|  | 313 | auto const rearmImminent = now > mIntendedWakeupTime; | 
|  | 314 | if (CC_UNLIKELY(rearmImminent)) { | 
|  | 315 | callback->addPendingWorkloadUpdate(workDuration, earliestVsync); | 
|  | 316 | return ScheduleResult::Scheduled; | 
|  | 317 | } | 
|  | 318 |  | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 319 | result = callback->schedule(workDuration, earliestVsync, mTracker, now); | 
|  | 320 | if (result == ScheduleResult::CannotSchedule) { | 
|  | 321 | return result; | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 322 | } | 
|  | 323 |  | 
| Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 324 | if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 325 | rearmTimerSkippingUpdateFor(now, it); | 
|  | 326 | } | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | return result; | 
|  | 330 | } | 
|  | 331 |  | 
| Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 332 | CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 333 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
|  | 334 |  | 
|  | 335 | auto it = mCallbacks.find(token); | 
|  | 336 | if (it == mCallbacks.end()) { | 
|  | 337 | return CancelResult::Error; | 
|  | 338 | } | 
|  | 339 | auto& callback = it->second; | 
|  | 340 |  | 
| Kevin DuBois | b340b73 | 2020-06-16 09:07:35 -0700 | [diff] [blame] | 341 | auto const wakeupTime = callback->wakeupTime(); | 
|  | 342 | if (wakeupTime) { | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 343 | callback->disarm(); | 
| Kevin DuBois | b340b73 | 2020-06-16 09:07:35 -0700 | [diff] [blame] | 344 |  | 
|  | 345 | if (*wakeupTime == mIntendedWakeupTime) { | 
|  | 346 | mIntendedWakeupTime = kInvalidTime; | 
|  | 347 | rearmTimer(mTimeKeeper->now()); | 
|  | 348 | } | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 349 | return CancelResult::Cancelled; | 
|  | 350 | } | 
|  | 351 | return CancelResult::TooLate; | 
|  | 352 | } | 
|  | 353 |  | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 354 | void VSyncDispatchTimerQueue::dump(std::string& result) const { | 
|  | 355 | std::lock_guard<decltype(mMutex)> lk(mMutex); | 
| Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 356 | StringAppendF(&result, "\tTimer:\n"); | 
|  | 357 | mTimeKeeper->dump(result); | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 358 | StringAppendF(&result, "\tmTimerSlack: %.2fms mMinVsyncDistance: %.2fms\n", mTimerSlack / 1e6f, | 
|  | 359 | mMinVsyncDistance / 1e6f); | 
|  | 360 | StringAppendF(&result, "\tmIntendedWakeupTime: %.2fms from now\n", | 
| Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 361 | (mIntendedWakeupTime - mTimeKeeper->now()) / 1e6f); | 
|  | 362 | StringAppendF(&result, "\tmLastTimerCallback: %.2fms ago mLastTimerSchedule: %.2fms ago\n", | 
|  | 363 | (mTimeKeeper->now() - mLastTimerCallback) / 1e6f, | 
|  | 364 | (mTimeKeeper->now() - mLastTimerSchedule) / 1e6f); | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 365 | StringAppendF(&result, "\tCallbacks:\n"); | 
|  | 366 | for (const auto& [token, entry] : mCallbacks) { | 
|  | 367 | entry->dump(result); | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 371 | VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch, | 
| Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 372 | VSyncDispatch::Callback const& callbackFn, | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 373 | std::string const& callbackName) | 
|  | 374 | : mDispatch(dispatch), | 
|  | 375 | mToken(dispatch.registerCallback(callbackFn, callbackName)), | 
|  | 376 | mValidToken(true) {} | 
|  | 377 |  | 
|  | 378 | VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other) | 
|  | 379 | : mDispatch(other.mDispatch), | 
|  | 380 | mToken(std::move(other.mToken)), | 
|  | 381 | mValidToken(std::move(other.mValidToken)) { | 
|  | 382 | other.mValidToken = false; | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 | VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) { | 
|  | 386 | mDispatch = std::move(other.mDispatch); | 
|  | 387 | mToken = std::move(other.mToken); | 
|  | 388 | mValidToken = std::move(other.mValidToken); | 
|  | 389 | other.mValidToken = false; | 
|  | 390 | return *this; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | VSyncCallbackRegistration::~VSyncCallbackRegistration() { | 
|  | 394 | if (mValidToken) mDispatch.get().unregisterCallback(mToken); | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | ScheduleResult VSyncCallbackRegistration::schedule(nsecs_t workDuration, nsecs_t earliestVsync) { | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 398 | if (!mValidToken) { | 
|  | 399 | return ScheduleResult::Error; | 
|  | 400 | } | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 401 | return mDispatch.get().schedule(mToken, workDuration, earliestVsync); | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | CancelResult VSyncCallbackRegistration::cancel() { | 
| Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 405 | if (!mValidToken) { | 
|  | 406 | return CancelResult::Error; | 
|  | 407 | } | 
| Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 408 | return mDispatch.get().cancel(mToken); | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | } // namespace android::scheduler |