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