Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 1 | /* |
| 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 DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Kevin DuBois | c94ca83 | 2019-11-26 12:56:24 -0800 | [diff] [blame] | 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "VSyncReactor" |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 21 | |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 23 | #include <log/log.h> |
Kevin DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 24 | #include <utils/Trace.h> |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 25 | |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 26 | #include "../TracedOrdinal.h" |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 27 | #include "VSyncDispatch.h" |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 28 | #include "VSyncReactor.h" |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 29 | #include "VSyncTracker.h" |
| 30 | |
| 31 | namespace android::scheduler { |
Dominik Laskowski | 4e0d20d | 2021-12-06 11:31:02 -0800 | [diff] [blame] | 32 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 33 | using base::StringAppendF; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 34 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 35 | VsyncController::~VsyncController() = default; |
| 36 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 37 | nsecs_t SystemClock::now() const { |
| 38 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 39 | } |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 40 | |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame] | 41 | VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker, |
| 42 | size_t pendingFenceLimit, bool supportKernelIdleTimer) |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 43 | : mClock(std::move(clock)), |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 44 | mTracker(tracker), |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 45 | mPendingLimit(pendingFenceLimit), |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 46 | // TODO(adyabr): change mSupportKernelIdleTimer when the active display changes |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 47 | mSupportKernelIdleTimer(supportKernelIdleTimer) {} |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 48 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 49 | VSyncReactor::~VSyncReactor() = default; |
| 50 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 51 | bool VSyncReactor::addPresentFence(std::shared_ptr<FenceTime> fence) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 52 | 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 Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 61 | std::lock_guard lock(mMutex); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 62 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 66 | bool timestampAccepted = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 67 | 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 Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 74 | timestampAccepted &= mTracker.addVsyncTimestamp(time); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 75 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 76 | 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 Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 84 | mUnfiredFences.push_back(std::move(fence)); |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 85 | } else { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 86 | timestampAccepted &= mTracker.addVsyncTimestamp(signalTime); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if (!timestampAccepted) { |
| 90 | mMoreSamplesNeeded = true; |
| 91 | setIgnorePresentFencesInternal(true); |
| 92 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 95 | return mMoreSamplesNeeded; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 98 | void VSyncReactor::setIgnorePresentFences(bool ignore) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 99 | std::lock_guard lock(mMutex); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 100 | mExternalIgnoreFences = ignore; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 101 | updateIgnorePresentFencesInternal(); |
| 102 | } |
| 103 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 104 | void VSyncReactor::setIgnorePresentFencesInternal(bool ignore) { |
| 105 | mInternalIgnoreFences = ignore; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 106 | updateIgnorePresentFencesInternal(); |
| 107 | } |
| 108 | |
| 109 | void VSyncReactor::updateIgnorePresentFencesInternal() { |
| 110 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 111 | mUnfiredFences.clear(); |
| 112 | } |
| 113 | } |
| 114 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 115 | void VSyncReactor::startPeriodTransitionInternal(nsecs_t newPeriod) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 116 | ATRACE_CALL(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 117 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 118 | mPeriodTransitioningTo = newPeriod; |
| 119 | mMoreSamplesNeeded = true; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 120 | setIgnorePresentFencesInternal(true); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void VSyncReactor::endPeriodTransition() { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 124 | ATRACE_CALL(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 125 | mPeriodTransitioningTo.reset(); |
| 126 | mPeriodConfirmationInProgress = false; |
| 127 | mLastHwVsync.reset(); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 130 | void VSyncReactor::startPeriodTransition(nsecs_t period) { |
Kevin DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 131 | ATRACE_INT64("VSR-setPeriod", period); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 132 | std::lock_guard lock(mMutex); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 133 | mLastHwVsync.reset(); |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 134 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 135 | if (!mSupportKernelIdleTimer && period == mTracker.currentPeriod()) { |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 136 | endPeriodTransition(); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 137 | setIgnorePresentFencesInternal(false); |
| 138 | mMoreSamplesNeeded = false; |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 139 | } else { |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 140 | startPeriodTransitionInternal(period); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 141 | } |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 144 | bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) { |
| 145 | if (!mPeriodConfirmationInProgress) { |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 146 | return false; |
| 147 | } |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 148 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 149 | if (!mLastHwVsync && !HwcVsyncPeriod) { |
| 150 | return false; |
| 151 | } |
| 152 | |
Dan Stoza | 09bf763 | 2020-06-10 14:28:50 -0700 | [diff] [blame] | 153 | const bool periodIsChanging = |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 154 | mPeriodTransitioningTo && (*mPeriodTransitioningTo != mTracker.currentPeriod()); |
Dan Stoza | 09bf763 | 2020-06-10 14:28:50 -0700 | [diff] [blame] | 155 | if (mSupportKernelIdleTimer && !periodIsChanging) { |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 156 | // Clear out the Composer-provided period and use the allowance logic below |
| 157 | HwcVsyncPeriod = {}; |
| 158 | } |
| 159 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 160 | auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : mTracker.currentPeriod(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 161 | static constexpr int allowancePercent = 10; |
| 162 | static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio; |
| 163 | auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den; |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 164 | if (HwcVsyncPeriod) { |
| 165 | return std::abs(*HwcVsyncPeriod - period) < allowance; |
| 166 | } |
| 167 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 168 | auto const distance = vsync_timestamp - *mLastHwVsync; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 169 | return std::abs(distance - period) < allowance; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 172 | bool VSyncReactor::addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, |
| 173 | bool* periodFlushed) { |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 174 | assert(periodFlushed); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 175 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 176 | std::lock_guard lock(mMutex); |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 177 | if (periodConfirmed(timestamp, hwcVsyncPeriod)) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 178 | ATRACE_NAME("VSR: period confirmed"); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 179 | if (mPeriodTransitioningTo) { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 180 | mTracker.setPeriod(*mPeriodTransitioningTo); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 181 | *periodFlushed = true; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 182 | } |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 183 | |
| 184 | if (mLastHwVsync) { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 185 | mTracker.addVsyncTimestamp(*mLastHwVsync); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 186 | } |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 187 | mTracker.addVsyncTimestamp(timestamp); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 188 | |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 189 | endPeriodTransition(); |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 190 | mMoreSamplesNeeded = mTracker.needsMoreSamples(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 191 | } else if (mPeriodConfirmationInProgress) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 192 | ATRACE_NAME("VSR: still confirming period"); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 193 | mLastHwVsync = timestamp; |
| 194 | mMoreSamplesNeeded = true; |
| 195 | *periodFlushed = false; |
| 196 | } else { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 197 | ATRACE_NAME("VSR: adding sample"); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 198 | *periodFlushed = false; |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 199 | mTracker.addVsyncTimestamp(timestamp); |
| 200 | mMoreSamplesNeeded = mTracker.needsMoreSamples(); |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 201 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 202 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 203 | if (!mMoreSamplesNeeded) { |
| 204 | setIgnorePresentFencesInternal(false); |
| 205 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 206 | return mMoreSamplesNeeded; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 209 | void VSyncReactor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 210 | std::lock_guard lock(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 211 | 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 Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 230 | StringAppendF(&result, "VSyncTracker:\n"); |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 231 | mTracker.dump(result); |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 234 | } // namespace android::scheduler |