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 | |
| 48 | VSyncPredictor::~VSyncPredictor() = default; |
| 49 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 50 | VSyncPredictor::VSyncPredictor(ftl::NonNull<DisplayModePtr> modePtr, size_t historySize, |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 51 | size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent, |
| 52 | IVsyncTrackerCallback& callback) |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 53 | : mId(modePtr->getPhysicalDisplayId()), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 54 | mTraceOn(property_get_bool("debug.sf.vsp_trace", false)), |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 55 | kHistorySize(historySize), |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 56 | kMinimumSamplesForPrediction(minimumSamplesForPrediction), |
| 57 | kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)), |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 58 | mVsyncTrackerCallback(callback), |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 59 | mDisplayModePtr(modePtr) { |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 60 | resetModel(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 63 | inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const { |
| 64 | if (CC_UNLIKELY(mTraceOn)) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 65 | traceInt64(name, value); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 69 | inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 70 | 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] | 71 | } |
| 72 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 73 | inline size_t VSyncPredictor::next(size_t i) const { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 74 | return (i + 1) % mTimestamps.size(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 77 | nsecs_t VSyncPredictor::idealPeriod() const { |
| 78 | return mDisplayModePtr->getVsyncRate().getPeriodNsecs(); |
| 79 | } |
| 80 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 81 | bool VSyncPredictor::validate(nsecs_t timestamp) const { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 82 | if (mLastTimestampIndex < 0 || mTimestamps.empty()) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 86 | const auto aValidTimestamp = mTimestamps[mLastTimestampIndex]; |
| 87 | const auto percent = |
| 88 | (timestamp - aValidTimestamp) % idealPeriod() * kMaxPercent / idealPeriod(); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 89 | if (percent >= kOutlierTolerancePercent && |
| 90 | percent <= (kMaxPercent - kOutlierTolerancePercent)) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(), |
| 95 | [timestamp](nsecs_t a, nsecs_t b) { |
| 96 | return std::abs(timestamp - a) < std::abs(timestamp - b); |
| 97 | }); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 98 | const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / idealPeriod(); |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 99 | if (distancePercent < kOutlierTolerancePercent) { |
| 100 | // duplicate timestamp |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 106 | nsecs_t VSyncPredictor::currentPeriod() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 107 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 108 | return mRateMap.find(idealPeriod())->second.slope; |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Ady Abraham | 3db8a3c | 2023-11-20 17:53:47 -0800 | [diff] [blame^] | 111 | Period VSyncPredictor::minFramePeriod() const { |
| 112 | if (!FlagManager::getInstance().vrr_config()) { |
| 113 | return Period::fromNs(currentPeriod()); |
| 114 | } |
| 115 | |
| 116 | std::lock_guard lock(mMutex); |
| 117 | const auto idealPeakRefreshPeriod = mDisplayModePtr->getPeakFps().getPeriodNsecs(); |
| 118 | const auto numPeriods = static_cast<int>(std::round(static_cast<float>(idealPeakRefreshPeriod) / |
| 119 | static_cast<float>(idealPeriod()))); |
| 120 | const auto slope = mRateMap.find(idealPeriod())->second.slope; |
| 121 | return Period::fromNs(slope * numPeriods); |
| 122 | } |
| 123 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 124 | bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 125 | std::lock_guard lock(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 126 | |
| 127 | if (!validate(timestamp)) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 128 | // 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] | 129 | // don't insert this ts into mTimestamps ringbuffer. If we are still |
| 130 | // in the learning phase we should just clear all timestamps and start |
| 131 | // over. |
| 132 | if (mTimestamps.size() < kMinimumSamplesForPrediction) { |
Ady Abraham | 4c56b64 | 2021-06-08 15:03:33 -0700 | [diff] [blame] | 133 | // Add the timestamp to mTimestamps before clearing it so we could |
| 134 | // update mKnownTimestamp based on the new timestamp. |
| 135 | mTimestamps.push_back(timestamp); |
Ady Abraham | 43a3e69 | 2020-11-13 12:43:39 -0800 | [diff] [blame] | 136 | clearTimestamps(); |
| 137 | } else if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 138 | mKnownTimestamp = |
| 139 | std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end())); |
| 140 | } else { |
| 141 | mKnownTimestamp = timestamp; |
| 142 | } |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 143 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 146 | if (mTimestamps.size() != kHistorySize) { |
| 147 | mTimestamps.push_back(timestamp); |
| 148 | mLastTimestampIndex = next(mLastTimestampIndex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 149 | } else { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 150 | mLastTimestampIndex = next(mLastTimestampIndex); |
| 151 | mTimestamps[mLastTimestampIndex] = timestamp; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 154 | traceInt64If("VSP-ts", timestamp); |
| 155 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 156 | const size_t numSamples = mTimestamps.size(); |
| 157 | if (numSamples < kMinimumSamplesForPrediction) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 158 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 159 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // This is a 'simple linear regression' calculation of Y over X, with Y being the |
| 163 | // vsync timestamps, and X being the ordinal of vsync count. |
| 164 | // The calculated slope is the vsync period. |
| 165 | // Formula for reference: |
| 166 | // Sigma_i: means sum over all timestamps. |
| 167 | // mean(variable): statistical mean of variable. |
| 168 | // X: snapped ordinal of the timestamp |
| 169 | // Y: vsync timestamp |
| 170 | // |
| 171 | // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) ) |
| 172 | // slope = ------------------------------------------- |
| 173 | // Sigma_i ( X_i - mean(X) ) ^ 2 |
| 174 | // |
| 175 | // intercept = mean(Y) - slope * mean(X) |
| 176 | // |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 177 | std::vector<nsecs_t> vsyncTS(numSamples); |
| 178 | std::vector<nsecs_t> ordinals(numSamples); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 179 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 180 | // Normalizing to the oldest timestamp cuts down on error in calculating the intercept. |
| 181 | const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 182 | auto it = mRateMap.find(idealPeriod()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 183 | auto const currentPeriod = it->second.slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 184 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 185 | // The mean of the ordinals must be precise for the intercept calculation, so scale them up for |
| 186 | // fixed-point arithmetic. |
| 187 | constexpr int64_t kScalingFactor = 1000; |
| 188 | |
| 189 | nsecs_t meanTS = 0; |
| 190 | nsecs_t meanOrdinal = 0; |
| 191 | |
| 192 | for (size_t i = 0; i < numSamples; i++) { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 193 | const auto timestamp = mTimestamps[i] - oldestTS; |
| 194 | vsyncTS[i] = timestamp; |
| 195 | meanTS += timestamp; |
| 196 | |
Rachel Lee | 934017e | 2022-08-10 15:34:14 -0700 | [diff] [blame] | 197 | const auto ordinal = currentPeriod == 0 |
| 198 | ? 0 |
| 199 | : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor; |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 200 | ordinals[i] = ordinal; |
| 201 | meanOrdinal += ordinal; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 204 | meanTS /= numSamples; |
| 205 | meanOrdinal /= numSamples; |
| 206 | |
| 207 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 208 | vsyncTS[i] -= meanTS; |
| 209 | ordinals[i] -= meanOrdinal; |
| 210 | } |
| 211 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 212 | nsecs_t top = 0; |
| 213 | nsecs_t bottom = 0; |
| 214 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 215 | top += vsyncTS[i] * ordinals[i]; |
| 216 | bottom += ordinals[i] * ordinals[i]; |
| 217 | } |
| 218 | |
| 219 | if (CC_UNLIKELY(bottom == 0)) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 220 | it->second = {idealPeriod(), 0}; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 221 | clearTimestamps(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 222 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 225 | nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 226 | nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor); |
| 227 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 228 | auto const percent = std::abs(anticipatedPeriod - idealPeriod()) * kMaxPercent / idealPeriod(); |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 229 | if (percent >= kOutlierTolerancePercent) { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 230 | it->second = {idealPeriod(), 0}; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 231 | clearTimestamps(); |
| 232 | return false; |
| 233 | } |
| 234 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 235 | traceInt64If("VSP-period", anticipatedPeriod); |
| 236 | traceInt64If("VSP-intercept", intercept); |
| 237 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 238 | it->second = {anticipatedPeriod, intercept}; |
| 239 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 240 | ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, |
| 241 | mId.value, timestamp, anticipatedPeriod, intercept); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 242 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 245 | auto VSyncPredictor::getVsyncSequenceLocked(nsecs_t timestamp) const -> VsyncSequence { |
| 246 | const auto vsync = nextAnticipatedVSyncTimeFromLocked(timestamp); |
| 247 | if (!mLastVsyncSequence) return {vsync, 0}; |
| 248 | |
| 249 | const auto [slope, _] = getVSyncPredictionModelLocked(); |
| 250 | const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence; |
| 251 | const auto vsyncSequence = lastVsyncSequence + |
| 252 | static_cast<int64_t>(std::round((vsync - lastVsyncTime) / static_cast<float>(slope))); |
| 253 | return {vsync, vsyncSequence}; |
| 254 | } |
| 255 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 256 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const { |
| 257 | auto const [slope, intercept] = getVSyncPredictionModelLocked(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 258 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 259 | if (mTimestamps.empty()) { |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 260 | traceInt64("VSP-mode", 1); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 261 | auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 262 | auto const numPeriodsOut = ((timePoint - knownTimestamp) / idealPeriod()) + 1; |
| 263 | return knownTimestamp + numPeriodsOut * idealPeriod(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 266 | auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 267 | |
| 268 | // See b/145667109, the ordinal calculation must take into account the intercept. |
| 269 | auto const zeroPoint = oldest + intercept; |
| 270 | auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 271 | auto const prediction = (ordinalRequest * slope) + intercept + oldest; |
| 272 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 273 | traceInt64("VSP-mode", 0); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 274 | traceInt64If("VSP-timePoint", timePoint); |
| 275 | traceInt64If("VSP-prediction", prediction); |
| 276 | |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 277 | auto const printer = [&, slope = slope, intercept = intercept] { |
| 278 | std::stringstream str; |
| 279 | str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+" |
| 280 | << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept |
| 281 | << "oldestTS: " << oldest << " ordinal: " << ordinalRequest; |
| 282 | return str.str(); |
| 283 | }; |
| 284 | |
| 285 | ALOGV("%s", printer().c_str()); |
| 286 | LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s", |
| 287 | printer().c_str()); |
| 288 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 289 | return prediction; |
| 290 | } |
| 291 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 292 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 293 | std::lock_guard lock(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 294 | |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 295 | // update the mLastVsyncSequence for reference point |
| 296 | mLastVsyncSequence = getVsyncSequenceLocked(timePoint); |
| 297 | |
Ady Abraham | fdc049c | 2023-02-17 14:52:05 +0000 | [diff] [blame] | 298 | const auto renderRatePhase = [&]() REQUIRES(mMutex) -> int { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 299 | if (!mRenderRateOpt) return 0; |
Ady Abraham | fdc049c | 2023-02-17 14:52:05 +0000 | [diff] [blame] | 300 | |
| 301 | const auto divisor = |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 302 | RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(idealPeriod()), |
| 303 | *mRenderRateOpt); |
Ady Abraham | fdc049c | 2023-02-17 14:52:05 +0000 | [diff] [blame] | 304 | if (divisor <= 1) return 0; |
| 305 | |
| 306 | const int mod = mLastVsyncSequence->seq % divisor; |
| 307 | if (mod == 0) return 0; |
| 308 | |
| 309 | return divisor - mod; |
| 310 | }(); |
| 311 | |
| 312 | if (renderRatePhase == 0) { |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 313 | const auto vsyncTime = mLastVsyncSequence->vsyncTime; |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 314 | if (FlagManager::getInstance().vrr_config()) { |
ramindani | 2c65b0d | 2023-10-30 10:37:31 -0700 | [diff] [blame] | 315 | const auto vsyncTimePoint = TimePoint::fromNs(vsyncTime); |
| 316 | ATRACE_FORMAT("%s InPhase vsyncIn %.2fms", __func__, |
| 317 | ticks<std::milli, float>(vsyncTimePoint - TimePoint::now())); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 318 | const Fps renderRate = mRenderRateOpt ? *mRenderRateOpt : mDisplayModePtr->getPeakFps(); |
| 319 | mVsyncTrackerCallback.onVsyncGenerated(vsyncTimePoint, mDisplayModePtr, renderRate); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 320 | } |
| 321 | return vsyncTime; |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 322 | } |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 323 | |
| 324 | auto const [slope, intercept] = getVSyncPredictionModelLocked(); |
Ady Abraham | fdc049c | 2023-02-17 14:52:05 +0000 | [diff] [blame] | 325 | const auto approximateNextVsync = mLastVsyncSequence->vsyncTime + slope * renderRatePhase; |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 326 | const auto nextAnticipatedVsyncTime = |
| 327 | nextAnticipatedVSyncTimeFromLocked(approximateNextVsync - slope / 2); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 328 | if (FlagManager::getInstance().vrr_config()) { |
ramindani | 2c65b0d | 2023-10-30 10:37:31 -0700 | [diff] [blame] | 329 | const auto nextAnticipatedVsyncTimePoint = TimePoint::fromNs(nextAnticipatedVsyncTime); |
| 330 | ATRACE_FORMAT("%s outOfPhase vsyncIn %.2fms", __func__, |
| 331 | ticks<std::milli, float>(nextAnticipatedVsyncTimePoint - TimePoint::now())); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 332 | const Fps renderRate = mRenderRateOpt ? *mRenderRateOpt : mDisplayModePtr->getPeakFps(); |
| 333 | mVsyncTrackerCallback.onVsyncGenerated(nextAnticipatedVsyncTimePoint, mDisplayModePtr, |
| 334 | renderRate); |
ramindani | d4354a9 | 2023-10-02 15:11:09 -0700 | [diff] [blame] | 335 | } |
| 336 | return nextAnticipatedVsyncTime; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 339 | /* |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 340 | * 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] | 341 | * 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] | 342 | * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6): |
| 343 | * isVSyncInPhase(16.6, 30) = true |
| 344 | * isVSyncInPhase(33.3, 30) = false |
| 345 | * isVSyncInPhase(50.0, 30) = true |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 346 | */ |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 347 | bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 348 | std::lock_guard lock(mMutex); |
| 349 | const auto divisor = |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 350 | RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(idealPeriod()), |
| 351 | frameRate); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 352 | return isVSyncInPhaseLocked(timePoint, static_cast<unsigned>(divisor)); |
| 353 | } |
| 354 | |
| 355 | bool VSyncPredictor::isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const { |
Ady Abraham | 9243bba | 2023-02-10 15:31:14 -0800 | [diff] [blame] | 356 | const TimePoint now = TimePoint::now(); |
| 357 | const auto getTimePointIn = [](TimePoint now, nsecs_t timePoint) -> float { |
| 358 | return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now); |
| 359 | }; |
| 360 | ATRACE_FORMAT("%s timePoint in: %.2f divisor: %zu", __func__, getTimePointIn(now, timePoint), |
| 361 | divisor); |
| 362 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 363 | if (divisor <= 1 || timePoint == 0) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 367 | const nsecs_t period = mRateMap[idealPeriod()].slope; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 368 | const nsecs_t justBeforeTimePoint = timePoint - period / 2; |
Ady Abraham | f34a813 | 2023-02-13 20:49:48 -0800 | [diff] [blame] | 369 | const auto vsyncSequence = getVsyncSequenceLocked(justBeforeTimePoint); |
| 370 | ATRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64, |
| 371 | getTimePointIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq); |
| 372 | return vsyncSequence.seq % divisor == 0; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 375 | void VSyncPredictor::setRenderRate(Fps renderRate) { |
| 376 | ATRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str()); |
| 377 | 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] | 378 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 379 | mRenderRateOpt = renderRate; |
| 380 | } |
| 381 | |
| 382 | void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) { |
| 383 | LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(), |
| 384 | "mode does not belong to the display"); |
| 385 | ATRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str()); |
| 386 | const auto timeout = modePtr->getVrrConfig() |
| 387 | ? modePtr->getVrrConfig()->notifyExpectedPresentConfig |
| 388 | : std::nullopt; |
| 389 | ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(), |
| 390 | to_string(*modePtr).c_str(), |
| 391 | timeout ? std::to_string(timeout->notifyExpectedPresentTimeoutNs).c_str() : "N/A"); |
| 392 | std::lock_guard lock(mMutex); |
| 393 | |
| 394 | mDisplayModePtr = modePtr; |
| 395 | traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs()); |
| 396 | |
| 397 | static constexpr size_t kSizeLimit = 30; |
| 398 | if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) { |
| 399 | mRateMap.erase(mRateMap.begin()); |
| 400 | } |
| 401 | |
| 402 | if (mRateMap.find(idealPeriod()) == mRateMap.end()) { |
| 403 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
| 404 | } |
| 405 | |
| 406 | clearTimestamps(); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 409 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const { |
| 410 | std::lock_guard lock(mMutex); |
| 411 | const auto model = VSyncPredictor::getVSyncPredictionModelLocked(); |
| 412 | return {model.slope, model.intercept}; |
| 413 | } |
| 414 | |
| 415 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const { |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 416 | return mRateMap.find(idealPeriod())->second; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void VSyncPredictor::clearTimestamps() { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 420 | if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 421 | auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end()); |
| 422 | if (mKnownTimestamp) { |
| 423 | mKnownTimestamp = std::max(*mKnownTimestamp, maxRb); |
| 424 | } else { |
| 425 | mKnownTimestamp = maxRb; |
| 426 | } |
| 427 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 428 | mTimestamps.clear(); |
| 429 | mLastTimestampIndex = 0; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 433 | bool VSyncPredictor::needsMoreSamples() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 434 | std::lock_guard lock(mMutex); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 435 | return mTimestamps.size() < kMinimumSamplesForPrediction; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 438 | void VSyncPredictor::resetModel() { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 439 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 440 | mRateMap[idealPeriod()] = {idealPeriod(), 0}; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 441 | clearTimestamps(); |
| 442 | } |
| 443 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 444 | void VSyncPredictor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 445 | std::lock_guard lock(mMutex); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 446 | StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str()); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 447 | StringAppendF(&result, "\tRefresh Rate Map:\n"); |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 448 | for (const auto& [period, periodInterceptTuple] : mRateMap) { |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 449 | StringAppendF(&result, |
| 450 | "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n", |
Ady Abraham | c585dba | 2023-11-15 18:41:35 -0800 | [diff] [blame] | 451 | period / 1e6f, periodInterceptTuple.slope / 1e6f, |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 452 | periodInterceptTuple.intercept); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 456 | } // namespace android::scheduler |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 457 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 458 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 459 | #pragma clang diagnostic pop // ignored "-Wextra" |