blob: a3b8a5619d1c895b1b839fd09186f8355f3ca10d [file] [log] [blame]
Kevin DuBois1678e2c2019-08-22 12:26:24 -07001/*
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 Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Dominik Laskowski62eff352021-12-06 09:59:41 -080021#undef LOG_TAG
22#define LOG_TAG "VSyncPredictor"
23
Kevin DuBois1678e2c2019-08-22 12:26:24 -070024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Dominik Laskowski62eff352021-12-06 09:59:41 -080025
26#include <algorithm>
27#include <chrono>
28#include <sstream>
29
Kevin DuBois1678e2c2019-08-22 12:26:24 -070030#include <android-base/logging.h>
Ady Abraham5e7371c2020-03-24 14:47:24 -070031#include <android-base/stringprintf.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070032#include <cutils/compiler.h>
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080033#include <cutils/properties.h>
Leon Scroggins III31d41412022-11-18 16:42:53 -050034#include <gui/TraceUtils.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070035#include <utils/Log.h>
36#include <utils/Trace.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070037
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040038#include "RefreshRateSelector.h"
Dominik Laskowski62eff352021-12-06 09:59:41 -080039#include "VSyncPredictor.h"
Ady Abraham0bb6a472020-10-12 10:22:13 -070040
Kevin DuBois1678e2c2019-08-22 12:26:24 -070041namespace android::scheduler {
Dominik Laskowski62eff352021-12-06 09:59:41 -080042
Ady Abraham5e7371c2020-03-24 14:47:24 -070043using base::StringAppendF;
Leon Scroggins III31d41412022-11-18 16:42:53 -050044using base::StringPrintf;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080045
Kevin DuBois1678e2c2019-08-22 12:26:24 -070046static auto constexpr kMaxPercent = 100u;
47
48VSyncPredictor::~VSyncPredictor() = default;
49
Leon Scroggins III31d41412022-11-18 16:42:53 -050050VSyncPredictor::VSyncPredictor(std::string name, nsecs_t idealPeriod, size_t historySize,
Kevin DuBois1678e2c2019-08-22 12:26:24 -070051 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent)
Leon Scroggins III31d41412022-11-18 16:42:53 -050052 : mName(name),
53 mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080054 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070055 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
56 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
57 mIdealPeriod(idealPeriod) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080058 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070059}
60
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080061inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
62 if (CC_UNLIKELY(mTraceOn)) {
Leon Scroggins III31d41412022-11-18 16:42:53 -050063 traceInt64(name, value);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080064 }
65}
66
Ady Abrahamd9b9a042023-01-13 11:30:58 -080067inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
Leon Scroggins III31d41412022-11-18 16:42:53 -050068 // TODO (b/266817103): Pass in PhysicalDisplayId and use ftl::Concat to
69 // avoid unnecessary allocations.
70 ATRACE_INT64(StringPrintf("%s %s", name, mName.c_str()).c_str(), value);
Ady Abrahamd9b9a042023-01-13 11:30:58 -080071}
72
Ady Abraham9c53ee72020-07-22 21:16:18 -070073inline size_t VSyncPredictor::next(size_t i) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080074 return (i + 1) % mTimestamps.size();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070075}
76
77bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080078 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -070079 return true;
80 }
81
Ady Abraham92fa2f42020-02-11 15:33:56 -080082 auto const aValidTimestamp = mTimestamps[mLastTimestampIndex];
Kevin DuBois1678e2c2019-08-22 12:26:24 -070083 auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod;
Ady Abraham99ca3362021-06-17 12:28:46 -070084 if (percent >= kOutlierTolerancePercent &&
85 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
86 return false;
87 }
88
89 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
90 [timestamp](nsecs_t a, nsecs_t b) {
91 return std::abs(timestamp - a) < std::abs(timestamp - b);
92 });
93 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / mIdealPeriod;
94 if (distancePercent < kOutlierTolerancePercent) {
95 // duplicate timestamp
96 return false;
97 }
98 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070099}
100
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800101nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 std::lock_guard lock(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700103 return mRateMap.find(mIdealPeriod)->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800104}
105
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800106bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700108
109 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700110 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800111 // don't insert this ts into mTimestamps ringbuffer. If we are still
112 // in the learning phase we should just clear all timestamps and start
113 // over.
114 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700115 // Add the timestamp to mTimestamps before clearing it so we could
116 // update mKnownTimestamp based on the new timestamp.
117 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800118 clearTimestamps();
119 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700120 mKnownTimestamp =
121 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
122 } else {
123 mKnownTimestamp = timestamp;
124 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800125 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700126 }
127
Ady Abraham92fa2f42020-02-11 15:33:56 -0800128 if (mTimestamps.size() != kHistorySize) {
129 mTimestamps.push_back(timestamp);
130 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700131 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800132 mLastTimestampIndex = next(mLastTimestampIndex);
133 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700134 }
135
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800136 traceInt64If("VSP-ts", timestamp);
137
Dominik Laskowski62eff352021-12-06 09:59:41 -0800138 const size_t numSamples = mTimestamps.size();
139 if (numSamples < kMinimumSamplesForPrediction) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700140 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800141 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700142 }
143
144 // This is a 'simple linear regression' calculation of Y over X, with Y being the
145 // vsync timestamps, and X being the ordinal of vsync count.
146 // The calculated slope is the vsync period.
147 // Formula for reference:
148 // Sigma_i: means sum over all timestamps.
149 // mean(variable): statistical mean of variable.
150 // X: snapped ordinal of the timestamp
151 // Y: vsync timestamp
152 //
153 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
154 // slope = -------------------------------------------
155 // Sigma_i ( X_i - mean(X) ) ^ 2
156 //
157 // intercept = mean(Y) - slope * mean(X)
158 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800159 std::vector<nsecs_t> vsyncTS(numSamples);
160 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700161
Dominik Laskowski62eff352021-12-06 09:59:41 -0800162 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
163 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700164 auto it = mRateMap.find(mIdealPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700165 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700166
Dominik Laskowski62eff352021-12-06 09:59:41 -0800167 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
168 // fixed-point arithmetic.
169 constexpr int64_t kScalingFactor = 1000;
170
171 nsecs_t meanTS = 0;
172 nsecs_t meanOrdinal = 0;
173
174 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800175 const auto timestamp = mTimestamps[i] - oldestTS;
176 vsyncTS[i] = timestamp;
177 meanTS += timestamp;
178
Rachel Lee934017e2022-08-10 15:34:14 -0700179 const auto ordinal = currentPeriod == 0
180 ? 0
181 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800182 ordinals[i] = ordinal;
183 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700184 }
185
Dominik Laskowski62eff352021-12-06 09:59:41 -0800186 meanTS /= numSamples;
187 meanOrdinal /= numSamples;
188
189 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700190 vsyncTS[i] -= meanTS;
191 ordinals[i] -= meanOrdinal;
192 }
193
Dominik Laskowski62eff352021-12-06 09:59:41 -0800194 nsecs_t top = 0;
195 nsecs_t bottom = 0;
196 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700197 top += vsyncTS[i] * ordinals[i];
198 bottom += ordinals[i] * ordinals[i];
199 }
200
201 if (CC_UNLIKELY(bottom == 0)) {
202 it->second = {mIdealPeriod, 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800203 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800204 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700205 }
206
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700207 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700208 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
209
Ady Abraham92fa2f42020-02-11 15:33:56 -0800210 auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod;
211 if (percent >= kOutlierTolerancePercent) {
212 it->second = {mIdealPeriod, 0};
213 clearTimestamps();
214 return false;
215 }
216
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800217 traceInt64If("VSP-period", anticipatedPeriod);
218 traceInt64If("VSP-intercept", intercept);
219
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700220 it->second = {anticipatedPeriod, intercept};
221
Leon Scroggins III31d41412022-11-18 16:42:53 -0500222 ALOGV("model update ts %s: %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, mName.c_str(),
223 timestamp, anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800224 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700225}
226
Ady Abraham0bb6a472020-10-12 10:22:13 -0700227nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const {
228 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700229
Ady Abraham92fa2f42020-02-11 15:33:56 -0800230 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800231 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700232 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
233 auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1;
234 return knownTimestamp + numPeriodsOut * mIdealPeriod;
235 }
236
Ady Abraham92fa2f42020-02-11 15:33:56 -0800237 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800238
239 // See b/145667109, the ordinal calculation must take into account the intercept.
240 auto const zeroPoint = oldest + intercept;
241 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700242 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
243
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800244 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800245 traceInt64If("VSP-timePoint", timePoint);
246 traceInt64If("VSP-prediction", prediction);
247
Kevin DuBois127a2d92019-12-04 13:52:52 -0800248 auto const printer = [&, slope = slope, intercept = intercept] {
249 std::stringstream str;
250 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
251 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
252 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
253 return str.str();
254 };
255
256 ALOGV("%s", printer().c_str());
257 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
258 printer().c_str());
259
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700260 return prediction;
261}
262
Ady Abraham0bb6a472020-10-12 10:22:13 -0700263nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700264 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800265
266 // TODO(b/246164114): This implementation is not efficient at all. Refactor.
267 nsecs_t nextVsync = nextAnticipatedVSyncTimeFromLocked(timePoint);
268 while (!isVSyncInPhaseLocked(nextVsync, mDivisor)) {
269 nextVsync = nextAnticipatedVSyncTimeFromLocked(nextVsync + 1);
270 }
271 return nextVsync;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700272}
273
Ady Abraham0bb6a472020-10-12 10:22:13 -0700274/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700275 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800276 * If the frame rate is not a divisor of the refresh rate, it is always considered in phase.
Ady Abraham5cc2e262021-03-25 13:09:17 -0700277 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
278 * isVSyncInPhase(16.6, 30) = true
279 * isVSyncInPhase(33.3, 30) = false
280 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700281 */
Ady Abraham5cc2e262021-03-25 13:09:17 -0700282bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const {
Ady Abrahamace3d052022-11-17 16:25:05 -0800283 std::lock_guard lock(mMutex);
284 const auto divisor =
285 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod), frameRate);
286 return isVSyncInPhaseLocked(timePoint, static_cast<unsigned>(divisor));
287}
288
289bool VSyncPredictor::isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700290 struct VsyncError {
291 nsecs_t vsyncTimestamp;
292 float error;
293
294 bool operator<(const VsyncError& other) const { return error < other.error; }
295 };
296
Ady Abrahamcc315492022-02-17 17:06:39 -0800297 if (divisor <= 1 || timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700298 return true;
299 }
300
301 const nsecs_t period = mRateMap[mIdealPeriod].slope;
302 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abrahamcc315492022-02-17 17:06:39 -0800303 const nsecs_t dividedPeriod = mIdealPeriod / divisor;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700304
Ady Abrahamcc315492022-02-17 17:06:39 -0800305 // If this is the first time we have asked about this divisor with the
Ady Abraham0bb6a472020-10-12 10:22:13 -0700306 // current vsync period, it is considered in phase and we store the closest
307 // vsync timestamp
Ady Abrahamcc315492022-02-17 17:06:39 -0800308 const auto knownTimestampIter = mRateDivisorKnownTimestampMap.find(dividedPeriod);
309 if (knownTimestampIter == mRateDivisorKnownTimestampMap.end()) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700310 const auto vsync = nextAnticipatedVSyncTimeFromLocked(justBeforeTimePoint);
Ady Abrahamcc315492022-02-17 17:06:39 -0800311 mRateDivisorKnownTimestampMap[dividedPeriod] = vsync;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700312 return true;
313 }
314
Ady Abrahamcc315492022-02-17 17:06:39 -0800315 // Find the next N vsync timestamp where N is the divisor.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700316 // One of these vsyncs will be in phase. We return the one which is
317 // the most aligned with the last known in phase vsync
Ady Abrahamcc315492022-02-17 17:06:39 -0800318 std::vector<VsyncError> vsyncs(static_cast<size_t>(divisor));
Ady Abraham0bb6a472020-10-12 10:22:13 -0700319 const nsecs_t knownVsync = knownTimestampIter->second;
320 nsecs_t point = justBeforeTimePoint;
Ady Abrahamcc315492022-02-17 17:06:39 -0800321 for (size_t i = 0; i < divisor; i++) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700322 const nsecs_t vsync = nextAnticipatedVSyncTimeFromLocked(point);
Ady Abrahamcc315492022-02-17 17:06:39 -0800323 const auto numPeriods = static_cast<float>(vsync - knownVsync) / (period * divisor);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700324 const auto error = std::abs(std::round(numPeriods) - numPeriods);
325 vsyncs[i] = {vsync, error};
326 point = vsync + 1;
327 }
328
329 const auto minVsyncError = std::min_element(vsyncs.begin(), vsyncs.end());
Ady Abrahamcc315492022-02-17 17:06:39 -0800330 mRateDivisorKnownTimestampMap[dividedPeriod] = minVsyncError->vsyncTimestamp;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700331 return std::abs(minVsyncError->vsyncTimestamp - timePoint) < period / 2;
332}
333
Ady Abrahamace3d052022-11-17 16:25:05 -0800334void VSyncPredictor::setDivisor(unsigned divisor) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500335 ALOGV("%s %s: %d", __func__, mName.c_str(), divisor);
Ady Abrahamace3d052022-11-17 16:25:05 -0800336 std::lock_guard lock(mMutex);
337 mDivisor = divisor;
338}
339
Ady Abraham0bb6a472020-10-12 10:22:13 -0700340VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
341 std::lock_guard lock(mMutex);
342 const auto model = VSyncPredictor::getVSyncPredictionModelLocked();
343 return {model.slope, model.intercept};
344}
345
346VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700347 return mRateMap.find(mIdealPeriod)->second;
348}
349
350void VSyncPredictor::setPeriod(nsecs_t period) {
Leon Scroggins III31d41412022-11-18 16:42:53 -0500351 ATRACE_FORMAT("%s %s", __func__, mName.c_str());
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800352 traceInt64("VSP-setPeriod", period);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700353
Ady Abraham9c53ee72020-07-22 21:16:18 -0700354 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700355 static constexpr size_t kSizeLimit = 30;
356 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
357 mRateMap.erase(mRateMap.begin());
358 }
359
360 mIdealPeriod = period;
361 if (mRateMap.find(period) == mRateMap.end()) {
362 mRateMap[mIdealPeriod] = {period, 0};
363 }
364
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800365 clearTimestamps();
366}
367
368void VSyncPredictor::clearTimestamps() {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800369 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700370 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
371 if (mKnownTimestamp) {
372 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
373 } else {
374 mKnownTimestamp = maxRb;
375 }
376
Ady Abraham92fa2f42020-02-11 15:33:56 -0800377 mTimestamps.clear();
378 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700379 }
380}
381
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700382bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700383 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700384 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700385}
386
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800387void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700388 std::lock_guard lock(mMutex);
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800389 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
390 clearTimestamps();
391}
392
Ady Abraham5e7371c2020-03-24 14:47:24 -0700393void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700394 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700395 StringAppendF(&result, "\tmIdealPeriod=%.2f\n", mIdealPeriod / 1e6f);
396 StringAppendF(&result, "\tRefresh Rate Map:\n");
397 for (const auto& [idealPeriod, periodInterceptTuple] : mRateMap) {
398 StringAppendF(&result,
399 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abraham0bb6a472020-10-12 10:22:13 -0700400 idealPeriod / 1e6f, periodInterceptTuple.slope / 1e6f,
401 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700402 }
403}
404
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700405} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800406
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100407// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800408#pragma clang diagnostic pop // ignored "-Wextra"