blob: 949ba4ca521d7c6a2f04b93453c9ef58d4215c1f [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 DuBoisbf7632e2020-02-13 10:11:53 -0800101 if (mStopped) {
102 return;
103 }
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800104 auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime);
105 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
106 "Error rescheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800107 }
108 }
109
110 // DispSync offsets are defined as time after the vsync before presentation.
111 // VSyncReactor workloads are defined as time before the intended presentation vsync.
112 // Note change in sign between the two defnitions.
113 nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; }
114
115 DispSync::Callback* const mCallback;
116
117 std::mutex mutable mMutex;
118 VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex);
119 bool mStopped GUARDED_BY(mMutex) = false;
120 nsecs_t mPeriod GUARDED_BY(mMutex);
121 nsecs_t mOffset GUARDED_BY(mMutex);
122 nsecs_t mLastCallTime GUARDED_BY(mMutex);
123};
124
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800125bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
126 if (!fence) {
127 return false;
128 }
129
130 nsecs_t const signalTime = fence->getCachedSignalTime();
131 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
132 return true;
133 }
134
135 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800136 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800137 return true;
138 }
139
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800140 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800141 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
142 auto const time = (*it)->getCachedSignalTime();
143 if (time == Fence::SIGNAL_TIME_PENDING) {
144 it++;
145 } else if (time == Fence::SIGNAL_TIME_INVALID) {
146 it = mUnfiredFences.erase(it);
147 } else {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800148 timestampAccepted &= mTracker->addVsyncTimestamp(time);
149
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800150 it = mUnfiredFences.erase(it);
151 }
152 }
153
154 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
155 if (mPendingLimit == mUnfiredFences.size()) {
156 mUnfiredFences.erase(mUnfiredFences.begin());
157 }
158 mUnfiredFences.push_back(fence);
159 } else {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800160 timestampAccepted &= mTracker->addVsyncTimestamp(signalTime);
161 }
162
163 if (!timestampAccepted) {
164 mMoreSamplesNeeded = true;
165 setIgnorePresentFencesInternal(true);
166 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800167 }
168
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800169 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800170}
171
172void VSyncReactor::setIgnorePresentFences(bool ignoration) {
173 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800174 mExternalIgnoreFences = ignoration;
175 updateIgnorePresentFencesInternal();
176}
177
178void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) {
179 mInternalIgnoreFences = ignoration;
180 updateIgnorePresentFencesInternal();
181}
182
183void VSyncReactor::updateIgnorePresentFencesInternal() {
184 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800185 mUnfiredFences.clear();
186 }
187}
188
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800189nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const {
190 auto const now = mClock->now();
191 auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0;
192 return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
193}
194
195nsecs_t VSyncReactor::expectedPresentTime() {
196 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
197}
198
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800199void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800200 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800201 mPeriodTransitioningTo = newPeriod;
202 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800203 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800204}
205
206void VSyncReactor::endPeriodTransition() {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800207 setIgnorePresentFencesInternal(false);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800208 mMoreSamplesNeeded = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800209 mPeriodTransitioningTo.reset();
210 mPeriodConfirmationInProgress = false;
211 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800212}
213
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800214void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800215 ATRACE_INT64("VSR-setPeriod", period);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800216 std::lock_guard lk(mMutex);
217 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800218 if (period == getPeriod()) {
219 endPeriodTransition();
220 } else {
221 startPeriodTransition(period);
222 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800223}
224
225nsecs_t VSyncReactor::getPeriod() {
226 return mTracker->currentPeriod();
227}
228
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800229void VSyncReactor::beginResync() {
230 mTracker->resetModel();
231}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800232
233void VSyncReactor::endResync() {}
234
Ady Abraham5dee2f12020-02-05 17:49:47 -0800235bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
236 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800237 return false;
238 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800239
Ady Abraham5dee2f12020-02-05 17:49:47 -0800240 if (!mLastHwVsync && !HwcVsyncPeriod) {
241 return false;
242 }
243
244 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800245 static constexpr int allowancePercent = 10;
246 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
247 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800248 if (HwcVsyncPeriod) {
249 return std::abs(*HwcVsyncPeriod - period) < allowance;
250 }
251
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800252 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800253 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800254}
255
Ady Abraham5dee2f12020-02-05 17:49:47 -0800256bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
257 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800258 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800259
260 std::lock_guard<std::mutex> lk(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800261 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800262 if (mPeriodTransitioningTo) {
263 mTracker->setPeriod(*mPeriodTransitioningTo);
264 for (auto& entry : mCallbacks) {
265 entry.second->setPeriod(*mPeriodTransitioningTo);
266 }
267 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800268 }
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800269 endPeriodTransition();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800270 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800271 mLastHwVsync = timestamp;
272 mMoreSamplesNeeded = true;
273 *periodFlushed = false;
274 } else {
275 mMoreSamplesNeeded = false;
276 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800277 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800278
279 mTracker->addVsyncTimestamp(timestamp);
280 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800281}
282
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800283status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
284 DispSync::Callback* callback,
285 nsecs_t /* lastCallbackTime */) {
286 std::lock_guard<std::mutex> lk(mMutex);
287 auto it = mCallbacks.find(callback);
288 if (it == mCallbacks.end()) {
289 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
290 static auto constexpr maxListeners = 3;
291 if (mCallbacks.size() >= maxListeners) {
292 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
293 maxListeners, mCallbacks.size());
294 return NO_MEMORY;
295 }
296
297 auto const period = mTracker->currentPeriod();
298 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
299 phase, mClock->now());
300 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
301 }
302
303 it->second->start(phase);
304 return NO_ERROR;
305}
306
307status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
308 nsecs_t* /* outLastCallback */) {
309 std::lock_guard<std::mutex> lk(mMutex);
310 auto const it = mCallbacks.find(callback);
311 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
312
313 it->second->stop();
314 return NO_ERROR;
315}
316
317status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
318 std::lock_guard<std::mutex> lk(mMutex);
319 auto const it = mCallbacks.find(callback);
320 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
321
322 it->second->start(phase);
323 return NO_ERROR;
324}
325
Kevin DuBois00287382019-11-19 15:11:55 -0800326void VSyncReactor::dump(std::string& result) const {
327 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
328}
329
330void VSyncReactor::reset() {}
331
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800332} // namespace android::scheduler