blob: 49ab6c1f856346c30d25f2a057af629ed1eb8836 [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 DuBoisf91e9232019-11-21 10:51:23 -080017//#define LOG_NDEBUG 0
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080018#include "VSyncReactor.h"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080019#include <log/log.h>
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080020#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080021#include "VSyncDispatch.h"
22#include "VSyncTracker.h"
23
24namespace android::scheduler {
25
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080026Clock::~Clock() = default;
27
28VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080029 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080030 : mClock(std::move(clock)),
31 mDispatch(std::move(dispatch)),
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080032 mTracker(std::move(tracker)),
33 mPendingLimit(pendingFenceLimit) {}
34
Kevin DuBoisf91e9232019-11-21 10:51:23 -080035VSyncReactor::~VSyncReactor() = default;
36
37// The DispSync interface has a 'repeat this callback at rate' semantic. This object adapts
38// VSyncDispatch's individually-scheduled callbacks so as to meet DispSync's existing semantic
39// for now.
40class CallbackRepeater {
41public:
42 CallbackRepeater(VSyncDispatch& dispatch, DispSync::Callback* cb, const char* name,
43 nsecs_t period, nsecs_t offset, nsecs_t notBefore)
44 : mCallback(cb),
45 mRegistration(dispatch,
46 std::bind(&CallbackRepeater::callback, this, std::placeholders::_1),
47 std::string(name)),
48 mPeriod(period),
49 mOffset(offset),
50 mLastCallTime(notBefore) {}
51
52 ~CallbackRepeater() {
53 std::lock_guard<std::mutex> lk(mMutex);
54 mRegistration.cancel();
55 }
56
57 void start(nsecs_t offset) {
58 std::lock_guard<std::mutex> lk(mMutex);
59 mStopped = false;
60 mOffset = offset;
61
62 // TODO: (b/145213786) check the return code here sensibly
63 mRegistration.schedule(calculateWorkload(), mLastCallTime);
64 }
65
66 void setPeriod(nsecs_t period) {
67 std::lock_guard<std::mutex> lk(mMutex);
68 if (period == mPeriod) {
69 return;
70 }
71 mPeriod = period;
72 }
73
74 void stop() {
75 std::lock_guard<std::mutex> lk(mMutex);
76 LOG_ALWAYS_FATAL_IF(mStopped, "DispSyncInterface misuse: callback already stopped");
77 mStopped = true;
78 mRegistration.cancel();
79 }
80
81private:
82 void callback(nsecs_t vsynctime) {
83 nsecs_t period = 0;
84 {
85 std::lock_guard<std::mutex> lk(mMutex);
86 period = mPeriod;
87 mLastCallTime = vsynctime;
88 }
89
90 mCallback->onDispSyncEvent(vsynctime - period);
91
92 {
93 std::lock_guard<std::mutex> lk(mMutex);
94 mRegistration.schedule(calculateWorkload(), vsynctime);
95 }
96 }
97
98 // DispSync offsets are defined as time after the vsync before presentation.
99 // VSyncReactor workloads are defined as time before the intended presentation vsync.
100 // Note change in sign between the two defnitions.
101 nsecs_t calculateWorkload() REQUIRES(mMutex) { return mPeriod - mOffset; }
102
103 DispSync::Callback* const mCallback;
104
105 std::mutex mutable mMutex;
106 VSyncCallbackRegistration mRegistration GUARDED_BY(mMutex);
107 bool mStopped GUARDED_BY(mMutex) = false;
108 nsecs_t mPeriod GUARDED_BY(mMutex);
109 nsecs_t mOffset GUARDED_BY(mMutex);
110 nsecs_t mLastCallTime GUARDED_BY(mMutex);
111};
112
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800113bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
114 if (!fence) {
115 return false;
116 }
117
118 nsecs_t const signalTime = fence->getCachedSignalTime();
119 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
120 return true;
121 }
122
123 std::lock_guard<std::mutex> lk(mMutex);
124 if (mIgnorePresentFences) {
125 return true;
126 }
127
128 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
129 auto const time = (*it)->getCachedSignalTime();
130 if (time == Fence::SIGNAL_TIME_PENDING) {
131 it++;
132 } else if (time == Fence::SIGNAL_TIME_INVALID) {
133 it = mUnfiredFences.erase(it);
134 } else {
135 mTracker->addVsyncTimestamp(time);
136 it = mUnfiredFences.erase(it);
137 }
138 }
139
140 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
141 if (mPendingLimit == mUnfiredFences.size()) {
142 mUnfiredFences.erase(mUnfiredFences.begin());
143 }
144 mUnfiredFences.push_back(fence);
145 } else {
146 mTracker->addVsyncTimestamp(signalTime);
147 }
148
149 return false; // TODO(b/144707443): add policy for turning on HWVsync.
150}
151
152void VSyncReactor::setIgnorePresentFences(bool ignoration) {
153 std::lock_guard<std::mutex> lk(mMutex);
154 mIgnorePresentFences = ignoration;
155 if (mIgnorePresentFences == true) {
156 mUnfiredFences.clear();
157 }
158}
159
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800160nsecs_t VSyncReactor::computeNextRefresh(int periodOffset) const {
161 auto const now = mClock->now();
162 auto const currentPeriod = periodOffset ? mTracker->currentPeriod() : 0;
163 return mTracker->nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
164}
165
166nsecs_t VSyncReactor::expectedPresentTime() {
167 return mTracker->nextAnticipatedVSyncTimeFrom(mClock->now());
168}
169
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800170void VSyncReactor::setPeriod(nsecs_t period) {
171 mTracker->setPeriod(period);
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800172 {
173 std::lock_guard<std::mutex> lk(mMutex);
174 mPeriodChangeInProgress = true;
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800175 for (auto& entry : mCallbacks) {
176 entry.second->setPeriod(period);
177 }
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800178 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800179}
180
181nsecs_t VSyncReactor::getPeriod() {
182 return mTracker->currentPeriod();
183}
184
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800185void VSyncReactor::beginResync() {}
186
187void VSyncReactor::endResync() {}
188
189bool VSyncReactor::addResyncSample(nsecs_t timestamp, bool* periodFlushed) {
190 assert(periodFlushed);
191 mTracker->addVsyncTimestamp(timestamp);
192 {
193 std::lock_guard<std::mutex> lk(mMutex);
194 *periodFlushed = mPeriodChangeInProgress;
195 mPeriodChangeInProgress = false;
196 }
197 return false;
198}
199
Kevin DuBoisf91e9232019-11-21 10:51:23 -0800200status_t VSyncReactor::addEventListener(const char* name, nsecs_t phase,
201 DispSync::Callback* callback,
202 nsecs_t /* lastCallbackTime */) {
203 std::lock_guard<std::mutex> lk(mMutex);
204 auto it = mCallbacks.find(callback);
205 if (it == mCallbacks.end()) {
206 // TODO (b/146557561): resolve lastCallbackTime semantics in DispSync i/f.
207 static auto constexpr maxListeners = 3;
208 if (mCallbacks.size() >= maxListeners) {
209 ALOGE("callback %s not added, exceeded callback limit of %i (currently %zu)", name,
210 maxListeners, mCallbacks.size());
211 return NO_MEMORY;
212 }
213
214 auto const period = mTracker->currentPeriod();
215 auto repeater = std::make_unique<CallbackRepeater>(*mDispatch, callback, name, period,
216 phase, mClock->now());
217 it = mCallbacks.emplace(std::pair(callback, std::move(repeater))).first;
218 }
219
220 it->second->start(phase);
221 return NO_ERROR;
222}
223
224status_t VSyncReactor::removeEventListener(DispSync::Callback* callback,
225 nsecs_t* /* outLastCallback */) {
226 std::lock_guard<std::mutex> lk(mMutex);
227 auto const it = mCallbacks.find(callback);
228 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback %p not registered", callback);
229
230 it->second->stop();
231 return NO_ERROR;
232}
233
234status_t VSyncReactor::changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) {
235 std::lock_guard<std::mutex> lk(mMutex);
236 auto const it = mCallbacks.find(callback);
237 LOG_ALWAYS_FATAL_IF(it == mCallbacks.end(), "callback was %p not registered", callback);
238
239 it->second->start(phase);
240 return NO_ERROR;
241}
242
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800243} // namespace android::scheduler