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 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 33 | Clock::~Clock() = default; |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 34 | nsecs_t SystemClock::now() const { |
| 35 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 36 | } |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 37 | |
Ady Abraham | 8735eac | 2020-08-12 16:35:04 -0700 | [diff] [blame^] | 38 | VSyncReactor::VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker, |
| 39 | size_t pendingFenceLimit, bool supportKernelIdleTimer) |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 40 | : mClock(std::move(clock)), |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 41 | mTracker(tracker), |
Ady Abraham | 13bc0be | 2020-02-27 16:48:20 -0800 | [diff] [blame] | 42 | mPendingLimit(pendingFenceLimit), |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 43 | mSupportKernelIdleTimer(supportKernelIdleTimer) {} |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 44 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 45 | VSyncReactor::~VSyncReactor() = default; |
| 46 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 47 | bool VSyncReactor::addPresentFence(const std::shared_ptr<FenceTime>& fence) { |
| 48 | if (!fence) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | nsecs_t const signalTime = fence->getCachedSignalTime(); |
| 53 | if (signalTime == Fence::SIGNAL_TIME_INVALID) { |
| 54 | return true; |
| 55 | } |
| 56 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 57 | std::lock_guard lock(mMutex); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 58 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 62 | bool timestampAccepted = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 63 | for (auto it = mUnfiredFences.begin(); it != mUnfiredFences.end();) { |
| 64 | auto const time = (*it)->getCachedSignalTime(); |
| 65 | if (time == Fence::SIGNAL_TIME_PENDING) { |
| 66 | it++; |
| 67 | } else if (time == Fence::SIGNAL_TIME_INVALID) { |
| 68 | it = mUnfiredFences.erase(it); |
| 69 | } else { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 70 | timestampAccepted &= mTracker.addVsyncTimestamp(time); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 71 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 72 | it = mUnfiredFences.erase(it); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (signalTime == Fence::SIGNAL_TIME_PENDING) { |
| 77 | if (mPendingLimit == mUnfiredFences.size()) { |
| 78 | mUnfiredFences.erase(mUnfiredFences.begin()); |
| 79 | } |
| 80 | mUnfiredFences.push_back(fence); |
| 81 | } else { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 82 | timestampAccepted &= mTracker.addVsyncTimestamp(signalTime); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (!timestampAccepted) { |
| 86 | mMoreSamplesNeeded = true; |
| 87 | setIgnorePresentFencesInternal(true); |
| 88 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 91 | return mMoreSamplesNeeded; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void VSyncReactor::setIgnorePresentFences(bool ignoration) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 95 | std::lock_guard lock(mMutex); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 96 | mExternalIgnoreFences = ignoration; |
| 97 | updateIgnorePresentFencesInternal(); |
| 98 | } |
| 99 | |
| 100 | void VSyncReactor::setIgnorePresentFencesInternal(bool ignoration) { |
| 101 | mInternalIgnoreFences = ignoration; |
| 102 | updateIgnorePresentFencesInternal(); |
| 103 | } |
| 104 | |
| 105 | void VSyncReactor::updateIgnorePresentFencesInternal() { |
| 106 | if (mExternalIgnoreFences || mInternalIgnoreFences) { |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 107 | mUnfiredFences.clear(); |
| 108 | } |
| 109 | } |
| 110 | |
Ady Abraham | 0ed31c9 | 2020-04-16 11:48:45 -0700 | [diff] [blame] | 111 | nsecs_t VSyncReactor::computeNextRefresh(int periodOffset, nsecs_t now) const { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 112 | auto const currentPeriod = periodOffset ? mTracker.currentPeriod() : 0; |
| 113 | return mTracker.nextAnticipatedVSyncTimeFrom(now + periodOffset * currentPeriod); |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Ady Abraham | 0ed31c9 | 2020-04-16 11:48:45 -0700 | [diff] [blame] | 116 | nsecs_t VSyncReactor::expectedPresentTime(nsecs_t now) { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 117 | return mTracker.nextAnticipatedVSyncTimeFrom(now); |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 120 | void VSyncReactor::startPeriodTransition(nsecs_t newPeriod) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 121 | ATRACE_CALL(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 122 | mPeriodConfirmationInProgress = true; |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 123 | mPeriodTransitioningTo = newPeriod; |
| 124 | mMoreSamplesNeeded = true; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 125 | setIgnorePresentFencesInternal(true); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void VSyncReactor::endPeriodTransition() { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 129 | ATRACE_CALL(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 130 | mPeriodTransitioningTo.reset(); |
| 131 | mPeriodConfirmationInProgress = false; |
| 132 | mLastHwVsync.reset(); |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 135 | void VSyncReactor::setPeriod(nsecs_t period) { |
Kevin DuBois | 5988f48 | 2020-01-17 09:03:32 -0800 | [diff] [blame] | 136 | ATRACE_INT64("VSR-setPeriod", period); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 137 | std::lock_guard lk(mMutex); |
| 138 | mLastHwVsync.reset(); |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 139 | |
| 140 | if (!mSupportKernelIdleTimer && period == getPeriod()) { |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 141 | endPeriodTransition(); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 142 | setIgnorePresentFencesInternal(false); |
| 143 | mMoreSamplesNeeded = false; |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 144 | } else { |
| 145 | startPeriodTransition(period); |
| 146 | } |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | nsecs_t VSyncReactor::getPeriod() { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 150 | return mTracker.currentPeriod(); |
Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 153 | void VSyncReactor::beginResync() { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 154 | mTracker.resetModel(); |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 155 | } |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 156 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 157 | bool VSyncReactor::periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> HwcVsyncPeriod) { |
| 158 | if (!mPeriodConfirmationInProgress) { |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 159 | return false; |
| 160 | } |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 161 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 162 | if (!mLastHwVsync && !HwcVsyncPeriod) { |
| 163 | return false; |
| 164 | } |
| 165 | |
Dan Stoza | 09bf763 | 2020-06-10 14:28:50 -0700 | [diff] [blame] | 166 | const bool periodIsChanging = |
| 167 | mPeriodTransitioningTo && (*mPeriodTransitioningTo != getPeriod()); |
| 168 | if (mSupportKernelIdleTimer && !periodIsChanging) { |
Dan Stoza | 027d365 | 2020-05-26 17:26:34 -0700 | [diff] [blame] | 169 | // Clear out the Composer-provided period and use the allowance logic below |
| 170 | HwcVsyncPeriod = {}; |
| 171 | } |
| 172 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 173 | auto const period = mPeriodTransitioningTo ? *mPeriodTransitioningTo : getPeriod(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 174 | static constexpr int allowancePercent = 10; |
| 175 | static constexpr std::ratio<allowancePercent, 100> allowancePercentRatio; |
| 176 | auto const allowance = period * allowancePercentRatio.num / allowancePercentRatio.den; |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 177 | if (HwcVsyncPeriod) { |
| 178 | return std::abs(*HwcVsyncPeriod - period) < allowance; |
| 179 | } |
| 180 | |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 181 | auto const distance = vsync_timestamp - *mLastHwVsync; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 182 | return std::abs(distance - period) < allowance; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 185 | bool VSyncReactor::addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, |
| 186 | bool* periodFlushed) { |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 187 | assert(periodFlushed); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 188 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 189 | std::lock_guard lock(mMutex); |
Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 190 | if (periodConfirmed(timestamp, hwcVsyncPeriod)) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 191 | ATRACE_NAME("VSR: period confirmed"); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 192 | if (mPeriodTransitioningTo) { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 193 | mTracker.setPeriod(*mPeriodTransitioningTo); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 194 | *periodFlushed = true; |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 195 | } |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 196 | |
| 197 | if (mLastHwVsync) { |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 198 | mTracker.addVsyncTimestamp(*mLastHwVsync); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 199 | } |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 200 | mTracker.addVsyncTimestamp(timestamp); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 201 | |
Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 202 | endPeriodTransition(); |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 203 | mMoreSamplesNeeded = mTracker.needsMoreSamples(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 204 | } else if (mPeriodConfirmationInProgress) { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 205 | ATRACE_NAME("VSR: still confirming period"); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 206 | mLastHwVsync = timestamp; |
| 207 | mMoreSamplesNeeded = true; |
| 208 | *periodFlushed = false; |
| 209 | } else { |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 210 | ATRACE_NAME("VSR: adding sample"); |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 211 | *periodFlushed = false; |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 212 | mTracker.addVsyncTimestamp(timestamp); |
| 213 | mMoreSamplesNeeded = mTracker.needsMoreSamples(); |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 214 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 215 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 216 | if (!mMoreSamplesNeeded) { |
| 217 | setIgnorePresentFencesInternal(false); |
| 218 | } |
Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 219 | return mMoreSamplesNeeded; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 222 | void VSyncReactor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 223 | std::lock_guard lock(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 224 | StringAppendF(&result, "VsyncReactor in use\n"); |
| 225 | StringAppendF(&result, "Has %zu unfired fences\n", mUnfiredFences.size()); |
| 226 | StringAppendF(&result, "mInternalIgnoreFences=%d mExternalIgnoreFences=%d\n", |
| 227 | mInternalIgnoreFences, mExternalIgnoreFences); |
| 228 | StringAppendF(&result, "mMoreSamplesNeeded=%d mPeriodConfirmationInProgress=%d\n", |
| 229 | mMoreSamplesNeeded, mPeriodConfirmationInProgress); |
| 230 | if (mPeriodTransitioningTo) { |
| 231 | StringAppendF(&result, "mPeriodTransitioningTo=%" PRId64 "\n", *mPeriodTransitioningTo); |
| 232 | } else { |
| 233 | StringAppendF(&result, "mPeriodTransitioningTo=nullptr\n"); |
| 234 | } |
| 235 | |
| 236 | if (mLastHwVsync) { |
| 237 | StringAppendF(&result, "Last HW vsync was %.2fms ago\n", |
| 238 | (mClock->now() - *mLastHwVsync) / 1e6f); |
| 239 | } else { |
| 240 | StringAppendF(&result, "No Last HW vsync\n"); |
| 241 | } |
| 242 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 243 | StringAppendF(&result, "VSyncTracker:\n"); |
Ady Abraham | 5a85855 | 2020-03-31 17:54:56 -0700 | [diff] [blame] | 244 | mTracker.dump(result); |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 247 | } // namespace android::scheduler |