blob: 82a638d9425e0cef4a6af8583ec74b25e0f3eb12 [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>
Alec Mouri9b133ca2023-11-14 19:00:01 +000032#include <common/FlagManager.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000033#include <common/trace.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070034#include <cutils/compiler.h>
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080035#include <cutils/properties.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050036#include <ftl/concat.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070037#include <utils/Log.h>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070038
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040039#include "RefreshRateSelector.h"
Dominik Laskowski62eff352021-12-06 09:59:41 -080040#include "VSyncPredictor.h"
Ady Abraham0bb6a472020-10-12 10:22:13 -070041
Kevin DuBois1678e2c2019-08-22 12:26:24 -070042namespace android::scheduler {
Dominik Laskowski62eff352021-12-06 09:59:41 -080043
Ady Abraham5e7371c2020-03-24 14:47:24 -070044using base::StringAppendF;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080045
Kevin DuBois1678e2c2019-08-22 12:26:24 -070046static auto constexpr kMaxPercent = 100u;
47
Ady Abraham940b7a62024-03-07 10:04:27 -080048namespace {
49int numVsyncsPerFrame(const ftl::NonNull<DisplayModePtr>& displayModePtr) {
50 const auto idealPeakRefreshPeriod = displayModePtr->getPeakFps().getPeriodNsecs();
51 const auto idealRefreshPeriod = displayModePtr->getVsyncRate().getPeriodNsecs();
52 return static_cast<int>(std::round(static_cast<float>(idealPeakRefreshPeriod) /
53 static_cast<float>(idealRefreshPeriod)));
54}
55} // namespace
56
Kevin DuBois1678e2c2019-08-22 12:26:24 -070057VSyncPredictor::~VSyncPredictor() = default;
58
Ady Abraham20024aa2024-03-05 01:32:49 +000059VSyncPredictor::VSyncPredictor(std::unique_ptr<Clock> clock, ftl::NonNull<DisplayModePtr> modePtr,
60 size_t historySize, size_t minimumSamplesForPrediction,
61 uint32_t outlierTolerancePercent)
62 : mClock(std::move(clock)),
63 mId(modePtr->getPhysicalDisplayId()),
Leon Scroggins III67388622023-02-06 20:36:20 -050064 mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080065 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070066 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
67 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
Ady Abraham940b7a62024-03-07 10:04:27 -080068 mDisplayModePtr(modePtr),
69 mNumVsyncsForFrame(numVsyncsPerFrame(mDisplayModePtr)) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080070 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070071}
72
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080073inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
74 if (CC_UNLIKELY(mTraceOn)) {
Leon Scroggins III67388622023-02-06 20:36:20 -050075 traceInt64(name, value);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080076 }
77}
78
Ady Abrahamd9b9a042023-01-13 11:30:58 -080079inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000080 SFTRACE_INT64(ftl::Concat(ftl::truncated<14>(name), " ", mId.value).c_str(), value);
Ady Abrahamd9b9a042023-01-13 11:30:58 -080081}
82
Ady Abraham9c53ee72020-07-22 21:16:18 -070083inline size_t VSyncPredictor::next(size_t i) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080084 return (i + 1) % mTimestamps.size();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070085}
86
Ady Abrahamc585dba2023-11-15 18:41:35 -080087nsecs_t VSyncPredictor::idealPeriod() const {
88 return mDisplayModePtr->getVsyncRate().getPeriodNsecs();
89}
90
Kevin DuBois1678e2c2019-08-22 12:26:24 -070091bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080092 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -070093 return true;
94 }
95
Ady Abrahamc585dba2023-11-15 18:41:35 -080096 const auto aValidTimestamp = mTimestamps[mLastTimestampIndex];
97 const auto percent =
98 (timestamp - aValidTimestamp) % idealPeriod() * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -070099 if (percent >= kOutlierTolerancePercent &&
100 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000101 SFTRACE_FORMAT_INSTANT("timestamp is not aligned with model");
Ady Abraham99ca3362021-06-17 12:28:46 -0700102 return false;
103 }
104
105 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
106 [timestamp](nsecs_t a, nsecs_t b) {
107 return std::abs(timestamp - a) < std::abs(timestamp - b);
108 });
Ady Abrahamc585dba2023-11-15 18:41:35 -0800109 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -0700110 if (distancePercent < kOutlierTolerancePercent) {
111 // duplicate timestamp
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000112 SFTRACE_FORMAT_INSTANT("duplicate timestamp");
Ady Abraham99ca3362021-06-17 12:28:46 -0700113 return false;
114 }
115 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700116}
117
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800118nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700119 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800120 return mRateMap.find(idealPeriod())->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800121}
122
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800123Period VSyncPredictor::minFramePeriod() const {
124 if (!FlagManager::getInstance().vrr_config()) {
125 return Period::fromNs(currentPeriod());
126 }
127
128 std::lock_guard lock(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800129 return minFramePeriodLocked();
130}
131
132Period VSyncPredictor::minFramePeriodLocked() const {
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800133 const auto slope = mRateMap.find(idealPeriod())->second.slope;
Ady Abraham940b7a62024-03-07 10:04:27 -0800134 return Period::fromNs(slope * mNumVsyncsForFrame);
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800135}
136
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800137bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000138 SFTRACE_CALL();
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000139
Ady Abraham9c53ee72020-07-22 21:16:18 -0700140 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700141
142 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700143 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800144 // don't insert this ts into mTimestamps ringbuffer. If we are still
145 // in the learning phase we should just clear all timestamps and start
146 // over.
147 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700148 // Add the timestamp to mTimestamps before clearing it so we could
149 // update mKnownTimestamp based on the new timestamp.
150 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800151 clearTimestamps();
152 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700153 mKnownTimestamp =
154 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
155 } else {
156 mKnownTimestamp = timestamp;
157 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000158 SFTRACE_FORMAT_INSTANT("timestamp rejected. mKnownTimestamp was %.2fms ago",
159 (mClock->now() - *mKnownTimestamp) / 1e6f);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800160 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700161 }
162
Ady Abraham92fa2f42020-02-11 15:33:56 -0800163 if (mTimestamps.size() != kHistorySize) {
164 mTimestamps.push_back(timestamp);
165 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700166 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800167 mLastTimestampIndex = next(mLastTimestampIndex);
168 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700169 }
170
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800171 traceInt64If("VSP-ts", timestamp);
172
Dominik Laskowski62eff352021-12-06 09:59:41 -0800173 const size_t numSamples = mTimestamps.size();
174 if (numSamples < kMinimumSamplesForPrediction) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800175 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800176 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700177 }
178
179 // This is a 'simple linear regression' calculation of Y over X, with Y being the
180 // vsync timestamps, and X being the ordinal of vsync count.
181 // The calculated slope is the vsync period.
182 // Formula for reference:
183 // Sigma_i: means sum over all timestamps.
184 // mean(variable): statistical mean of variable.
185 // X: snapped ordinal of the timestamp
186 // Y: vsync timestamp
187 //
188 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
189 // slope = -------------------------------------------
190 // Sigma_i ( X_i - mean(X) ) ^ 2
191 //
192 // intercept = mean(Y) - slope * mean(X)
193 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800194 std::vector<nsecs_t> vsyncTS(numSamples);
195 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700196
Dominik Laskowski62eff352021-12-06 09:59:41 -0800197 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
198 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Ady Abrahamc585dba2023-11-15 18:41:35 -0800199 auto it = mRateMap.find(idealPeriod());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700200 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700201
Dominik Laskowski62eff352021-12-06 09:59:41 -0800202 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
203 // fixed-point arithmetic.
204 constexpr int64_t kScalingFactor = 1000;
205
206 nsecs_t meanTS = 0;
207 nsecs_t meanOrdinal = 0;
208
209 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800210 const auto timestamp = mTimestamps[i] - oldestTS;
211 vsyncTS[i] = timestamp;
212 meanTS += timestamp;
213
Rachel Lee934017e2022-08-10 15:34:14 -0700214 const auto ordinal = currentPeriod == 0
215 ? 0
216 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800217 ordinals[i] = ordinal;
218 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700219 }
220
Dominik Laskowski62eff352021-12-06 09:59:41 -0800221 meanTS /= numSamples;
222 meanOrdinal /= numSamples;
223
224 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700225 vsyncTS[i] -= meanTS;
226 ordinals[i] -= meanOrdinal;
227 }
228
Dominik Laskowski62eff352021-12-06 09:59:41 -0800229 nsecs_t top = 0;
230 nsecs_t bottom = 0;
231 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700232 top += vsyncTS[i] * ordinals[i];
233 bottom += ordinals[i] * ordinals[i];
234 }
235
236 if (CC_UNLIKELY(bottom == 0)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800237 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800238 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800239 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700240 }
241
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700242 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700243 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
244
Ady Abrahamc585dba2023-11-15 18:41:35 -0800245 auto const percent = std::abs(anticipatedPeriod - idealPeriod()) * kMaxPercent / idealPeriod();
Ady Abraham92fa2f42020-02-11 15:33:56 -0800246 if (percent >= kOutlierTolerancePercent) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800247 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800248 clearTimestamps();
249 return false;
250 }
251
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800252 traceInt64If("VSP-period", anticipatedPeriod);
253 traceInt64If("VSP-intercept", intercept);
254
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700255 it->second = {anticipatedPeriod, intercept};
256
Leon Scroggins III67388622023-02-06 20:36:20 -0500257 ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64,
258 mId.value, timestamp, anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800259 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700260}
261
Ady Abraham4335afd2023-12-18 19:10:47 -0800262nsecs_t VSyncPredictor::snapToVsync(nsecs_t timePoint) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700263 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700264
Ady Abraham92fa2f42020-02-11 15:33:56 -0800265 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800266 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700267 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800268 auto const numPeriodsOut = ((timePoint - knownTimestamp) / idealPeriod()) + 1;
269 return knownTimestamp + numPeriodsOut * idealPeriod();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700270 }
271
Ady Abraham92fa2f42020-02-11 15:33:56 -0800272 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800273
274 // See b/145667109, the ordinal calculation must take into account the intercept.
275 auto const zeroPoint = oldest + intercept;
276 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700277 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
278
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800279 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800280 traceInt64If("VSP-timePoint", timePoint);
281 traceInt64If("VSP-prediction", prediction);
282
Kevin DuBois127a2d92019-12-04 13:52:52 -0800283 auto const printer = [&, slope = slope, intercept = intercept] {
284 std::stringstream str;
285 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
286 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
287 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
288 return str.str();
289 };
290
291 ALOGV("%s", printer().c_str());
292 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
293 printer().c_str());
294
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700295 return prediction;
296}
297
Ady Abraham4335afd2023-12-18 19:10:47 -0800298nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint,
Ady Abraham20024aa2024-03-05 01:32:49 +0000299 std::optional<nsecs_t> lastVsyncOpt) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000300 SFTRACE_CALL();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700301 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800302
Ady Abraham20024aa2024-03-05 01:32:49 +0000303 const auto now = TimePoint::fromNs(mClock->now());
304 purgeTimelines(now);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800305
Ady Abrahamee6365b2024-03-06 14:31:45 -0800306 if (lastVsyncOpt && *lastVsyncOpt > timePoint) {
307 timePoint = *lastVsyncOpt;
308 }
309
Ady Abraham77b4fb12024-03-05 17:51:53 -0800310 const auto model = getVSyncPredictionModelLocked();
311 const auto threshold = model.slope / 2;
Ady Abraham940b7a62024-03-07 10:04:27 -0800312 std::optional<Period> minFramePeriodOpt;
313
314 if (mNumVsyncsForFrame > 1) {
315 minFramePeriodOpt = minFramePeriodLocked();
316 }
317
Ady Abraham20024aa2024-03-05 01:32:49 +0000318 std::optional<TimePoint> vsyncOpt;
319 for (auto& timeline : mTimelines) {
Ady Abraham940b7a62024-03-07 10:04:27 -0800320 vsyncOpt = timeline.nextAnticipatedVSyncTimeFrom(model, minFramePeriodOpt,
Ady Abraham20024aa2024-03-05 01:32:49 +0000321 snapToVsync(timePoint), mMissedVsync,
Ady Abraham77b4fb12024-03-05 17:51:53 -0800322 lastVsyncOpt ? snapToVsync(*lastVsyncOpt -
323 threshold)
324 : lastVsyncOpt);
Ady Abraham20024aa2024-03-05 01:32:49 +0000325 if (vsyncOpt) {
326 break;
Ady Abrahame9883032023-11-20 17:54:54 -0800327 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000328 }
329 LOG_ALWAYS_FATAL_IF(!vsyncOpt);
Ady Abrahame9883032023-11-20 17:54:54 -0800330
Ady Abraham20024aa2024-03-05 01:32:49 +0000331 if (*vsyncOpt > mLastCommittedVsync) {
332 mLastCommittedVsync = *vsyncOpt;
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000333 SFTRACE_FORMAT_INSTANT("mLastCommittedVsync in %.2fms",
334 float(mLastCommittedVsync.ns() - mClock->now()) / 1e6f);
Ady Abrahamace3d052022-11-17 16:25:05 -0800335 }
Ady Abrahamf34a8132023-02-13 20:49:48 -0800336
Ady Abraham20024aa2024-03-05 01:32:49 +0000337 return vsyncOpt->ns();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700338}
339
Ady Abraham0bb6a472020-10-12 10:22:13 -0700340/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700341 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800342 * 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 -0700343 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
344 * isVSyncInPhase(16.6, 30) = true
345 * isVSyncInPhase(33.3, 30) = false
346 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700347 */
Ady Abraham20024aa2024-03-05 01:32:49 +0000348bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) {
349 if (timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700350 return true;
351 }
352
Ady Abraham20024aa2024-03-05 01:32:49 +0000353 std::lock_guard lock(mMutex);
354 const auto model = getVSyncPredictionModelLocked();
355 const nsecs_t period = model.slope;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700356 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abraham20024aa2024-03-05 01:32:49 +0000357 const auto now = TimePoint::fromNs(mClock->now());
358 const auto vsync = snapToVsync(justBeforeTimePoint);
359
360 purgeTimelines(now);
361
362 for (auto& timeline : mTimelines) {
ramindani548f4492024-06-13 10:29:04 -0700363 const bool isVsyncValid = FlagManager::getInstance().vrr_bugfix_24q4()
364 ? timeline.isWithin(TimePoint::fromNs(vsync)) ==
365 VsyncTimeline::VsyncOnTimeline::Unique
366 : timeline.validUntil() && timeline.validUntil()->ns() > vsync;
367 if (isVsyncValid) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000368 return timeline.isVSyncInPhase(model, vsync, frameRate);
369 }
370 }
371
372 // The last timeline should always be valid
373 return mTimelines.back().isVSyncInPhase(model, vsync, frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700374}
375
Ady Abrahamee6365b2024-03-06 14:31:45 -0800376void VSyncPredictor::setRenderRate(Fps renderRate, bool applyImmediately) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000377 SFTRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str());
Ady Abrahamc585dba2023-11-15 18:41:35 -0800378 ALOGV("%s %s: RenderRate %s ", __func__, to_string(mId).c_str(), to_string(renderRate).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800379 std::lock_guard lock(mMutex);
Ady Abraham77b4fb12024-03-05 17:51:53 -0800380 const auto prevRenderRate = mRenderRateOpt;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800381 mRenderRateOpt = renderRate;
Ady Abraham77b4fb12024-03-05 17:51:53 -0800382 const auto renderPeriodDelta =
383 prevRenderRate ? prevRenderRate->getPeriodNsecs() - renderRate.getPeriodNsecs() : 0;
Ady Abraham4fc2fce2024-03-08 06:43:44 +0000384 if (applyImmediately) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000385 SFTRACE_FORMAT_INSTANT("applyImmediately");
Ady Abraham4fc2fce2024-03-08 06:43:44 +0000386 while (mTimelines.size() > 1) {
387 mTimelines.pop_front();
388 }
389
390 mTimelines.front().setRenderRate(renderRate);
Ady Abraham45ed7a82024-03-14 17:36:22 -0700391 return;
392 }
393
394 const bool newRenderRateIsHigher = renderPeriodDelta > renderRate.getPeriodNsecs() &&
395 mLastCommittedVsync.ns() - mClock->now() > 2 * renderRate.getPeriodNsecs();
396 if (newRenderRateIsHigher) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000397 SFTRACE_FORMAT_INSTANT("newRenderRateIsHigher");
Ady Abraham77b4fb12024-03-05 17:51:53 -0800398 mTimelines.clear();
399 mLastCommittedVsync = TimePoint::fromNs(0);
Ady Abraham4fc2fce2024-03-08 06:43:44 +0000400
Ady Abraham77b4fb12024-03-05 17:51:53 -0800401 } else {
ramindani548f4492024-06-13 10:29:04 -0700402 if (FlagManager::getInstance().vrr_bugfix_24q4()) {
403 // We need to freeze the timeline at the committed vsync so that we don't
404 // overshoot the deadline.
405 mTimelines.back().freeze(mLastCommittedVsync);
406 } else {
407 mTimelines.back().freeze(
408 TimePoint::fromNs(mLastCommittedVsync.ns() + mIdealPeriod.ns() / 2));
409 }
Ady Abraham77b4fb12024-03-05 17:51:53 -0800410 }
411 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, renderRate);
Ady Abraham20024aa2024-03-05 01:32:49 +0000412 purgeTimelines(TimePoint::fromNs(mClock->now()));
Ady Abrahamc585dba2023-11-15 18:41:35 -0800413}
414
415void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) {
416 LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(),
417 "mode does not belong to the display");
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000418 SFTRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str());
Ady Abrahamc585dba2023-11-15 18:41:35 -0800419 const auto timeout = modePtr->getVrrConfig()
420 ? modePtr->getVrrConfig()->notifyExpectedPresentConfig
421 : std::nullopt;
422 ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(),
423 to_string(*modePtr).c_str(),
ramindanicbd7a6d2023-12-19 16:00:30 -0800424 timeout ? std::to_string(timeout->timeoutNs).c_str() : "N/A");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800425 std::lock_guard lock(mMutex);
426
427 mDisplayModePtr = modePtr;
Ady Abraham940b7a62024-03-07 10:04:27 -0800428 mNumVsyncsForFrame = numVsyncsPerFrame(mDisplayModePtr);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800429 traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs());
430
431 static constexpr size_t kSizeLimit = 30;
432 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
433 mRateMap.erase(mRateMap.begin());
434 }
435
436 if (mRateMap.find(idealPeriod()) == mRateMap.end()) {
437 mRateMap[idealPeriod()] = {idealPeriod(), 0};
438 }
439
Ady Abraham54410052024-03-25 23:08:59 +0000440 mTimelines.clear();
Ady Abrahamc585dba2023-11-15 18:41:35 -0800441 clearTimestamps();
Ady Abrahamace3d052022-11-17 16:25:05 -0800442}
443
Ady Abraham20024aa2024-03-05 01:32:49 +0000444Duration VSyncPredictor::ensureMinFrameDurationIsKept(TimePoint expectedPresentTime,
445 TimePoint lastConfirmedPresentTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000446 SFTRACE_CALL();
Ady Abraham940b7a62024-03-07 10:04:27 -0800447
448 if (mNumVsyncsForFrame <= 1) {
449 return 0ns;
450 }
451
Ady Abrahame9883032023-11-20 17:54:54 -0800452 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
453 const auto threshold = currentPeriod / 2;
ramindani3614e0b2024-07-18 18:44:33 -0700454 const auto minFramePeriod = minFramePeriodLocked();
Ady Abrahame9883032023-11-20 17:54:54 -0800455
456 auto prev = lastConfirmedPresentTime.ns();
457 for (auto& current : mPastExpectedPresentTimes) {
458 if (CC_UNLIKELY(mTraceOn)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000459 SFTRACE_FORMAT_INSTANT("current %.2f past last signaled fence",
460 static_cast<float>(current.ns() -
461 lastConfirmedPresentTime.ns()) /
462 1e6f);
Ady Abrahame9883032023-11-20 17:54:54 -0800463 }
464
ramindani3614e0b2024-07-18 18:44:33 -0700465 const auto minPeriodViolation = current.ns() - prev + threshold < minFramePeriod.ns();
Ady Abrahame9883032023-11-20 17:54:54 -0800466 if (minPeriodViolation) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000467 SFTRACE_NAME("minPeriodViolation");
ramindani3614e0b2024-07-18 18:44:33 -0700468 current = TimePoint::fromNs(prev + minFramePeriod.ns());
Ady Abrahame9883032023-11-20 17:54:54 -0800469 prev = current.ns();
470 } else {
471 break;
472 }
473 }
474
475 if (!mPastExpectedPresentTimes.empty()) {
476 const auto phase = Duration(mPastExpectedPresentTimes.back() - expectedPresentTime);
477 if (phase > 0ns) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000478 for (auto& timeline : mTimelines) {
ramindani3614e0b2024-07-18 18:44:33 -0700479 timeline.shiftVsyncSequence(phase, minFramePeriod);
Ady Abrahame9883032023-11-20 17:54:54 -0800480 }
Ady Abraham4335afd2023-12-18 19:10:47 -0800481 mPastExpectedPresentTimes.clear();
Ady Abraham20024aa2024-03-05 01:32:49 +0000482 return phase;
Ady Abrahame9883032023-11-20 17:54:54 -0800483 }
484 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000485
486 return 0ns;
Ady Abrahame9883032023-11-20 17:54:54 -0800487}
488
ramindani7b32b3a2024-07-02 10:17:47 -0700489void VSyncPredictor::onFrameBegin(TimePoint expectedPresentTime, FrameTime lastSignaledFrameTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000490 SFTRACE_NAME("VSyncPredictor::onFrameBegin");
Ady Abrahame9883032023-11-20 17:54:54 -0800491 std::lock_guard lock(mMutex);
492
493 if (!mDisplayModePtr->getVrrConfig()) return;
494
ramindani7b32b3a2024-07-02 10:17:47 -0700495 const auto [lastConfirmedPresentTime, lastConfirmedExpectedPresentTime] = lastSignaledFrameTime;
Ady Abrahame9883032023-11-20 17:54:54 -0800496 if (CC_UNLIKELY(mTraceOn)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000497 SFTRACE_FORMAT_INSTANT("vsync is %.2f past last signaled fence",
498 static_cast<float>(expectedPresentTime.ns() -
499 lastConfirmedPresentTime.ns()) /
500 1e6f);
Ady Abrahame9883032023-11-20 17:54:54 -0800501 }
Ady Abrahame9883032023-11-20 17:54:54 -0800502 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
503 const auto threshold = currentPeriod / 2;
Ady Abraham4335afd2023-12-18 19:10:47 -0800504 mPastExpectedPresentTimes.push_back(expectedPresentTime);
Ady Abrahame9883032023-11-20 17:54:54 -0800505
Ady Abrahame9883032023-11-20 17:54:54 -0800506 while (!mPastExpectedPresentTimes.empty()) {
507 const auto front = mPastExpectedPresentTimes.front().ns();
Ady Abraham4335afd2023-12-18 19:10:47 -0800508 const bool frontIsBeforeConfirmed = front < lastConfirmedPresentTime.ns() + threshold;
509 if (frontIsBeforeConfirmed) {
Ady Abrahame9883032023-11-20 17:54:54 -0800510 if (CC_UNLIKELY(mTraceOn)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000511 SFTRACE_FORMAT_INSTANT("Discarding old vsync - %.2f before last signaled fence",
512 static_cast<float>(lastConfirmedPresentTime.ns() - front) /
513 1e6f);
Ady Abrahame9883032023-11-20 17:54:54 -0800514 }
515 mPastExpectedPresentTimes.pop_front();
516 } else {
517 break;
518 }
519 }
520
ramindani7b32b3a2024-07-02 10:17:47 -0700521 if (lastConfirmedExpectedPresentTime.ns() - lastConfirmedPresentTime.ns() > threshold) {
522 SFTRACE_FORMAT_INSTANT("lastFramePresentedEarly");
523 return;
524 }
525
Ady Abraham20024aa2024-03-05 01:32:49 +0000526 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
527 if (phase > 0ns) {
528 mMissedVsync = {expectedPresentTime, minFramePeriodLocked()};
529 }
Ady Abrahame9883032023-11-20 17:54:54 -0800530}
531
532void VSyncPredictor::onFrameMissed(TimePoint expectedPresentTime) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000533 SFTRACE_NAME("VSyncPredictor::onFrameMissed");
Ady Abrahame9883032023-11-20 17:54:54 -0800534
535 std::lock_guard lock(mMutex);
536 if (!mDisplayModePtr->getVrrConfig()) return;
537
538 // We don't know when the frame is going to be presented, so we assume it missed one vsync
539 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
540 const auto lastConfirmedPresentTime =
541 TimePoint::fromNs(expectedPresentTime.ns() + currentPeriod);
542
Ady Abraham20024aa2024-03-05 01:32:49 +0000543 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
544 if (phase > 0ns) {
545 mMissedVsync = {expectedPresentTime, Duration::fromNs(0)};
546 }
Ady Abrahame9883032023-11-20 17:54:54 -0800547}
548
Ady Abraham0bb6a472020-10-12 10:22:13 -0700549VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
550 std::lock_guard lock(mMutex);
Ady Abraham20024aa2024-03-05 01:32:49 +0000551 return VSyncPredictor::getVSyncPredictionModelLocked();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700552}
553
554VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800555 return mRateMap.find(idealPeriod())->second;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800556}
557
558void VSyncPredictor::clearTimestamps() {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000559 SFTRACE_CALL();
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000560
Ady Abraham92fa2f42020-02-11 15:33:56 -0800561 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700562 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
563 if (mKnownTimestamp) {
564 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
565 } else {
566 mKnownTimestamp = maxRb;
567 }
568
Ady Abraham92fa2f42020-02-11 15:33:56 -0800569 mTimestamps.clear();
570 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700571 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000572
Ady Abraham20024aa2024-03-05 01:32:49 +0000573 mIdealPeriod = Period::fromNs(idealPeriod());
Ady Abrahamc5d72462024-03-23 23:56:33 +0000574 if (mTimelines.empty()) {
575 mLastCommittedVsync = TimePoint::fromNs(0);
576 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt);
577 } else {
578 while (mTimelines.size() > 1) {
579 mTimelines.pop_front();
580 }
581 mTimelines.front().setRenderRate(mRenderRateOpt);
582 // set mLastCommittedVsync to a valid vsync but don't commit too much in the future
583 const auto vsyncOpt = mTimelines.front().nextAnticipatedVSyncTimeFrom(
584 getVSyncPredictionModelLocked(),
585 /* minFramePeriodOpt */ std::nullopt,
586 snapToVsync(mClock->now()), MissedVsync{},
587 /* lastVsyncOpt */ std::nullopt);
588 mLastCommittedVsync = *vsyncOpt;
589 }
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700590}
591
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700592bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700593 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700594 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700595}
596
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800597void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700598 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800599 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800600 clearTimestamps();
601}
602
Ady Abraham5e7371c2020-03-24 14:47:24 -0700603void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700604 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800605 StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700606 StringAppendF(&result, "\tRefresh Rate Map:\n");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800607 for (const auto& [period, periodInterceptTuple] : mRateMap) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700608 StringAppendF(&result,
609 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abrahamc585dba2023-11-15 18:41:35 -0800610 period / 1e6f, periodInterceptTuple.slope / 1e6f,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700611 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700612 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000613 StringAppendF(&result, "\tmTimelines.size()=%zu\n", mTimelines.size());
614}
615
616void VSyncPredictor::purgeTimelines(android::TimePoint now) {
Ady Abraham77b4fb12024-03-05 17:51:53 -0800617 const auto kEnoughFramesToBreakPhase = 5;
618 if (mRenderRateOpt &&
619 mLastCommittedVsync.ns() + mRenderRateOpt->getPeriodNsecs() * kEnoughFramesToBreakPhase <
620 mClock->now()) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000621 SFTRACE_FORMAT_INSTANT("kEnoughFramesToBreakPhase");
Ady Abraham77b4fb12024-03-05 17:51:53 -0800622 mTimelines.clear();
623 mLastCommittedVsync = TimePoint::fromNs(0);
624 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt);
625 return;
626 }
627
Ady Abraham20024aa2024-03-05 01:32:49 +0000628 while (mTimelines.size() > 1) {
629 const auto validUntilOpt = mTimelines.front().validUntil();
ramindani548f4492024-06-13 10:29:04 -0700630 const bool isTimelineOutDated = FlagManager::getInstance().vrr_bugfix_24q4()
631 ? mTimelines.front().isWithin(now) == VsyncTimeline::VsyncOnTimeline::Outside
632 : validUntilOpt && *validUntilOpt < now;
633 if (isTimelineOutDated) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000634 mTimelines.pop_front();
635 } else {
636 break;
637 }
638 }
639 LOG_ALWAYS_FATAL_IF(mTimelines.empty());
640 LOG_ALWAYS_FATAL_IF(mTimelines.back().validUntil().has_value());
641}
642
Ady Abraham77b4fb12024-03-05 17:51:53 -0800643auto VSyncPredictor::VsyncTimeline::makeVsyncSequence(TimePoint knownVsync)
644 -> std::optional<VsyncSequence> {
645 if (knownVsync.ns() == 0) return std::nullopt;
646 return std::make_optional<VsyncSequence>({knownVsync.ns(), 0});
647}
648
649VSyncPredictor::VsyncTimeline::VsyncTimeline(TimePoint knownVsync, Period idealPeriod,
650 std::optional<Fps> renderRateOpt)
651 : mIdealPeriod(idealPeriod),
652 mRenderRateOpt(renderRateOpt),
653 mLastVsyncSequence(makeVsyncSequence(knownVsync)) {}
Ady Abraham20024aa2024-03-05 01:32:49 +0000654
655void VSyncPredictor::VsyncTimeline::freeze(TimePoint lastVsync) {
656 LOG_ALWAYS_FATAL_IF(mValidUntil.has_value());
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000657 SFTRACE_FORMAT_INSTANT("renderRate %s valid for %.2f",
658 mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA",
659 float(lastVsync.ns() - TimePoint::now().ns()) / 1e6f);
Ady Abraham20024aa2024-03-05 01:32:49 +0000660 mValidUntil = lastVsync;
661}
662
663std::optional<TimePoint> VSyncPredictor::VsyncTimeline::nextAnticipatedVSyncTimeFrom(
Ady Abraham940b7a62024-03-07 10:04:27 -0800664 Model model, std::optional<Period> minFramePeriodOpt, nsecs_t vsync,
665 MissedVsync missedVsync, std::optional<nsecs_t> lastVsyncOpt) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000666 SFTRACE_FORMAT("renderRate %s", mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA");
Ady Abraham20024aa2024-03-05 01:32:49 +0000667
Ady Abrahame54ce102024-03-04 23:18:38 +0000668 nsecs_t vsyncTime = snapToVsyncAlignedWithRenderRate(model, vsync);
Ady Abraham20024aa2024-03-05 01:32:49 +0000669 const auto threshold = model.slope / 2;
670 const auto lastFrameMissed =
671 lastVsyncOpt && std::abs(*lastVsyncOpt - missedVsync.vsync.ns()) < threshold;
Ady Abrahame9dcf792024-05-14 15:24:54 -0700672 const auto mightBackpressure = minFramePeriodOpt && mRenderRateOpt &&
673 mRenderRateOpt->getPeriod() < 2 * (*minFramePeriodOpt);
674 if (FlagManager::getInstance().vrr_config()) {
675 if (lastFrameMissed) {
676 // If the last frame missed is the last vsync, we already shifted the timeline. Depends
677 // on whether we skipped the frame (onFrameMissed) or not (onFrameBegin) we apply a
678 // different fixup. There is no need to to shift the vsync timeline again.
679 vsyncTime += missedVsync.fixup.ns();
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000680 SFTRACE_FORMAT_INSTANT("lastFrameMissed");
Ady Abrahame9dcf792024-05-14 15:24:54 -0700681 } else if (mightBackpressure && lastVsyncOpt) {
ramindani548f4492024-06-13 10:29:04 -0700682 if (!FlagManager::getInstance().vrr_bugfix_24q4()) {
683 // lastVsyncOpt does not need to be corrected with the new rate, and
684 // it should be used as is to avoid skipping a frame when changing rates are
685 // aligned at vsync time.
686 lastVsyncOpt = snapToVsyncAlignedWithRenderRate(model, *lastVsyncOpt);
687 }
Ady Abrahame9dcf792024-05-14 15:24:54 -0700688 const auto vsyncDiff = vsyncTime - *lastVsyncOpt;
689 if (vsyncDiff <= minFramePeriodOpt->ns() - threshold) {
690 // avoid a duplicate vsync
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000691 SFTRACE_FORMAT_INSTANT("skipping a vsync to avoid duplicate frame. next in %.2f "
692 "which "
693 "is %.2f "
694 "from "
695 "prev. "
696 "adjust by %.2f",
697 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f,
698 static_cast<float>(vsyncDiff) / 1e6f,
699 static_cast<float>(mRenderRateOpt->getPeriodNsecs()) / 1e6f);
Ady Abrahame9dcf792024-05-14 15:24:54 -0700700 vsyncTime += mRenderRateOpt->getPeriodNsecs();
701 }
Ady Abrahame54ce102024-03-04 23:18:38 +0000702 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000703 }
704
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000705 SFTRACE_FORMAT_INSTANT("vsync in %.2fms", float(vsyncTime - TimePoint::now().ns()) / 1e6f);
ramindani548f4492024-06-13 10:29:04 -0700706 const bool isVsyncInvalid = FlagManager::getInstance().vrr_bugfix_24q4()
707 ? isWithin(TimePoint::fromNs(vsyncTime)) == VsyncOnTimeline::Outside
708 : mValidUntil && vsyncTime > mValidUntil->ns();
709 if (isVsyncInvalid) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000710 SFTRACE_FORMAT_INSTANT("no longer valid for vsync in %.2f",
711 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f);
Ady Abraham20024aa2024-03-05 01:32:49 +0000712 return std::nullopt;
713 }
714
Ady Abraham20024aa2024-03-05 01:32:49 +0000715 return TimePoint::fromNs(vsyncTime);
716}
717
718auto VSyncPredictor::VsyncTimeline::getVsyncSequenceLocked(Model model, nsecs_t vsync)
719 -> VsyncSequence {
720 if (!mLastVsyncSequence) return {vsync, 0};
721
722 const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence;
723 const auto vsyncSequence = lastVsyncSequence +
724 static_cast<int64_t>(std::round((vsync - lastVsyncTime) /
725 static_cast<float>(model.slope)));
726 return {vsync, vsyncSequence};
727}
728
729nsecs_t VSyncPredictor::VsyncTimeline::snapToVsyncAlignedWithRenderRate(Model model,
730 nsecs_t vsync) {
731 // update the mLastVsyncSequence for reference point
732 mLastVsyncSequence = getVsyncSequenceLocked(model, vsync);
733
734 const auto renderRatePhase = [&]() -> int {
735 if (!mRenderRateOpt) return 0;
736 const auto divisor =
737 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod.ns()),
738 *mRenderRateOpt);
739 if (divisor <= 1) return 0;
740
741 int mod = mLastVsyncSequence->seq % divisor;
742 if (mod == 0) return 0;
743
744 // This is actually a bug fix, but guarded with vrr_config since we found it with this
745 // config
746 if (FlagManager::getInstance().vrr_config()) {
747 if (mod < 0) mod += divisor;
748 }
749
750 return divisor - mod;
751 }();
752
753 if (renderRatePhase == 0) {
754 return mLastVsyncSequence->vsyncTime;
755 }
756
757 return mLastVsyncSequence->vsyncTime + model.slope * renderRatePhase;
758}
759
760bool VSyncPredictor::VsyncTimeline::isVSyncInPhase(Model model, nsecs_t vsync, Fps frameRate) {
761 const auto getVsyncIn = [](TimePoint now, nsecs_t timePoint) -> float {
762 return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now);
763 };
764
Ady Abraham4a719e82024-06-06 12:12:09 -0700765 Fps displayFps = !FlagManager::getInstance().vrr_bugfix_24q4() && mRenderRateOpt
766 ? *mRenderRateOpt
767 : Fps::fromPeriodNsecs(mIdealPeriod.ns());
Ady Abraham20024aa2024-03-05 01:32:49 +0000768 const auto divisor = RefreshRateSelector::getFrameRateDivisor(displayFps, frameRate);
769 const auto now = TimePoint::now();
770
771 if (divisor <= 1) {
772 return true;
773 }
774 const auto vsyncSequence = getVsyncSequenceLocked(model, vsync);
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000775 SFTRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64 " divisor: %zu",
776 getVsyncIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq, divisor);
Ady Abraham20024aa2024-03-05 01:32:49 +0000777 return vsyncSequence.seq % divisor == 0;
778}
779
ramindani3614e0b2024-07-18 18:44:33 -0700780void VSyncPredictor::VsyncTimeline::shiftVsyncSequence(Duration phase, Period minFramePeriod) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000781 if (mLastVsyncSequence) {
ramindani3614e0b2024-07-18 18:44:33 -0700782 const auto renderRate = mRenderRateOpt.value_or(Fps::fromPeriodNsecs(mIdealPeriod.ns()));
783 const auto threshold = mIdealPeriod.ns() / 2;
784 if (renderRate.getPeriodNsecs() - phase.ns() + threshold >= minFramePeriod.ns()) {
785 SFTRACE_FORMAT_INSTANT("Not-Adjusting vsync by %.2f",
786 static_cast<float>(phase.ns()) / 1e6f);
787 return;
788 }
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000789 SFTRACE_FORMAT_INSTANT("adjusting vsync by %.2f", static_cast<float>(phase.ns()) / 1e6f);
Ady Abraham20024aa2024-03-05 01:32:49 +0000790 mLastVsyncSequence->vsyncTime += phase.ns();
791 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700792}
793
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700794} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800795
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100796// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800797#pragma clang diagnostic pop // ignored "-Wextra"