Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [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 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wextra" |
| 20 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "VSyncPredictor" |
| 23 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 25 | |
| 26 | #include <algorithm> |
| 27 | #include <chrono> |
| 28 | #include <sstream> |
| 29 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Alec Mouri | 9b133ca | 2023-11-14 19:00:01 +0000 | [diff] [blame] | 32 | #include <common/FlagManager.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 33 | #include <common/trace.h> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 34 | #include <cutils/compiler.h> |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 36 | #include <ftl/concat.h> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 38 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 39 | #include "RefreshRateSelector.h" |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 40 | #include "VSyncPredictor.h" |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 41 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 42 | namespace android::scheduler { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 43 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 44 | using base::StringAppendF; |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 45 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 46 | static auto constexpr kMaxPercent = 100u; |
| 47 | |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 48 | namespace { |
| 49 | int numVsyncsPerFrame(const ftl::NonNull<DisplayModePtr>& displayModePtr) { |
| 50 | const auto idealPeakRefreshPeriod = displayModePtr->getPeakFps().getPeriodNsecs(); |
| 51 | const auto idealRefreshPeriod = displayModePtr->getVsyncRate().getPeriodNsecs(); |
| 52 | return static_cast<int>(std::round(static_cast<float>(idealPeakRefreshPeriod) / |
| 53 | static_cast<float>(idealRefreshPeriod))); |
| 54 | } |
| 55 | } // namespace |
| 56 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 57 | VSyncPredictor::~VSyncPredictor() = default; |
| 58 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 59 | VSyncPredictor::VSyncPredictor(std::unique_ptr<Clock> clock, ftl::NonNull<DisplayModePtr> modePtr, |
| 60 | size_t historySize, size_t minimumSamplesForPrediction, |
| 61 | uint32_t outlierTolerancePercent) |
| 62 | : mClock(std::move(clock)), |
| 63 | mId(modePtr->getPhysicalDisplayId()), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 64 | mTraceOn(property_get_bool("debug.sf.vsp_trace", false)), |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 65 | kHistorySize(historySize), |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 66 | kMinimumSamplesForPrediction(minimumSamplesForPrediction), |
| 67 | kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)), |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 68 | mDisplayModePtr(modePtr), |
| 69 | mNumVsyncsForFrame(numVsyncsPerFrame(mDisplayModePtr)) { |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 70 | resetModel(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 73 | inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const { |
| 74 | if (CC_UNLIKELY(mTraceOn)) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 75 | traceInt64(name, value); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 79 | inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 80 | SFTRACE_INT64(ftl::Concat(ftl::truncated<14>(name), " ", mId.value).c_str(), value); |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 83 | inline size_t VSyncPredictor::next(size_t i) const { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 84 | return (i + 1) % mTimestamps.size(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 87 | nsecs_t VSyncPredictor::idealPeriod() const { |
| 88 | return mDisplayModePtr->getVsyncRate().getPeriodNsecs(); |
| 89 | } |
| 90 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 91 | bool VSyncPredictor::validate(nsecs_t timestamp) const { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 92 | if (mLastTimestampIndex < 0 || mTimestamps.empty()) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 96 | const auto aValidTimestamp = mTimestamps[mLastTimestampIndex]; |
| 97 | const auto percent = |
| 98 | (timestamp - aValidTimestamp) % idealPeriod() * kMaxPercent / idealPeriod(); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 99 | if (percent >= kOutlierTolerancePercent && |
| 100 | percent <= (kMaxPercent - kOutlierTolerancePercent)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 101 | SFTRACE_FORMAT_INSTANT("timestamp is not aligned with model"); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
| 105 | const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(), |
| 106 | [timestamp](nsecs_t a, nsecs_t b) { |
| 107 | return std::abs(timestamp - a) < std::abs(timestamp - b); |
| 108 | }); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 109 | const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / idealPeriod(); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 110 | if (distancePercent < kOutlierTolerancePercent) { |
| 111 | // duplicate timestamp |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 112 | SFTRACE_FORMAT_INSTANT("duplicate timestamp"); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 118 | nsecs_t VSyncPredictor::currentPeriod() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 119 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 120 | return mRateMap.find(idealPeriod())->second.slope; |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 123 | Period VSyncPredictor::minFramePeriod() const { |
| 124 | if (!FlagManager::getInstance().vrr_config()) { |
| 125 | return Period::fromNs(currentPeriod()); |
| 126 | } |
| 127 | |
| 128 | std::lock_guard lock(mMutex); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 129 | return minFramePeriodLocked(); |
| 130 | } |
| 131 | |
| 132 | Period VSyncPredictor::minFramePeriodLocked() const { |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 133 | const auto slope = mRateMap.find(idealPeriod())->second.slope; |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 134 | return Period::fromNs(slope * mNumVsyncsForFrame); |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 137 | bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 138 | SFTRACE_CALL(); |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 139 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 140 | std::lock_guard lock(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 141 | |
| 142 | if (!validate(timestamp)) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 143 | // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored, |
Ady Abraham | 43a3e69 | 2020-11-13 12:43:39 -0800 | [diff] [blame] | 144 | // don't insert this ts into mTimestamps ringbuffer. If we are still |
| 145 | // in the learning phase we should just clear all timestamps and start |
| 146 | // over. |
| 147 | if (mTimestamps.size() < kMinimumSamplesForPrediction) { |
Ady Abraham | 4c56b64 | 2021-06-08 15:03:33 -0700 | [diff] [blame] | 148 | // Add the timestamp to mTimestamps before clearing it so we could |
| 149 | // update mKnownTimestamp based on the new timestamp. |
| 150 | mTimestamps.push_back(timestamp); |
Ady Abraham | 43a3e69 | 2020-11-13 12:43:39 -0800 | [diff] [blame] | 151 | clearTimestamps(); |
| 152 | } else if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 153 | mKnownTimestamp = |
| 154 | std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end())); |
| 155 | } else { |
| 156 | mKnownTimestamp = timestamp; |
| 157 | } |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 158 | SFTRACE_FORMAT_INSTANT("timestamp rejected. mKnownTimestamp was %.2fms ago", |
| 159 | (mClock->now() - *mKnownTimestamp) / 1e6f); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 160 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 163 | if (mTimestamps.size() != kHistorySize) { |
| 164 | mTimestamps.push_back(timestamp); |
| 165 | mLastTimestampIndex = next(mLastTimestampIndex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 166 | } else { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 167 | mLastTimestampIndex = next(mLastTimestampIndex); |
| 168 | mTimestamps[mLastTimestampIndex] = timestamp; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 171 | traceInt64If("VSP-ts", timestamp); |
| 172 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 173 | const size_t numSamples = mTimestamps.size(); |
| 174 | if (numSamples < kMinimumSamplesForPrediction) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 175 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 176 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // This is a 'simple linear regression' calculation of Y over X, with Y being the |
| 180 | // vsync timestamps, and X being the ordinal of vsync count. |
| 181 | // The calculated slope is the vsync period. |
| 182 | // Formula for reference: |
| 183 | // Sigma_i: means sum over all timestamps. |
| 184 | // mean(variable): statistical mean of variable. |
| 185 | // X: snapped ordinal of the timestamp |
| 186 | // Y: vsync timestamp |
| 187 | // |
| 188 | // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) ) |
| 189 | // slope = ------------------------------------------- |
| 190 | // Sigma_i ( X_i - mean(X) ) ^ 2 |
| 191 | // |
| 192 | // intercept = mean(Y) - slope * mean(X) |
| 193 | // |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 194 | std::vector<nsecs_t> vsyncTS(numSamples); |
| 195 | std::vector<nsecs_t> ordinals(numSamples); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 196 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 197 | // Normalizing to the oldest timestamp cuts down on error in calculating the intercept. |
| 198 | const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 199 | auto it = mRateMap.find(idealPeriod()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 200 | auto const currentPeriod = it->second.slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 201 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 202 | // The mean of the ordinals must be precise for the intercept calculation, so scale them up for |
| 203 | // fixed-point arithmetic. |
| 204 | constexpr int64_t kScalingFactor = 1000; |
| 205 | |
| 206 | nsecs_t meanTS = 0; |
| 207 | nsecs_t meanOrdinal = 0; |
| 208 | |
| 209 | for (size_t i = 0; i < numSamples; i++) { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 210 | const auto timestamp = mTimestamps[i] - oldestTS; |
| 211 | vsyncTS[i] = timestamp; |
| 212 | meanTS += timestamp; |
| 213 | |
Rachel Lee | 934017e | 2022-08-10 15:34:14 -0700 | [diff] [blame] | 214 | const auto ordinal = currentPeriod == 0 |
| 215 | ? 0 |
| 216 | : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor; |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 217 | ordinals[i] = ordinal; |
| 218 | meanOrdinal += ordinal; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 221 | meanTS /= numSamples; |
| 222 | meanOrdinal /= numSamples; |
| 223 | |
| 224 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 225 | vsyncTS[i] -= meanTS; |
| 226 | ordinals[i] -= meanOrdinal; |
| 227 | } |
| 228 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 229 | nsecs_t top = 0; |
| 230 | nsecs_t bottom = 0; |
| 231 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 232 | top += vsyncTS[i] * ordinals[i]; |
| 233 | bottom += ordinals[i] * ordinals[i]; |
| 234 | } |
| 235 | |
| 236 | if (CC_UNLIKELY(bottom == 0)) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 237 | it->second = {idealPeriod(), 0}; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 238 | clearTimestamps(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 239 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 242 | nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 243 | nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor); |
| 244 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 245 | auto const percent = std::abs(anticipatedPeriod - idealPeriod()) * kMaxPercent / idealPeriod(); |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 246 | if (percent >= kOutlierTolerancePercent) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 247 | it->second = {idealPeriod(), 0}; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 248 | clearTimestamps(); |
| 249 | return false; |
| 250 | } |
| 251 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 252 | traceInt64If("VSP-period", anticipatedPeriod); |
| 253 | traceInt64If("VSP-intercept", intercept); |
| 254 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 255 | it->second = {anticipatedPeriod, intercept}; |
| 256 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 257 | ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, |
| 258 | mId.value, timestamp, anticipatedPeriod, intercept); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 259 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 262 | nsecs_t VSyncPredictor::snapToVsync(nsecs_t timePoint) const { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 263 | auto const [slope, intercept] = getVSyncPredictionModelLocked(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 264 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 265 | if (mTimestamps.empty()) { |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 266 | traceInt64("VSP-mode", 1); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 267 | auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 268 | auto const numPeriodsOut = ((timePoint - knownTimestamp) / idealPeriod()) + 1; |
| 269 | return knownTimestamp + numPeriodsOut * idealPeriod(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 272 | auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 273 | |
| 274 | // See b/145667109, the ordinal calculation must take into account the intercept. |
| 275 | auto const zeroPoint = oldest + intercept; |
| 276 | auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 277 | auto const prediction = (ordinalRequest * slope) + intercept + oldest; |
| 278 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 279 | traceInt64("VSP-mode", 0); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 280 | traceInt64If("VSP-timePoint", timePoint); |
| 281 | traceInt64If("VSP-prediction", prediction); |
| 282 | |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 283 | auto const printer = [&, slope = slope, intercept = intercept] { |
| 284 | std::stringstream str; |
| 285 | str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+" |
| 286 | << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept |
| 287 | << "oldestTS: " << oldest << " ordinal: " << ordinalRequest; |
| 288 | return str.str(); |
| 289 | }; |
| 290 | |
| 291 | ALOGV("%s", printer().c_str()); |
| 292 | LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s", |
| 293 | printer().c_str()); |
| 294 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 295 | return prediction; |
| 296 | } |
| 297 | |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 298 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint, |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 299 | std::optional<nsecs_t> lastVsyncOpt) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 300 | SFTRACE_CALL(); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 301 | std::lock_guard lock(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 302 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 303 | const auto now = TimePoint::fromNs(mClock->now()); |
| 304 | purgeTimelines(now); |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 305 | |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 306 | if (lastVsyncOpt && *lastVsyncOpt > timePoint) { |
| 307 | timePoint = *lastVsyncOpt; |
| 308 | } |
| 309 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 310 | const auto model = getVSyncPredictionModelLocked(); |
| 311 | const auto threshold = model.slope / 2; |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 312 | std::optional<Period> minFramePeriodOpt; |
| 313 | |
| 314 | if (mNumVsyncsForFrame > 1) { |
| 315 | minFramePeriodOpt = minFramePeriodLocked(); |
| 316 | } |
| 317 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 318 | std::optional<TimePoint> vsyncOpt; |
| 319 | for (auto& timeline : mTimelines) { |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 320 | vsyncOpt = timeline.nextAnticipatedVSyncTimeFrom(model, minFramePeriodOpt, |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 321 | snapToVsync(timePoint), mMissedVsync, |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 322 | lastVsyncOpt ? snapToVsync(*lastVsyncOpt - |
| 323 | threshold) |
| 324 | : lastVsyncOpt); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 325 | if (vsyncOpt) { |
| 326 | break; |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 327 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 328 | } |
| 329 | LOG_ALWAYS_FATAL_IF(!vsyncOpt); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 330 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 331 | if (*vsyncOpt > mLastCommittedVsync) { |
| 332 | mLastCommittedVsync = *vsyncOpt; |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 333 | SFTRACE_FORMAT_INSTANT("mLastCommittedVsync in %.2fms", |
| 334 | float(mLastCommittedVsync.ns() - mClock->now()) / 1e6f); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 335 | } |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 336 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 337 | return vsyncOpt->ns(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 340 | /* |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 341 | * Returns whether a given vsync timestamp is in phase with a frame rate. |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 342 | * If the frame rate is not a divisor of the refresh rate, it is always considered in phase. |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 343 | * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6): |
| 344 | * isVSyncInPhase(16.6, 30) = true |
| 345 | * isVSyncInPhase(33.3, 30) = false |
| 346 | * isVSyncInPhase(50.0, 30) = true |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 347 | */ |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 348 | bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) { |
| 349 | if (timePoint == 0) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 353 | std::lock_guard lock(mMutex); |
| 354 | const auto model = getVSyncPredictionModelLocked(); |
| 355 | const nsecs_t period = model.slope; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 356 | const nsecs_t justBeforeTimePoint = timePoint - period / 2; |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 357 | const auto now = TimePoint::fromNs(mClock->now()); |
| 358 | const auto vsync = snapToVsync(justBeforeTimePoint); |
| 359 | |
| 360 | purgeTimelines(now); |
| 361 | |
| 362 | for (auto& timeline : mTimelines) { |
ramindani | 548f449 | 2024-06-13 10:29:04 -0700 | [diff] [blame] | 363 | const bool isVsyncValid = FlagManager::getInstance().vrr_bugfix_24q4() |
| 364 | ? timeline.isWithin(TimePoint::fromNs(vsync)) == |
| 365 | VsyncTimeline::VsyncOnTimeline::Unique |
| 366 | : timeline.validUntil() && timeline.validUntil()->ns() > vsync; |
| 367 | if (isVsyncValid) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 368 | return timeline.isVSyncInPhase(model, vsync, frameRate); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // The last timeline should always be valid |
| 373 | return mTimelines.back().isVSyncInPhase(model, vsync, frameRate); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 376 | void VSyncPredictor::setRenderRate(Fps renderRate, bool applyImmediately) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 377 | SFTRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str()); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 378 | ALOGV("%s %s: RenderRate %s ", __func__, to_string(mId).c_str(), to_string(renderRate).c_str()); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 379 | std::lock_guard lock(mMutex); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 380 | const auto prevRenderRate = mRenderRateOpt; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 381 | mRenderRateOpt = renderRate; |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 382 | const auto renderPeriodDelta = |
| 383 | prevRenderRate ? prevRenderRate->getPeriodNsecs() - renderRate.getPeriodNsecs() : 0; |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 384 | if (applyImmediately) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 385 | SFTRACE_FORMAT_INSTANT("applyImmediately"); |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 386 | while (mTimelines.size() > 1) { |
| 387 | mTimelines.pop_front(); |
| 388 | } |
| 389 | |
| 390 | mTimelines.front().setRenderRate(renderRate); |
Ady Abraham | 45ed7a8 | 2024-03-14 17:36:22 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | |
| 394 | const bool newRenderRateIsHigher = renderPeriodDelta > renderRate.getPeriodNsecs() && |
| 395 | mLastCommittedVsync.ns() - mClock->now() > 2 * renderRate.getPeriodNsecs(); |
| 396 | if (newRenderRateIsHigher) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 397 | SFTRACE_FORMAT_INSTANT("newRenderRateIsHigher"); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 398 | mTimelines.clear(); |
| 399 | mLastCommittedVsync = TimePoint::fromNs(0); |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 400 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 401 | } else { |
ramindani | 548f449 | 2024-06-13 10:29:04 -0700 | [diff] [blame] | 402 | if (FlagManager::getInstance().vrr_bugfix_24q4()) { |
| 403 | // We need to freeze the timeline at the committed vsync so that we don't |
| 404 | // overshoot the deadline. |
| 405 | mTimelines.back().freeze(mLastCommittedVsync); |
| 406 | } else { |
| 407 | mTimelines.back().freeze( |
| 408 | TimePoint::fromNs(mLastCommittedVsync.ns() + mIdealPeriod.ns() / 2)); |
| 409 | } |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 410 | } |
| 411 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, renderRate); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 412 | purgeTimelines(TimePoint::fromNs(mClock->now())); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) { |
| 416 | LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(), |
| 417 | "mode does not belong to the display"); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 418 | SFTRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str()); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 419 | const auto timeout = modePtr->getVrrConfig() |
| 420 | ? modePtr->getVrrConfig()->notifyExpectedPresentConfig |
| 421 | : std::nullopt; |
| 422 | ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(), |
| 423 | to_string(*modePtr).c_str(), |
ramindani | cbd7a6d | 2023-12-19 16:00:30 -0800 | [diff] [blame] | 424 | timeout ? std::to_string(timeout->timeoutNs).c_str() : "N/A"); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 425 | std::lock_guard lock(mMutex); |
| 426 | |
| 427 | mDisplayModePtr = modePtr; |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 428 | mNumVsyncsForFrame = numVsyncsPerFrame(mDisplayModePtr); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 429 | traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs()); |
| 430 | |
| 431 | static constexpr size_t kSizeLimit = 30; |
| 432 | if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) { |
| 433 | mRateMap.erase(mRateMap.begin()); |
| 434 | } |
| 435 | |
| 436 | if (mRateMap.find(idealPeriod()) == mRateMap.end()) { |
| 437 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
| 438 | } |
| 439 | |
Ady Abraham | 5441005 | 2024-03-25 23:08:59 +0000 | [diff] [blame] | 440 | mTimelines.clear(); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 441 | clearTimestamps(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 444 | Duration VSyncPredictor::ensureMinFrameDurationIsKept(TimePoint expectedPresentTime, |
| 445 | TimePoint lastConfirmedPresentTime) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 446 | SFTRACE_CALL(); |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 447 | |
| 448 | if (mNumVsyncsForFrame <= 1) { |
| 449 | return 0ns; |
| 450 | } |
| 451 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 452 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 453 | const auto threshold = currentPeriod / 2; |
| 454 | const auto minFramePeriod = minFramePeriodLocked().ns(); |
| 455 | |
| 456 | auto prev = lastConfirmedPresentTime.ns(); |
| 457 | for (auto& current : mPastExpectedPresentTimes) { |
| 458 | if (CC_UNLIKELY(mTraceOn)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 459 | SFTRACE_FORMAT_INSTANT("current %.2f past last signaled fence", |
| 460 | static_cast<float>(current.ns() - |
| 461 | lastConfirmedPresentTime.ns()) / |
| 462 | 1e6f); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | const auto minPeriodViolation = current.ns() - prev + threshold < minFramePeriod; |
| 466 | if (minPeriodViolation) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 467 | SFTRACE_NAME("minPeriodViolation"); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 468 | current = TimePoint::fromNs(prev + minFramePeriod); |
| 469 | prev = current.ns(); |
| 470 | } else { |
| 471 | break; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (!mPastExpectedPresentTimes.empty()) { |
| 476 | const auto phase = Duration(mPastExpectedPresentTimes.back() - expectedPresentTime); |
| 477 | if (phase > 0ns) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 478 | for (auto& timeline : mTimelines) { |
| 479 | timeline.shiftVsyncSequence(phase); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 480 | } |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 481 | mPastExpectedPresentTimes.clear(); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 482 | return phase; |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 483 | } |
| 484 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 485 | |
| 486 | return 0ns; |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void VSyncPredictor::onFrameBegin(TimePoint expectedPresentTime, |
| 490 | TimePoint lastConfirmedPresentTime) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 491 | SFTRACE_NAME("VSyncPredictor::onFrameBegin"); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 492 | std::lock_guard lock(mMutex); |
| 493 | |
| 494 | if (!mDisplayModePtr->getVrrConfig()) return; |
| 495 | |
| 496 | if (CC_UNLIKELY(mTraceOn)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 497 | SFTRACE_FORMAT_INSTANT("vsync is %.2f past last signaled fence", |
| 498 | static_cast<float>(expectedPresentTime.ns() - |
| 499 | lastConfirmedPresentTime.ns()) / |
| 500 | 1e6f); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 501 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 502 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 503 | const auto threshold = currentPeriod / 2; |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 504 | mPastExpectedPresentTimes.push_back(expectedPresentTime); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 505 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 506 | while (!mPastExpectedPresentTimes.empty()) { |
| 507 | const auto front = mPastExpectedPresentTimes.front().ns(); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 508 | const bool frontIsBeforeConfirmed = front < lastConfirmedPresentTime.ns() + threshold; |
| 509 | if (frontIsBeforeConfirmed) { |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 510 | if (CC_UNLIKELY(mTraceOn)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 511 | SFTRACE_FORMAT_INSTANT("Discarding old vsync - %.2f before last signaled fence", |
| 512 | static_cast<float>(lastConfirmedPresentTime.ns() - front) / |
| 513 | 1e6f); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 514 | } |
| 515 | mPastExpectedPresentTimes.pop_front(); |
| 516 | } else { |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 521 | const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime); |
| 522 | if (phase > 0ns) { |
| 523 | mMissedVsync = {expectedPresentTime, minFramePeriodLocked()}; |
| 524 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | void VSyncPredictor::onFrameMissed(TimePoint expectedPresentTime) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 528 | SFTRACE_NAME("VSyncPredictor::onFrameMissed"); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 529 | |
| 530 | std::lock_guard lock(mMutex); |
| 531 | if (!mDisplayModePtr->getVrrConfig()) return; |
| 532 | |
| 533 | // We don't know when the frame is going to be presented, so we assume it missed one vsync |
| 534 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 535 | const auto lastConfirmedPresentTime = |
| 536 | TimePoint::fromNs(expectedPresentTime.ns() + currentPeriod); |
| 537 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 538 | const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime); |
| 539 | if (phase > 0ns) { |
| 540 | mMissedVsync = {expectedPresentTime, Duration::fromNs(0)}; |
| 541 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 544 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const { |
| 545 | std::lock_guard lock(mMutex); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 546 | return VSyncPredictor::getVSyncPredictionModelLocked(); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 550 | return mRateMap.find(idealPeriod())->second; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void VSyncPredictor::clearTimestamps() { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 554 | SFTRACE_CALL(); |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 555 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 556 | if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 557 | auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end()); |
| 558 | if (mKnownTimestamp) { |
| 559 | mKnownTimestamp = std::max(*mKnownTimestamp, maxRb); |
| 560 | } else { |
| 561 | mKnownTimestamp = maxRb; |
| 562 | } |
| 563 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 564 | mTimestamps.clear(); |
| 565 | mLastTimestampIndex = 0; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 566 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 567 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 568 | mIdealPeriod = Period::fromNs(idealPeriod()); |
Ady Abraham | c5d7246 | 2024-03-23 23:56:33 +0000 | [diff] [blame] | 569 | if (mTimelines.empty()) { |
| 570 | mLastCommittedVsync = TimePoint::fromNs(0); |
| 571 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt); |
| 572 | } else { |
| 573 | while (mTimelines.size() > 1) { |
| 574 | mTimelines.pop_front(); |
| 575 | } |
| 576 | mTimelines.front().setRenderRate(mRenderRateOpt); |
| 577 | // set mLastCommittedVsync to a valid vsync but don't commit too much in the future |
| 578 | const auto vsyncOpt = mTimelines.front().nextAnticipatedVSyncTimeFrom( |
| 579 | getVSyncPredictionModelLocked(), |
| 580 | /* minFramePeriodOpt */ std::nullopt, |
| 581 | snapToVsync(mClock->now()), MissedVsync{}, |
| 582 | /* lastVsyncOpt */ std::nullopt); |
| 583 | mLastCommittedVsync = *vsyncOpt; |
| 584 | } |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 587 | bool VSyncPredictor::needsMoreSamples() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 588 | std::lock_guard lock(mMutex); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 589 | return mTimestamps.size() < kMinimumSamplesForPrediction; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 592 | void VSyncPredictor::resetModel() { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 593 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 594 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 595 | clearTimestamps(); |
| 596 | } |
| 597 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 598 | void VSyncPredictor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 599 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 600 | StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str()); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 601 | StringAppendF(&result, "\tRefresh Rate Map:\n"); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 602 | for (const auto& [period, periodInterceptTuple] : mRateMap) { |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 603 | StringAppendF(&result, |
| 604 | "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n", |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 605 | period / 1e6f, periodInterceptTuple.slope / 1e6f, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 606 | periodInterceptTuple.intercept); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 607 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 608 | StringAppendF(&result, "\tmTimelines.size()=%zu\n", mTimelines.size()); |
| 609 | } |
| 610 | |
| 611 | void VSyncPredictor::purgeTimelines(android::TimePoint now) { |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 612 | const auto kEnoughFramesToBreakPhase = 5; |
| 613 | if (mRenderRateOpt && |
| 614 | mLastCommittedVsync.ns() + mRenderRateOpt->getPeriodNsecs() * kEnoughFramesToBreakPhase < |
| 615 | mClock->now()) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 616 | SFTRACE_FORMAT_INSTANT("kEnoughFramesToBreakPhase"); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 617 | mTimelines.clear(); |
| 618 | mLastCommittedVsync = TimePoint::fromNs(0); |
| 619 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt); |
| 620 | return; |
| 621 | } |
| 622 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 623 | while (mTimelines.size() > 1) { |
| 624 | const auto validUntilOpt = mTimelines.front().validUntil(); |
ramindani | 548f449 | 2024-06-13 10:29:04 -0700 | [diff] [blame] | 625 | const bool isTimelineOutDated = FlagManager::getInstance().vrr_bugfix_24q4() |
| 626 | ? mTimelines.front().isWithin(now) == VsyncTimeline::VsyncOnTimeline::Outside |
| 627 | : validUntilOpt && *validUntilOpt < now; |
| 628 | if (isTimelineOutDated) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 629 | mTimelines.pop_front(); |
| 630 | } else { |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | LOG_ALWAYS_FATAL_IF(mTimelines.empty()); |
| 635 | LOG_ALWAYS_FATAL_IF(mTimelines.back().validUntil().has_value()); |
| 636 | } |
| 637 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 638 | auto VSyncPredictor::VsyncTimeline::makeVsyncSequence(TimePoint knownVsync) |
| 639 | -> std::optional<VsyncSequence> { |
| 640 | if (knownVsync.ns() == 0) return std::nullopt; |
| 641 | return std::make_optional<VsyncSequence>({knownVsync.ns(), 0}); |
| 642 | } |
| 643 | |
| 644 | VSyncPredictor::VsyncTimeline::VsyncTimeline(TimePoint knownVsync, Period idealPeriod, |
| 645 | std::optional<Fps> renderRateOpt) |
| 646 | : mIdealPeriod(idealPeriod), |
| 647 | mRenderRateOpt(renderRateOpt), |
| 648 | mLastVsyncSequence(makeVsyncSequence(knownVsync)) {} |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 649 | |
| 650 | void VSyncPredictor::VsyncTimeline::freeze(TimePoint lastVsync) { |
| 651 | LOG_ALWAYS_FATAL_IF(mValidUntil.has_value()); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 652 | SFTRACE_FORMAT_INSTANT("renderRate %s valid for %.2f", |
| 653 | mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA", |
| 654 | float(lastVsync.ns() - TimePoint::now().ns()) / 1e6f); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 655 | mValidUntil = lastVsync; |
| 656 | } |
| 657 | |
| 658 | std::optional<TimePoint> VSyncPredictor::VsyncTimeline::nextAnticipatedVSyncTimeFrom( |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 659 | Model model, std::optional<Period> minFramePeriodOpt, nsecs_t vsync, |
| 660 | MissedVsync missedVsync, std::optional<nsecs_t> lastVsyncOpt) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 661 | SFTRACE_FORMAT("renderRate %s", mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA"); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 662 | |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 663 | nsecs_t vsyncTime = snapToVsyncAlignedWithRenderRate(model, vsync); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 664 | const auto threshold = model.slope / 2; |
| 665 | const auto lastFrameMissed = |
| 666 | lastVsyncOpt && std::abs(*lastVsyncOpt - missedVsync.vsync.ns()) < threshold; |
Ady Abraham | e9dcf79 | 2024-05-14 15:24:54 -0700 | [diff] [blame] | 667 | const auto mightBackpressure = minFramePeriodOpt && mRenderRateOpt && |
| 668 | mRenderRateOpt->getPeriod() < 2 * (*minFramePeriodOpt); |
| 669 | if (FlagManager::getInstance().vrr_config()) { |
| 670 | if (lastFrameMissed) { |
| 671 | // If the last frame missed is the last vsync, we already shifted the timeline. Depends |
| 672 | // on whether we skipped the frame (onFrameMissed) or not (onFrameBegin) we apply a |
| 673 | // different fixup. There is no need to to shift the vsync timeline again. |
| 674 | vsyncTime += missedVsync.fixup.ns(); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 675 | SFTRACE_FORMAT_INSTANT("lastFrameMissed"); |
Ady Abraham | e9dcf79 | 2024-05-14 15:24:54 -0700 | [diff] [blame] | 676 | } else if (mightBackpressure && lastVsyncOpt) { |
ramindani | 548f449 | 2024-06-13 10:29:04 -0700 | [diff] [blame] | 677 | if (!FlagManager::getInstance().vrr_bugfix_24q4()) { |
| 678 | // lastVsyncOpt does not need to be corrected with the new rate, and |
| 679 | // it should be used as is to avoid skipping a frame when changing rates are |
| 680 | // aligned at vsync time. |
| 681 | lastVsyncOpt = snapToVsyncAlignedWithRenderRate(model, *lastVsyncOpt); |
| 682 | } |
Ady Abraham | e9dcf79 | 2024-05-14 15:24:54 -0700 | [diff] [blame] | 683 | const auto vsyncDiff = vsyncTime - *lastVsyncOpt; |
| 684 | if (vsyncDiff <= minFramePeriodOpt->ns() - threshold) { |
| 685 | // avoid a duplicate vsync |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 686 | SFTRACE_FORMAT_INSTANT("skipping a vsync to avoid duplicate frame. next in %.2f " |
| 687 | "which " |
| 688 | "is %.2f " |
| 689 | "from " |
| 690 | "prev. " |
| 691 | "adjust by %.2f", |
| 692 | static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f, |
| 693 | static_cast<float>(vsyncDiff) / 1e6f, |
| 694 | static_cast<float>(mRenderRateOpt->getPeriodNsecs()) / 1e6f); |
Ady Abraham | e9dcf79 | 2024-05-14 15:24:54 -0700 | [diff] [blame] | 695 | vsyncTime += mRenderRateOpt->getPeriodNsecs(); |
| 696 | } |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 697 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 700 | SFTRACE_FORMAT_INSTANT("vsync in %.2fms", float(vsyncTime - TimePoint::now().ns()) / 1e6f); |
ramindani | 548f449 | 2024-06-13 10:29:04 -0700 | [diff] [blame] | 701 | const bool isVsyncInvalid = FlagManager::getInstance().vrr_bugfix_24q4() |
| 702 | ? isWithin(TimePoint::fromNs(vsyncTime)) == VsyncOnTimeline::Outside |
| 703 | : mValidUntil && vsyncTime > mValidUntil->ns(); |
| 704 | if (isVsyncInvalid) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 705 | SFTRACE_FORMAT_INSTANT("no longer valid for vsync in %.2f", |
| 706 | static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 707 | return std::nullopt; |
| 708 | } |
| 709 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 710 | return TimePoint::fromNs(vsyncTime); |
| 711 | } |
| 712 | |
| 713 | auto VSyncPredictor::VsyncTimeline::getVsyncSequenceLocked(Model model, nsecs_t vsync) |
| 714 | -> VsyncSequence { |
| 715 | if (!mLastVsyncSequence) return {vsync, 0}; |
| 716 | |
| 717 | const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence; |
| 718 | const auto vsyncSequence = lastVsyncSequence + |
| 719 | static_cast<int64_t>(std::round((vsync - lastVsyncTime) / |
| 720 | static_cast<float>(model.slope))); |
| 721 | return {vsync, vsyncSequence}; |
| 722 | } |
| 723 | |
| 724 | nsecs_t VSyncPredictor::VsyncTimeline::snapToVsyncAlignedWithRenderRate(Model model, |
| 725 | nsecs_t vsync) { |
| 726 | // update the mLastVsyncSequence for reference point |
| 727 | mLastVsyncSequence = getVsyncSequenceLocked(model, vsync); |
| 728 | |
| 729 | const auto renderRatePhase = [&]() -> int { |
| 730 | if (!mRenderRateOpt) return 0; |
| 731 | const auto divisor = |
| 732 | RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod.ns()), |
| 733 | *mRenderRateOpt); |
| 734 | if (divisor <= 1) return 0; |
| 735 | |
| 736 | int mod = mLastVsyncSequence->seq % divisor; |
| 737 | if (mod == 0) return 0; |
| 738 | |
| 739 | // This is actually a bug fix, but guarded with vrr_config since we found it with this |
| 740 | // config |
| 741 | if (FlagManager::getInstance().vrr_config()) { |
| 742 | if (mod < 0) mod += divisor; |
| 743 | } |
| 744 | |
| 745 | return divisor - mod; |
| 746 | }(); |
| 747 | |
| 748 | if (renderRatePhase == 0) { |
| 749 | return mLastVsyncSequence->vsyncTime; |
| 750 | } |
| 751 | |
| 752 | return mLastVsyncSequence->vsyncTime + model.slope * renderRatePhase; |
| 753 | } |
| 754 | |
| 755 | bool VSyncPredictor::VsyncTimeline::isVSyncInPhase(Model model, nsecs_t vsync, Fps frameRate) { |
| 756 | const auto getVsyncIn = [](TimePoint now, nsecs_t timePoint) -> float { |
| 757 | return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now); |
| 758 | }; |
| 759 | |
Ady Abraham | 4a719e8 | 2024-06-06 12:12:09 -0700 | [diff] [blame] | 760 | Fps displayFps = !FlagManager::getInstance().vrr_bugfix_24q4() && mRenderRateOpt |
| 761 | ? *mRenderRateOpt |
| 762 | : Fps::fromPeriodNsecs(mIdealPeriod.ns()); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 763 | const auto divisor = RefreshRateSelector::getFrameRateDivisor(displayFps, frameRate); |
| 764 | const auto now = TimePoint::now(); |
| 765 | |
| 766 | if (divisor <= 1) { |
| 767 | return true; |
| 768 | } |
| 769 | const auto vsyncSequence = getVsyncSequenceLocked(model, vsync); |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 770 | SFTRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64 " divisor: %zu", |
| 771 | getVsyncIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq, divisor); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 772 | return vsyncSequence.seq % divisor == 0; |
| 773 | } |
| 774 | |
| 775 | void VSyncPredictor::VsyncTimeline::shiftVsyncSequence(Duration phase) { |
| 776 | if (mLastVsyncSequence) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame^] | 777 | SFTRACE_FORMAT_INSTANT("adjusting vsync by %.2f", static_cast<float>(phase.ns()) / 1e6f); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 778 | mLastVsyncSequence->vsyncTime += phase.ns(); |
| 779 | } |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 782 | } // namespace android::scheduler |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 783 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 784 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 785 | #pragma clang diagnostic pop // ignored "-Wextra" |