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