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