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 |
| 18 | #include <utils/Trace.h> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "TimeKeeper.h" |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 22 | #include "VSyncDispatchTimerQueue.h" |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 23 | #include "VSyncTracker.h" |
| 24 | |
| 25 | namespace android::scheduler { |
| 26 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 27 | VSyncDispatch::~VSyncDispatch() = default; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 28 | VSyncTracker::~VSyncTracker() = default; |
| 29 | TimeKeeper::~TimeKeeper() = default; |
| 30 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 31 | VSyncDispatchTimerQueueEntry::VSyncDispatchTimerQueueEntry(std::string const& name, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 32 | std::function<void(nsecs_t)> const& cb, |
| 33 | nsecs_t minVsyncDistance) |
| 34 | : mName(name), |
| 35 | mCallback(cb), |
| 36 | mWorkDuration(0), |
| 37 | mEarliestVsync(0), |
| 38 | mMinVsyncDistance(minVsyncDistance) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 39 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 40 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::lastExecutedVsyncTarget() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 41 | return mLastDispatchTime; |
| 42 | } |
| 43 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 44 | std::string_view VSyncDispatchTimerQueueEntry::name() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 45 | return mName; |
| 46 | } |
| 47 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 48 | std::optional<nsecs_t> VSyncDispatchTimerQueueEntry::wakeupTime() const { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 49 | if (!mArmedInfo) { |
| 50 | return {}; |
| 51 | } |
| 52 | return {mArmedInfo->mActualWakeupTime}; |
| 53 | } |
| 54 | |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 55 | ScheduleResult VSyncDispatchTimerQueueEntry::schedule(nsecs_t workDuration, nsecs_t earliestVsync, |
| 56 | VSyncTracker& tracker, nsecs_t now) { |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 57 | auto nextVsyncTime = |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 58 | tracker.nextAnticipatedVSyncTimeFrom(std::max(earliestVsync, now + workDuration)); |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 59 | |
| 60 | bool const wouldSkipAVsyncTarget = |
| 61 | mArmedInfo && (nextVsyncTime > (mArmedInfo->mActualVsyncTime + mMinVsyncDistance)); |
| 62 | if (wouldSkipAVsyncTarget) { |
| 63 | return ScheduleResult::Scheduled; |
| 64 | } |
| 65 | |
| 66 | bool const alreadyDispatchedForVsync = mLastDispatchTime && |
| 67 | ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime && |
| 68 | (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime); |
| 69 | if (alreadyDispatchedForVsync) { |
| 70 | nextVsyncTime = |
| 71 | tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance); |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | auto const nextWakeupTime = nextVsyncTime - workDuration; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 75 | mWorkDuration = workDuration; |
| 76 | mEarliestVsync = earliestVsync; |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 77 | mArmedInfo = {nextWakeupTime, nextVsyncTime}; |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 78 | return ScheduleResult::Scheduled; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 81 | void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 82 | if (!mArmedInfo) { |
| 83 | return; |
| 84 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 85 | auto const nextVsyncTime = |
| 86 | tracker.nextAnticipatedVSyncTimeFrom(std::max(mEarliestVsync, now + mWorkDuration)); |
| 87 | mArmedInfo = {nextVsyncTime - mWorkDuration, nextVsyncTime}; |
| 88 | } |
| 89 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 90 | void VSyncDispatchTimerQueueEntry::disarm() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 91 | mArmedInfo.reset(); |
| 92 | } |
| 93 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 94 | nsecs_t VSyncDispatchTimerQueueEntry::executing() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 95 | mLastDispatchTime = mArmedInfo->mActualVsyncTime; |
| 96 | disarm(); |
| 97 | return *mLastDispatchTime; |
| 98 | } |
| 99 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 100 | void VSyncDispatchTimerQueueEntry::callback(nsecs_t t) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 101 | { |
| 102 | std::lock_guard<std::mutex> lk(mRunningMutex); |
| 103 | mRunning = true; |
| 104 | } |
| 105 | |
| 106 | mCallback(t); |
| 107 | |
| 108 | std::lock_guard<std::mutex> lk(mRunningMutex); |
| 109 | mRunning = false; |
| 110 | mCv.notify_all(); |
| 111 | } |
| 112 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 113 | void VSyncDispatchTimerQueueEntry::ensureNotRunning() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 114 | std::unique_lock<std::mutex> lk(mRunningMutex); |
| 115 | mCv.wait(lk, [this]() REQUIRES(mRunningMutex) { return !mRunning; }); |
| 116 | } |
| 117 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 118 | VSyncDispatchTimerQueue::VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 119 | VSyncTracker& tracker, nsecs_t timerSlack, |
| 120 | nsecs_t minVsyncDistance) |
| 121 | : mTimeKeeper(std::move(tk)), |
| 122 | mTracker(tracker), |
| 123 | mTimerSlack(timerSlack), |
| 124 | mMinVsyncDistance(minVsyncDistance) {} |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 125 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 126 | VSyncDispatchTimerQueue::~VSyncDispatchTimerQueue() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 127 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 128 | cancelTimer(); |
| 129 | } |
| 130 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 131 | void VSyncDispatchTimerQueue::cancelTimer() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 132 | mIntendedWakeupTime = kInvalidTime; |
| 133 | mTimeKeeper->alarmCancel(); |
| 134 | } |
| 135 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 136 | void VSyncDispatchTimerQueue::setTimer(nsecs_t targetTime, nsecs_t now) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 137 | mIntendedWakeupTime = targetTime; |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 138 | mTimeKeeper->alarmIn(std::bind(&VSyncDispatchTimerQueue::timerCallback, this), |
| 139 | targetTime - now); |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 142 | void VSyncDispatchTimerQueue::rearmTimer(nsecs_t now) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 143 | rearmTimerSkippingUpdateFor(now, mCallbacks.end()); |
| 144 | } |
| 145 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 146 | void VSyncDispatchTimerQueue::rearmTimerSkippingUpdateFor( |
| 147 | nsecs_t now, CallbackMap::iterator const& skipUpdateIt) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 148 | std::optional<nsecs_t> min; |
| 149 | for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { |
| 150 | auto& callback = it->second; |
| 151 | if (!callback->wakeupTime()) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | if (it != skipUpdateIt) { |
| 156 | callback->update(mTracker, now); |
| 157 | } |
| 158 | auto const wakeupTime = *callback->wakeupTime(); |
| 159 | if (!min || (min && *min > wakeupTime)) { |
| 160 | min = wakeupTime; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (min && (min < mIntendedWakeupTime)) { |
| 165 | setTimer(*min, now); |
| 166 | } else { |
| 167 | cancelTimer(); |
| 168 | } |
| 169 | } |
| 170 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 171 | void VSyncDispatchTimerQueue::timerCallback() { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 172 | struct Invocation { |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 173 | std::shared_ptr<VSyncDispatchTimerQueueEntry> callback; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 174 | nsecs_t timestamp; |
| 175 | }; |
| 176 | std::vector<Invocation> invocations; |
| 177 | { |
| 178 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 179 | for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) { |
| 180 | auto& callback = it->second; |
| 181 | auto const wakeupTime = callback->wakeupTime(); |
| 182 | if (!wakeupTime) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | if (*wakeupTime < mIntendedWakeupTime + mTimerSlack) { |
| 187 | callback->executing(); |
| 188 | invocations.emplace_back( |
| 189 | Invocation{callback, *callback->lastExecutedVsyncTarget()}); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | mIntendedWakeupTime = kInvalidTime; |
| 194 | rearmTimer(mTimeKeeper->now()); |
| 195 | } |
| 196 | |
| 197 | for (auto const& invocation : invocations) { |
| 198 | invocation.callback->callback(invocation.timestamp); |
| 199 | } |
| 200 | } |
| 201 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 202 | VSyncDispatchTimerQueue::CallbackToken VSyncDispatchTimerQueue::registerCallback( |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 203 | std::function<void(nsecs_t)> const& callbackFn, std::string callbackName) { |
| 204 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 205 | return CallbackToken{ |
| 206 | mCallbacks |
| 207 | .emplace(++mCallbackToken, |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 208 | std::make_shared<VSyncDispatchTimerQueueEntry>(callbackName, |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 209 | callbackFn, |
| 210 | mMinVsyncDistance)) |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 211 | .first->first}; |
| 212 | } |
| 213 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 214 | void VSyncDispatchTimerQueue::unregisterCallback(CallbackToken token) { |
| 215 | std::shared_ptr<VSyncDispatchTimerQueueEntry> entry = nullptr; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 216 | { |
| 217 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 218 | auto it = mCallbacks.find(token); |
| 219 | if (it != mCallbacks.end()) { |
| 220 | entry = it->second; |
| 221 | mCallbacks.erase(it); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (entry) { |
| 226 | entry->ensureNotRunning(); |
| 227 | } |
| 228 | } |
| 229 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 230 | ScheduleResult VSyncDispatchTimerQueue::schedule(CallbackToken token, nsecs_t workDuration, |
| 231 | nsecs_t earliestVsync) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 232 | auto result = ScheduleResult::Error; |
| 233 | { |
| 234 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 235 | |
| 236 | auto it = mCallbacks.find(token); |
| 237 | if (it == mCallbacks.end()) { |
| 238 | return result; |
| 239 | } |
| 240 | auto& callback = it->second; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 241 | auto const now = mTimeKeeper->now(); |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 242 | result = callback->schedule(workDuration, earliestVsync, mTracker, now); |
| 243 | if (result == ScheduleResult::CannotSchedule) { |
| 244 | return result; |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Kevin DuBois | 2311b1a | 2019-11-18 16:19:08 -0800 | [diff] [blame] | 247 | if (callback->wakeupTime() < mIntendedWakeupTime - mTimerSlack) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 248 | rearmTimerSkippingUpdateFor(now, it); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return result; |
| 253 | } |
| 254 | |
Kevin DuBois | e4f27a8 | 2019-11-12 11:41:41 -0800 | [diff] [blame] | 255 | CancelResult VSyncDispatchTimerQueue::cancel(CallbackToken token) { |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 256 | std::lock_guard<decltype(mMutex)> lk(mMutex); |
| 257 | |
| 258 | auto it = mCallbacks.find(token); |
| 259 | if (it == mCallbacks.end()) { |
| 260 | return CancelResult::Error; |
| 261 | } |
| 262 | auto& callback = it->second; |
| 263 | |
| 264 | if (callback->wakeupTime()) { |
| 265 | callback->disarm(); |
| 266 | mIntendedWakeupTime = kInvalidTime; |
| 267 | rearmTimer(mTimeKeeper->now()); |
| 268 | return CancelResult::Cancelled; |
| 269 | } |
| 270 | return CancelResult::TooLate; |
| 271 | } |
| 272 | |
| 273 | VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncDispatch& dispatch, |
| 274 | std::function<void(nsecs_t)> const& callbackFn, |
| 275 | std::string const& callbackName) |
| 276 | : mDispatch(dispatch), |
| 277 | mToken(dispatch.registerCallback(callbackFn, callbackName)), |
| 278 | mValidToken(true) {} |
| 279 | |
| 280 | VSyncCallbackRegistration::VSyncCallbackRegistration(VSyncCallbackRegistration&& other) |
| 281 | : mDispatch(other.mDispatch), |
| 282 | mToken(std::move(other.mToken)), |
| 283 | mValidToken(std::move(other.mValidToken)) { |
| 284 | other.mValidToken = false; |
| 285 | } |
| 286 | |
| 287 | VSyncCallbackRegistration& VSyncCallbackRegistration::operator=(VSyncCallbackRegistration&& other) { |
| 288 | mDispatch = std::move(other.mDispatch); |
| 289 | mToken = std::move(other.mToken); |
| 290 | mValidToken = std::move(other.mValidToken); |
| 291 | other.mValidToken = false; |
| 292 | return *this; |
| 293 | } |
| 294 | |
| 295 | VSyncCallbackRegistration::~VSyncCallbackRegistration() { |
| 296 | if (mValidToken) mDispatch.get().unregisterCallback(mToken); |
| 297 | } |
| 298 | |
| 299 | ScheduleResult VSyncCallbackRegistration::schedule(nsecs_t workDuration, nsecs_t earliestVsync) { |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 300 | if (!mValidToken) { |
| 301 | return ScheduleResult::Error; |
| 302 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 303 | return mDispatch.get().schedule(mToken, workDuration, earliestVsync); |
| 304 | } |
| 305 | |
| 306 | CancelResult VSyncCallbackRegistration::cancel() { |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame^] | 307 | if (!mValidToken) { |
| 308 | return CancelResult::Error; |
| 309 | } |
Kevin DuBois | 305bef1 | 2019-10-09 13:23:27 -0700 | [diff] [blame] | 310 | return mDispatch.get().cancel(mToken); |
| 311 | } |
| 312 | |
| 313 | } // namespace android::scheduler |