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