blob: 803836445302fef6805c29b666e4063a229e96f6 [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 III67388622023-02-06 20:36:20 -050024#include <ftl/concat.h>
25#include <gui/TraceUtils.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080026#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080027#include <utils/Trace.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080028
Ady Abraham13bc0be2020-02-27 16:48:20 -080029#include "../TracedOrdinal.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080030#include "VSyncDispatch.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080031#include "VSyncReactor.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080032#include "VSyncTracker.h"
33
34namespace android::scheduler {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080035
Ady Abraham5e7371c2020-03-24 14:47:24 -070036using base::StringAppendF;
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 III67388622023-02-06 20:36:20 -050044VSyncReactor::VSyncReactor(PhysicalDisplayId id, std::unique_ptr<Clock> clock,
45 VSyncTracker& tracker, size_t pendingFenceLimit,
46 bool supportKernelIdleTimer)
47 : mId(id),
48 mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070049 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080050 mPendingLimit(pendingFenceLimit),
Dan Stoza027d3652020-05-26 17:26:34 -070051 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080052
Kevin DuBoisf91e9232019-11-21 10:51:23 -080053VSyncReactor::~VSyncReactor() = default;
54
Dominik Laskowski068173d2021-08-11 17:22:59 -070055bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +000056 ATRACE_CALL();
57
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080058 if (!fence) {
59 return false;
60 }
61
62 nsecs_t const signalTime = fence->getCachedSignalTime();
63 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
64 return true;
65 }
66
Ady Abraham9c53ee72020-07-22 21:16:18 -070067 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080068 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +000069 ATRACE_FORMAT_INSTANT("mExternalIgnoreFences=%d mInternalIgnoreFences=%d",
70 mExternalIgnoreFences, mInternalIgnoreFences);
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080071 return true;
72 }
73
Kevin DuBois02d5ed92020-01-27 11:05:46 -080074 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080075 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
76 auto const time = (*it)->getCachedSignalTime();
77 if (time == Fence::SIGNAL_TIME_PENDING) {
78 it++;
79 } else if (time == Fence::SIGNAL_TIME_INVALID) {
80 it = mUnfiredFences.erase(it);
81 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070082 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080083
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080084 it = mUnfiredFences.erase(it);
85 }
86 }
87
88 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
89 if (mPendingLimit == mUnfiredFences.size()) {
90 mUnfiredFences.erase(mUnfiredFences.begin());
91 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070092 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080093 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070094 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080095 }
96
97 if (!timestampAccepted) {
98 mMoreSamplesNeeded = true;
99 setIgnorePresentFencesInternal(true);
100 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800101 }
102
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800103 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800104}
105
Ady Abraham8cb21882020-08-26 18:22:05 -0700106void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700108 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800109 updateIgnorePresentFencesInternal();
110}
111
Ady Abraham8cb21882020-08-26 18:22:05 -0700112void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
113 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800114 updateIgnorePresentFencesInternal();
115}
116
117void VSyncReactor::updateIgnorePresentFencesInternal() {
118 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800119 mUnfiredFences.clear();
120 }
121}
122
Ady Abrahamc585dba2023-11-15 18:41:35 -0800123void VSyncReactor::startPeriodTransitionInternal(ftl::NonNull<DisplayModePtr> modePtr) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500124 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800125 mPeriodConfirmationInProgress = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800126 mModePtrTransitioningTo = modePtr.get();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800127 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800128 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800129}
130
131void VSyncReactor::endPeriodTransition() {
Leon Scroggins III67388622023-02-06 20:36:20 -0500132 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800133 mModePtrTransitioningTo.reset();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800134 mPeriodConfirmationInProgress = false;
135 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800136}
137
Ady Abrahamc585dba2023-11-15 18:41:35 -0800138void VSyncReactor::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
139 ATRACE_INT64(ftl::Concat("VSR-", __func__, " ", mId.value).c_str(),
140 modePtr->getVsyncRate().getPeriodNsecs());
Ady Abraham8cb21882020-08-26 18:22:05 -0700141 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800142 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700143
ramindanid17261e2024-03-27 17:50:25 -0700144 if (!mSupportKernelIdleTimer && mTracker.isCurrentMode(modePtr) && !force) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800145 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700146 setIgnorePresentFencesInternal(false);
147 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800148 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800149 startPeriodTransitionInternal(modePtr);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800150 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800151}
152
Ady Abraham5dee2f12020-02-05 17:49:47 -0800153bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
154 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800155 return false;
156 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800157
Rachel Lee6a9731d2022-06-06 17:08:14 -0700158 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
159 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
160 return true;
161 }
162
Ady Abraham5dee2f12020-02-05 17:49:47 -0800163 if (!mLastHwVsync && !HwcVsyncPeriod) {
164 return false;
165 }
166
Ady Abrahamc585dba2023-11-15 18:41:35 -0800167 const std::optional<Period> newPeriod = mModePtrTransitioningTo
168 ? mModePtrTransitioningTo->getVsyncRate().getPeriod()
169 : std::optional<Period>{};
170 const bool periodIsChanging = newPeriod && (newPeriod->ns() != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700171 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700172 // Clear out the Composer-provided period and use the allowance logic below
173 HwcVsyncPeriod = {};
174 }
175
Ady Abrahamc585dba2023-11-15 18:41:35 -0800176 auto const period = newPeriod ? newPeriod->ns() : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800177 static constexpr int allowancePercent = 10;
178 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
179 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800180 if (HwcVsyncPeriod) {
181 return std::abs(*HwcVsyncPeriod - period) < allowance;
182 }
183
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800184 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800185 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800186}
187
Ady Abraham8cb21882020-08-26 18:22:05 -0700188bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
189 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800190 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800191
Ady Abraham9c53ee72020-07-22 21:16:18 -0700192 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800193 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500194 ATRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800195 if (mModePtrTransitioningTo) {
196 mTracker.setDisplayModePtr(ftl::as_non_null(mModePtrTransitioningTo));
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800197 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800198 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700199
200 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700201 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700202 }
Ady Abraham5a858552020-03-31 17:54:56 -0700203 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700204
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800205 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700206 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800207 } else if (mPeriodConfirmationInProgress) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500208 ATRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800209 mLastHwVsync = timestamp;
210 mMoreSamplesNeeded = true;
211 *periodFlushed = false;
212 } else {
Leon Scroggins III67388622023-02-06 20:36:20 -0500213 ATRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800214 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700215 mTracker.addVsyncTimestamp(timestamp);
216 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800217 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800218
Ady Abrahama16ac672024-03-21 20:40:23 +0000219 if (mExternalIgnoreFences) {
220 // keep HWVSync on as long as we ignore present fences.
221 mMoreSamplesNeeded = true;
222 }
223
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700224 if (!mMoreSamplesNeeded) {
225 setIgnorePresentFencesInternal(false);
226 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800227 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800228}
229
Rachel Lee6a9731d2022-06-06 17:08:14 -0700230void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
231 std::scoped_lock lock(mMutex);
232 mDisplayPowerMode = powerMode;
233}
234
Kevin DuBois00287382019-11-19 15:11:55 -0800235void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700236 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700237 StringAppendF(&result, "VsyncReactor in use\n");
238 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
239 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
240 mInternalIgnoreFences, mExternalIgnoreFences);
241 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
242 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800243 if (mModePtrTransitioningTo) {
244 StringAppendF(&result, "mModePtrTransitioningTo=%s\n",
245 to_string(*mModePtrTransitioningTo).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700246 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800247 StringAppendF(&result, "mModePtrTransitioningTo=nullptr\n");
Ady Abraham5e7371c2020-03-24 14:47:24 -0700248 }
249
250 if (mLastHwVsync) {
251 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
252 (mClock->now() - *mLastHwVsync) / 1e6f);
253 } else {
254 StringAppendF(&result, "No Last HW vsync\n");
255 }
256
Ady Abraham5e7371c2020-03-24 14:47:24 -0700257 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700258 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800259}
260
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800261} // namespace android::scheduler