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