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