blob: 7b5d4626dc602589cccea127f919d6947d85aaca [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),
Dan Stoza027d3652020-05-26 17:26:34 -070045 mSupportKernelIdleTimer(supportKernelIdleTimer) {}
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080046
Kevin DuBoisf91e9232019-11-21 10:51:23 -080047VSyncReactor::~VSyncReactor() = default;
48
Ady Abraham8cb21882020-08-26 18:22:05 -070049bool VSyncReactor::addPresentFence(const std::shared_ptr<android::FenceTime>& fence) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080050 if (!fence) {
51 return false;
52 }
53
54 nsecs_t const signalTime = fence->getCachedSignalTime();
55 if (signalTime == Fence::SIGNAL_TIME_INVALID) {
56 return true;
57 }
58
Ady Abraham9c53ee72020-07-22 21:16:18 -070059 std::lock_guard lock(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080060 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080061 return true;
62 }
63
Kevin DuBois02d5ed92020-01-27 11:05:46 -080064 bool timestampAccepted = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080065 for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) {
66 auto const time = (*it)->getCachedSignalTime();
67 if (time == Fence::SIGNAL_TIME_PENDING) {
68 it++;
69 } else if (time == Fence::SIGNAL_TIME_INVALID) {
70 it = mUnfiredFences.erase(it);
71 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070072 timestampAccepted &= mTracker.addVsyncTimestamp(time);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080073
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080074 it = mUnfiredFences.erase(it);
75 }
76 }
77
78 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
79 if (mPendingLimit == mUnfiredFences.size()) {
80 mUnfiredFences.erase(mUnfiredFences.begin());
81 }
82 mUnfiredFences.push_back(fence);
83 } else {
Ady Abraham5a858552020-03-31 17:54:56 -070084 timestampAccepted &= mTracker.addVsyncTimestamp(signalTime);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080085 }
86
87 if (!timestampAccepted) {
88 mMoreSamplesNeeded = true;
89 setIgnorePresentFencesInternal(true);
90 mPeriodConfirmationInProgress = true;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080091 }
92
Kevin DuBoisf77025c2019-12-18 16:13:24 -080093 return mMoreSamplesNeeded;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080094}
95
Ady Abraham8cb21882020-08-26 18:22:05 -070096void VSyncReactor::setIgnorePresentFences(bool ignore) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070097 std::lock_guard lock(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -070098 mExternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080099 updateIgnorePresentFencesInternal();
100}
101
Ady Abraham8cb21882020-08-26 18:22:05 -0700102void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) {
103 mInternalIgnoreFences = ignore;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800104 updateIgnorePresentFencesInternal();
105}
106
107void VSyncReactor::updateIgnorePresentFencesInternal() {
108 if (mExternalIgnoreFences || mInternalIgnoreFences) {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800109 mUnfiredFences.clear();
110 }
111}
112
Ady Abraham8cb21882020-08-26 18:22:05 -0700113void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700114 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800115 mPeriodConfirmationInProgress = true;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800116 mPeriodTransitioningTo = newPeriod;
117 mMoreSamplesNeeded = true;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800118 setIgnorePresentFencesInternal(true);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800119}
120
121void VSyncReactor::endPeriodTransition() {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700122 ATRACE_CALL();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800123 mPeriodTransitioningTo.reset();
124 mPeriodConfirmationInProgress = false;
125 mLastHwVsync.reset();
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800126}
127
Ady Abraham8cb21882020-08-26 18:22:05 -0700128void VSyncReactor::startPeriodTransition(nsecs_t period) {
Kevin DuBois5988f482020-01-17 09:03:32 -0800129 ATRACE_INT64("VSR-setPeriod", period);
Ady Abraham8cb21882020-08-26 18:22:05 -0700130 std::lock_guard lock(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800131 mLastHwVsync.reset();
Dan Stoza027d3652020-05-26 17:26:34 -0700132
Ady Abraham8cb21882020-08-26 18:22:05 -0700133 if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) {
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800134 endPeriodTransition();
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700135 setIgnorePresentFencesInternal(false);
136 mMoreSamplesNeeded = false;
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800137 } else {
Ady Abraham8cb21882020-08-26 18:22:05 -0700138 startPeriodTransitionInternal(period);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800139 }
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -0800140}
141
Ady Abraham5dee2f12020-02-05 17:49:47 -0800142bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) {
143 if (!mPeriodConfirmationInProgress) {
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800144 return false;
145 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800146
Ady Abraham5dee2f12020-02-05 17:49:47 -0800147 if (!mLastHwVsync && !HwcVsyncPeriod) {
148 return false;
149 }
150
Dan Stoza09bf7632020-06-10 14:28:50 -0700151 const bool periodIsChanging =
Ady Abraham8cb21882020-08-26 18:22:05 -0700152 mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod());
Dan Stoza09bf7632020-06-10 14:28:50 -0700153 if (mSupportKernelIdleTimer && !periodIsChanging) {
Dan Stoza027d3652020-05-26 17:26:34 -0700154 // Clear out the Composer-provided period and use the allowance logic below
155 HwcVsyncPeriod = {};
156 }
157
Ady Abraham8cb21882020-08-26 18:22:05 -0700158 auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800159 static constexpr int allowancePercent = 10;
160 static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio;
161 auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den;
Ady Abraham5dee2f12020-02-05 17:49:47 -0800162 if (HwcVsyncPeriod) {
163 return std::abs(*HwcVsyncPeriod - period) < allowance;
164 }
165
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800166 auto const distance = vsync_timestamp - *mLastHwVsync;
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800167 return std::abs(distance - period) < allowance;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800168}
169
Ady Abraham8cb21882020-08-26 18:22:05 -0700170bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
171 bool* periodFlushed) {
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800172 assert(periodFlushed);
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800173
Ady Abraham9c53ee72020-07-22 21:16:18 -0700174 std::lock_guard lock(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -0800175 if (periodConfirmed(timestamp, hwcVsyncPeriod)) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700176 ATRACE_NAME("VSR: period confirmed");
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800177 if (mPeriodTransitioningTo) {
Ady Abraham5a858552020-03-31 17:54:56 -0700178 mTracker.setPeriod(*mPeriodTransitioningTo);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800179 *periodFlushed = true;
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800180 }
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700181
182 if (mLastHwVsync) {
Ady Abraham5a858552020-03-31 17:54:56 -0700183 mTracker.addVsyncTimestamp(*mLastHwVsync);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700184 }
Ady Abraham5a858552020-03-31 17:54:56 -0700185 mTracker.addVsyncTimestamp(timestamp);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700186
Kevin DuBoisc4cdd372020-01-09 14:12:02 -0800187 endPeriodTransition();
Ady Abraham5a858552020-03-31 17:54:56 -0700188 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800189 } else if (mPeriodConfirmationInProgress) {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700190 ATRACE_NAME("VSR: still confirming period");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800191 mLastHwVsync = timestamp;
192 mMoreSamplesNeeded = true;
193 *periodFlushed = false;
194 } else {
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700195 ATRACE_NAME("VSR: adding sample");
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800196 *periodFlushed = false;
Ady Abraham5a858552020-03-31 17:54:56 -0700197 mTracker.addVsyncTimestamp(timestamp);
198 mMoreSamplesNeeded = mTracker.needsMoreSamples();
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800199 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800200
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700201 if (!mMoreSamplesNeeded) {
202 setIgnorePresentFencesInternal(false);
203 }
Kevin DuBoisf77025c2019-12-18 16:13:24 -0800204 return mMoreSamplesNeeded;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -0800205}
206
Kevin DuBois00287382019-11-19 15:11:55 -0800207void VSyncReactor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700208 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700209 StringAppendF(&result, "VsyncReactor in use\n");
210 StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size());
211 StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n",
212 mInternalIgnoreFences, mExternalIgnoreFences);
213 StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n",
214 mMoreSamplesNeeded, mPeriodConfirmationInProgress);
215 if (mPeriodTransitioningTo) {
216 StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo);
217 } else {
218 StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n");
219 }
220
221 if (mLastHwVsync) {
222 StringAppendF(&result, "Last HW vsync was %.2fms ago\n",
223 (mClock->now() - *mLastHwVsync) / 1e6f);
224 } else {
225 StringAppendF(&result, "No Last HW vsync\n");
226 }
227
Ady Abraham5e7371c2020-03-24 14:47:24 -0700228 StringAppendF(&result, "VSyncTracker:\n");
Ady Abraham5a858552020-03-31 17:54:56 -0700229 mTracker.dump(result);
Kevin DuBois00287382019-11-19 15:11:55 -0800230}
231
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800232} // namespace android::scheduler