blob: 57e05d39a3dfbba39614b62011ad26ac52907a6b [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,
51 std::bind(&CallbackRepeater::callback, this, std::placeholders::_1),
52 std::string(name)),
53 mPeriod(period),
54 mOffset(offset),
55 mLastCallTime(notBefore) {}
56
57 ~CallbackRepeater() {
58 std::lock_guard<std::mutex> lk(mMutex);
59 mRegistration.cancel();
60 }
61
62 void start(nsecs_t offset) {
63 std::lock_guard<std::mutex> lk(mMutex);
64 mStopped = false;
65 mOffset = offset;
66
Kevin DuBoisc94ca832019-11-26 12:56:24 -080067 auto const schedule_result = mRegistration.schedule(calculateWorkload(), mLastCallTime);
68 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
69 "Error scheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080070 }
71
72 void setPeriod(nsecs_t period) {
73 std::lock_guard<std::mutex> lk(mMutex);
74 if (period == mPeriod) {
75 return;
76 }
77 mPeriod = period;
78 }
79
80 void stop() {
81 std::lock_guard<std::mutex> lk(mMutex);
82 LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped");
83 mStopped = true;
84 mRegistration.cancel();
85 }
86
87private:
88 void callback(nsecs_t vsynctime) {
89 nsecs_t period = 0;
90 {
91 std::lock_guard<std::mutex> lk(mMutex);
92 period = mPeriod;
93 mLastCallTime = vsynctime;
94 }
95
96 mCallback->onDispSyncEvent(vsynctime - period);
97
98 {
99 std::lock_guard<std::mutex> lk(mMutex);
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800100 auto const schedule_result = mRegistration.schedule(calculateWorkload(), vsynctime);
101 LOG_ALWAYS_FATAL_IF((schedule_result != ScheduleResult::Scheduled),
102 "Error rescheduling callback: rc %X", schedule_result);
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800103 }
104 }
105
106 // DispSync offsets are defined as time after the vsync before presentation.
107 // VSyncReactor workloads are defined as time before the intended presentation vsync.
108 // Note change in sign between the two defnitions.
109 nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; }
110
111 DispSync::Callback* const mCallback;
112
113 std::mutex mutable mMutex;
114 VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex);
115 bool mStopped GUARDED_BY(mMutex) = false;
116 nsecs_t mPeriod GUARDED_BY(mMutex);
117 nsecs_t mOffset GUARDED_BY(mMutex);
118 nsecs_t mLastCallTime GUARDED_BY(mMutex);
119};
120
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800121bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
122 if (!fence) {
123 return false;
124 }
125
126 nsecs_t const signalTime = fence->getCachedSignalTime();
127 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
128 return true;
129 }
130
131 std::lock_guard<std::mutex> lk(mMutex);
132 if (mIgnorePresentFences) {
133 return true;
134 }
135
136 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
137 auto const time = (*it)->getCachedSignalTime();
138 if (time == Fence::SIGNAL_TIME_PENDING) {
139 it++;
140 } else if (time == Fence::SIGNAL_TIME_INVALID) {
141 it = mUnfiredFences.erase(it);
142 } else {
143 mTracker->addVsyncTimestamp(time);
144 it = mUnfiredFences.erase(it);
145 }
146 }
147
148 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
149 if (mPendingLimit == mUnfiredFences.size()) {
150 mUnfiredFences.erase(mUnfiredFences.begin());
151 }
152 mUnfiredFences.push_back(fence);
153 } else {
154 mTracker->addVsyncTimestamp(signalTime);
155 }
156
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800157 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800158}
159
160void VSyncReactor::setIgnorePresentFences(bool ignoration) {
161 std::lock_guard<std::mutex> lk(mMutex);
162 mIgnorePresentFences = ignoration;
163 if (mIgnorePresentFences == true) {
164 mUnfiredFences.clear();
165 }
166}
167
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800168nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const {
169 auto const now = mClock->now();
170 auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0;
171 return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
172}
173
174nsecs_t VSyncReactor::expectedPresentTime() {
175 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
176}
177
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800178void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800179 std::lock_guard lk(mMutex);
180 mLastHwVsync.reset();
181 mPeriodTransitioningTo = period;
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800182}
183
184nsecs_t VSyncReactor::getPeriod() {
185 return mTracker->currentPeriod();
186}
187
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800188void VSyncReactor::beginResync() {
189 mTracker->resetModel();
190}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800191
192void VSyncReactor::endResync() {}
193
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800194bool VSyncReactor::periodChangeDetected(nsecs_t vsync_timestamp) {
195 if (!mLastHwVsync || !mPeriodTransitioningTo) {
196 return false;
197 }
198 auto const distance = vsync_timestamp - *mLastHwVsync;
199 return std::abs(distance - *mPeriodTransitioningTo) < std::abs(distance - getPeriod());
200}
201
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800202bool VSyncReactor::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
203 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800204
205 std::lock_guard<std::mutex> lk(mMutex);
206 if (periodChangeDetected(timestamp)) {
207 mMoreSamplesNeeded = false;
208 *periodFlushed = true;
209
210 mTracker->setPeriod(*mPeriodTransitioningTo);
211 for (auto& entry : mCallbacks) {
212 entry.second->setPeriod(*mPeriodTransitioningTo);
213 }
214
215 mPeriodTransitioningTo.reset();
216 mLastHwVsync.reset();
217 } else if (mPeriodTransitioningTo) {
218 mLastHwVsync = timestamp;
219 mMoreSamplesNeeded = true;
220 *periodFlushed = false;
221 } else {
222 mMoreSamplesNeeded = false;
223 *periodFlushed = false;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800224 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800225
226 mTracker->addVsyncTimestamp(timestamp);
227 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800228}
229
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800230status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
231 DispSync::Callback* callback,
232 nsecs_t /* lastCallbackTime */) {
233 std::lock_guard<std::mutex> lk(mMutex);
234 auto it = mCallbacks.find(callback);
235 if (it == mCallbacks.end()) {
236 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
237 static auto constexpr maxListeners = 3;
238 if (mCallbacks.size() >= maxListeners) {
239 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
240 maxListeners, mCallbacks.size());
241 return NO_MEMORY;
242 }
243
244 auto const period = mTracker->currentPeriod();
245 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
246 phase, mClock->now());
247 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
248 }
249
250 it->second->start(phase);
251 return NO_ERROR;
252}
253
254status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
255 nsecs_t* /* outLastCallback */) {
256 std::lock_guard<std::mutex> lk(mMutex);
257 auto const it = mCallbacks.find(callback);
258 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
259
260 it->second->stop();
261 return NO_ERROR;
262}
263
264status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
265 std::lock_guard<std::mutex> lk(mMutex);
266 auto const it = mCallbacks.find(callback);
267 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
268
269 it->second->start(phase);
270 return NO_ERROR;
271}
272
Kevin DuBois00287382019-11-19 15:11:55 -0800273void VSyncReactor::dump(std::string& result) const {
274 result += "VsyncReactor in use\n"; // TODO (b/144927823): add more information!
275}
276
277void VSyncReactor::reset() {}
278
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800279} // namespace android::scheduler