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