blob: a831f6624dd518c610575acfa96ba4583ce6670e [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
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080021
Ryan Prichard430dde32022-08-30 17:08:20 -070022#include <assert.h>
Ady Abraham13bc0be2020-02-27 16:48:20 -080023#include <cutils/properties.h>
Leon Scroggins III31d41412022-11-18 16:42:53 -050024#include <gui/TraceUtils.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080025#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080026#include <utils/Trace.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080027
Ady Abraham13bc0be2020-02-27 16:48:20 -080028#include "../TracedOrdinal.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080029#include "VSyncDispatch.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080030#include "VSyncReactor.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080031#include "VSyncTracker.h"
32
33namespace android::scheduler {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080034
Ady Abraham5e7371c2020-03-24 14:47:24 -070035using base::StringAppendF;
Leon Scroggins III31d41412022-11-18 16:42:53 -050036using base::StringPrintf;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080037
Ady Abraham8cb21882020-08-26 18:22:05 -070038VsyncController::~VsyncController() = default;
39
Kevin DuBois00287382019-11-19 15:11:55 -080040nsecs_t SystemClock::now() const {
41 return systemTime(SYSTEM_TIME_MONOTONIC);
42}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080043
Leon Scroggins III31d41412022-11-18 16:42:53 -050044VSyncReactor::VSyncReactor(std::string name, std::unique_ptr<Clock> clock, VSyncTracker& tracker,
Ady Abraham8735eac2020-08-12 16:35:04 -070045 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Leon Scroggins III31d41412022-11-18 16:42:53 -050046 : mName(name),
47 mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070048 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080049 mPendingLimit(pendingFenceLimit),
Dan Stoza027d3652020-05-26 17:26:34 -070050 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080051
Kevin DuBoisf91e9232019-11-21 10:51:23 -080052VSyncReactor::~VSyncReactor() = default;
53
Dominik Laskowski068173d2021-08-11 17:22:59 -070054bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080055 if (!fence) {
56 return false;
57 }
58
59 nsecs_t const signalTime = fence->getCachedSignalTime();
60 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
61 return true;
62 }
63
Ady Abraham9c53ee72020-07-22 21:16:18 -070064 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080065 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080066 return true;
67 }
68
Kevin DuBois02d5ed92020-01-27 11:05:46 -080069 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080070 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
71 auto const time = (*it)->getCachedSignalTime();
72 if (time == Fence::SIGNAL_TIME_PENDING) {
73 it++;
74 } else if (time == Fence::SIGNAL_TIME_INVALID) {
75 it = mUnfiredFences.erase(it);
76 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070077 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080078
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080079 it = mUnfiredFences.erase(it);
80 }
81 }
82
83 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
84 if (mPendingLimit == mUnfiredFences.size()) {
85 mUnfiredFences.erase(mUnfiredFences.begin());
86 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070087 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080088 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070089 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080090 }
91
92 if (!timestampAccepted) {
93 mMoreSamplesNeeded = true;
94 setIgnorePresentFencesInternal(true);
95 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080096 }
97
Kevin DuBoisf77025c2019-12-18 16:13:24 -080098 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080099}
100
Ady Abraham8cb21882020-08-26 18:22:05 -0700101void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700103 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800104 updateIgnorePresentFencesInternal();
105}
106
Ady Abraham8cb21882020-08-26 18:22:05 -0700107void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
108 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800109 updateIgnorePresentFencesInternal();
110}
111
112void VSyncReactor::updateIgnorePresentFencesInternal() {
113 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800114 mUnfiredFences.clear();
115 }
116}
117
Ady Abraham8cb21882020-08-26 18:22:05 -0700118void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500119 ATRACE_FORMAT("%s %s", __func__, mName.c_str());
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800120 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800121 mPeriodTransitioningTo = newPeriod;
122 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800123 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800124}
125
126void VSyncReactor::endPeriodTransition() {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500127 ATRACE_FORMAT("%s %s", __func__, mName.c_str());
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800128 mPeriodTransitioningTo.reset();
129 mPeriodConfirmationInProgress = false;
130 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800131}
132
Leon Scroggins III31d41412022-11-18 16:42:53 -0500133void VSyncReactor::startPeriodTransition(nsecs_t period, bool force) {
134 // TODO (b/266817103): Pass in PhysicalDisplayId and use ftl::Concat to
135 // avoid unnecessary allocations.
136 ATRACE_INT64(StringPrintf("VSR-startPeriodTransition %s", mName.c_str()).c_str(), period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700137 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800138 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700139
Leon Scroggins III31d41412022-11-18 16:42:53 -0500140 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod() && !force) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800141 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700142 setIgnorePresentFencesInternal(false);
143 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800144 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700145 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800146 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800147}
148
Ady Abraham5dee2f12020-02-05 17:49:47 -0800149bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
150 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800151 return false;
152 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800153
Rachel Lee6a9731d2022-06-06 17:08:14 -0700154 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
155 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
156 return true;
157 }
158
Ady Abraham5dee2f12020-02-05 17:49:47 -0800159 if (!mLastHwVsync && !HwcVsyncPeriod) {
160 return false;
161 }
162
Dan Stoza09bf7632020-06-10 14:28:50 -0700163 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700164 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700165 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700166 // Clear out the Composer-provided period and use the allowance logic below
167 HwcVsyncPeriod = {};
168 }
169
Ady Abraham8cb21882020-08-26 18:22:05 -0700170 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800171 static constexpr int allowancePercent = 10;
172 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
173 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800174 if (HwcVsyncPeriod) {
175 return std::abs(*HwcVsyncPeriod - period) < allowance;
176 }
177
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800178 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800179 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800180}
181
Ady Abraham8cb21882020-08-26 18:22:05 -0700182bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
183 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800184 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800185
Ady Abraham9c53ee72020-07-22 21:16:18 -0700186 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800187 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500188 ATRACE_FORMAT("VSR %s: period confirmed", mName.c_str());
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800189 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700190 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800191 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800192 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700193
194 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700195 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700196 }
Ady Abraham5a858552020-03-31 17:54:56 -0700197 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700198
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800199 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700200 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800201 } else if (mPeriodConfirmationInProgress) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500202 ATRACE_FORMAT("VSR %s: still confirming period", mName.c_str());
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800203 mLastHwVsync = timestamp;
204 mMoreSamplesNeeded = true;
205 *periodFlushed = false;
206 } else {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500207 ATRACE_FORMAT("VSR %s: adding sample", mName.c_str());
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800208 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700209 mTracker.addVsyncTimestamp(timestamp);
210 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800211 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800212
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700213 if (!mMoreSamplesNeeded) {
214 setIgnorePresentFencesInternal(false);
215 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800216 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800217}
218
Rachel Lee6a9731d2022-06-06 17:08:14 -0700219void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
220 std::scoped_lock lock(mMutex);
221 mDisplayPowerMode = powerMode;
222}
223
Kevin DuBois00287382019-11-19 15:11:55 -0800224void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700225 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700226 StringAppendF(&result, "VsyncReactor in use\n");
227 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
228 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
229 mInternalIgnoreFences, mExternalIgnoreFences);
230 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
231 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
232 if (mPeriodTransitioningTo) {
233 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
234 } else {
235 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
236 }
237
238 if (mLastHwVsync) {
239 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
240 (mClock->now() - *mLastHwVsync) / 1e6f);
241 } else {
242 StringAppendF(&result, "No Last HW vsync\n");
243 }
244
Ady Abraham5e7371c2020-03-24 14:47:24 -0700245 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700246 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800247}
248
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800249} // namespace android::scheduler