blob: 20b6238c928f8ee88edfde6127da97ba5397b918 [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 DuBoisc94ca832019-11-26 12:56:24 -080017#undef LOG_TAG
18#define LOG_TAG "VSyncReactor"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080019//#define LOG_NDEBUG 0
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080020#include "VSyncReactor.h"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080021#include <log/log.h>
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080022#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080023#include "VSyncDispatch.h"
24#include "VSyncTracker.h"
25
26namespace android::scheduler {
27
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080028Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080029nsecs_t SystemClock::now() const {
30 return systemTime(SYSTEM_TIME_MONOTONIC);
31}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080032
33VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080034 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080035 : mClock(std::move(clock)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080036 mTracker(std::move(tracker)),
Kevin DuBois00287382019-11-19 15:11:55 -080037 mDispatch(std::move(dispatch)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080038 mPendingLimit(pendingFenceLimit) {}
39
Kevin DuBoisf91e9232019-11-21 10:51:23 -080040VSyncReactor::~VSyncReactor() = default;
41
42// The DispSync interface has a 'repeat this callback at rate' semantic. This object adapts
43// VSyncDispatch's individually-scheduled callbacks so as to meet DispSync's existing semantic
44// for now.
45class CallbackRepeater {
46public:
47 CallbackRepeater(VSyncDispatch& dispatch, DispSync::Callback* cb, const char* name,
48 nsecs_t period, nsecs_t offset, nsecs_t notBefore)
49 : mCallback(cb),
50 mRegistration(dispatch,
Kevin DuBois2968afc2020-01-14 09:48:50 -080051 std::bind(&CallbackRepeater::callback, this, std::placeholders::_1,
52 std::placeholders::_2),
Kevin DuBoisf91e9232019-11-21 10:51:23 -080053 std::string(name)),
54 mPeriod(period),
55 mOffset(offset),
56 mLastCallTime(notBefore) {}
57
58 ~CallbackRepeater() {
59 std::lock_guard<std::mutex> lk(mMutex);
60 mRegistration.cancel();
61 }
62
63 void start(nsecs_t offset) {
64 std::lock_guard<std::mutex> lk(mMutex);
65 mStopped = false;
66 mOffset = offset;
67
Kevin DuBoisc94ca832019-11-26 12:56:24 -080068 auto const schedule_result = mRegistration.schedule(calculateWorkload(), mLastCallTime);
69 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
70 "Error scheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080071 }
72
73 void setPeriod(nsecs_t period) {
74 std::lock_guard<std::mutex> lk(mMutex);
75 if (period == mPeriod) {
76 return;
77 }
78 mPeriod = period;
79 }
80
81 void stop() {
82 std::lock_guard<std::mutex> lk(mMutex);
83 LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped");
84 mStopped = true;
85 mRegistration.cancel();
86 }
87
88private:
Kevin DuBois2968afc2020-01-14 09:48:50 -080089 void callback(nsecs_t vsynctime, nsecs_t wakeupTime) {
Kevin DuBoisf91e9232019-11-21 10:51:23 -080090 {
91 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080092 mLastCallTime = vsynctime;
93 }
94
Kevin DuBois2968afc2020-01-14 09:48:50 -080095 mCallback->onDispSyncEvent(wakeupTime);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080096
97 {
98 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisc94ca832019-11-26 12:56:24 -080099 auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime);
100 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
101 "Error rescheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800102 }
103 }
104
105 // DispSync offsets are defined as time after the vsync before presentation.
106 // VSyncReactor workloads are defined as time before the intended presentation vsync.
107 // Note change in sign between the two defnitions.
108 nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; }
109
110 DispSync::Callback* const mCallback;
111
112 std::mutex mutable mMutex;
113 VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex);
114 bool mStopped GUARDED_BY(mMutex) = false;
115 nsecs_t mPeriod GUARDED_BY(mMutex);
116 nsecs_t mOffset GUARDED_BY(mMutex);
117 nsecs_t mLastCallTime GUARDED_BY(mMutex);
118};
119
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800120bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
121 if (!fence) {
122 return false;
123 }
124
125 nsecs_t const signalTime = fence->getCachedSignalTime();
126 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
127 return true;
128 }
129
130 std::lock_guard<std::mutex> lk(mMutex);
131 if (mIgnorePresentFences) {
132 return true;
133 }
134
135 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
136 auto const time = (*it)->getCachedSignalTime();
137 if (time == Fence::SIGNAL_TIME_PENDING) {
138 it++;
139 } else if (time == Fence::SIGNAL_TIME_INVALID) {
140 it = mUnfiredFences.erase(it);
141 } else {
142 mTracker->addVsyncTimestamp(time);
143 it = mUnfiredFences.erase(it);
144 }
145 }
146
147 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
148 if (mPendingLimit == mUnfiredFences.size()) {
149 mUnfiredFences.erase(mUnfiredFences.begin());
150 }
151 mUnfiredFences.push_back(fence);
152 } else {
153 mTracker->addVsyncTimestamp(signalTime);
154 }
155
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800156 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800157}
158
159void VSyncReactor::setIgnorePresentFences(bool ignoration) {
160 std::lock_guard<std::mutex> lk(mMutex);
161 mIgnorePresentFences = ignoration;
162 if (mIgnorePresentFences == true) {
163 mUnfiredFences.clear();
164 }
165}
166
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800167nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const {
168 auto const now = mClock->now();
169 auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0;
170 return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
171}
172
173nsecs_t VSyncReactor::expectedPresentTime() {
174 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
175}
176
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800177void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
178 mPeriodTransitioningTo = newPeriod;
179 mMoreSamplesNeeded = true;
180}
181
182void VSyncReactor::endPeriodTransition() {
183 mPeriodTransitioningTo.reset();
184 mLastHwVsync.reset();
185 mMoreSamplesNeeded = false;
186}
187
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800188void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800189 std::lock_guard lk(mMutex);
190 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800191 if (period == getPeriod()) {
192 endPeriodTransition();
193 } else {
194 startPeriodTransition(period);
195 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800196}
197
198nsecs_t VSyncReactor::getPeriod() {
199 return mTracker->currentPeriod();
200}
201
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800202void VSyncReactor::beginResync() {}
203
204void VSyncReactor::endResync() {}
205
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800206bool VSyncReactor::periodChangeDetected(nsecs_t vsync_timestamp) {
207 if (!mLastHwVsync || !mPeriodTransitioningTo) {
208 return false;
209 }
210 auto const distance = vsync_timestamp - *mLastHwVsync;
211 return std::abs(distance - *mPeriodTransitioningTo) < std::abs(distance - getPeriod());
212}
213
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800214bool VSyncReactor::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
215 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800216
217 std::lock_guard<std::mutex> lk(mMutex);
218 if (periodChangeDetected(timestamp)) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800219 mTracker->setPeriod(*mPeriodTransitioningTo);
220 for (auto& entry : mCallbacks) {
221 entry.second->setPeriod(*mPeriodTransitioningTo);
222 }
223
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800224 endPeriodTransition();
225 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800226 } else if (mPeriodTransitioningTo) {
227 mLastHwVsync = timestamp;
228 mMoreSamplesNeeded = true;
229 *periodFlushed = false;
230 } else {
231 mMoreSamplesNeeded = false;
232 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800233 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800234
235 mTracker->addVsyncTimestamp(timestamp);
236 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800237}
238
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800239status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
240 DispSync::Callback* callback,
241 nsecs_t /* lastCallbackTime */) {
242 std::lock_guard<std::mutex> lk(mMutex);
243 auto it = mCallbacks.find(callback);
244 if (it == mCallbacks.end()) {
245 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
246 static auto constexpr maxListeners = 3;
247 if (mCallbacks.size() >= maxListeners) {
248 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
249 maxListeners, mCallbacks.size());
250 return NO_MEMORY;
251 }
252
253 auto const period = mTracker->currentPeriod();
254 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
255 phase, mClock->now());
256 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
257 }
258
259 it->second->start(phase);
260 return NO_ERROR;
261}
262
263status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
264 nsecs_t* /* outLastCallback */) {
265 std::lock_guard<std::mutex> lk(mMutex);
266 auto const it = mCallbacks.find(callback);
267 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
268
269 it->second->stop();
270 return NO_ERROR;
271}
272
273status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
274 std::lock_guard<std::mutex> lk(mMutex);
275 auto const it = mCallbacks.find(callback);
276 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
277
278 it->second->start(phase);
279 return NO_ERROR;
280}
281
Kevin DuBois00287382019-11-19 15:11:55 -0800282void VSyncReactor::dump(std::string& result) const {
283 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
284}
285
286void VSyncReactor::reset() {}
287
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800288} // namespace android::scheduler