blob: 792313efee344e180b95dc73e79f528b57408b34 [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 DuBois5988f482020-01-17 09:03:32 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Kevin DuBoisc94ca832019-11-26 12:56:24 -080018#undef LOG_TAG
19#define LOG_TAG "VSyncReactor"
Kevin DuBoisf91e9232019-11-21 10:51:23 -080020//#define LOG_NDEBUG 0
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080021#include "VSyncReactor.h"
Ady Abraham13bc0be2020-02-27 16:48:20 -080022#include <cutils/properties.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080023#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080024#include <utils/Trace.h>
Ady Abraham13bc0be2020-02-27 16:48:20 -080025#include "../TracedOrdinal.h"
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080026#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027#include "VSyncDispatch.h"
28#include "VSyncTracker.h"
29
30namespace android::scheduler {
Ady Abraham5e7371c2020-03-24 14:47:24 -070031using base::StringAppendF;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080032
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080033Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080034nsecs_t SystemClock::now() const {
35 return systemTime(SYSTEM_TIME_MONOTONIC);
36}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080037
Ady Abraham13bc0be2020-02-27 16:48:20 -080038class PredictedVsyncTracer {
39public:
40 PredictedVsyncTracer(VSyncDispatch& dispatch)
Ady Abraham9c53ee72020-07-22 21:16:18 -070041 : mRegistration(dispatch, std::bind(&PredictedVsyncTracer::callback, this),
Ady Abraham13bc0be2020-02-27 16:48:20 -080042 "PredictedVsyncTracer") {
Ady Abraham9c53ee72020-07-22 21:16:18 -070043 scheduleRegistration();
Ady Abraham13bc0be2020-02-27 16:48:20 -080044 }
45
46private:
47 TracedOrdinal<bool> mParity = {"VSYNC-predicted", 0};
48 VSyncCallbackRegistration mRegistration;
49
Ady Abraham9c53ee72020-07-22 21:16:18 -070050 void scheduleRegistration() { mRegistration.schedule({0, 0, 0}); }
51
52 void callback() {
Ady Abraham13bc0be2020-02-27 16:48:20 -080053 mParity = !mParity;
Ady Abraham9c53ee72020-07-22 21:16:18 -070054 scheduleRegistration();
Ady Abraham13bc0be2020-02-27 16:48:20 -080055 }
56};
57
Ady Abraham5a858552020-03-31 17:54:56 -070058VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncDispatch& dispatch,
59 VSyncTracker& tracker, size_t pendingFenceLimit,
Dan Stoza027d3652020-05-26 17:26:34 -070060 bool supportKernelIdleTimer)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080061 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070062 mTracker(tracker),
63 mDispatch(dispatch),
Ady Abraham13bc0be2020-02-27 16:48:20 -080064 mPendingLimit(pendingFenceLimit),
65 mPredictedVsyncTracer(property_get_bool("debug.sf.show_predicted_vsync", false)
Ady Abraham5a858552020-03-31 17:54:56 -070066 ? std::make_unique<PredictedVsyncTracer>(mDispatch)
Dan Stoza027d3652020-05-26 17:26:34 -070067 : nullptr),
68 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080069
Kevin DuBoisf91e9232019-11-21 10:51:23 -080070VSyncReactor::~VSyncReactor() = default;
71
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080072bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) {
73 if (!fence) {
74 return false;
75 }
76
77 nsecs_t const signalTime = fence->getCachedSignalTime();
78 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
79 return true;
80 }
81
Ady Abraham9c53ee72020-07-22 21:16:18 -070082 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080083 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080084 return true;
85 }
86
Kevin DuBois02d5ed92020-01-27 11:05:46 -080087 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080088 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
89 auto const time = (*it)->getCachedSignalTime();
90 if (time == Fence::SIGNAL_TIME_PENDING) {
91 it++;
92 } else if (time == Fence::SIGNAL_TIME_INVALID) {
93 it = mUnfiredFences.erase(it);
94 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070095 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080096
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080097 it = mUnfiredFences.erase(it);
98 }
99 }
100
101 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
102 if (mPendingLimit == mUnfiredFences.size()) {
103 mUnfiredFences.erase(mUnfiredFences.begin());
104 }
105 mUnfiredFences.push_back(fence);
106 } else {
Ady Abraham5a858552020-03-31 17:54:56 -0700107 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800108 }
109
110 if (!timestampAccepted) {
111 mMoreSamplesNeeded = true;
112 setIgnorePresentFencesInternal(true);
113 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800114 }
115
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800116 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800117}
118
119void VSyncReactor::setIgnorePresentFences(bool ignoration) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700120 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800121 mExternalIgnoreFences = ignoration;
122 updateIgnorePresentFencesInternal();
123}
124
125void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) {
126 mInternalIgnoreFences = ignoration;
127 updateIgnorePresentFencesInternal();
128}
129
130void VSyncReactor::updateIgnorePresentFencesInternal() {
131 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800132 mUnfiredFences.clear();
133 }
134}
135
Ady Abraham0ed31c92020-04-16 11:48:45 -0700136nsecs_t VSyncReactor::computeNextRefresh(int periodOffset, nsecs_t now) const {
Ady Abraham5a858552020-03-31 17:54:56 -0700137 auto const currentPeriod = periodOffset ? mTracker.currentPeriod() : 0;
138 return mTracker.nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800139}
140
Ady Abraham0ed31c92020-04-16 11:48:45 -0700141nsecs_t VSyncReactor::expectedPresentTime(nsecs_t now) {
Ady Abraham5a858552020-03-31 17:54:56 -0700142 return mTracker.nextAnticipatedVSyncTimeFrom(now);
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800143}
144
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800145void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700146 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800147 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800148 mPeriodTransitioningTo = newPeriod;
149 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800150 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800151}
152
153void VSyncReactor::endPeriodTransition() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700154 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800155 mPeriodTransitioningTo.reset();
156 mPeriodConfirmationInProgress = false;
157 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800158}
159
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800160void VSyncReactor::setPeriod(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800161 ATRACE_INT64("VSR-setPeriod", period);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800162 std::lock_guard lk(mMutex);
163 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700164
165 if (!mSupportKernelIdleTimer && period == getPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800166 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700167 setIgnorePresentFencesInternal(false);
168 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800169 } else {
170 startPeriodTransition(period);
171 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800172}
173
174nsecs_t VSyncReactor::getPeriod() {
Ady Abraham5a858552020-03-31 17:54:56 -0700175 return mTracker.currentPeriod();
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800176}
177
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800178void VSyncReactor::beginResync() {
Ady Abraham5a858552020-03-31 17:54:56 -0700179 mTracker.resetModel();
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800180}
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800181
Ady Abraham5dee2f12020-02-05 17:49:47 -0800182bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
183 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800184 return false;
185 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800186
Ady Abraham5dee2f12020-02-05 17:49:47 -0800187 if (!mLastHwVsync && !HwcVsyncPeriod) {
188 return false;
189 }
190
Dan Stoza09bf7632020-06-10 14:28:50 -0700191 const bool periodIsChanging =
192 mPeriodTransitioningTo && (*mPeriodTransitioningTo != getPeriod());
193 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700194 // Clear out the Composer-provided period and use the allowance logic below
195 HwcVsyncPeriod = {};
196 }
197
Ady Abraham5dee2f12020-02-05 17:49:47 -0800198 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800199 static constexpr int allowancePercent = 10;
200 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
201 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800202 if (HwcVsyncPeriod) {
203 return std::abs(*HwcVsyncPeriod - period) < allowance;
204 }
205
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800206 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800207 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800208}
209
Ady Abraham5dee2f12020-02-05 17:49:47 -0800210bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
211 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800212 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800213
Ady Abraham9c53ee72020-07-22 21:16:18 -0700214 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800215 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700216 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800217 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700218 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800219 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800220 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700221
222 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700223 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700224 }
Ady Abraham5a858552020-03-31 17:54:56 -0700225 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700226
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800227 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700228 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800229 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700230 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800231 mLastHwVsync = timestamp;
232 mMoreSamplesNeeded = true;
233 *periodFlushed = false;
234 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700235 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800236 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700237 mTracker.addVsyncTimestamp(timestamp);
238 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800239 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800240
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700241 if (!mMoreSamplesNeeded) {
242 setIgnorePresentFencesInternal(false);
243 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800244 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800245}
246
Kevin DuBois00287382019-11-19 15:11:55 -0800247void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700248 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700249 StringAppendF(&result, "VsyncReactor in use\n");
250 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
251 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
252 mInternalIgnoreFences, mExternalIgnoreFences);
253 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
254 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
255 if (mPeriodTransitioningTo) {
256 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
257 } else {
258 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
259 }
260
261 if (mLastHwVsync) {
262 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
263 (mClock->now() - *mLastHwVsync) / 1e6f);
264 } else {
265 StringAppendF(&result, "No Last HW vsync\n");
266 }
267
Ady Abraham5e7371c2020-03-24 14:47:24 -0700268 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700269 mTracker.dump(result);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700270 StringAppendF(&result, "VSyncDispatch:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700271 mDispatch.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800272}
273
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800274} // namespace android::scheduler