blob: 186a2d6740f37fecf38b1c4d82fe52dbd100c51b [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
Ady Abrahamc585dba2023-11-15 18:41:35 -0800144 if (!mSupportKernelIdleTimer &&
145 modePtr->getVsyncRate().getPeriodNsecs() == mTracker.currentPeriod() && !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)) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500195 ATRACE_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) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500209 ATRACE_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 {
Leon Scroggins III67388622023-02-06 20:36:20 -0500214 ATRACE_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
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700220 if (!mMoreSamplesNeeded) {
221 setIgnorePresentFencesInternal(false);
222 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800223 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800224}
225
Rachel Lee6a9731d2022-06-06 17:08:14 -0700226void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
227 std::scoped_lock lock(mMutex);
228 mDisplayPowerMode = powerMode;
229}
230
Kevin DuBois00287382019-11-19 15:11:55 -0800231void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700232 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700233 StringAppendF(&result, "VsyncReactor in use\n");
234 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
235 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
236 mInternalIgnoreFences, mExternalIgnoreFences);
237 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
238 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800239 if (mModePtrTransitioningTo) {
240 StringAppendF(&result, "mModePtrTransitioningTo=%s\n",
241 to_string(*mModePtrTransitioningTo).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700242 } else {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800243 StringAppendF(&result, "mModePtrTransitioningTo=nullptr\n");
Ady Abraham5e7371c2020-03-24 14:47:24 -0700244 }
245
246 if (mLastHwVsync) {
247 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
248 (mClock->now() - *mLastHwVsync) / 1e6f);
249 } else {
250 StringAppendF(&result, "No Last HW vsync\n");
251 }
252
Ady Abraham5e7371c2020-03-24 14:47:24 -0700253 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700254 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800255}
256
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800257} // namespace android::scheduler