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