blob: 8987dcdc58b3b755b3b07ee39571d402b04172ae [file] [log] [blame]
Kevin DuBoisb2501ba2019-11-12 14:20:29 -08001/*
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 DuBois5988f482020-01-17 09:03:32 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Kevin DuBoisc94ca832019-11-26 12:56:24 -080018#undef LOG_TAG
19#define LOG_TAG "VSyncReactor"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080020//#define LOG_NDEBUG 0
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080021#include "VSyncReactor.h"
Ady Abraham13bc0be2020-02-27 16:48:20 -080022#include <cutils/properties.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080023#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080024#include <utils/Trace.h>
Ady Abraham13bc0be2020-02-27 16:48:20 -080025#include "../TracedOrdinal.h"
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080026#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027#include "VSyncDispatch.h"
28#include "VSyncTracker.h"
29
30namespace android::scheduler {
31
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080032Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080033nsecs_t SystemClock::now() const {
34 return systemTime(SYSTEM_TIME_MONOTONIC);
35}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080036
Ady Abraham13bc0be2020-02-27 16:48:20 -080037class PredictedVsyncTracer {
38public:
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
47private:
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 DuBois2fd3cea2019-11-14 08:52:45 -080057VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080058 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080059 : mClock(std::move(clock)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080060 mTracker(std::move(tracker)),
Kevin DuBois00287382019-11-19 15:11:55 -080061 mDispatch(std::move(dispatch)),
Ady Abraham13bc0be2020-02-27 16:48:20 -080062 mPendingLimit(pendingFenceLimit),
63 mPredictedVsyncTracer(property_get_bool("debug.sf.show_predicted_vsync", false)
64 ? std::make_unique<PredictedVsyncTracer>(*mDispatch)
65 : nullptr) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080066
Kevin DuBoisf91e9232019-11-21 10:51:23 -080067VSyncReactor::~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.
72class CallbackRepeater {
73public:
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 DuBois2968afc2020-01-14 09:48:50 -080078 std::bind(&CallbackRepeater::callback, this, std::placeholders::_1,
79 std::placeholders::_2),
Kevin DuBoisf91e9232019-11-21 10:51:23 -080080 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 DuBoisc94ca832019-11-26 12:56:24 -080095 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 DuBoisf91e9232019-11-21 10:51:23 -080098 }
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
115private:
Kevin DuBois2968afc2020-01-14 09:48:50 -0800116 void callback(nsecs_t vsynctime, nsecs_t wakeupTime) {
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800117 {
118 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800119 mLastCallTime = vsynctime;
120 }
121
Kevin DuBois2968afc2020-01-14 09:48:50 -0800122 mCallback->onDispSyncEvent(wakeupTime);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800123
124 {
125 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisbf7632e2020-02-13 10:11:53 -0800126 if (mStopped) {
127 return;
128 }
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800129 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 DuBoisf91e9232019-11-21 10:51:23 -0800132 }
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 DuBoisb2501ba2019-11-12 14:20:29 -0800150bool 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 DuBois02d5ed92020-01-27 11:05:46 -0800161 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800162 return true;
163 }
164
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800165 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800166 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 DuBois02d5ed92020-01-27 11:05:46 -0800173 timestampAccepted &= mTracker->addVsyncTimestamp(time);
174
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800175 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 DuBois02d5ed92020-01-27 11:05:46 -0800185 timestampAccepted &= mTracker->addVsyncTimestamp(signalTime);
186 }
187
188 if (!timestampAccepted) {
189 mMoreSamplesNeeded = true;
190 setIgnorePresentFencesInternal(true);
191 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800192 }
193
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800194 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800195}
196
197void VSyncReactor::setIgnorePresentFences(bool ignoration) {
198 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800199 mExternalIgnoreFences = ignoration;
200 updateIgnorePresentFencesInternal();
201}
202
203void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) {
204 mInternalIgnoreFences = ignoration;
205 updateIgnorePresentFencesInternal();
206}
207
208void VSyncReactor::updateIgnorePresentFencesInternal() {
209 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800210 mUnfiredFences.clear();
211 }
212}
213
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800214nsecs_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
220nsecs_t VSyncReactor::expectedPresentTime() {
221 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
222}
223
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800224void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800225 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800226 mPeriodTransitioningTo = newPeriod;
227 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800228 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800229}
230
231void VSyncReactor::endPeriodTransition() {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800232 setIgnorePresentFencesInternal(false);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800233 mMoreSamplesNeeded = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800234 mPeriodTransitioningTo.reset();
235 mPeriodConfirmationInProgress = false;
236 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800237}
238
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800239void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800240 ATRACE_INT64("VSR-setPeriod", period);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800241 std::lock_guard lk(mMutex);
242 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800243 if (period == getPeriod()) {
244 endPeriodTransition();
245 } else {
246 startPeriodTransition(period);
247 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800248}
249
250nsecs_t VSyncReactor::getPeriod() {
251 return mTracker->currentPeriod();
252}
253
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800254void VSyncReactor::beginResync() {
255 mTracker->resetModel();
256}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800257
258void VSyncReactor::endResync() {}
259
Ady Abraham5dee2f12020-02-05 17:49:47 -0800260bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
261 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800262 return false;
263 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800264
Ady Abraham5dee2f12020-02-05 17:49:47 -0800265 if (!mLastHwVsync && !HwcVsyncPeriod) {
266 return false;
267 }
268
269 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800270 static constexpr int allowancePercent = 10;
271 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
272 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800273 if (HwcVsyncPeriod) {
274 return std::abs(*HwcVsyncPeriod - period) < allowance;
275 }
276
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800277 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800278 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800279}
280
Ady Abraham5dee2f12020-02-05 17:49:47 -0800281bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
282 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800283 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800284
285 std::lock_guard<std::mutex> lk(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800286 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800287 if (mPeriodTransitioningTo) {
288 mTracker->setPeriod(*mPeriodTransitioningTo);
289 for (auto& entry : mCallbacks) {
290 entry.second->setPeriod(*mPeriodTransitioningTo);
291 }
292 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800293 }
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800294 endPeriodTransition();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800295 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800296 mLastHwVsync = timestamp;
297 mMoreSamplesNeeded = true;
298 *periodFlushed = false;
299 } else {
300 mMoreSamplesNeeded = false;
301 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800302 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800303
304 mTracker->addVsyncTimestamp(timestamp);
305 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800306}
307
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800308status_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 Abraham13bc0be2020-02-27 16:48:20 -0800315 static auto constexpr maxListeners = 4;
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800316 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
332status_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
342status_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 DuBois00287382019-11-19 15:11:55 -0800351void VSyncReactor::dump(std::string& result) const {
352 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
353}
354
355void VSyncReactor::reset() {}
356
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800357} // namespace android::scheduler