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