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