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