blob: 02e12fd942bfb5beccfb063718932fbd33ea7478 [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>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070034#include <utils/Log.h>
35#include <utils/Trace.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070036
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040037#include "RefreshRateSelector.h"
Dominik Laskowski62eff352021-12-06 09:59:41 -080038#include "VSyncPredictor.h"
Ady Abraham0bb6a472020-10-12 10:22:13 -070039
Kevin DuBois1678e2c2019-08-22 12:26:24 -070040namespace android::scheduler {
Dominik Laskowski62eff352021-12-06 09:59:41 -080041
Ady Abraham5e7371c2020-03-24 14:47:24 -070042using base::StringAppendF;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080043
Kevin DuBois1678e2c2019-08-22 12:26:24 -070044static auto constexpr kMaxPercent = 100u;
45
46VSyncPredictor::~VSyncPredictor() = default;
47
48VSyncPredictor::VSyncPredictor(nsecs_t idealPeriod, size_t historySize,
49 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent)
Ady Abrahamd9b9a042023-01-13 11:30:58 -080050 : mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080051 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070052 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
53 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
54 mIdealPeriod(idealPeriod) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080055 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070056}
57
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080058inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
59 if (CC_UNLIKELY(mTraceOn)) {
60 ATRACE_INT64(name, value);
61 }
62}
63
Ady Abrahamd9b9a042023-01-13 11:30:58 -080064inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
65 ATRACE_INT64(name, value);
66}
67
Ady Abraham9c53ee72020-07-22 21:16:18 -070068inline size_t VSyncPredictor::next(size_t i) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080069 return (i + 1) % mTimestamps.size();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070070}
71
72bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080073 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -070074 return true;
75 }
76
Ady Abraham92fa2f42020-02-11 15:33:56 -080077 auto const aValidTimestamp = mTimestamps[mLastTimestampIndex];
Kevin DuBois1678e2c2019-08-22 12:26:24 -070078 auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod;
Ady Abraham99ca3362021-06-17 12:28:46 -070079 if (percent >= kOutlierTolerancePercent &&
80 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
81 return false;
82 }
83
84 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
85 [timestamp](nsecs_t a, nsecs_t b) {
86 return std::abs(timestamp - a) < std::abs(timestamp - b);
87 });
88 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / mIdealPeriod;
89 if (distancePercent < kOutlierTolerancePercent) {
90 // duplicate timestamp
91 return false;
92 }
93 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070094}
95
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080096nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -070097 std::lock_guard lock(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -070098 return mRateMap.find(mIdealPeriod)->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080099}
100
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800101bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700102 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700103
104 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700105 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800106 // don't insert this ts into mTimestamps ringbuffer. If we are still
107 // in the learning phase we should just clear all timestamps and start
108 // over.
109 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700110 // Add the timestamp to mTimestamps before clearing it so we could
111 // update mKnownTimestamp based on the new timestamp.
112 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800113 clearTimestamps();
114 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700115 mKnownTimestamp =
116 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
117 } else {
118 mKnownTimestamp = timestamp;
119 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800120 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700121 }
122
Ady Abraham92fa2f42020-02-11 15:33:56 -0800123 if (mTimestamps.size() != kHistorySize) {
124 mTimestamps.push_back(timestamp);
125 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700126 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800127 mLastTimestampIndex = next(mLastTimestampIndex);
128 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700129 }
130
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800131 traceInt64If("VSP-ts", timestamp);
132
Dominik Laskowski62eff352021-12-06 09:59:41 -0800133 const size_t numSamples = mTimestamps.size();
134 if (numSamples < kMinimumSamplesForPrediction) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700135 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800136 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700137 }
138
139 // This is a 'simple linear regression' calculation of Y over X, with Y being the
140 // vsync timestamps, and X being the ordinal of vsync count.
141 // The calculated slope is the vsync period.
142 // Formula for reference:
143 // Sigma_i: means sum over all timestamps.
144 // mean(variable): statistical mean of variable.
145 // X: snapped ordinal of the timestamp
146 // Y: vsync timestamp
147 //
148 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
149 // slope = -------------------------------------------
150 // Sigma_i ( X_i - mean(X) ) ^ 2
151 //
152 // intercept = mean(Y) - slope * mean(X)
153 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800154 std::vector<nsecs_t> vsyncTS(numSamples);
155 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700156
Dominik Laskowski62eff352021-12-06 09:59:41 -0800157 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
158 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700159 auto it = mRateMap.find(mIdealPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700160 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700161
Dominik Laskowski62eff352021-12-06 09:59:41 -0800162 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
163 // fixed-point arithmetic.
164 constexpr int64_t kScalingFactor = 1000;
165
166 nsecs_t meanTS = 0;
167 nsecs_t meanOrdinal = 0;
168
169 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800170 const auto timestamp = mTimestamps[i] - oldestTS;
171 vsyncTS[i] = timestamp;
172 meanTS += timestamp;
173
Rachel Lee934017e2022-08-10 15:34:14 -0700174 const auto ordinal = currentPeriod == 0
175 ? 0
176 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800177 ordinals[i] = ordinal;
178 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700179 }
180
Dominik Laskowski62eff352021-12-06 09:59:41 -0800181 meanTS /= numSamples;
182 meanOrdinal /= numSamples;
183
184 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700185 vsyncTS[i] -= meanTS;
186 ordinals[i] -= meanOrdinal;
187 }
188
Dominik Laskowski62eff352021-12-06 09:59:41 -0800189 nsecs_t top = 0;
190 nsecs_t bottom = 0;
191 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700192 top += vsyncTS[i] * ordinals[i];
193 bottom += ordinals[i] * ordinals[i];
194 }
195
196 if (CC_UNLIKELY(bottom == 0)) {
197 it->second = {mIdealPeriod, 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800198 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800199 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700200 }
201
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700202 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700203 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
204
Ady Abraham92fa2f42020-02-11 15:33:56 -0800205 auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod;
206 if (percent >= kOutlierTolerancePercent) {
207 it->second = {mIdealPeriod, 0};
208 clearTimestamps();
209 return false;
210 }
211
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800212 traceInt64If("VSP-period", anticipatedPeriod);
213 traceInt64If("VSP-intercept", intercept);
214
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700215 it->second = {anticipatedPeriod, intercept};
216
217 ALOGV("model update ts: %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, timestamp,
218 anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800219 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700220}
221
Ady Abraham0bb6a472020-10-12 10:22:13 -0700222nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const {
223 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700224
Ady Abraham92fa2f42020-02-11 15:33:56 -0800225 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800226 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700227 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
228 auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1;
229 return knownTimestamp + numPeriodsOut * mIdealPeriod;
230 }
231
Ady Abraham92fa2f42020-02-11 15:33:56 -0800232 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800233
234 // See b/145667109, the ordinal calculation must take into account the intercept.
235 auto const zeroPoint = oldest + intercept;
236 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700237 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
238
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800239 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800240 traceInt64If("VSP-timePoint", timePoint);
241 traceInt64If("VSP-prediction", prediction);
242
Kevin DuBois127a2d92019-12-04 13:52:52 -0800243 auto const printer = [&, slope = slope, intercept = intercept] {
244 std::stringstream str;
245 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
246 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
247 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
248 return str.str();
249 };
250
251 ALOGV("%s", printer().c_str());
252 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
253 printer().c_str());
254
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700255 return prediction;
256}
257
Ady Abraham0bb6a472020-10-12 10:22:13 -0700258nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700259 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800260
261 // TODO(b/246164114): This implementation is not efficient at all. Refactor.
262 nsecs_t nextVsync = nextAnticipatedVSyncTimeFromLocked(timePoint);
263 while (!isVSyncInPhaseLocked(nextVsync, mDivisor)) {
264 nextVsync = nextAnticipatedVSyncTimeFromLocked(nextVsync + 1);
265 }
266 return nextVsync;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700267}
268
Ady Abraham0bb6a472020-10-12 10:22:13 -0700269/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700270 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800271 * 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 -0700272 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
273 * isVSyncInPhase(16.6, 30) = true
274 * isVSyncInPhase(33.3, 30) = false
275 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700276 */
Ady Abraham5cc2e262021-03-25 13:09:17 -0700277bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const {
Ady Abrahamace3d052022-11-17 16:25:05 -0800278 std::lock_guard lock(mMutex);
279 const auto divisor =
280 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod), frameRate);
281 return isVSyncInPhaseLocked(timePoint, static_cast<unsigned>(divisor));
282}
283
284bool VSyncPredictor::isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700285 struct VsyncError {
286 nsecs_t vsyncTimestamp;
287 float error;
288
289 bool operator<(const VsyncError& other) const { return error < other.error; }
290 };
291
Ady Abrahamcc315492022-02-17 17:06:39 -0800292 if (divisor <= 1 || timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700293 return true;
294 }
295
296 const nsecs_t period = mRateMap[mIdealPeriod].slope;
297 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abrahamcc315492022-02-17 17:06:39 -0800298 const nsecs_t dividedPeriod = mIdealPeriod / divisor;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700299
Ady Abrahamcc315492022-02-17 17:06:39 -0800300 // If this is the first time we have asked about this divisor with the
Ady Abraham0bb6a472020-10-12 10:22:13 -0700301 // current vsync period, it is considered in phase and we store the closest
302 // vsync timestamp
Ady Abrahamcc315492022-02-17 17:06:39 -0800303 const auto knownTimestampIter = mRateDivisorKnownTimestampMap.find(dividedPeriod);
304 if (knownTimestampIter == mRateDivisorKnownTimestampMap.end()) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700305 const auto vsync = nextAnticipatedVSyncTimeFromLocked(justBeforeTimePoint);
Ady Abrahamcc315492022-02-17 17:06:39 -0800306 mRateDivisorKnownTimestampMap[dividedPeriod] = vsync;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700307 return true;
308 }
309
Ady Abrahamcc315492022-02-17 17:06:39 -0800310 // Find the next N vsync timestamp where N is the divisor.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700311 // One of these vsyncs will be in phase. We return the one which is
312 // the most aligned with the last known in phase vsync
Ady Abrahamcc315492022-02-17 17:06:39 -0800313 std::vector<VsyncError> vsyncs(static_cast<size_t>(divisor));
Ady Abraham0bb6a472020-10-12 10:22:13 -0700314 const nsecs_t knownVsync = knownTimestampIter->second;
315 nsecs_t point = justBeforeTimePoint;
Ady Abrahamcc315492022-02-17 17:06:39 -0800316 for (size_t i = 0; i < divisor; i++) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700317 const nsecs_t vsync = nextAnticipatedVSyncTimeFromLocked(point);
Ady Abrahamcc315492022-02-17 17:06:39 -0800318 const auto numPeriods = static_cast<float>(vsync - knownVsync) / (period * divisor);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700319 const auto error = std::abs(std::round(numPeriods) - numPeriods);
320 vsyncs[i] = {vsync, error};
321 point = vsync + 1;
322 }
323
324 const auto minVsyncError = std::min_element(vsyncs.begin(), vsyncs.end());
Ady Abrahamcc315492022-02-17 17:06:39 -0800325 mRateDivisorKnownTimestampMap[dividedPeriod] = minVsyncError->vsyncTimestamp;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700326 return std::abs(minVsyncError->vsyncTimestamp - timePoint) < period / 2;
327}
328
Ady Abrahamace3d052022-11-17 16:25:05 -0800329void VSyncPredictor::setDivisor(unsigned divisor) {
330 ALOGV("%s: %d", __func__, divisor);
331 std::lock_guard lock(mMutex);
332 mDivisor = divisor;
333}
334
Ady Abraham0bb6a472020-10-12 10:22:13 -0700335VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
336 std::lock_guard lock(mMutex);
337 const auto model = VSyncPredictor::getVSyncPredictionModelLocked();
338 return {model.slope, model.intercept};
339}
340
341VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700342 return mRateMap.find(mIdealPeriod)->second;
343}
344
345void VSyncPredictor::setPeriod(nsecs_t period) {
346 ATRACE_CALL();
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800347 traceInt64("VSP-setPeriod", period);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700348
Ady Abraham9c53ee72020-07-22 21:16:18 -0700349 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700350 static constexpr size_t kSizeLimit = 30;
351 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
352 mRateMap.erase(mRateMap.begin());
353 }
354
355 mIdealPeriod = period;
356 if (mRateMap.find(period) == mRateMap.end()) {
357 mRateMap[mIdealPeriod] = {period, 0};
358 }
359
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800360 clearTimestamps();
361}
362
363void VSyncPredictor::clearTimestamps() {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800364 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700365 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
366 if (mKnownTimestamp) {
367 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
368 } else {
369 mKnownTimestamp = maxRb;
370 }
371
Ady Abraham92fa2f42020-02-11 15:33:56 -0800372 mTimestamps.clear();
373 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700374 }
375}
376
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700377bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700378 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700379 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700380}
381
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800382void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700383 std::lock_guard lock(mMutex);
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800384 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
385 clearTimestamps();
386}
387
Ady Abraham5e7371c2020-03-24 14:47:24 -0700388void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700389 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700390 StringAppendF(&result, "\tmIdealPeriod=%.2f\n", mIdealPeriod / 1e6f);
391 StringAppendF(&result, "\tRefresh Rate Map:\n");
392 for (const auto& [idealPeriod, periodInterceptTuple] : mRateMap) {
393 StringAppendF(&result,
394 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abraham0bb6a472020-10-12 10:22:13 -0700395 idealPeriod / 1e6f, periodInterceptTuple.slope / 1e6f,
396 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700397 }
398}
399
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700400} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800401
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100402// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800403#pragma clang diagnostic pop // ignored "-Wextra"