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 | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "VSyncReactor" |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 21 | #include "VSyncReactor.h" |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 23 | #include <log/log.h> |
Kevin DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 24 | #include <utils/Trace.h> |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 25 | #include "../TracedOrdinal.h" |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 26 | #include "TimeKeeper.h" |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 27 | #include "VSyncDispatch.h" |
| 28 | #include "VSyncTracker.h" |
| 29 | |
| 30 | namespace android::scheduler { |
| 31 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 32 | Clock::~Clock() = default; |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 33 | nsecs_t SystemClock::now() const { |
| 34 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 35 | } |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 36 | |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 37 | class PredictedVsyncTracer { |
| 38 | public: |
| 39 | PredictedVsyncTracer(VSyncDispatch& dispatch) |
| 40 | : mRegistration(dispatch, |
| 41 | std::bind(&PredictedVsyncTracer::callback, this, std::placeholders::_1, |
| 42 | std::placeholders::_2), |
| 43 | "PredictedVsyncTracer") { |
| 44 | mRegistration.schedule(0, 0); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0}; |
| 49 | VSyncCallbackRegistration mRegistration; |
| 50 | |
| 51 | void callback(nsecs_t /*vsyncTime*/, nsecs_t /*targetWakeupTim*/) { |
| 52 | mParity = !mParity; |
| 53 | mRegistration.schedule(0, 0); |
| 54 | } |
| 55 | }; |
| 56 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 57 | VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch, |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 58 | std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit) |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 59 | : mClock(std::move(clock)), |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 60 | mTracker(std::move(tracker)), |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 61 | mDispatch(std::move(dispatch)), |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 62 | mPendingLimit(pendingFenceLimit), |
| 63 | mPredictedVsyncTracer(property_get_bool("debug.sf.show_predicted_vsync", false) |
| 64 | ? std::make_unique<PredictedVsyncTracer>(*mDispatch) |
| 65 | : nullptr) {} |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 66 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 67 | VSyncReactor::~VSyncReactor() = default; |
| 68 | |
| 69 | // The DispSync interface has a 'repeat this callback at rate' semantic. This object adapts |
| 70 | // VSyncDispatch's individually-scheduled callbacks so as to meet DispSync's existing semantic |
| 71 | // for now. |
| 72 | class CallbackRepeater { |
| 73 | public: |
| 74 | CallbackRepeater(VSyncDispatch& dispatch, DispSync::Callback* cb, const char* name, |
| 75 | nsecs_t period, nsecs_t offset, nsecs_t notBefore) |
| 76 | : mCallback(cb), |
| 77 | mRegistration(dispatch, |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 78 | std::bind(&CallbackRepeater::callback, this, std::placeholders::_1, |
| 79 | std::placeholders::_2), |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 80 | std::string(name)), |
| 81 | mPeriod(period), |
| 82 | mOffset(offset), |
| 83 | mLastCallTime(notBefore) {} |
| 84 | |
| 85 | ~CallbackRepeater() { |
| 86 | std::lock_guard<std::mutex> lk(mMutex); |
| 87 | mRegistration.cancel(); |
| 88 | } |
| 89 | |
| 90 | void start(nsecs_t offset) { |
| 91 | std::lock_guard<std::mutex> lk(mMutex); |
| 92 | mStopped = false; |
| 93 | mOffset = offset; |
| 94 | |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 95 | auto const schedule_result = mRegistration.schedule(calculateWorkload(), mLastCallTime); |
| 96 | LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled), |
| 97 | "Error scheduling callback: rc %X", schedule_result); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void setPeriod(nsecs_t period) { |
| 101 | std::lock_guard<std::mutex> lk(mMutex); |
| 102 | if (period == mPeriod) { |
| 103 | return; |
| 104 | } |
| 105 | mPeriod = period; |
| 106 | } |
| 107 | |
| 108 | void stop() { |
| 109 | std::lock_guard<std::mutex> lk(mMutex); |
| 110 | LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped"); |
| 111 | mStopped = true; |
| 112 | mRegistration.cancel(); |
| 113 | } |
| 114 | |
| 115 | private: |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 116 | void callback(nsecs_t vsynctime, nsecs_t wakeupTime) { |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 117 | { |
| 118 | std::lock_guard<std::mutex> lk(mMutex); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 119 | mLastCallTime = vsynctime; |
| 120 | } |
| 121 | |
Kevin DuBois | 2968afc | 2020-01-14 09:48:50 -0800 | [diff] [blame] | 122 | mCallback->onDispSyncEvent(wakeupTime); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 123 | |
| 124 | { |
| 125 | std::lock_guard<std::mutex> lk(mMutex); |
Kevin DuBois | bf7632e | 2020-02-13 10:11:53 -0800 | [diff] [blame] | 126 | if (mStopped) { |
| 127 | return; |
| 128 | } |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 129 | auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime); |
| 130 | LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled), |
| 131 | "Error rescheduling callback: rc %X", schedule_result); |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | // DispSync offsets are defined as time after the vsync before presentation. |
| 136 | // VSyncReactor workloads are defined as time before the intended presentation vsync. |
| 137 | // Note change in sign between the two defnitions. |
| 138 | nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; } |
| 139 | |
| 140 | DispSync::Callback* const mCallback; |
| 141 | |
| 142 | std::mutex mutable mMutex; |
| 143 | VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex); |
| 144 | bool mStopped GUARDED_BY(mMutex) = false; |
| 145 | nsecs_t mPeriod GUARDED_BY(mMutex); |
| 146 | nsecs_t mOffset GUARDED_BY(mMutex); |
| 147 | nsecs_t mLastCallTime GUARDED_BY(mMutex); |
| 148 | }; |
| 149 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 150 | bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) { |
| 151 | if (!fence) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | nsecs_t const signalTime = fence->getCachedSignalTime(); |
| 156 | if (signalTime == Fence::SIGNAL_TIME_INVALID) { |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | std::lock_guard<std::mutex> lk(mMutex); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 161 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 165 | bool timestampAccepted = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 166 | for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) { |
| 167 | auto const time = (*it)->getCachedSignalTime(); |
| 168 | if (time == Fence::SIGNAL_TIME_PENDING) { |
| 169 | it++; |
| 170 | } else if (time == Fence::SIGNAL_TIME_INVALID) { |
| 171 | it = mUnfiredFences.erase(it); |
| 172 | } else { |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 173 | timestampAccepted &= mTracker->addVsyncTimestamp(time); |
| 174 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 175 | it = mUnfiredFences.erase(it); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (signalTime == Fence::SIGNAL_TIME_PENDING) { |
| 180 | if (mPendingLimit == mUnfiredFences.size()) { |
| 181 | mUnfiredFences.erase(mUnfiredFences.begin()); |
| 182 | } |
| 183 | mUnfiredFences.push_back(fence); |
| 184 | } else { |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 185 | timestampAccepted &= mTracker->addVsyncTimestamp(signalTime); |
| 186 | } |
| 187 | |
| 188 | if (!timestampAccepted) { |
| 189 | mMoreSamplesNeeded = true; |
| 190 | setIgnorePresentFencesInternal(true); |
| 191 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 194 | return mMoreSamplesNeeded; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void VSyncReactor::setIgnorePresentFences(bool ignoration) { |
| 198 | std::lock_guard<std::mutex> lk(mMutex); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 199 | mExternalIgnoreFences = ignoration; |
| 200 | updateIgnorePresentFencesInternal(); |
| 201 | } |
| 202 | |
| 203 | void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) { |
| 204 | mInternalIgnoreFences = ignoration; |
| 205 | updateIgnorePresentFencesInternal(); |
| 206 | } |
| 207 | |
| 208 | void VSyncReactor::updateIgnorePresentFencesInternal() { |
| 209 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 210 | mUnfiredFences.clear(); |
| 211 | } |
| 212 | } |
| 213 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 214 | nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const { |
| 215 | auto const now = mClock->now(); |
| 216 | auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0; |
| 217 | return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod); |
| 218 | } |
| 219 | |
| 220 | nsecs_t VSyncReactor::expectedPresentTime() { |
| 221 | return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now()); |
| 222 | } |
| 223 | |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 224 | void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) { |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 225 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 226 | mPeriodTransitioningTo = newPeriod; |
| 227 | mMoreSamplesNeeded = true; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 228 | setIgnorePresentFencesInternal(true); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void VSyncReactor::endPeriodTransition() { |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 232 | setIgnorePresentFencesInternal(false); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 233 | mMoreSamplesNeeded = false; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 234 | mPeriodTransitioningTo.reset(); |
| 235 | mPeriodConfirmationInProgress = false; |
| 236 | mLastHwVsync.reset(); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 239 | void VSyncReactor::setPeriod(nsecs_t period) { |
Kevin DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 240 | ATRACE_INT64("VSR-setPeriod", period); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 241 | std::lock_guard lk(mMutex); |
| 242 | mLastHwVsync.reset(); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 243 | if (period == getPeriod()) { |
| 244 | endPeriodTransition(); |
| 245 | } else { |
| 246 | startPeriodTransition(period); |
| 247 | } |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | nsecs_t VSyncReactor::getPeriod() { |
| 251 | return mTracker->currentPeriod(); |
| 252 | } |
| 253 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 254 | void VSyncReactor::beginResync() { |
| 255 | mTracker->resetModel(); |
| 256 | } |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 257 | |
| 258 | void VSyncReactor::endResync() {} |
| 259 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 260 | bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) { |
| 261 | if (!mPeriodConfirmationInProgress) { |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 262 | return false; |
| 263 | } |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 264 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 265 | if (!mLastHwVsync && !HwcVsyncPeriod) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 270 | static constexpr int allowancePercent = 10; |
| 271 | static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio; |
| 272 | auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den; |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 273 | if (HwcVsyncPeriod) { |
| 274 | return std::abs(*HwcVsyncPeriod - period) < allowance; |
| 275 | } |
| 276 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 277 | auto const distance = vsync_timestamp - *mLastHwVsync; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 278 | return std::abs(distance - period) < allowance; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 281 | bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, |
| 282 | bool* periodFlushed) { |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 283 | assert(periodFlushed); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 284 | |
| 285 | std::lock_guard<std::mutex> lk(mMutex); |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 286 | if (periodConfirmed(timestamp, hwcVsyncPeriod)) { |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 287 | if (mPeriodTransitioningTo) { |
| 288 | mTracker->setPeriod(*mPeriodTransitioningTo); |
| 289 | for (auto& entry : mCallbacks) { |
| 290 | entry.second->setPeriod(*mPeriodTransitioningTo); |
| 291 | } |
| 292 | *periodFlushed = true; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 293 | } |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 294 | endPeriodTransition(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 295 | } else if (mPeriodConfirmationInProgress) { |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 296 | mLastHwVsync = timestamp; |
| 297 | mMoreSamplesNeeded = true; |
| 298 | *periodFlushed = false; |
| 299 | } else { |
| 300 | mMoreSamplesNeeded = false; |
| 301 | *periodFlushed = false; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 302 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 303 | |
| 304 | mTracker->addVsyncTimestamp(timestamp); |
| 305 | return mMoreSamplesNeeded; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 308 | status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase, |
| 309 | DispSync::Callback* callback, |
| 310 | nsecs_t /* lastCallbackTime */) { |
| 311 | std::lock_guard<std::mutex> lk(mMutex); |
| 312 | auto it = mCallbacks.find(callback); |
| 313 | if (it == mCallbacks.end()) { |
| 314 | // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f. |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 315 | static auto constexpr maxListeners = 4; |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 316 | if (mCallbacks.size() >= maxListeners) { |
| 317 | ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name, |
| 318 | maxListeners, mCallbacks.size()); |
| 319 | return NO_MEMORY; |
| 320 | } |
| 321 | |
| 322 | auto const period = mTracker->currentPeriod(); |
| 323 | auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period, |
| 324 | phase, mClock->now()); |
| 325 | it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first; |
| 326 | } |
| 327 | |
| 328 | it->second->start(phase); |
| 329 | return NO_ERROR; |
| 330 | } |
| 331 | |
| 332 | status_t VSyncReactor::removeEventListener(DispSync::Callback* callback, |
| 333 | nsecs_t* /* outLastCallback */) { |
| 334 | std::lock_guard<std::mutex> lk(mMutex); |
| 335 | auto const it = mCallbacks.find(callback); |
| 336 | LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback); |
| 337 | |
| 338 | it->second->stop(); |
| 339 | return NO_ERROR; |
| 340 | } |
| 341 | |
| 342 | status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) { |
| 343 | std::lock_guard<std::mutex> lk(mMutex); |
| 344 | auto const it = mCallbacks.find(callback); |
| 345 | LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback); |
| 346 | |
| 347 | it->second->start(phase); |
| 348 | return NO_ERROR; |
| 349 | } |
| 350 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 351 | void VSyncReactor::dump(std::string& result) const { |
| 352 | result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information! |
| 353 | } |
| 354 | |
| 355 | void VSyncReactor::reset() {} |
| 356 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 357 | } // namespace android::scheduler |