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> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 33 | #include <cutils/compiler.h> |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 35 | #include <ftl/concat.h> |
Ady Abraham | 9243bba | 2023-02-10 15:31:14 -0800 | [diff] [blame] | 36 | #include <gui/TraceUtils.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 { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 80 | ATRACE_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)) { |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 101 | ATRACE_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 |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 112 | ATRACE_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) { |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 138 | ATRACE_CALL(); |
| 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 | } |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 158 | ATRACE_FORMAT_INSTANT("timestamp rejected. mKnownTimestamp was %.2fms ago", |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 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) { |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 300 | ATRACE_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; |
| 333 | ATRACE_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) { |
| 363 | if (timeline.validUntil() && timeline.validUntil()->ns() > vsync) { |
| 364 | return timeline.isVSyncInPhase(model, vsync, frameRate); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // The last timeline should always be valid |
| 369 | return mTimelines.back().isVSyncInPhase(model, vsync, frameRate); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Ady Abraham | ee6365b | 2024-03-06 14:31:45 -0800 | [diff] [blame] | 372 | void VSyncPredictor::setRenderRate(Fps renderRate, bool applyImmediately) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 373 | ATRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str()); |
| 374 | 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] | 375 | std::lock_guard lock(mMutex); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 376 | const auto prevRenderRate = mRenderRateOpt; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 377 | mRenderRateOpt = renderRate; |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 378 | const auto renderPeriodDelta = |
| 379 | prevRenderRate ? prevRenderRate->getPeriodNsecs() - renderRate.getPeriodNsecs() : 0; |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 380 | if (applyImmediately) { |
Ady Abraham | 45ed7a8 | 2024-03-14 17:36:22 -0700 | [diff] [blame^] | 381 | ATRACE_FORMAT_INSTANT("applyImmediately"); |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 382 | while (mTimelines.size() > 1) { |
| 383 | mTimelines.pop_front(); |
| 384 | } |
| 385 | |
| 386 | mTimelines.front().setRenderRate(renderRate); |
Ady Abraham | 45ed7a8 | 2024-03-14 17:36:22 -0700 | [diff] [blame^] | 387 | return; |
| 388 | } |
| 389 | |
| 390 | const bool newRenderRateIsHigher = renderPeriodDelta > renderRate.getPeriodNsecs() && |
| 391 | mLastCommittedVsync.ns() - mClock->now() > 2 * renderRate.getPeriodNsecs(); |
| 392 | if (newRenderRateIsHigher) { |
| 393 | ATRACE_FORMAT_INSTANT("newRenderRateIsHigher"); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 394 | mTimelines.clear(); |
| 395 | mLastCommittedVsync = TimePoint::fromNs(0); |
Ady Abraham | 4fc2fce | 2024-03-08 06:43:44 +0000 | [diff] [blame] | 396 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 397 | } else { |
| 398 | mTimelines.back().freeze( |
| 399 | TimePoint::fromNs(mLastCommittedVsync.ns() + mIdealPeriod.ns() / 2)); |
| 400 | } |
| 401 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, renderRate); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 402 | purgeTimelines(TimePoint::fromNs(mClock->now())); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) { |
| 406 | LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(), |
| 407 | "mode does not belong to the display"); |
| 408 | ATRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str()); |
| 409 | const auto timeout = modePtr->getVrrConfig() |
| 410 | ? modePtr->getVrrConfig()->notifyExpectedPresentConfig |
| 411 | : std::nullopt; |
| 412 | ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(), |
| 413 | to_string(*modePtr).c_str(), |
ramindani | cbd7a6d | 2023-12-19 16:00:30 -0800 | [diff] [blame] | 414 | timeout ? std::to_string(timeout->timeoutNs).c_str() : "N/A"); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 415 | std::lock_guard lock(mMutex); |
| 416 | |
| 417 | mDisplayModePtr = modePtr; |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 418 | mNumVsyncsForFrame = numVsyncsPerFrame(mDisplayModePtr); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 419 | traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs()); |
| 420 | |
| 421 | static constexpr size_t kSizeLimit = 30; |
| 422 | if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) { |
| 423 | mRateMap.erase(mRateMap.begin()); |
| 424 | } |
| 425 | |
| 426 | if (mRateMap.find(idealPeriod()) == mRateMap.end()) { |
| 427 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
| 428 | } |
| 429 | |
| 430 | clearTimestamps(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 433 | Duration VSyncPredictor::ensureMinFrameDurationIsKept(TimePoint expectedPresentTime, |
| 434 | TimePoint lastConfirmedPresentTime) { |
| 435 | ATRACE_CALL(); |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 436 | |
| 437 | if (mNumVsyncsForFrame <= 1) { |
| 438 | return 0ns; |
| 439 | } |
| 440 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 441 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 442 | const auto threshold = currentPeriod / 2; |
| 443 | const auto minFramePeriod = minFramePeriodLocked().ns(); |
| 444 | |
| 445 | auto prev = lastConfirmedPresentTime.ns(); |
| 446 | for (auto& current : mPastExpectedPresentTimes) { |
| 447 | if (CC_UNLIKELY(mTraceOn)) { |
| 448 | ATRACE_FORMAT_INSTANT("current %.2f past last signaled fence", |
| 449 | static_cast<float>(current.ns() - lastConfirmedPresentTime.ns()) / |
| 450 | 1e6f); |
| 451 | } |
| 452 | |
| 453 | const auto minPeriodViolation = current.ns() - prev + threshold < minFramePeriod; |
| 454 | if (minPeriodViolation) { |
| 455 | ATRACE_NAME("minPeriodViolation"); |
| 456 | current = TimePoint::fromNs(prev + minFramePeriod); |
| 457 | prev = current.ns(); |
| 458 | } else { |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (!mPastExpectedPresentTimes.empty()) { |
| 464 | const auto phase = Duration(mPastExpectedPresentTimes.back() - expectedPresentTime); |
| 465 | if (phase > 0ns) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 466 | for (auto& timeline : mTimelines) { |
| 467 | timeline.shiftVsyncSequence(phase); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 468 | } |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 469 | mPastExpectedPresentTimes.clear(); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 470 | return phase; |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 473 | |
| 474 | return 0ns; |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void VSyncPredictor::onFrameBegin(TimePoint expectedPresentTime, |
| 478 | TimePoint lastConfirmedPresentTime) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 479 | ATRACE_NAME("VSyncPredictor::onFrameBegin"); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 480 | std::lock_guard lock(mMutex); |
| 481 | |
| 482 | if (!mDisplayModePtr->getVrrConfig()) return; |
| 483 | |
| 484 | if (CC_UNLIKELY(mTraceOn)) { |
| 485 | ATRACE_FORMAT_INSTANT("vsync is %.2f past last signaled fence", |
| 486 | static_cast<float>(expectedPresentTime.ns() - |
| 487 | lastConfirmedPresentTime.ns()) / |
| 488 | 1e6f); |
| 489 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 490 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 491 | const auto threshold = currentPeriod / 2; |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 492 | mPastExpectedPresentTimes.push_back(expectedPresentTime); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 493 | |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 494 | while (!mPastExpectedPresentTimes.empty()) { |
| 495 | const auto front = mPastExpectedPresentTimes.front().ns(); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 496 | const bool frontIsBeforeConfirmed = front < lastConfirmedPresentTime.ns() + threshold; |
| 497 | if (frontIsBeforeConfirmed) { |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 498 | if (CC_UNLIKELY(mTraceOn)) { |
| 499 | ATRACE_FORMAT_INSTANT("Discarding old vsync - %.2f before last signaled fence", |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 500 | static_cast<float>(lastConfirmedPresentTime.ns() - front) / |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 501 | 1e6f); |
| 502 | } |
| 503 | mPastExpectedPresentTimes.pop_front(); |
| 504 | } else { |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 509 | const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime); |
| 510 | if (phase > 0ns) { |
| 511 | mMissedVsync = {expectedPresentTime, minFramePeriodLocked()}; |
| 512 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void VSyncPredictor::onFrameMissed(TimePoint expectedPresentTime) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 516 | ATRACE_NAME("VSyncPredictor::onFrameMissed"); |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 517 | |
| 518 | std::lock_guard lock(mMutex); |
| 519 | if (!mDisplayModePtr->getVrrConfig()) return; |
| 520 | |
| 521 | // We don't know when the frame is going to be presented, so we assume it missed one vsync |
| 522 | const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope; |
| 523 | const auto lastConfirmedPresentTime = |
| 524 | TimePoint::fromNs(expectedPresentTime.ns() + currentPeriod); |
| 525 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 526 | const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime); |
| 527 | if (phase > 0ns) { |
| 528 | mMissedVsync = {expectedPresentTime, Duration::fromNs(0)}; |
| 529 | } |
Ady Abraham | e988303 | 2023-11-20 17:54:54 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 532 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const { |
| 533 | std::lock_guard lock(mMutex); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 534 | return VSyncPredictor::getVSyncPredictionModelLocked(); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 538 | return mRateMap.find(idealPeriod())->second; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | void VSyncPredictor::clearTimestamps() { |
Ady Abraham | f0b2bf9 | 2023-12-13 23:36:35 +0000 | [diff] [blame] | 542 | ATRACE_CALL(); |
| 543 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 544 | if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 545 | auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end()); |
| 546 | if (mKnownTimestamp) { |
| 547 | mKnownTimestamp = std::max(*mKnownTimestamp, maxRb); |
| 548 | } else { |
| 549 | mKnownTimestamp = maxRb; |
| 550 | } |
| 551 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 552 | mTimestamps.clear(); |
| 553 | mLastTimestampIndex = 0; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 554 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 555 | |
| 556 | mTimelines.clear(); |
| 557 | mLastCommittedVsync = TimePoint::fromNs(0); |
| 558 | mIdealPeriod = Period::fromNs(idealPeriod()); |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 559 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 562 | bool VSyncPredictor::needsMoreSamples() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 563 | std::lock_guard lock(mMutex); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 564 | return mTimestamps.size() < kMinimumSamplesForPrediction; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 567 | void VSyncPredictor::resetModel() { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 568 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 569 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 570 | clearTimestamps(); |
| 571 | } |
| 572 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 573 | void VSyncPredictor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 574 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 575 | StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str()); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 576 | StringAppendF(&result, "\tRefresh Rate Map:\n"); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 577 | for (const auto& [period, periodInterceptTuple] : mRateMap) { |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 578 | StringAppendF(&result, |
| 579 | "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n", |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 580 | period / 1e6f, periodInterceptTuple.slope / 1e6f, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 581 | periodInterceptTuple.intercept); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 582 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 583 | StringAppendF(&result, "\tmTimelines.size()=%zu\n", mTimelines.size()); |
| 584 | } |
| 585 | |
| 586 | void VSyncPredictor::purgeTimelines(android::TimePoint now) { |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 587 | const auto kEnoughFramesToBreakPhase = 5; |
| 588 | if (mRenderRateOpt && |
| 589 | mLastCommittedVsync.ns() + mRenderRateOpt->getPeriodNsecs() * kEnoughFramesToBreakPhase < |
| 590 | mClock->now()) { |
| 591 | mTimelines.clear(); |
| 592 | mLastCommittedVsync = TimePoint::fromNs(0); |
| 593 | mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt); |
| 594 | return; |
| 595 | } |
| 596 | |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 597 | while (mTimelines.size() > 1) { |
| 598 | const auto validUntilOpt = mTimelines.front().validUntil(); |
| 599 | if (validUntilOpt && *validUntilOpt < now) { |
| 600 | mTimelines.pop_front(); |
| 601 | } else { |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | LOG_ALWAYS_FATAL_IF(mTimelines.empty()); |
| 606 | LOG_ALWAYS_FATAL_IF(mTimelines.back().validUntil().has_value()); |
| 607 | } |
| 608 | |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 609 | auto VSyncPredictor::VsyncTimeline::makeVsyncSequence(TimePoint knownVsync) |
| 610 | -> std::optional<VsyncSequence> { |
| 611 | if (knownVsync.ns() == 0) return std::nullopt; |
| 612 | return std::make_optional<VsyncSequence>({knownVsync.ns(), 0}); |
| 613 | } |
| 614 | |
| 615 | VSyncPredictor::VsyncTimeline::VsyncTimeline(TimePoint knownVsync, Period idealPeriod, |
| 616 | std::optional<Fps> renderRateOpt) |
| 617 | : mIdealPeriod(idealPeriod), |
| 618 | mRenderRateOpt(renderRateOpt), |
| 619 | mLastVsyncSequence(makeVsyncSequence(knownVsync)) {} |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 620 | |
| 621 | void VSyncPredictor::VsyncTimeline::freeze(TimePoint lastVsync) { |
| 622 | LOG_ALWAYS_FATAL_IF(mValidUntil.has_value()); |
| 623 | ATRACE_FORMAT_INSTANT("renderRate %s valid for %.2f", |
| 624 | mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA", |
| 625 | float(lastVsync.ns() - TimePoint::now().ns()) / 1e6f); |
| 626 | mValidUntil = lastVsync; |
| 627 | } |
| 628 | |
| 629 | std::optional<TimePoint> VSyncPredictor::VsyncTimeline::nextAnticipatedVSyncTimeFrom( |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 630 | Model model, std::optional<Period> minFramePeriodOpt, nsecs_t vsync, |
| 631 | MissedVsync missedVsync, std::optional<nsecs_t> lastVsyncOpt) { |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 632 | ATRACE_FORMAT("renderRate %s", mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA"); |
| 633 | |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 634 | nsecs_t vsyncTime = snapToVsyncAlignedWithRenderRate(model, vsync); |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 635 | const auto threshold = model.slope / 2; |
| 636 | const auto lastFrameMissed = |
| 637 | lastVsyncOpt && std::abs(*lastVsyncOpt - missedVsync.vsync.ns()) < threshold; |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 638 | nsecs_t vsyncFixupTime = 0; |
| 639 | if (FlagManager::getInstance().vrr_config() && lastFrameMissed) { |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 640 | // If the last frame missed is the last vsync, we already shifted the timeline. Depends on |
| 641 | // whether we skipped the frame (onFrameMissed) or not (onFrameBegin) we apply a different |
| 642 | // fixup. There is no need to to shift the vsync timeline again. |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 643 | vsyncTime += missedVsync.fixup.ns(); |
| 644 | ATRACE_FORMAT_INSTANT("lastFrameMissed"); |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 645 | } else if (minFramePeriodOpt) { |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 646 | if (FlagManager::getInstance().vrr_config() && lastVsyncOpt) { |
| 647 | // lastVsyncOpt is based on the old timeline before we shifted it. we should correct it |
| 648 | // first before trying to use it. |
Ady Abraham | 77b4fb1 | 2024-03-05 17:51:53 -0800 | [diff] [blame] | 649 | if (mLastVsyncSequence->seq > 0) { |
| 650 | lastVsyncOpt = snapToVsyncAlignedWithRenderRate(model, *lastVsyncOpt); |
| 651 | } |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 652 | const auto vsyncDiff = vsyncTime - *lastVsyncOpt; |
Ady Abraham | 940b7a6 | 2024-03-07 10:04:27 -0800 | [diff] [blame] | 653 | if (vsyncDiff <= minFramePeriodOpt->ns() - threshold) { |
| 654 | vsyncFixupTime = *lastVsyncOpt + minFramePeriodOpt->ns() - vsyncTime; |
| 655 | ATRACE_FORMAT_INSTANT("minFramePeriod violation. next in %.2f which is %.2f " |
| 656 | "from " |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 657 | "prev. " |
| 658 | "adjust by %.2f", |
| 659 | static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f, |
| 660 | static_cast<float>(vsyncTime - *lastVsyncOpt) / 1e6f, |
| 661 | static_cast<float>(vsyncFixupTime) / 1e6f); |
| 662 | } |
| 663 | } |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 664 | vsyncTime += vsyncFixupTime; |
| 665 | } |
| 666 | |
| 667 | ATRACE_FORMAT_INSTANT("vsync in %.2fms", float(vsyncTime - TimePoint::now().ns()) / 1e6f); |
| 668 | if (mValidUntil && vsyncTime > mValidUntil->ns()) { |
| 669 | ATRACE_FORMAT_INSTANT("no longer valid for vsync in %.2f", |
| 670 | static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f); |
| 671 | return std::nullopt; |
| 672 | } |
| 673 | |
Ady Abraham | e54ce10 | 2024-03-04 23:18:38 +0000 | [diff] [blame] | 674 | // If we needed a fixup, it means that we changed the render rate and the chosen vsync would |
| 675 | // cross minFramePeriod. In that case we need to shift the entire vsync timeline. |
Ady Abraham | 20024aa | 2024-03-05 01:32:49 +0000 | [diff] [blame] | 676 | if (vsyncFixupTime > 0) { |
| 677 | shiftVsyncSequence(Duration::fromNs(vsyncFixupTime)); |
| 678 | } |
| 679 | |
| 680 | return TimePoint::fromNs(vsyncTime); |
| 681 | } |
| 682 | |
| 683 | auto VSyncPredictor::VsyncTimeline::getVsyncSequenceLocked(Model model, nsecs_t vsync) |
| 684 | -> VsyncSequence { |
| 685 | if (!mLastVsyncSequence) return {vsync, 0}; |
| 686 | |
| 687 | const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence; |
| 688 | const auto vsyncSequence = lastVsyncSequence + |
| 689 | static_cast<int64_t>(std::round((vsync - lastVsyncTime) / |
| 690 | static_cast<float>(model.slope))); |
| 691 | return {vsync, vsyncSequence}; |
| 692 | } |
| 693 | |
| 694 | nsecs_t VSyncPredictor::VsyncTimeline::snapToVsyncAlignedWithRenderRate(Model model, |
| 695 | nsecs_t vsync) { |
| 696 | // update the mLastVsyncSequence for reference point |
| 697 | mLastVsyncSequence = getVsyncSequenceLocked(model, vsync); |
| 698 | |
| 699 | const auto renderRatePhase = [&]() -> int { |
| 700 | if (!mRenderRateOpt) return 0; |
| 701 | const auto divisor = |
| 702 | RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod.ns()), |
| 703 | *mRenderRateOpt); |
| 704 | if (divisor <= 1) return 0; |
| 705 | |
| 706 | int mod = mLastVsyncSequence->seq % divisor; |
| 707 | if (mod == 0) return 0; |
| 708 | |
| 709 | // This is actually a bug fix, but guarded with vrr_config since we found it with this |
| 710 | // config |
| 711 | if (FlagManager::getInstance().vrr_config()) { |
| 712 | if (mod < 0) mod += divisor; |
| 713 | } |
| 714 | |
| 715 | return divisor - mod; |
| 716 | }(); |
| 717 | |
| 718 | if (renderRatePhase == 0) { |
| 719 | return mLastVsyncSequence->vsyncTime; |
| 720 | } |
| 721 | |
| 722 | return mLastVsyncSequence->vsyncTime + model.slope * renderRatePhase; |
| 723 | } |
| 724 | |
| 725 | bool VSyncPredictor::VsyncTimeline::isVSyncInPhase(Model model, nsecs_t vsync, Fps frameRate) { |
| 726 | const auto getVsyncIn = [](TimePoint now, nsecs_t timePoint) -> float { |
| 727 | return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now); |
| 728 | }; |
| 729 | |
| 730 | Fps displayFps = mRenderRateOpt ? *mRenderRateOpt : Fps::fromPeriodNsecs(mIdealPeriod.ns()); |
| 731 | const auto divisor = RefreshRateSelector::getFrameRateDivisor(displayFps, frameRate); |
| 732 | const auto now = TimePoint::now(); |
| 733 | |
| 734 | if (divisor <= 1) { |
| 735 | return true; |
| 736 | } |
| 737 | const auto vsyncSequence = getVsyncSequenceLocked(model, vsync); |
| 738 | ATRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64 " divisor: %zu", |
| 739 | getVsyncIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq, divisor); |
| 740 | return vsyncSequence.seq % divisor == 0; |
| 741 | } |
| 742 | |
| 743 | void VSyncPredictor::VsyncTimeline::shiftVsyncSequence(Duration phase) { |
| 744 | if (mLastVsyncSequence) { |
| 745 | ATRACE_FORMAT_INSTANT("adjusting vsync by %.2f", static_cast<float>(phase.ns()) / 1e6f); |
| 746 | mLastVsyncSequence->vsyncTime += phase.ns(); |
| 747 | } |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 750 | } // namespace android::scheduler |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 751 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 752 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 753 | #pragma clang diagnostic pop // ignored "-Wextra" |