blob: 665a7ee7ee2ce2f3a9bf3307e0c3bf8064baa0b5 [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
Ady Abraham8735eac2020-08-12 16:35:04 -070042VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
43 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080044 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070045 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080046 mPendingLimit(pendingFenceLimit),
Ady Abraham9a2ea342021-09-03 17:32:34 -070047 // 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) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700117 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() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700125 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
Ady Abraham8cb21882020-08-26 18:22:05 -0700131void VSyncReactor::startPeriodTransition(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800132 ATRACE_INT64("VSR-setPeriod", 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
Ady Abraham8cb21882020-08-26 18:22:05 -0700136 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
Ady Abraham5dee2f12020-02-05 17:49:47 -0800150 if (!mLastHwVsync && !HwcVsyncPeriod) {
151 return false;
152 }
153
Dan Stoza09bf7632020-06-10 14:28:50 -0700154 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700155 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700156 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700157 // Clear out the Composer-provided period and use the allowance logic below
158 HwcVsyncPeriod = {};
159 }
160
Ady Abraham8cb21882020-08-26 18:22:05 -0700161 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800162 static constexpr int allowancePercent = 10;
163 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
164 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800165 if (HwcVsyncPeriod) {
166 return std::abs(*HwcVsyncPeriod - period) < allowance;
167 }
168
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800169 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800170 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800171}
172
Ady Abraham8cb21882020-08-26 18:22:05 -0700173bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
174 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800175 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800176
Ady Abraham9c53ee72020-07-22 21:16:18 -0700177 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800178 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700179 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800180 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700181 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800182 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800183 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700184
185 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700186 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700187 }
Ady Abraham5a858552020-03-31 17:54:56 -0700188 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700189
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800190 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700191 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800192 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700193 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800194 mLastHwVsync = timestamp;
195 mMoreSamplesNeeded = true;
196 *periodFlushed = false;
197 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700198 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800199 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700200 mTracker.addVsyncTimestamp(timestamp);
201 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800202 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800203
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700204 if (!mMoreSamplesNeeded) {
205 setIgnorePresentFencesInternal(false);
206 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800207 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800208}
209
Kevin DuBois00287382019-11-19 15:11:55 -0800210void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700211 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700212 StringAppendF(&result, "VsyncReactor in use\n");
213 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
214 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
215 mInternalIgnoreFences, mExternalIgnoreFences);
216 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
217 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
218 if (mPeriodTransitioningTo) {
219 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
220 } else {
221 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
222 }
223
224 if (mLastHwVsync) {
225 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
226 (mClock->now() - *mLastHwVsync) / 1e6f);
227 } else {
228 StringAppendF(&result, "No Last HW vsync\n");
229 }
230
Ady Abraham5e7371c2020-03-24 14:47:24 -0700231 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700232 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800233}
234
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800235} // namespace android::scheduler