blob: da73e4e194d87a3da5220553e2b53062838f7c0d [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"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080022#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080023#include <utils/Trace.h>
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080024#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080025#include "VSyncDispatch.h"
26#include "VSyncTracker.h"
27
28namespace android::scheduler {
29
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080030Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080031nsecs_t SystemClock::now() const {
32 return systemTime(SYSTEM_TIME_MONOTONIC);
33}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080034
35VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080036 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080037 : mClock(std::move(clock)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080038 mTracker(std::move(tracker)),
Kevin DuBois00287382019-11-19 15:11:55 -080039 mDispatch(std::move(dispatch)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080040 mPendingLimit(pendingFenceLimit) {}
41
Kevin DuBoisf91e9232019-11-21 10:51:23 -080042VSyncReactor::~VSyncReactor() = default;
43
44// The DispSync interface has a 'repeat this callback at rate' semantic. This object adapts
45// VSyncDispatch's individually-scheduled callbacks so as to meet DispSync's existing semantic
46// for now.
47class CallbackRepeater {
48public:
49 CallbackRepeater(VSyncDispatch& dispatch, DispSync::Callback* cb, const char* name,
50 nsecs_t period, nsecs_t offset, nsecs_t notBefore)
51 : mCallback(cb),
52 mRegistration(dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -080053 std::bind(&CallbackRepeater::callback, this, std::placeholders::_1,
54 std::placeholders::_2),
Kevin DuBoisf91e9232019-11-21 10:51:23 -080055 std::string(name)),
56 mPeriod(period),
57 mOffset(offset),
58 mLastCallTime(notBefore) {}
59
60 ~CallbackRepeater() {
61 std::lock_guard<std::mutex> lk(mMutex);
62 mRegistration.cancel();
63 }
64
65 void start(nsecs_t offset) {
66 std::lock_guard<std::mutex> lk(mMutex);
67 mStopped = false;
68 mOffset = offset;
69
Kevin DuBoisc94ca832019-11-26 12:56:24 -080070 auto const schedule_result = mRegistration.schedule(calculateWorkload(), mLastCallTime);
71 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
72 "Error scheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080073 }
74
75 void setPeriod(nsecs_t period) {
76 std::lock_guard<std::mutex> lk(mMutex);
77 if (period == mPeriod) {
78 return;
79 }
80 mPeriod = period;
81 }
82
83 void stop() {
84 std::lock_guard<std::mutex> lk(mMutex);
85 LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped");
86 mStopped = true;
87 mRegistration.cancel();
88 }
89
90private:
Kevin DuBois2968afc2020-01-14 09:48:50 -080091 void callback(nsecs_t vsynctime, nsecs_t wakeupTime) {
Kevin DuBoisf91e9232019-11-21 10:51:23 -080092 {
93 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080094 mLastCallTime = vsynctime;
95 }
96
Kevin DuBois2968afc2020-01-14 09:48:50 -080097 mCallback->onDispSyncEvent(wakeupTime);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080098
99 {
100 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800101 auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime);
102 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
103 "Error rescheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800104 }
105 }
106
107 // DispSync offsets are defined as time after the vsync before presentation.
108 // VSyncReactor workloads are defined as time before the intended presentation vsync.
109 // Note change in sign between the two defnitions.
110 nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; }
111
112 DispSync::Callback* const mCallback;
113
114 std::mutex mutable mMutex;
115 VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex);
116 bool mStopped GUARDED_BY(mMutex) = false;
117 nsecs_t mPeriod GUARDED_BY(mMutex);
118 nsecs_t mOffset GUARDED_BY(mMutex);
119 nsecs_t mLastCallTime GUARDED_BY(mMutex);
120};
121
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800122bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
123 if (!fence) {
124 return false;
125 }
126
127 nsecs_t const signalTime = fence->getCachedSignalTime();
128 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
129 return true;
130 }
131
132 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800133 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800134 return true;
135 }
136
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800137 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800138 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
139 auto const time = (*it)->getCachedSignalTime();
140 if (time == Fence::SIGNAL_TIME_PENDING) {
141 it++;
142 } else if (time == Fence::SIGNAL_TIME_INVALID) {
143 it = mUnfiredFences.erase(it);
144 } else {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800145 timestampAccepted &= mTracker->addVsyncTimestamp(time);
146
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800147 it = mUnfiredFences.erase(it);
148 }
149 }
150
151 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
152 if (mPendingLimit == mUnfiredFences.size()) {
153 mUnfiredFences.erase(mUnfiredFences.begin());
154 }
155 mUnfiredFences.push_back(fence);
156 } else {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800157 timestampAccepted &= mTracker->addVsyncTimestamp(signalTime);
158 }
159
160 if (!timestampAccepted) {
161 mMoreSamplesNeeded = true;
162 setIgnorePresentFencesInternal(true);
163 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800164 }
165
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800166 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800167}
168
169void VSyncReactor::setIgnorePresentFences(bool ignoration) {
170 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800171 mExternalIgnoreFences = ignoration;
172 updateIgnorePresentFencesInternal();
173}
174
175void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) {
176 mInternalIgnoreFences = ignoration;
177 updateIgnorePresentFencesInternal();
178}
179
180void VSyncReactor::updateIgnorePresentFencesInternal() {
181 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800182 mUnfiredFences.clear();
183 }
184}
185
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800186nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const {
187 auto const now = mClock->now();
188 auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0;
189 return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
190}
191
192nsecs_t VSyncReactor::expectedPresentTime() {
193 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
194}
195
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800196void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800197 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800198 mPeriodTransitioningTo = newPeriod;
199 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800200 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800201}
202
203void VSyncReactor::endPeriodTransition() {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800204 setIgnorePresentFencesInternal(false);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800205 mMoreSamplesNeeded = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800206 mPeriodTransitioningTo.reset();
207 mPeriodConfirmationInProgress = false;
208 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800209}
210
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800211void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800212 ATRACE_INT64("VSR-setPeriod", period);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800213 std::lock_guard lk(mMutex);
214 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800215 if (period == getPeriod()) {
216 endPeriodTransition();
217 } else {
218 startPeriodTransition(period);
219 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800220}
221
222nsecs_t VSyncReactor::getPeriod() {
223 return mTracker->currentPeriod();
224}
225
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800226void VSyncReactor::beginResync() {
227 mTracker->resetModel();
228}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800229
230void VSyncReactor::endResync() {}
231
Ady Abraham5dee2f12020-02-05 17:49:47 -0800232bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
233 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800234 return false;
235 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800236
Ady Abraham5dee2f12020-02-05 17:49:47 -0800237 if (!mLastHwVsync && !HwcVsyncPeriod) {
238 return false;
239 }
240
241 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800242 static constexpr int allowancePercent = 10;
243 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
244 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800245 if (HwcVsyncPeriod) {
246 return std::abs(*HwcVsyncPeriod - period) < allowance;
247 }
248
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800249 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800250 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800251}
252
Ady Abraham5dee2f12020-02-05 17:49:47 -0800253bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
254 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800255 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800256
257 std::lock_guard<std::mutex> lk(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800258 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800259 if (mPeriodTransitioningTo) {
260 mTracker->setPeriod(*mPeriodTransitioningTo);
261 for (auto& entry : mCallbacks) {
262 entry.second->setPeriod(*mPeriodTransitioningTo);
263 }
264 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800265 }
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800266 endPeriodTransition();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800267 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800268 mLastHwVsync = timestamp;
269 mMoreSamplesNeeded = true;
270 *periodFlushed = false;
271 } else {
272 mMoreSamplesNeeded = false;
273 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800274 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800275
276 mTracker->addVsyncTimestamp(timestamp);
277 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800278}
279
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800280status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
281 DispSync::Callback* callback,
282 nsecs_t /* lastCallbackTime */) {
283 std::lock_guard<std::mutex> lk(mMutex);
284 auto it = mCallbacks.find(callback);
285 if (it == mCallbacks.end()) {
286 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
287 static auto constexpr maxListeners = 3;
288 if (mCallbacks.size() >= maxListeners) {
289 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
290 maxListeners, mCallbacks.size());
291 return NO_MEMORY;
292 }
293
294 auto const period = mTracker->currentPeriod();
295 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
296 phase, mClock->now());
297 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
298 }
299
300 it->second->start(phase);
301 return NO_ERROR;
302}
303
304status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
305 nsecs_t* /* outLastCallback */) {
306 std::lock_guard<std::mutex> lk(mMutex);
307 auto const it = mCallbacks.find(callback);
308 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
309
310 it->second->stop();
311 return NO_ERROR;
312}
313
314status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
315 std::lock_guard<std::mutex> lk(mMutex);
316 auto const it = mCallbacks.find(callback);
317 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
318
319 it->second->start(phase);
320 return NO_ERROR;
321}
322
Kevin DuBois00287382019-11-19 15:11:55 -0800323void VSyncReactor::dump(std::string& result) const {
324 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
325}
326
327void VSyncReactor::reset() {}
328
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800329} // namespace android::scheduler