blob: e969fdc6799e13ef8b32bbf0a918116bac7a06a9 [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 III67388622023-02-06 20:36:20 -050034#include <ftl/concat.h>
Ady Abraham9243bba2023-02-10 15:31:14 -080035#include <gui/TraceUtils.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070036#include <utils/Log.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;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080044
Kevin DuBois1678e2c2019-08-22 12:26:24 -070045static auto constexpr kMaxPercent = 100u;
46
47VSyncPredictor::~VSyncPredictor() = default;
48
Leon Scroggins III67388622023-02-06 20:36:20 -050049VSyncPredictor::VSyncPredictor(PhysicalDisplayId id, nsecs_t idealPeriod, size_t historySize,
Kevin DuBois1678e2c2019-08-22 12:26:24 -070050 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent)
Leon Scroggins III67388622023-02-06 20:36:20 -050051 : mId(id),
52 mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080053 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070054 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
55 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
56 mIdealPeriod(idealPeriod) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080057 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070058}
59
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080060inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
61 if (CC_UNLIKELY(mTraceOn)) {
Leon Scroggins III67388622023-02-06 20:36:20 -050062 traceInt64(name, value);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080063 }
64}
65
Ady Abrahamd9b9a042023-01-13 11:30:58 -080066inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
Leon Scroggins III67388622023-02-06 20:36:20 -050067 ATRACE_INT64(ftl::Concat(ftl::truncated<14>(name), " ", mId.value).c_str(), value);
Ady Abrahamd9b9a042023-01-13 11:30:58 -080068}
69
Ady Abraham9c53ee72020-07-22 21:16:18 -070070inline size_t VSyncPredictor::next(size_t i) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080071 return (i + 1) % mTimestamps.size();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070072}
73
74bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080075 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -070076 return true;
77 }
78
Ady Abraham92fa2f42020-02-11 15:33:56 -080079 auto const aValidTimestamp = mTimestamps[mLastTimestampIndex];
Kevin DuBois1678e2c2019-08-22 12:26:24 -070080 auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod;
Ady Abraham99ca3362021-06-17 12:28:46 -070081 if (percent >= kOutlierTolerancePercent &&
82 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
83 return false;
84 }
85
86 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
87 [timestamp](nsecs_t a, nsecs_t b) {
88 return std::abs(timestamp - a) < std::abs(timestamp - b);
89 });
90 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / mIdealPeriod;
91 if (distancePercent < kOutlierTolerancePercent) {
92 // duplicate timestamp
93 return false;
94 }
95 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -070096}
97
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080098nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -070099 std::lock_guard lock(mMutex);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700100 return mRateMap.find(mIdealPeriod)->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800101}
102
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800103bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700104 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700105
106 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700107 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800108 // don't insert this ts into mTimestamps ringbuffer. If we are still
109 // in the learning phase we should just clear all timestamps and start
110 // over.
111 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700112 // Add the timestamp to mTimestamps before clearing it so we could
113 // update mKnownTimestamp based on the new timestamp.
114 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800115 clearTimestamps();
116 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700117 mKnownTimestamp =
118 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
119 } else {
120 mKnownTimestamp = timestamp;
121 }
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800122 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700123 }
124
Ady Abraham92fa2f42020-02-11 15:33:56 -0800125 if (mTimestamps.size() != kHistorySize) {
126 mTimestamps.push_back(timestamp);
127 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700128 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800129 mLastTimestampIndex = next(mLastTimestampIndex);
130 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700131 }
132
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800133 traceInt64If("VSP-ts", timestamp);
134
Dominik Laskowski62eff352021-12-06 09:59:41 -0800135 const size_t numSamples = mTimestamps.size();
136 if (numSamples < kMinimumSamplesForPrediction) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700137 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800138 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700139 }
140
141 // This is a 'simple linear regression' calculation of Y over X, with Y being the
142 // vsync timestamps, and X being the ordinal of vsync count.
143 // The calculated slope is the vsync period.
144 // Formula for reference:
145 // Sigma_i: means sum over all timestamps.
146 // mean(variable): statistical mean of variable.
147 // X: snapped ordinal of the timestamp
148 // Y: vsync timestamp
149 //
150 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
151 // slope = -------------------------------------------
152 // Sigma_i ( X_i - mean(X) ) ^ 2
153 //
154 // intercept = mean(Y) - slope * mean(X)
155 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800156 std::vector<nsecs_t> vsyncTS(numSamples);
157 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700158
Dominik Laskowski62eff352021-12-06 09:59:41 -0800159 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
160 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700161 auto it = mRateMap.find(mIdealPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700162 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700163
Dominik Laskowski62eff352021-12-06 09:59:41 -0800164 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
165 // fixed-point arithmetic.
166 constexpr int64_t kScalingFactor = 1000;
167
168 nsecs_t meanTS = 0;
169 nsecs_t meanOrdinal = 0;
170
171 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800172 const auto timestamp = mTimestamps[i] - oldestTS;
173 vsyncTS[i] = timestamp;
174 meanTS += timestamp;
175
Rachel Lee934017e2022-08-10 15:34:14 -0700176 const auto ordinal = currentPeriod == 0
177 ? 0
178 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800179 ordinals[i] = ordinal;
180 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700181 }
182
Dominik Laskowski62eff352021-12-06 09:59:41 -0800183 meanTS /= numSamples;
184 meanOrdinal /= numSamples;
185
186 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700187 vsyncTS[i] -= meanTS;
188 ordinals[i] -= meanOrdinal;
189 }
190
Dominik Laskowski62eff352021-12-06 09:59:41 -0800191 nsecs_t top = 0;
192 nsecs_t bottom = 0;
193 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700194 top += vsyncTS[i] * ordinals[i];
195 bottom += ordinals[i] * ordinals[i];
196 }
197
198 if (CC_UNLIKELY(bottom == 0)) {
199 it->second = {mIdealPeriod, 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800200 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800201 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700202 }
203
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700204 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700205 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
206
Ady Abraham92fa2f42020-02-11 15:33:56 -0800207 auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod;
208 if (percent >= kOutlierTolerancePercent) {
209 it->second = {mIdealPeriod, 0};
210 clearTimestamps();
211 return false;
212 }
213
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800214 traceInt64If("VSP-period", anticipatedPeriod);
215 traceInt64If("VSP-intercept", intercept);
216
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700217 it->second = {anticipatedPeriod, intercept};
218
Leon Scroggins III67388622023-02-06 20:36:20 -0500219 ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64,
220 mId.value, timestamp, anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800221 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700222}
223
Ady Abrahamf34a8132023-02-13 20:49:48 -0800224auto VSyncPredictor::getVsyncSequenceLocked(nsecs_t timestamp) const -> VsyncSequence {
225 const auto vsync = nextAnticipatedVSyncTimeFromLocked(timestamp);
226 if (!mLastVsyncSequence) return {vsync, 0};
227
228 const auto [slope, _] = getVSyncPredictionModelLocked();
229 const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence;
230 const auto vsyncSequence = lastVsyncSequence +
231 static_cast<int64_t>(std::round((vsync - lastVsyncTime) / static_cast<float>(slope)));
232 return {vsync, vsyncSequence};
233}
234
Ady Abraham0bb6a472020-10-12 10:22:13 -0700235nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const {
236 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700237
Ady Abraham92fa2f42020-02-11 15:33:56 -0800238 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800239 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700240 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
241 auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1;
242 return knownTimestamp + numPeriodsOut * mIdealPeriod;
243 }
244
Ady Abraham92fa2f42020-02-11 15:33:56 -0800245 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800246
247 // See b/145667109, the ordinal calculation must take into account the intercept.
248 auto const zeroPoint = oldest + intercept;
249 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700250 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
251
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800252 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800253 traceInt64If("VSP-timePoint", timePoint);
254 traceInt64If("VSP-prediction", prediction);
255
Kevin DuBois127a2d92019-12-04 13:52:52 -0800256 auto const printer = [&, slope = slope, intercept = intercept] {
257 std::stringstream str;
258 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
259 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
260 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
261 return str.str();
262 };
263
264 ALOGV("%s", printer().c_str());
265 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
266 printer().c_str());
267
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700268 return prediction;
269}
270
Ady Abraham0bb6a472020-10-12 10:22:13 -0700271nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700272 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800273
Ady Abrahamf34a8132023-02-13 20:49:48 -0800274 // update the mLastVsyncSequence for reference point
275 mLastVsyncSequence = getVsyncSequenceLocked(timePoint);
276
Ady Abrahamfdc049c2023-02-17 14:52:05 +0000277 const auto renderRatePhase = [&]() REQUIRES(mMutex) -> int {
278 if (!mRenderRate) return 0;
279
280 const auto divisor =
281 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod),
282 *mRenderRate);
283 if (divisor <= 1) return 0;
284
285 const int mod = mLastVsyncSequence->seq % divisor;
286 if (mod == 0) return 0;
287
288 return divisor - mod;
289 }();
290
291 if (renderRatePhase == 0) {
Ady Abrahamf34a8132023-02-13 20:49:48 -0800292 return mLastVsyncSequence->vsyncTime;
Ady Abrahamace3d052022-11-17 16:25:05 -0800293 }
Ady Abrahamf34a8132023-02-13 20:49:48 -0800294
295 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Ady Abrahamfdc049c2023-02-17 14:52:05 +0000296 const auto approximateNextVsync = mLastVsyncSequence->vsyncTime + slope * renderRatePhase;
Ady Abrahamf34a8132023-02-13 20:49:48 -0800297 return nextAnticipatedVSyncTimeFromLocked(approximateNextVsync - slope / 2);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700298}
299
Ady Abraham0bb6a472020-10-12 10:22:13 -0700300/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700301 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800302 * 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 -0700303 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
304 * isVSyncInPhase(16.6, 30) = true
305 * isVSyncInPhase(33.3, 30) = false
306 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700307 */
Ady Abraham5cc2e262021-03-25 13:09:17 -0700308bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const {
Ady Abrahamace3d052022-11-17 16:25:05 -0800309 std::lock_guard lock(mMutex);
310 const auto divisor =
311 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod), frameRate);
312 return isVSyncInPhaseLocked(timePoint, static_cast<unsigned>(divisor));
313}
314
315bool VSyncPredictor::isVSyncInPhaseLocked(nsecs_t timePoint, unsigned divisor) const {
Ady Abraham9243bba2023-02-10 15:31:14 -0800316 const TimePoint now = TimePoint::now();
317 const auto getTimePointIn = [](TimePoint now, nsecs_t timePoint) -> float {
318 return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now);
319 };
320 ATRACE_FORMAT("%s timePoint in: %.2f divisor: %zu", __func__, getTimePointIn(now, timePoint),
321 divisor);
322
Ady Abrahamcc315492022-02-17 17:06:39 -0800323 if (divisor <= 1 || timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700324 return true;
325 }
326
327 const nsecs_t period = mRateMap[mIdealPeriod].slope;
328 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abrahamf34a8132023-02-13 20:49:48 -0800329 const auto vsyncSequence = getVsyncSequenceLocked(justBeforeTimePoint);
330 ATRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64,
331 getTimePointIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq);
332 return vsyncSequence.seq % divisor == 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700333}
334
Ady Abrahamfdc049c2023-02-17 14:52:05 +0000335void VSyncPredictor::setRenderRate(Fps fps) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500336 ALOGV("%s %s: %s", __func__, to_string(mId).c_str(), to_string(fps).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800337 std::lock_guard lock(mMutex);
Ady Abrahamfdc049c2023-02-17 14:52:05 +0000338 mRenderRate = fps;
Ady Abrahamace3d052022-11-17 16:25:05 -0800339}
340
Ady Abraham0bb6a472020-10-12 10:22:13 -0700341VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
342 std::lock_guard lock(mMutex);
343 const auto model = VSyncPredictor::getVSyncPredictionModelLocked();
344 return {model.slope, model.intercept};
345}
346
347VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700348 return mRateMap.find(mIdealPeriod)->second;
349}
350
351void VSyncPredictor::setPeriod(nsecs_t period) {
Leon Scroggins III67388622023-02-06 20:36:20 -0500352 ATRACE_FORMAT("%s %s", __func__, to_string(mId).c_str());
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800353 traceInt64("VSP-setPeriod", period);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700354
Ady Abraham9c53ee72020-07-22 21:16:18 -0700355 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700356 static constexpr size_t kSizeLimit = 30;
357 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
358 mRateMap.erase(mRateMap.begin());
359 }
360
361 mIdealPeriod = period;
362 if (mRateMap.find(period) == mRateMap.end()) {
363 mRateMap[mIdealPeriod] = {period, 0};
364 }
365
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800366 clearTimestamps();
367}
368
369void VSyncPredictor::clearTimestamps() {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800370 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700371 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
372 if (mKnownTimestamp) {
373 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
374 } else {
375 mKnownTimestamp = maxRb;
376 }
377
Ady Abraham92fa2f42020-02-11 15:33:56 -0800378 mTimestamps.clear();
379 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700380 }
381}
382
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700383bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700384 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700385 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700386}
387
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800388void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700389 std::lock_guard lock(mMutex);
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800390 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
391 clearTimestamps();
392}
393
Ady Abraham5e7371c2020-03-24 14:47:24 -0700394void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700395 std::lock_guard lock(mMutex);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700396 StringAppendF(&result, "\tmIdealPeriod=%.2f\n", mIdealPeriod / 1e6f);
397 StringAppendF(&result, "\tRefresh Rate Map:\n");
398 for (const auto& [idealPeriod, periodInterceptTuple] : mRateMap) {
399 StringAppendF(&result,
400 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abraham0bb6a472020-10-12 10:22:13 -0700401 idealPeriod / 1e6f, periodInterceptTuple.slope / 1e6f,
402 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700403 }
404}
405
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700406} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800407
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100408// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800409#pragma clang diagnostic pop // ignored "-Wextra"