blob: b5f212e085bd581a096bfeabedab799b1a61e13f [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>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080024#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080025#include <utils/Trace.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026
Ady Abraham13bc0be2020-02-27 16:48:20 -080027#include "../TracedOrdinal.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080028#include "VSyncDispatch.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080029#include "VSyncReactor.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080030#include "VSyncTracker.h"
31
32namespace android::scheduler {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080033
Ady Abraham5e7371c2020-03-24 14:47:24 -070034using base::StringAppendF;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080035
Ady Abraham8cb21882020-08-26 18:22:05 -070036VsyncController::~VsyncController() = default;
37
Kevin DuBois00287382019-11-19 15:11:55 -080038nsecs_t SystemClock::now() const {
39 return systemTime(SYSTEM_TIME_MONOTONIC);
40}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080041
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050042VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
Ady Abraham8735eac2020-08-12 16:35:04 -070043 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050044 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070045 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080046 mPendingLimit(pendingFenceLimit),
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050047 // TODO(adyabr): change mSupportKernelIdleTimer when the active display changes
Dan Stoza027d3652020-05-26 17:26:34 -070048 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080049
Kevin DuBoisf91e9232019-11-21 10:51:23 -080050VSyncReactor::~VSyncReactor() = default;
51
Dominik Laskowski068173d2021-08-11 17:22:59 -070052bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080053 if (!fence) {
54 return false;
55 }
56
57 nsecs_t const signalTime = fence->getCachedSignalTime();
58 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
59 return true;
60 }
61
Ady Abraham9c53ee72020-07-22 21:16:18 -070062 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080063 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080064 return true;
65 }
66
Kevin DuBois02d5ed92020-01-27 11:05:46 -080067 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080068 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
69 auto const time = (*it)->getCachedSignalTime();
70 if (time == Fence::SIGNAL_TIME_PENDING) {
71 it++;
72 } else if (time == Fence::SIGNAL_TIME_INVALID) {
73 it = mUnfiredFences.erase(it);
74 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070075 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080076
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080077 it = mUnfiredFences.erase(it);
78 }
79 }
80
81 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
82 if (mPendingLimit == mUnfiredFences.size()) {
83 mUnfiredFences.erase(mUnfiredFences.begin());
84 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070085 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080086 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070087 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080088 }
89
90 if (!timestampAccepted) {
91 mMoreSamplesNeeded = true;
92 setIgnorePresentFencesInternal(true);
93 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080094 }
95
Kevin DuBoisf77025c2019-12-18 16:13:24 -080096 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080097}
98
Ady Abraham8cb21882020-08-26 18:22:05 -070099void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700100 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700101 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800102 updateIgnorePresentFencesInternal();
103}
104
Ady Abraham8cb21882020-08-26 18:22:05 -0700105void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
106 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800107 updateIgnorePresentFencesInternal();
108}
109
110void VSyncReactor::updateIgnorePresentFencesInternal() {
111 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800112 mUnfiredFences.clear();
113 }
114}
115
Ady Abraham8cb21882020-08-26 18:22:05 -0700116void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500117 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800118 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800119 mPeriodTransitioningTo = newPeriod;
120 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800121 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800122}
123
124void VSyncReactor::endPeriodTransition() {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500125 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800126 mPeriodTransitioningTo.reset();
127 mPeriodConfirmationInProgress = false;
128 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800129}
130
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500131void VSyncReactor::startPeriodTransition(nsecs_t period) {
132 ATRACE_INT64("VSR-startPeriodTransition", period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700133 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800134 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700135
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500136 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800137 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700138 setIgnorePresentFencesInternal(false);
139 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800140 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700141 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800142 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800143}
144
Ady Abraham5dee2f12020-02-05 17:49:47 -0800145bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
146 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800147 return false;
148 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800149
Rachel Lee6a9731d2022-06-06 17:08:14 -0700150 if (mDisplayPowerMode == hal::PowerMode::DOZE ||
151 mDisplayPowerMode == hal::PowerMode::DOZE_SUSPEND) {
152 return true;
153 }
154
Ady Abraham5dee2f12020-02-05 17:49:47 -0800155 if (!mLastHwVsync && !HwcVsyncPeriod) {
156 return false;
157 }
158
Dan Stoza09bf7632020-06-10 14:28:50 -0700159 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700160 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700161 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700162 // Clear out the Composer-provided period and use the allowance logic below
163 HwcVsyncPeriod = {};
164 }
165
Ady Abraham8cb21882020-08-26 18:22:05 -0700166 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800167 static constexpr int allowancePercent = 10;
168 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
169 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800170 if (HwcVsyncPeriod) {
171 return std::abs(*HwcVsyncPeriod - period) < allowance;
172 }
173
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800174 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800175 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800176}
177
Ady Abraham8cb21882020-08-26 18:22:05 -0700178bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
179 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800180 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800181
Ady Abraham9c53ee72020-07-22 21:16:18 -0700182 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800183 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500184 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800185 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700186 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800187 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800188 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700189
190 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700191 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700192 }
Ady Abraham5a858552020-03-31 17:54:56 -0700193 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700194
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800195 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700196 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800197 } else if (mPeriodConfirmationInProgress) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500198 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800199 mLastHwVsync = timestamp;
200 mMoreSamplesNeeded = true;
201 *periodFlushed = false;
202 } else {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500203 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800204 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700205 mTracker.addVsyncTimestamp(timestamp);
206 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800207 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800208
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700209 if (!mMoreSamplesNeeded) {
210 setIgnorePresentFencesInternal(false);
211 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800212 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800213}
214
Rachel Lee6a9731d2022-06-06 17:08:14 -0700215void VSyncReactor::setDisplayPowerMode(hal::PowerMode powerMode) {
216 std::scoped_lock lock(mMutex);
217 mDisplayPowerMode = powerMode;
218}
219
Kevin DuBois00287382019-11-19 15:11:55 -0800220void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700221 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700222 StringAppendF(&result, "VsyncReactor in use\n");
223 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
224 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
225 mInternalIgnoreFences, mExternalIgnoreFences);
226 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
227 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
228 if (mPeriodTransitioningTo) {
229 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
230 } else {
231 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
232 }
233
234 if (mLastHwVsync) {
235 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
236 (mClock->now() - *mLastHwVsync) / 1e6f);
237 } else {
238 StringAppendF(&result, "No Last HW vsync\n");
239 }
240
Ady Abraham5e7371c2020-03-24 14:47:24 -0700241 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700242 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800243}
244
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800245} // namespace android::scheduler