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> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 32 | #include <cutils/compiler.h> |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 33 | #include <cutils/properties.h> |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 34 | #include <gui/TraceUtils.h> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> |
| 36 | #include <utils/Trace.h> |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 37 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 38 | #include "RefreshRateSelector.h" |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 39 | #include "VSyncPredictor.h" |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 40 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 41 | namespace android::scheduler { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 42 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 43 | using base::StringAppendF; |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 44 | using base::StringPrintf; |
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 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 50 | VSyncPredictor::VSyncPredictor(std::string name, nsecs_t idealPeriod, size_t historySize, |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 51 | size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent) |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 52 | : mName(name), |
| 53 | mTraceOn(property_get_bool("debug.sf.vsp_trace", false)), |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 54 | kHistorySize(historySize), |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 55 | kMinimumSamplesForPrediction(minimumSamplesForPrediction), |
| 56 | kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)), |
| 57 | mIdealPeriod(idealPeriod) { |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 58 | resetModel(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 61 | inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const { |
| 62 | if (CC_UNLIKELY(mTraceOn)) { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 63 | traceInt64(name, value); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 67 | inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 68 | // TODO (b/266817103): Pass in PhysicalDisplayId and use ftl::Concat to |
| 69 | // avoid unnecessary allocations. |
| 70 | ATRACE_INT64(StringPrintf("%s %s", name, mName.c_str()).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 | |
| 77 | bool VSyncPredictor::validate(nsecs_t timestamp) const { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 78 | if (mLastTimestampIndex < 0 || mTimestamps.empty()) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 82 | auto const aValidTimestamp = mTimestamps[mLastTimestampIndex]; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 83 | auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod; |
Ady Abraham | 99ca336 | 2021-06-17 12:28:46 -0700 | [diff] [blame] | 84 | if (percent >= kOutlierTolerancePercent && |
| 85 | percent <= (kMaxPercent - kOutlierTolerancePercent)) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(), |
| 90 | [timestamp](nsecs_t a, nsecs_t b) { |
| 91 | return std::abs(timestamp - a) < std::abs(timestamp - b); |
| 92 | }); |
| 93 | const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / mIdealPeriod; |
| 94 | if (distancePercent < kOutlierTolerancePercent) { |
| 95 | // duplicate timestamp |
| 96 | return false; |
| 97 | } |
| 98 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 101 | nsecs_t VSyncPredictor::currentPeriod() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 102 | std::lock_guard lock(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 103 | return mRateMap.find(mIdealPeriod)->second.slope; |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 106 | bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 107 | std::lock_guard lock(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 108 | |
| 109 | if (!validate(timestamp)) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 110 | // 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] | 111 | // don't insert this ts into mTimestamps ringbuffer. If we are still |
| 112 | // in the learning phase we should just clear all timestamps and start |
| 113 | // over. |
| 114 | if (mTimestamps.size() < kMinimumSamplesForPrediction) { |
Ady Abraham | 4c56b64 | 2021-06-08 15:03:33 -0700 | [diff] [blame] | 115 | // Add the timestamp to mTimestamps before clearing it so we could |
| 116 | // update mKnownTimestamp based on the new timestamp. |
| 117 | mTimestamps.push_back(timestamp); |
Ady Abraham | 43a3e69 | 2020-11-13 12:43:39 -0800 | [diff] [blame] | 118 | clearTimestamps(); |
| 119 | } else if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 120 | mKnownTimestamp = |
| 121 | std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end())); |
| 122 | } else { |
| 123 | mKnownTimestamp = timestamp; |
| 124 | } |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 125 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 128 | if (mTimestamps.size() != kHistorySize) { |
| 129 | mTimestamps.push_back(timestamp); |
| 130 | mLastTimestampIndex = next(mLastTimestampIndex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 131 | } else { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 132 | mLastTimestampIndex = next(mLastTimestampIndex); |
| 133 | mTimestamps[mLastTimestampIndex] = timestamp; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 136 | traceInt64If("VSP-ts", timestamp); |
| 137 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 138 | const size_t numSamples = mTimestamps.size(); |
| 139 | if (numSamples < kMinimumSamplesForPrediction) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 140 | mRateMap[mIdealPeriod] = {mIdealPeriod, 0}; |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 141 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // This is a 'simple linear regression' calculation of Y over X, with Y being the |
| 145 | // vsync timestamps, and X being the ordinal of vsync count. |
| 146 | // The calculated slope is the vsync period. |
| 147 | // Formula for reference: |
| 148 | // Sigma_i: means sum over all timestamps. |
| 149 | // mean(variable): statistical mean of variable. |
| 150 | // X: snapped ordinal of the timestamp |
| 151 | // Y: vsync timestamp |
| 152 | // |
| 153 | // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) ) |
| 154 | // slope = ------------------------------------------- |
| 155 | // Sigma_i ( X_i - mean(X) ) ^ 2 |
| 156 | // |
| 157 | // intercept = mean(Y) - slope * mean(X) |
| 158 | // |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 159 | std::vector<nsecs_t> vsyncTS(numSamples); |
| 160 | std::vector<nsecs_t> ordinals(numSamples); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 161 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 162 | // Normalizing to the oldest timestamp cuts down on error in calculating the intercept. |
| 163 | const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 164 | auto it = mRateMap.find(mIdealPeriod); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 165 | auto const currentPeriod = it->second.slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 166 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 167 | // The mean of the ordinals must be precise for the intercept calculation, so scale them up for |
| 168 | // fixed-point arithmetic. |
| 169 | constexpr int64_t kScalingFactor = 1000; |
| 170 | |
| 171 | nsecs_t meanTS = 0; |
| 172 | nsecs_t meanOrdinal = 0; |
| 173 | |
| 174 | for (size_t i = 0; i < numSamples; i++) { |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 175 | const auto timestamp = mTimestamps[i] - oldestTS; |
| 176 | vsyncTS[i] = timestamp; |
| 177 | meanTS += timestamp; |
| 178 | |
Rachel Lee | 934017e | 2022-08-10 15:34:14 -0700 | [diff] [blame] | 179 | const auto ordinal = currentPeriod == 0 |
| 180 | ? 0 |
| 181 | : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor; |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 182 | ordinals[i] = ordinal; |
| 183 | meanOrdinal += ordinal; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 186 | meanTS /= numSamples; |
| 187 | meanOrdinal /= numSamples; |
| 188 | |
| 189 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 190 | vsyncTS[i] -= meanTS; |
| 191 | ordinals[i] -= meanOrdinal; |
| 192 | } |
| 193 | |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 194 | nsecs_t top = 0; |
| 195 | nsecs_t bottom = 0; |
| 196 | for (size_t i = 0; i < numSamples; i++) { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 197 | top += vsyncTS[i] * ordinals[i]; |
| 198 | bottom += ordinals[i] * ordinals[i]; |
| 199 | } |
| 200 | |
| 201 | if (CC_UNLIKELY(bottom == 0)) { |
| 202 | it->second = {mIdealPeriod, 0}; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 203 | clearTimestamps(); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 204 | return false; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 207 | nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 208 | nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor); |
| 209 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 210 | auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod; |
| 211 | if (percent >= kOutlierTolerancePercent) { |
| 212 | it->second = {mIdealPeriod, 0}; |
| 213 | clearTimestamps(); |
| 214 | return false; |
| 215 | } |
| 216 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 217 | traceInt64If("VSP-period", anticipatedPeriod); |
| 218 | traceInt64If("VSP-intercept", intercept); |
| 219 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 220 | it->second = {anticipatedPeriod, intercept}; |
| 221 | |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 222 | ALOGV("model update ts %s: %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, mName.c_str(), |
| 223 | timestamp, anticipatedPeriod, intercept); |
Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 224 | return true; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 227 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const { |
| 228 | auto const [slope, intercept] = getVSyncPredictionModelLocked(); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 229 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 230 | if (mTimestamps.empty()) { |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 231 | traceInt64("VSP-mode", 1); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 232 | auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint; |
| 233 | auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1; |
| 234 | return knownTimestamp + numPeriodsOut * mIdealPeriod; |
| 235 | } |
| 236 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 237 | auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end()); |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 238 | |
| 239 | // See b/145667109, the ordinal calculation must take into account the intercept. |
| 240 | auto const zeroPoint = oldest + intercept; |
| 241 | auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 242 | auto const prediction = (ordinalRequest * slope) + intercept + oldest; |
| 243 | |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 244 | traceInt64("VSP-mode", 0); |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 245 | traceInt64If("VSP-timePoint", timePoint); |
| 246 | traceInt64If("VSP-prediction", prediction); |
| 247 | |
Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 248 | auto const printer = [&, slope = slope, intercept = intercept] { |
| 249 | std::stringstream str; |
| 250 | str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+" |
| 251 | << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept |
| 252 | << "oldestTS: " << oldest << " ordinal: " << ordinalRequest; |
| 253 | return str.str(); |
| 254 | }; |
| 255 | |
| 256 | ALOGV("%s", printer().c_str()); |
| 257 | LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s", |
| 258 | printer().c_str()); |
| 259 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 260 | return prediction; |
| 261 | } |
| 262 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 263 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 264 | std::lock_guard lock(mMutex); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 265 | |
| 266 | // TODO(b/246164114): This implementation is not efficient at all. Refactor. |
| 267 | nsecs_t nextVsync = nextAnticipatedVSyncTimeFromLocked(timePoint); |
| 268 | while (!isVSyncInPhaseLocked(nextVsync, mDivisor)) { |
| 269 | nextVsync = nextAnticipatedVSyncTimeFromLocked(nextVsync + 1); |
| 270 | } |
| 271 | return nextVsync; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 274 | /* |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 275 | * 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] | 276 | * 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] | 277 | * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6): |
| 278 | * isVSyncInPhase(16.6, 30) = true |
| 279 | * isVSyncInPhase(33.3, 30) = false |
| 280 | * isVSyncInPhase(50.0, 30) = true |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 281 | */ |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 282 | bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const { |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 283 | std::lock_guard lock(mMutex); |
| 284 | const auto divisor = |
| 285 | RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod), frameRate); |
| 286 | return isVSyncInPhaseLocked(timePoint, static_cast<unsigned>(divisor)); |
| 287 | } |
| 288 | |
| 289 | bool VSyncPredictor::isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 290 | struct VsyncError { |
| 291 | nsecs_t vsyncTimestamp; |
| 292 | float error; |
| 293 | |
| 294 | bool operator<(const VsyncError& other) const { return error < other.error; } |
| 295 | }; |
| 296 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 297 | if (divisor <= 1 || timePoint == 0) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | |
| 301 | const nsecs_t period = mRateMap[mIdealPeriod].slope; |
| 302 | const nsecs_t justBeforeTimePoint = timePoint - period / 2; |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 303 | const nsecs_t dividedPeriod = mIdealPeriod / divisor; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 304 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 305 | // If this is the first time we have asked about this divisor with the |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 306 | // current vsync period, it is considered in phase and we store the closest |
| 307 | // vsync timestamp |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 308 | const auto knownTimestampIter = mRateDivisorKnownTimestampMap.find(dividedPeriod); |
| 309 | if (knownTimestampIter == mRateDivisorKnownTimestampMap.end()) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 310 | const auto vsync = nextAnticipatedVSyncTimeFromLocked(justBeforeTimePoint); |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 311 | mRateDivisorKnownTimestampMap[dividedPeriod] = vsync; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 315 | // Find the next N vsync timestamp where N is the divisor. |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 316 | // One of these vsyncs will be in phase. We return the one which is |
| 317 | // the most aligned with the last known in phase vsync |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 318 | std::vector<VsyncError> vsyncs(static_cast<size_t>(divisor)); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 319 | const nsecs_t knownVsync = knownTimestampIter->second; |
| 320 | nsecs_t point = justBeforeTimePoint; |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 321 | for (size_t i = 0; i < divisor; i++) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 322 | const nsecs_t vsync = nextAnticipatedVSyncTimeFromLocked(point); |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 323 | const auto numPeriods = static_cast<float>(vsync - knownVsync) / (period * divisor); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 324 | const auto error = std::abs(std::round(numPeriods) - numPeriods); |
| 325 | vsyncs[i] = {vsync, error}; |
| 326 | point = vsync + 1; |
| 327 | } |
| 328 | |
| 329 | const auto minVsyncError = std::min_element(vsyncs.begin(), vsyncs.end()); |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 330 | mRateDivisorKnownTimestampMap[dividedPeriod] = minVsyncError->vsyncTimestamp; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 331 | return std::abs(minVsyncError->vsyncTimestamp - timePoint) < period / 2; |
| 332 | } |
| 333 | |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 334 | void VSyncPredictor::setDivisor(unsigned divisor) { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 335 | ALOGV("%s %s: %d", __func__, mName.c_str(), divisor); |
Ady Abraham | ace3d05 | 2022-11-17 16:25:05 -0800 | [diff] [blame] | 336 | std::lock_guard lock(mMutex); |
| 337 | mDivisor = divisor; |
| 338 | } |
| 339 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 340 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const { |
| 341 | std::lock_guard lock(mMutex); |
| 342 | const auto model = VSyncPredictor::getVSyncPredictionModelLocked(); |
| 343 | return {model.slope, model.intercept}; |
| 344 | } |
| 345 | |
| 346 | VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const { |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 347 | return mRateMap.find(mIdealPeriod)->second; |
| 348 | } |
| 349 | |
| 350 | void VSyncPredictor::setPeriod(nsecs_t period) { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 351 | ATRACE_FORMAT("%s %s", __func__, mName.c_str()); |
Ady Abraham | d9b9a04 | 2023-01-13 11:30:58 -0800 | [diff] [blame] | 352 | traceInt64("VSP-setPeriod", period); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 353 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 354 | std::lock_guard lock(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 355 | static constexpr size_t kSizeLimit = 30; |
| 356 | if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) { |
| 357 | mRateMap.erase(mRateMap.begin()); |
| 358 | } |
| 359 | |
| 360 | mIdealPeriod = period; |
| 361 | if (mRateMap.find(period) == mRateMap.end()) { |
| 362 | mRateMap[mIdealPeriod] = {period, 0}; |
| 363 | } |
| 364 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 365 | clearTimestamps(); |
| 366 | } |
| 367 | |
| 368 | void VSyncPredictor::clearTimestamps() { |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 369 | if (!mTimestamps.empty()) { |
Kevin DuBois | 241d0ee | 2020-06-26 17:00:15 -0700 | [diff] [blame] | 370 | auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end()); |
| 371 | if (mKnownTimestamp) { |
| 372 | mKnownTimestamp = std::max(*mKnownTimestamp, maxRb); |
| 373 | } else { |
| 374 | mKnownTimestamp = maxRb; |
| 375 | } |
| 376 | |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 377 | mTimestamps.clear(); |
| 378 | mLastTimestampIndex = 0; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 382 | bool VSyncPredictor::needsMoreSamples() const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 383 | std::lock_guard lock(mMutex); |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 384 | return mTimestamps.size() < kMinimumSamplesForPrediction; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 387 | void VSyncPredictor::resetModel() { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 388 | std::lock_guard lock(mMutex); |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 389 | mRateMap[mIdealPeriod] = {mIdealPeriod, 0}; |
| 390 | clearTimestamps(); |
| 391 | } |
| 392 | |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 393 | void VSyncPredictor::dump(std::string& result) const { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 394 | std::lock_guard lock(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 395 | StringAppendF(&result, "\tmIdealPeriod=%.2f\n", mIdealPeriod / 1e6f); |
| 396 | StringAppendF(&result, "\tRefresh Rate Map:\n"); |
| 397 | for (const auto& [idealPeriod, periodInterceptTuple] : mRateMap) { |
| 398 | StringAppendF(&result, |
| 399 | "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n", |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 400 | idealPeriod / 1e6f, periodInterceptTuple.slope / 1e6f, |
| 401 | periodInterceptTuple.intercept); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 405 | } // namespace android::scheduler |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 406 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 407 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 62eff35 | 2021-12-06 09:59:41 -0800 | [diff] [blame] | 408 | #pragma clang diagnostic pop // ignored "-Wextra" |