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