blob: 2455822c7d52351e857eed77076872e99d053e1a [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
ramindanid17261e2024-03-27 17:50:25 -0700143 if (!mSupportKernelIdleTimer && mTracker.isCurrentMode(modePtr) && !force) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800144 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700145 setIgnorePresentFencesInternal(false);
146 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800147 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800148 startPeriodTransitionInternal(modePtr);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800149 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800150}
151
Ady Abraham5dee2f12020-02-05 17:49:47 -0800152bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
153 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800154 return false;
155 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800156
Rachel Lee6a9731d2022-06-06 17:08:14 -0700157 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
158 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
159 return true;
160 }
161
Ady Abraham5dee2f12020-02-05 17:49:47 -0800162 if (!mLastHwVsync && !HwcVsyncPeriod) {
163 return false;
164 }
165
Ady Abrahamc585dba2023-11-15 18:41:35 -0800166 const std::optional<Period> newPeriod = mModePtrTransitioningTo
167 ? mModePtrTransitioningTo->getVsyncRate().getPeriod()
168 : std::optional<Period>{};
169 const bool periodIsChanging = newPeriod && (newPeriod->ns() != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700170 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700171 // Clear out the Composer-provided period and use the allowance logic below
172 HwcVsyncPeriod = {};
173 }
174
Ady Abrahamc585dba2023-11-15 18:41:35 -0800175 auto const period = newPeriod ? newPeriod->ns() : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800176 static constexpr int allowancePercent = 10;
177 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
178 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800179 if (HwcVsyncPeriod) {
180 return std::abs(*HwcVsyncPeriod - period) < allowance;
181 }
182
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800183 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800184 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800185}
186
Ady Abraham8cb21882020-08-26 18:22:05 -0700187bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
188 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800189 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800190
Ady Abraham9c53ee72020-07-22 21:16:18 -0700191 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800192 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000193 SFTRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800194 if (mModePtrTransitioningTo) {
195 mTracker.setDisplayModePtr(ftl::as_non_null(mModePtrTransitioningTo));
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800196 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800197 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700198
199 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700200 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700201 }
Ady Abraham5a858552020-03-31 17:54:56 -0700202 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700203
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800204 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700205 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800206 } else if (mPeriodConfirmationInProgress) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000207 SFTRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800208 mLastHwVsync = timestamp;
209 mMoreSamplesNeeded = true;
210 *periodFlushed = false;
211 } else {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000212 SFTRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800213 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700214 mTracker.addVsyncTimestamp(timestamp);
215 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800216 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800217
Ady Abrahama16ac672024-03-21 20:40:23 +0000218 if (mExternalIgnoreFences) {
219 // keep HWVSync on as long as we ignore present fences.
220 mMoreSamplesNeeded = true;
221 }
222
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700223 if (!mMoreSamplesNeeded) {
224 setIgnorePresentFencesInternal(false);
225 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800226 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800227}
228
Rachel Lee6a9731d2022-06-06 17:08:14 -0700229void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
230 std::scoped_lock lock(mMutex);
231 mDisplayPowerMode = powerMode;
232}
233
Kevin DuBois00287382019-11-19 15:11:55 -0800234void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700235 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700236 StringAppendF(&result, "VsyncReactor in use\n");
237 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
238 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
239 mInternalIgnoreFences, mExternalIgnoreFences);
240 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
241 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800242 if (mModePtrTransitioningTo) {
243 StringAppendF(&result, "mModePtrTransitioningTo=%s\n",
244 to_string(*mModePtrTransitioningTo).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700245 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800246 StringAppendF(&result, "mModePtrTransitioningTo=nullptr\n");
Ady Abraham5e7371c2020-03-24 14:47:24 -0700247 }
248
249 if (mLastHwVsync) {
250 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
251 (mClock->now() - *mLastHwVsync) / 1e6f);
252 } else {
253 StringAppendF(&result, "No Last HW vsync\n");
254 }
255
Ady Abraham5e7371c2020-03-24 14:47:24 -0700256 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700257 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800258}
259
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800260} // namespace android::scheduler