blob: bdcab515f258693563ef695b210784a196f15ab8 [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
Ady Abraham13bc0be2020-02-27 16:48:20 -080022#include <cutils/properties.h>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080023#include <log/log.h>
Kevin DuBois5988f482020-01-17 09:03:32 -080024#include <utils/Trace.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080025
Ady Abraham13bc0be2020-02-27 16:48:20 -080026#include "../TracedOrdinal.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027#include "VSyncDispatch.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080028#include "VSyncReactor.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080029#include "VSyncTracker.h"
30
31namespace android::scheduler {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080032
Ady Abraham5e7371c2020-03-24 14:47:24 -070033using base::StringAppendF;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080034
Ady Abraham8cb21882020-08-26 18:22:05 -070035VsyncController::~VsyncController() = default;
36
Kevin DuBois00287382019-11-19 15:11:55 -080037nsecs_t SystemClock::now() const {
38 return systemTime(SYSTEM_TIME_MONOTONIC);
39}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080040
Ady Abraham8735eac2020-08-12 16:35:04 -070041VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
42 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080043 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070044 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080045 mPendingLimit(pendingFenceLimit),
Ady Abraham9a2ea342021-09-03 17:32:34 -070046 // TODO(adyabr): change mSupportKernelIdleTimer when the active display changes
Dan Stoza027d3652020-05-26 17:26:34 -070047 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080048
Kevin DuBoisf91e9232019-11-21 10:51:23 -080049VSyncReactor::~VSyncReactor() = default;
50
Dominik Laskowski068173d2021-08-11 17:22:59 -070051bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080052 if (!fence) {
53 return false;
54 }
55
56 nsecs_t const signalTime = fence->getCachedSignalTime();
57 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
58 return true;
59 }
60
Ady Abraham9c53ee72020-07-22 21:16:18 -070061 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080062 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080063 return true;
64 }
65
Kevin DuBois02d5ed92020-01-27 11:05:46 -080066 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080067 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
68 auto const time = (*it)->getCachedSignalTime();
69 if (time == Fence::SIGNAL_TIME_PENDING) {
70 it++;
71 } else if (time == Fence::SIGNAL_TIME_INVALID) {
72 it = mUnfiredFences.erase(it);
73 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070074 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080075
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080076 it = mUnfiredFences.erase(it);
77 }
78 }
79
80 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
81 if (mPendingLimit == mUnfiredFences.size()) {
82 mUnfiredFences.erase(mUnfiredFences.begin());
83 }
Dominik Laskowski068173d2021-08-11 17:22:59 -070084 mUnfiredFences.push_back(std::move(fence));
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080085 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070086 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080087 }
88
89 if (!timestampAccepted) {
90 mMoreSamplesNeeded = true;
91 setIgnorePresentFencesInternal(true);
92 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080093 }
94
Kevin DuBoisf77025c2019-12-18 16:13:24 -080095 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080096}
97
Ady Abraham8cb21882020-08-26 18:22:05 -070098void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070099 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -0700100 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800101 updateIgnorePresentFencesInternal();
102}
103
Ady Abraham8cb21882020-08-26 18:22:05 -0700104void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
105 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800106 updateIgnorePresentFencesInternal();
107}
108
109void VSyncReactor::updateIgnorePresentFencesInternal() {
110 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800111 mUnfiredFences.clear();
112 }
113}
114
Ady Abraham8cb21882020-08-26 18:22:05 -0700115void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700116 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800117 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800118 mPeriodTransitioningTo = newPeriod;
119 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800120 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800121}
122
123void VSyncReactor::endPeriodTransition() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700124 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800125 mPeriodTransitioningTo.reset();
126 mPeriodConfirmationInProgress = false;
127 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800128}
129
Ady Abraham8cb21882020-08-26 18:22:05 -0700130void VSyncReactor::startPeriodTransition(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800131 ATRACE_INT64("VSR-setPeriod", period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700132 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800133 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700134
Ady Abraham8cb21882020-08-26 18:22:05 -0700135 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800136 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700137 setIgnorePresentFencesInternal(false);
138 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800139 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700140 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800141 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800142}
143
Ady Abraham5dee2f12020-02-05 17:49:47 -0800144bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
145 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800146 return false;
147 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800148
Ady Abraham5dee2f12020-02-05 17:49:47 -0800149 if (!mLastHwVsync && !HwcVsyncPeriod) {
150 return false;
151 }
152
Dan Stoza09bf7632020-06-10 14:28:50 -0700153 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700154 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700155 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700156 // Clear out the Composer-provided period and use the allowance logic below
157 HwcVsyncPeriod = {};
158 }
159
Ady Abraham8cb21882020-08-26 18:22:05 -0700160 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800161 static constexpr int allowancePercent = 10;
162 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
163 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800164 if (HwcVsyncPeriod) {
165 return std::abs(*HwcVsyncPeriod - period) < allowance;
166 }
167
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800168 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800169 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800170}
171
Ady Abraham8cb21882020-08-26 18:22:05 -0700172bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
173 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800174 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800175
Ady Abraham9c53ee72020-07-22 21:16:18 -0700176 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800177 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700178 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800179 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700180 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800181 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800182 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700183
184 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700185 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700186 }
Ady Abraham5a858552020-03-31 17:54:56 -0700187 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700188
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800189 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700190 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800191 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700192 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800193 mLastHwVsync = timestamp;
194 mMoreSamplesNeeded = true;
195 *periodFlushed = false;
196 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700197 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800198 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700199 mTracker.addVsyncTimestamp(timestamp);
200 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800201 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800202
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700203 if (!mMoreSamplesNeeded) {
204 setIgnorePresentFencesInternal(false);
205 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800206 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800207}
208
Kevin DuBois00287382019-11-19 15:11:55 -0800209void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700210 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700211 StringAppendF(&result, "VsyncReactor in use\n");
212 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
213 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
214 mInternalIgnoreFences, mExternalIgnoreFences);
215 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
216 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
217 if (mPeriodTransitioningTo) {
218 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
219 } else {
220 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
221 }
222
223 if (mLastHwVsync) {
224 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
225 (mClock->now() - *mLastHwVsync) / 1e6f);
226 } else {
227 StringAppendF(&result, "No Last HW vsync\n");
228 }
229
Ady Abraham5e7371c2020-03-24 14:47:24 -0700230 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700231 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800232}
233
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800234} // namespace android::scheduler