Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [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 | |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "VSyncReactor" |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 20 | #include "VSyncReactor.h" |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 21 | #include <log/log.h> |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 22 | #include "TimeKeeper.h" |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 23 | #include "VSyncDispatch.h" |
| 24 | #include "VSyncTracker.h" |
| 25 | |
| 26 | namespace android::scheduler { |
| 27 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 28 | Clock::~Clock() = default; |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 29 | nsecs_t SystemClock::now() const { |
| 30 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 31 | } |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 32 | |
| 33 | VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch, |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 34 | std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit) |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 35 | : mClock(std::move(clock)), |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 36 | mTracker(std::move(tracker)), |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 37 | mDispatch(std::move(dispatch)), |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 38 | mPendingLimit(pendingFenceLimit) {} |
| 39 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 40 | VSyncReactor::~VSyncReactor() = default; |
| 41 | |
| 42 | // The DispSync interface has a 'repeat this callback at rate' semantic. This object adapts |
| 43 | // VSyncDispatch's individually-scheduled callbacks so as to meet DispSync's existing semantic |
| 44 | // for now. |
| 45 | class CallbackRepeater { |
| 46 | public: |
| 47 | CallbackRepeater(VSyncDispatch& dispatch, DispSync::Callback* cb, const char* name, |
| 48 | nsecs_t period, nsecs_t offset, nsecs_t notBefore) |
| 49 | : mCallback(cb), |
| 50 | mRegistration(dispatch, |
| 51 | std::bind(&CallbackRepeater::callback, this, std::placeholders::_1), |
| 52 | std::string(name)), |
| 53 | mPeriod(period), |
| 54 | mOffset(offset), |
| 55 | mLastCallTime(notBefore) {} |
| 56 | |
| 57 | ~CallbackRepeater() { |
| 58 | std::lock_guard<std::mutex> lk(mMutex); |
| 59 | mRegistration.cancel(); |
| 60 | } |
| 61 | |
| 62 | void start(nsecs_t offset) { |
| 63 | std::lock_guard<std::mutex> lk(mMutex); |
| 64 | mStopped = false; |
| 65 | mOffset = offset; |
| 66 | |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 67 | auto const schedule_result = mRegistration.schedule(calculateWorkload(), mLastCallTime); |
| 68 | LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled), |
| 69 | "Error scheduling callback: rc %X", schedule_result); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void setPeriod(nsecs_t period) { |
| 73 | std::lock_guard<std::mutex> lk(mMutex); |
| 74 | if (period == mPeriod) { |
| 75 | return; |
| 76 | } |
| 77 | mPeriod = period; |
| 78 | } |
| 79 | |
| 80 | void stop() { |
| 81 | std::lock_guard<std::mutex> lk(mMutex); |
| 82 | LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped"); |
| 83 | mStopped = true; |
| 84 | mRegistration.cancel(); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | void callback(nsecs_t vsynctime) { |
| 89 | nsecs_t period = 0; |
| 90 | { |
| 91 | std::lock_guard<std::mutex> lk(mMutex); |
| 92 | period = mPeriod; |
| 93 | mLastCallTime = vsynctime; |
| 94 | } |
| 95 | |
| 96 | mCallback->onDispSyncEvent(vsynctime - period); |
| 97 | |
| 98 | { |
| 99 | std::lock_guard<std::mutex> lk(mMutex); |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 100 | auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime); |
| 101 | LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled), |
| 102 | "Error rescheduling callback: rc %X", schedule_result); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // DispSync offsets are defined as time after the vsync before presentation. |
| 107 | // VSyncReactor workloads are defined as time before the intended presentation vsync. |
| 108 | // Note change in sign between the two defnitions. |
| 109 | nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; } |
| 110 | |
| 111 | DispSync::Callback* const mCallback; |
| 112 | |
| 113 | std::mutex mutable mMutex; |
| 114 | VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex); |
| 115 | bool mStopped GUARDED_BY(mMutex) = false; |
| 116 | nsecs_t mPeriod GUARDED_BY(mMutex); |
| 117 | nsecs_t mOffset GUARDED_BY(mMutex); |
| 118 | nsecs_t mLastCallTime GUARDED_BY(mMutex); |
| 119 | }; |
| 120 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 121 | bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) { |
| 122 | if (!fence) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | nsecs_t const signalTime = fence->getCachedSignalTime(); |
| 127 | if (signalTime == Fence::SIGNAL_TIME_INVALID) { |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | std::lock_guard<std::mutex> lk(mMutex); |
| 132 | if (mIgnorePresentFences) { |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) { |
| 137 | auto const time = (*it)->getCachedSignalTime(); |
| 138 | if (time == Fence::SIGNAL_TIME_PENDING) { |
| 139 | it++; |
| 140 | } else if (time == Fence::SIGNAL_TIME_INVALID) { |
| 141 | it = mUnfiredFences.erase(it); |
| 142 | } else { |
| 143 | mTracker->addVsyncTimestamp(time); |
| 144 | it = mUnfiredFences.erase(it); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (signalTime == Fence::SIGNAL_TIME_PENDING) { |
| 149 | if (mPendingLimit == mUnfiredFences.size()) { |
| 150 | mUnfiredFences.erase(mUnfiredFences.begin()); |
| 151 | } |
| 152 | mUnfiredFences.push_back(fence); |
| 153 | } else { |
| 154 | mTracker->addVsyncTimestamp(signalTime); |
| 155 | } |
| 156 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 157 | return mMoreSamplesNeeded; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void VSyncReactor::setIgnorePresentFences(bool ignoration) { |
| 161 | std::lock_guard<std::mutex> lk(mMutex); |
| 162 | mIgnorePresentFences = ignoration; |
| 163 | if (mIgnorePresentFences == true) { |
| 164 | mUnfiredFences.clear(); |
| 165 | } |
| 166 | } |
| 167 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 168 | nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const { |
| 169 | auto const now = mClock->now(); |
| 170 | auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0; |
| 171 | return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod); |
| 172 | } |
| 173 | |
| 174 | nsecs_t VSyncReactor::expectedPresentTime() { |
| 175 | return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now()); |
| 176 | } |
| 177 | |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 178 | void VSyncReactor::setPeriod(nsecs_t period) { |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 179 | std::lock_guard lk(mMutex); |
| 180 | mLastHwVsync.reset(); |
| 181 | mPeriodTransitioningTo = period; |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | nsecs_t VSyncReactor::getPeriod() { |
| 185 | return mTracker->currentPeriod(); |
| 186 | } |
| 187 | |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 188 | void VSyncReactor::beginResync() {} |
| 189 | |
| 190 | void VSyncReactor::endResync() {} |
| 191 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 192 | bool VSyncReactor::periodChangeDetected(nsecs_t vsync_timestamp) { |
| 193 | if (!mLastHwVsync || !mPeriodTransitioningTo) { |
| 194 | return false; |
| 195 | } |
| 196 | auto const distance = vsync_timestamp - *mLastHwVsync; |
| 197 | return std::abs(distance - *mPeriodTransitioningTo) < std::abs(distance - getPeriod()); |
| 198 | } |
| 199 | |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 200 | bool VSyncReactor::addResyncSample(nsecs_t timestamp, bool* periodFlushed) { |
| 201 | assert(periodFlushed); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 202 | |
| 203 | std::lock_guard<std::mutex> lk(mMutex); |
| 204 | if (periodChangeDetected(timestamp)) { |
| 205 | mMoreSamplesNeeded = false; |
| 206 | *periodFlushed = true; |
| 207 | |
| 208 | mTracker->setPeriod(*mPeriodTransitioningTo); |
| 209 | for (auto& entry : mCallbacks) { |
| 210 | entry.second->setPeriod(*mPeriodTransitioningTo); |
| 211 | } |
| 212 | |
| 213 | mPeriodTransitioningTo.reset(); |
| 214 | mLastHwVsync.reset(); |
| 215 | } else if (mPeriodTransitioningTo) { |
| 216 | mLastHwVsync = timestamp; |
| 217 | mMoreSamplesNeeded = true; |
| 218 | *periodFlushed = false; |
| 219 | } else { |
| 220 | mMoreSamplesNeeded = false; |
| 221 | *periodFlushed = false; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 222 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 223 | |
| 224 | mTracker->addVsyncTimestamp(timestamp); |
| 225 | return mMoreSamplesNeeded; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 228 | status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase, |
| 229 | DispSync::Callback* callback, |
| 230 | nsecs_t /* lastCallbackTime */) { |
| 231 | std::lock_guard<std::mutex> lk(mMutex); |
| 232 | auto it = mCallbacks.find(callback); |
| 233 | if (it == mCallbacks.end()) { |
| 234 | // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f. |
| 235 | static auto constexpr maxListeners = 3; |
| 236 | if (mCallbacks.size() >= maxListeners) { |
| 237 | ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name, |
| 238 | maxListeners, mCallbacks.size()); |
| 239 | return NO_MEMORY; |
| 240 | } |
| 241 | |
| 242 | auto const period = mTracker->currentPeriod(); |
| 243 | auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period, |
| 244 | phase, mClock->now()); |
| 245 | it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first; |
| 246 | } |
| 247 | |
| 248 | it->second->start(phase); |
| 249 | return NO_ERROR; |
| 250 | } |
| 251 | |
| 252 | status_t VSyncReactor::removeEventListener(DispSync::Callback* callback, |
| 253 | nsecs_t* /* outLastCallback */) { |
| 254 | std::lock_guard<std::mutex> lk(mMutex); |
| 255 | auto const it = mCallbacks.find(callback); |
| 256 | LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback); |
| 257 | |
| 258 | it->second->stop(); |
| 259 | return NO_ERROR; |
| 260 | } |
| 261 | |
| 262 | status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) { |
| 263 | std::lock_guard<std::mutex> lk(mMutex); |
| 264 | auto const it = mCallbacks.find(callback); |
| 265 | LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback); |
| 266 | |
| 267 | it->second->start(phase); |
| 268 | return NO_ERROR; |
| 269 | } |
| 270 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 271 | void VSyncReactor::dump(std::string& result) const { |
| 272 | result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information! |
| 273 | } |
| 274 | |
| 275 | void VSyncReactor::reset() {} |
| 276 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 277 | } // namespace android::scheduler |