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