blob: ee973f718a7af205cf862817d8e5c9965f92ad4a [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
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080021#include "VSyncReactor.h"
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>
Ady Abraham13bc0be2020-02-27 16:48:20 -080025#include "../TracedOrdinal.h"
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080026#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027#include "VSyncDispatch.h"
28#include "VSyncTracker.h"
29
30namespace android::scheduler {
Ady Abraham5e7371c2020-03-24 14:47:24 -070031using base::StringAppendF;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080032
Ady Abraham8cb21882020-08-26 18:22:05 -070033VsyncController::~VsyncController() = default;
34
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080035Clock::~Clock() = default;
Kevin DuBois00287382019-11-19 15:11:55 -080036nsecs_t SystemClock::now() const {
37 return systemTime(SYSTEM_TIME_MONOTONIC);
38}
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080039
Ady Abraham8735eac2020-08-12 16:35:04 -070040VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker,
41 size_t pendingFenceLimit, bool supportKernelIdleTimer)
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080042 : mClock(std::move(clock)),
Ady Abraham5a858552020-03-31 17:54:56 -070043 mTracker(tracker),
Ady Abraham13bc0be2020-02-27 16:48:20 -080044 mPendingLimit(pendingFenceLimit),
Ady Abraham9a2ea342021-09-03 17:32:34 -070045 // TODO(adyabr): change mSupportKernelIdleTimer when the active display changes
Dan Stoza027d3652020-05-26 17:26:34 -070046 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080047
Kevin DuBoisf91e9232019-11-21 10:51:23 -080048VSyncReactor::~VSyncReactor() = default;
49
Ady Abraham8cb21882020-08-26 18:22:05 -070050bool VSyncReactor::addPresentFence(const std::shared_ptr<android::FenceTime>& fence) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080051 if (!fence) {
52 return false;
53 }
54
55 nsecs_t const signalTime = fence->getCachedSignalTime();
56 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
57 return true;
58 }
59
Ady Abraham9c53ee72020-07-22 21:16:18 -070060 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080061 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080062 return true;
63 }
64
Kevin DuBois02d5ed92020-01-27 11:05:46 -080065 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080066 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
67 auto const time = (*it)->getCachedSignalTime();
68 if (time == Fence::SIGNAL_TIME_PENDING) {
69 it++;
70 } else if (time == Fence::SIGNAL_TIME_INVALID) {
71 it = mUnfiredFences.erase(it);
72 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070073 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080074
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080075 it = mUnfiredFences.erase(it);
76 }
77 }
78
79 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
80 if (mPendingLimit == mUnfiredFences.size()) {
81 mUnfiredFences.erase(mUnfiredFences.begin());
82 }
83 mUnfiredFences.push_back(fence);
84 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070085 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080086 }
87
88 if (!timestampAccepted) {
89 mMoreSamplesNeeded = true;
90 setIgnorePresentFencesInternal(true);
91 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080092 }
93
Kevin DuBoisf77025c2019-12-18 16:13:24 -080094 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080095}
96
Ady Abraham8cb21882020-08-26 18:22:05 -070097void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070098 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -070099 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800100 updateIgnorePresentFencesInternal();
101}
102
Ady Abraham8cb21882020-08-26 18:22:05 -0700103void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
104 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800105 updateIgnorePresentFencesInternal();
106}
107
108void VSyncReactor::updateIgnorePresentFencesInternal() {
109 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800110 mUnfiredFences.clear();
111 }
112}
113
Ady Abraham8cb21882020-08-26 18:22:05 -0700114void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700115 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800116 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800117 mPeriodTransitioningTo = newPeriod;
118 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800119 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800120}
121
122void VSyncReactor::endPeriodTransition() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700123 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800124 mPeriodTransitioningTo.reset();
125 mPeriodConfirmationInProgress = false;
126 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800127}
128
Ady Abraham8cb21882020-08-26 18:22:05 -0700129void VSyncReactor::startPeriodTransition(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800130 ATRACE_INT64("VSR-setPeriod", period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700131 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800132 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700133
Ady Abraham8cb21882020-08-26 18:22:05 -0700134 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800135 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700136 setIgnorePresentFencesInternal(false);
137 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800138 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700139 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800140 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800141}
142
Ady Abraham5dee2f12020-02-05 17:49:47 -0800143bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
144 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800145 return false;
146 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800147
Ady Abraham5dee2f12020-02-05 17:49:47 -0800148 if (!mLastHwVsync && !HwcVsyncPeriod) {
149 return false;
150 }
151
Dan Stoza09bf7632020-06-10 14:28:50 -0700152 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700153 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700154 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700155 // Clear out the Composer-provided period and use the allowance logic below
156 HwcVsyncPeriod = {};
157 }
158
Ady Abraham8cb21882020-08-26 18:22:05 -0700159 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800160 static constexpr int allowancePercent = 10;
161 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
162 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800163 if (HwcVsyncPeriod) {
164 return std::abs(*HwcVsyncPeriod - period) < allowance;
165 }
166
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800167 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800168 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800169}
170
Ady Abraham8cb21882020-08-26 18:22:05 -0700171bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
172 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800173 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800174
Ady Abraham9c53ee72020-07-22 21:16:18 -0700175 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800176 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700177 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800178 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700179 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800180 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800181 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700182
183 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700184 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700185 }
Ady Abraham5a858552020-03-31 17:54:56 -0700186 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700187
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800188 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700189 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800190 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700191 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800192 mLastHwVsync = timestamp;
193 mMoreSamplesNeeded = true;
194 *periodFlushed = false;
195 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700196 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800197 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700198 mTracker.addVsyncTimestamp(timestamp);
199 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800200 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800201
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700202 if (!mMoreSamplesNeeded) {
203 setIgnorePresentFencesInternal(false);
204 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800205 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800206}
207
Kevin DuBois00287382019-11-19 15:11:55 -0800208void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700209 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700210 StringAppendF(&result, "VsyncReactor in use\n");
211 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
212 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
213 mInternalIgnoreFences, mExternalIgnoreFences);
214 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
215 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
216 if (mPeriodTransitioningTo) {
217 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
218 } else {
219 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
220 }
221
222 if (mLastHwVsync) {
223 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
224 (mClock->now() - *mLastHwVsync) / 1e6f);
225 } else {
226 StringAppendF(&result, "No Last HW vsync\n");
227 }
228
Ady Abraham5e7371c2020-03-24 14:47:24 -0700229 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700230 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800231}
232
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800233} // namespace android::scheduler