blob: b974cd2b042b7c1179cab2792f7b9453525a724f [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>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000023#include <common/trace.h>
Ady Abraham13bc0be2020-02-27 16:48:20 -080024#include <cutils/properties.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050025#include <ftl/concat.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080026#include <log/log.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;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080036
Ady Abraham8cb21882020-08-26 18:22:05 -070037VsyncController::~VsyncController() = default;
38
Kevin DuBois00287382019-11-19 15:11:55 -080039nsecs_t SystemClock::now() const {
40 return systemTime(SYSTEM_TIME_MONOTONIC);
41}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080042
Leon Scroggins III67388622023-02-06 20:36:20 -050043VSyncReactor::VSyncReactor(PhysicalDisplayId id, std::unique_ptr<Clock> clock,
44 VSyncTracker& tracker, size_t pendingFenceLimit,
45 bool supportKernelIdleTimer)
46 : mId(id),
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) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000055 SFTRACE_CALL();
Ady Abrahamf0b2bf92023-12-13 23:36:35 +000056
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080057 if (!fence) {
58 return false;
59 }
60
61 nsecs_t const signalTime = fence->getCachedSignalTime();
62 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
63 return true;
64 }
65
Ady Abraham9c53ee72020-07-22 21:16:18 -070066 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080067 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000068 SFTRACE_FORMAT_INSTANT("mExternalIgnoreFences=%d mInternalIgnoreFences=%d",
69 mExternalIgnoreFences, mInternalIgnoreFences);
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080070 return true;
71 }
72
Kevin DuBois02d5ed92020-01-27 11:05:46 -080073 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080074 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
75 auto const time = (*it)->getCachedSignalTime();
76 if (time == Fence::SIGNAL_TIME_PENDING) {
77 it++;
78 } else if (time == Fence::SIGNAL_TIME_INVALID) {
79 it = mUnfiredFences.erase(it);
80 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070081 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080082
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080083 it = mUnfiredFences.erase(it);
84 }
85 }
86
87 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
88 if (mPendingLimit == mUnfiredFences.size()) {
89 mUnfiredFences.erase(mUnfiredFences.begin());
90 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070091 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080092 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070093 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080094 }
95
96 if (!timestampAccepted) {
97 mMoreSamplesNeeded = true;
98 setIgnorePresentFencesInternal(true);
99 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800100 }
101
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800102 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800103}
104
Ady Abraham8cb21882020-08-26 18:22:05 -0700105void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700106 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700107 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800108 updateIgnorePresentFencesInternal();
109}
110
Ady Abraham8cb21882020-08-26 18:22:05 -0700111void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
112 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800113 updateIgnorePresentFencesInternal();
114}
115
116void VSyncReactor::updateIgnorePresentFencesInternal() {
117 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800118 mUnfiredFences.clear();
119 }
120}
121
Ady Abrahamc585dba2023-11-15 18:41:35 -0800122void VSyncReactor::startPeriodTransitionInternal(ftl::NonNull<DisplayModePtr> modePtr) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000123 SFTRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800124 mPeriodConfirmationInProgress = true;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800125 mModePtrTransitioningTo = modePtr.get();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800126 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800127 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800128}
129
130void VSyncReactor::endPeriodTransition() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000131 SFTRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800132 mModePtrTransitioningTo.reset();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800133 mPeriodConfirmationInProgress = false;
134 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800135}
136
Ady Abrahamc585dba2023-11-15 18:41:35 -0800137void VSyncReactor::onDisplayModeChanged(ftl::NonNull<DisplayModePtr> modePtr, bool force) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000138 SFTRACE_INT64(ftl::Concat("VSR-", __func__, " ", mId.value).c_str(),
139 modePtr->getVsyncRate().getPeriodNsecs());
Ady Abraham8cb21882020-08-26 18:22:05 -0700140 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800141 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700142
Ady Abraham9ee31132024-08-06 16:44:08 +0000143 // kernel idle timer is not applicable for VRR
144 const bool supportKernelIdleTimer = mSupportKernelIdleTimer && !modePtr->getVrrConfig();
145 if (!supportKernelIdleTimer && mTracker.isCurrentMode(modePtr) && !force) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800146 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700147 setIgnorePresentFencesInternal(false);
148 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800149 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800150 startPeriodTransitionInternal(modePtr);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800151 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800152}
153
Ady Abraham5dee2f12020-02-05 17:49:47 -0800154bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
155 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800156 return false;
157 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800158
Rachel Lee6a9731d2022-06-06 17:08:14 -0700159 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
160 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
161 return true;
162 }
163
Ady Abraham5dee2f12020-02-05 17:49:47 -0800164 if (!mLastHwVsync && !HwcVsyncPeriod) {
165 return false;
166 }
167
Ady Abrahamc585dba2023-11-15 18:41:35 -0800168 const std::optional<Period> newPeriod = mModePtrTransitioningTo
169 ? mModePtrTransitioningTo->getVsyncRate().getPeriod()
170 : std::optional<Period>{};
171 const bool periodIsChanging = newPeriod && (newPeriod->ns() != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700172 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700173 // Clear out the Composer-provided period and use the allowance logic below
174 HwcVsyncPeriod = {};
175 }
176
Ady Abrahamc585dba2023-11-15 18:41:35 -0800177 auto const period = newPeriod ? newPeriod->ns() : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800178 static constexpr int allowancePercent = 10;
179 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
180 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800181 if (HwcVsyncPeriod) {
182 return std::abs(*HwcVsyncPeriod - period) < allowance;
183 }
184
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800185 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800186 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800187}
188
Ady Abraham8cb21882020-08-26 18:22:05 -0700189bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
190 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800191 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800192
Ady Abraham9c53ee72020-07-22 21:16:18 -0700193 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800194 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000195 SFTRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800196 if (mModePtrTransitioningTo) {
197 mTracker.setDisplayModePtr(ftl::as_non_null(mModePtrTransitioningTo));
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800198 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800199 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700200
201 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700202 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700203 }
Ady Abraham5a858552020-03-31 17:54:56 -0700204 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700205
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800206 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700207 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800208 } else if (mPeriodConfirmationInProgress) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000209 SFTRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800210 mLastHwVsync = timestamp;
211 mMoreSamplesNeeded = true;
212 *periodFlushed = false;
213 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000214 SFTRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800215 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700216 mTracker.addVsyncTimestamp(timestamp);
217 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800218 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800219
Ady Abrahama16ac672024-03-21 20:40:23 +0000220 if (mExternalIgnoreFences) {
221 // keep HWVSync on as long as we ignore present fences.
222 mMoreSamplesNeeded = true;
223 }
224
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700225 if (!mMoreSamplesNeeded) {
226 setIgnorePresentFencesInternal(false);
227 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800228 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800229}
230
Rachel Lee6a9731d2022-06-06 17:08:14 -0700231void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
232 std::scoped_lock lock(mMutex);
233 mDisplayPowerMode = powerMode;
234}
235
Kevin DuBois00287382019-11-19 15:11:55 -0800236void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700237 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700238 StringAppendF(&result, "VsyncReactor in use\n");
239 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
240 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
241 mInternalIgnoreFences, mExternalIgnoreFences);
242 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
243 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800244 if (mModePtrTransitioningTo) {
245 StringAppendF(&result, "mModePtrTransitioningTo=%s\n",
246 to_string(*mModePtrTransitioningTo).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700247 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800248 StringAppendF(&result, "mModePtrTransitioningTo=nullptr\n");
Ady Abraham5e7371c2020-03-24 14:47:24 -0700249 }
250
251 if (mLastHwVsync) {
252 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
253 (mClock->now() - *mLastHwVsync) / 1e6f);
254 } else {
255 StringAppendF(&result, "No Last HW vsync\n");
256 }
257
Ady Abraham5e7371c2020-03-24 14:47:24 -0700258 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700259 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800260}
261
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800262} // namespace android::scheduler