blob: 6c0a64eca03daae5c431fdf2cb58e87df1f4e273 [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 DuBoisee2ad9f2019-11-21 11:10:57 -0800177void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800178 std::lock_guard lk(mMutex);
179 mLastHwVsync.reset();
180 mPeriodTransitioningTo = period;
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800181}
182
183nsecs_t VSyncReactor::getPeriod() {
184 return mTracker->currentPeriod();
185}
186
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800187void VSyncReactor::beginResync() {}
188
189void VSyncReactor::endResync() {}
190
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800191bool VSyncReactor::periodChangeDetected(nsecs_t vsync_timestamp) {
192 if (!mLastHwVsync || !mPeriodTransitioningTo) {
193 return false;
194 }
195 auto const distance = vsync_timestamp - *mLastHwVsync;
196 return std::abs(distance - *mPeriodTransitioningTo) < std::abs(distance - getPeriod());
197}
198
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800199bool VSyncReactor::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
200 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800201
202 std::lock_guard<std::mutex> lk(mMutex);
203 if (periodChangeDetected(timestamp)) {
204 mMoreSamplesNeeded = false;
205 *periodFlushed = true;
206
207 mTracker->setPeriod(*mPeriodTransitioningTo);
208 for (auto& entry : mCallbacks) {
209 entry.second->setPeriod(*mPeriodTransitioningTo);
210 }
211
212 mPeriodTransitioningTo.reset();
213 mLastHwVsync.reset();
214 } else if (mPeriodTransitioningTo) {
215 mLastHwVsync = timestamp;
216 mMoreSamplesNeeded = true;
217 *periodFlushed = false;
218 } else {
219 mMoreSamplesNeeded = false;
220 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800221 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800222
223 mTracker->addVsyncTimestamp(timestamp);
224 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800225}
226
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800227status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
228 DispSync::Callback* callback,
229 nsecs_t /* lastCallbackTime */) {
230 std::lock_guard<std::mutex> lk(mMutex);
231 auto it = mCallbacks.find(callback);
232 if (it == mCallbacks.end()) {
233 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
234 static auto constexpr maxListeners = 3;
235 if (mCallbacks.size() >= maxListeners) {
236 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
237 maxListeners, mCallbacks.size());
238 return NO_MEMORY;
239 }
240
241 auto const period = mTracker->currentPeriod();
242 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
243 phase, mClock->now());
244 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
245 }
246
247 it->second->start(phase);
248 return NO_ERROR;
249}
250
251status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
252 nsecs_t* /* outLastCallback */) {
253 std::lock_guard<std::mutex> lk(mMutex);
254 auto const it = mCallbacks.find(callback);
255 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
256
257 it->second->stop();
258 return NO_ERROR;
259}
260
261status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
262 std::lock_guard<std::mutex> lk(mMutex);
263 auto const it = mCallbacks.find(callback);
264 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
265
266 it->second->start(phase);
267 return NO_ERROR;
268}
269
Kevin DuBois00287382019-11-19 15:11:55 -0800270void VSyncReactor::dump(std::string& result) const {
271 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
272}
273
274void VSyncReactor::reset() {}
275
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800276} // namespace android::scheduler