blob: 2938aa3fb34efc1da57307305b4544584875c3ec [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) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080056 if (!fence) {
57 return false;
58 }
59
60 nsecs_t const signalTime = fence->getCachedSignalTime();
61 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
62 return true;
63 }
64
Ady Abraham9c53ee72020-07-22 21:16:18 -070065 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080066 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080067 return true;
68 }
69
Kevin DuBois02d5ed92020-01-27 11:05:46 -080070 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080071 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
72 auto const time = (*it)->getCachedSignalTime();
73 if (time == Fence::SIGNAL_TIME_PENDING) {
74 it++;
75 } else if (time == Fence::SIGNAL_TIME_INVALID) {
76 it = mUnfiredFences.erase(it);
77 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070078 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080079
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080080 it = mUnfiredFences.erase(it);
81 }
82 }
83
84 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
85 if (mPendingLimit == mUnfiredFences.size()) {
86 mUnfiredFences.erase(mUnfiredFences.begin());
87 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070088 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080089 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070090 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080091 }
92
93 if (!timestampAccepted) {
94 mMoreSamplesNeeded = true;
95 setIgnorePresentFencesInternal(true);
96 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080097 }
98
Kevin DuBoisf77025c2019-12-18 16:13:24 -080099 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800100}
101
Ady Abraham8cb21882020-08-26 18:22:05 -0700102void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700103 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700104 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800105 updateIgnorePresentFencesInternal();
106}
107
Ady Abraham8cb21882020-08-26 18:22:05 -0700108void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
109 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800110 updateIgnorePresentFencesInternal();
111}
112
113void VSyncReactor::updateIgnorePresentFencesInternal() {
114 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800115 mUnfiredFences.clear();
116 }
117}
118
Ady Abraham8cb21882020-08-26 18:22:05 -0700119void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500120 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800121 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800122 mPeriodTransitioningTo = newPeriod;
123 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800124 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800125}
126
127void VSyncReactor::endPeriodTransition() {
Leon Scroggins III67388622023-02-06 20:36:20 -0500128 ATRACE_FORMAT("%s %" PRIu64, __func__, mId.value);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800129 mPeriodTransitioningTo.reset();
130 mPeriodConfirmationInProgress = false;
131 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800132}
133
Leon Scroggins III67388622023-02-06 20:36:20 -0500134void VSyncReactor::startPeriodTransition(nsecs_t period, bool force) {
135 ATRACE_INT64(ftl::Concat("VSR-", __func__, " ", mId.value).c_str(), period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700136 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800137 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700138
Leon Scroggins III67388622023-02-06 20:36:20 -0500139 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod() && !force) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800140 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700141 setIgnorePresentFencesInternal(false);
142 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800143 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700144 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800145 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800146}
147
Ady Abraham5dee2f12020-02-05 17:49:47 -0800148bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
149 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800150 return false;
151 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800152
Rachel Lee6a9731d2022-06-06 17:08:14 -0700153 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
154 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
155 return true;
156 }
157
Ady Abraham5dee2f12020-02-05 17:49:47 -0800158 if (!mLastHwVsync && !HwcVsyncPeriod) {
159 return false;
160 }
161
Dan Stoza09bf7632020-06-10 14:28:50 -0700162 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700163 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700164 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700165 // Clear out the Composer-provided period and use the allowance logic below
166 HwcVsyncPeriod = {};
167 }
168
Ady Abraham8cb21882020-08-26 18:22:05 -0700169 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800170 static constexpr int allowancePercent = 10;
171 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
172 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800173 if (HwcVsyncPeriod) {
174 return std::abs(*HwcVsyncPeriod - period) < allowance;
175 }
176
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800177 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800178 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800179}
180
Ady Abraham8cb21882020-08-26 18:22:05 -0700181bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
182 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800183 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800184
Ady Abraham9c53ee72020-07-22 21:16:18 -0700185 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800186 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500187 ATRACE_FORMAT("VSR %" PRIu64 ": period confirmed", mId.value);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800188 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700189 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800190 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800191 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700192
193 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700194 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700195 }
Ady Abraham5a858552020-03-31 17:54:56 -0700196 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700197
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800198 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700199 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800200 } else if (mPeriodConfirmationInProgress) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500201 ATRACE_FORMAT("VSR %" PRIu64 ": still confirming period", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800202 mLastHwVsync = timestamp;
203 mMoreSamplesNeeded = true;
204 *periodFlushed = false;
205 } else {
Leon Scroggins III67388622023-02-06 20:36:20 -0500206 ATRACE_FORMAT("VSR %" PRIu64 ": adding sample", mId.value);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800207 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700208 mTracker.addVsyncTimestamp(timestamp);
209 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800210 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800211
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700212 if (!mMoreSamplesNeeded) {
213 setIgnorePresentFencesInternal(false);
214 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800215 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800216}
217
Rachel Lee6a9731d2022-06-06 17:08:14 -0700218void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
219 std::scoped_lock lock(mMutex);
220 mDisplayPowerMode = powerMode;
221}
222
Kevin DuBois00287382019-11-19 15:11:55 -0800223void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700224 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700225 StringAppendF(&result, "VsyncReactor in use\n");
226 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
227 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
228 mInternalIgnoreFences, mExternalIgnoreFences);
229 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
230 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
231 if (mPeriodTransitioningTo) {
232 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
233 } else {
234 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
235 }
236
237 if (mLastHwVsync) {
238 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
239 (mClock->now() - *mLastHwVsync) / 1e6f);
240 } else {
241 StringAppendF(&result, "No Last HW vsync\n");
242 }
243
Ady Abraham5e7371c2020-03-24 14:47:24 -0700244 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700245 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800246}
247
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800248} // namespace android::scheduler