blob: 58457d82fcf5181d6b5c37bb03178bee80cf854b [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>
Kevin DuBois1678e2c2019-08-22 12:26:24 -070033#include <cutils/compiler.h>
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080034#include <cutils/properties.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050035#include <ftl/concat.h>
Ady Abraham9243bba2023-02-10 15:31:14 -080036#include <gui/TraceUtils.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
48VSyncPredictor::~VSyncPredictor() = default;
49
Ady Abraham20024aa2024-03-05 01:32:49 +000050VSyncPredictor::VSyncPredictor(std::unique_ptr<Clock> clock, ftl::NonNull<DisplayModePtr> modePtr,
51 size_t historySize, size_t minimumSamplesForPrediction,
52 uint32_t outlierTolerancePercent)
53 : mClock(std::move(clock)),
54 mId(modePtr->getPhysicalDisplayId()),
Leon Scroggins III67388622023-02-06 20:36:20 -050055 mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080056 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070057 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
58 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080059 mDisplayModePtr(modePtr) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080060 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070061}
62
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080063inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
64 if (CC_UNLIKELY(mTraceOn)) {
Leon Scroggins III67388622023-02-06 20:36:20 -050065 traceInt64(name, value);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080066 }
67}
68
Ady Abrahamd9b9a042023-01-13 11:30:58 -080069inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
Leon Scroggins III67388622023-02-06 20:36:20 -050070 ATRACE_INT64(ftl::Concat(ftl::truncated<14>(name), " ", mId.value).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
Ady Abrahamc585dba2023-11-15 18:41:35 -080077nsecs_t VSyncPredictor::idealPeriod() const {
78 return mDisplayModePtr->getVsyncRate().getPeriodNsecs();
79}
80
Kevin DuBois1678e2c2019-08-22 12:26:24 -070081bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080082 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -070083 return true;
84 }
85
Ady Abrahamc585dba2023-11-15 18:41:35 -080086 const auto aValidTimestamp = mTimestamps[mLastTimestampIndex];
87 const auto percent =
88 (timestamp - aValidTimestamp) % idealPeriod() * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -070089 if (percent >= kOutlierTolerancePercent &&
90 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +000091 ATRACE_FORMAT_INSTANT("timestamp is not aligned with model");
Ady Abraham99ca3362021-06-17 12:28:46 -070092 return false;
93 }
94
95 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
96 [timestamp](nsecs_t a, nsecs_t b) {
97 return std::abs(timestamp - a) < std::abs(timestamp - b);
98 });
Ady Abrahamc585dba2023-11-15 18:41:35 -080099 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -0700100 if (distancePercent < kOutlierTolerancePercent) {
101 // duplicate timestamp
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000102 ATRACE_FORMAT_INSTANT("duplicate timestamp");
Ady Abraham99ca3362021-06-17 12:28:46 -0700103 return false;
104 }
105 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700106}
107
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800108nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700109 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800110 return mRateMap.find(idealPeriod())->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800111}
112
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800113Period VSyncPredictor::minFramePeriod() const {
114 if (!FlagManager::getInstance().vrr_config()) {
115 return Period::fromNs(currentPeriod());
116 }
117
118 std::lock_guard lock(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800119 return minFramePeriodLocked();
120}
121
122Period VSyncPredictor::minFramePeriodLocked() const {
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800123 const auto idealPeakRefreshPeriod = mDisplayModePtr->getPeakFps().getPeriodNsecs();
124 const auto numPeriods = static_cast<int>(std::round(static_cast<float>(idealPeakRefreshPeriod) /
125 static_cast<float>(idealPeriod())));
126 const auto slope = mRateMap.find(idealPeriod())->second.slope;
127 return Period::fromNs(slope * numPeriods);
128}
129
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800130bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000131 ATRACE_CALL();
132
Ady Abraham9c53ee72020-07-22 21:16:18 -0700133 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700134
135 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700136 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800137 // don't insert this ts into mTimestamps ringbuffer. If we are still
138 // in the learning phase we should just clear all timestamps and start
139 // over.
140 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700141 // Add the timestamp to mTimestamps before clearing it so we could
142 // update mKnownTimestamp based on the new timestamp.
143 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800144 clearTimestamps();
145 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700146 mKnownTimestamp =
147 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
148 } else {
149 mKnownTimestamp = timestamp;
150 }
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000151 ATRACE_FORMAT_INSTANT("timestamp rejected. mKnownTimestamp was %.2fms ago",
Ady Abraham20024aa2024-03-05 01:32:49 +0000152 (mClock->now() - *mKnownTimestamp) / 1e6f);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800153 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700154 }
155
Ady Abraham92fa2f42020-02-11 15:33:56 -0800156 if (mTimestamps.size() != kHistorySize) {
157 mTimestamps.push_back(timestamp);
158 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700159 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800160 mLastTimestampIndex = next(mLastTimestampIndex);
161 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700162 }
163
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800164 traceInt64If("VSP-ts", timestamp);
165
Dominik Laskowski62eff352021-12-06 09:59:41 -0800166 const size_t numSamples = mTimestamps.size();
167 if (numSamples < kMinimumSamplesForPrediction) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800168 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800169 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700170 }
171
172 // This is a 'simple linear regression' calculation of Y over X, with Y being the
173 // vsync timestamps, and X being the ordinal of vsync count.
174 // The calculated slope is the vsync period.
175 // Formula for reference:
176 // Sigma_i: means sum over all timestamps.
177 // mean(variable): statistical mean of variable.
178 // X: snapped ordinal of the timestamp
179 // Y: vsync timestamp
180 //
181 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
182 // slope = -------------------------------------------
183 // Sigma_i ( X_i - mean(X) ) ^ 2
184 //
185 // intercept = mean(Y) - slope * mean(X)
186 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800187 std::vector<nsecs_t> vsyncTS(numSamples);
188 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700189
Dominik Laskowski62eff352021-12-06 09:59:41 -0800190 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
191 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Ady Abrahamc585dba2023-11-15 18:41:35 -0800192 auto it = mRateMap.find(idealPeriod());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700193 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700194
Dominik Laskowski62eff352021-12-06 09:59:41 -0800195 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
196 // fixed-point arithmetic.
197 constexpr int64_t kScalingFactor = 1000;
198
199 nsecs_t meanTS = 0;
200 nsecs_t meanOrdinal = 0;
201
202 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800203 const auto timestamp = mTimestamps[i] - oldestTS;
204 vsyncTS[i] = timestamp;
205 meanTS += timestamp;
206
Rachel Lee934017e2022-08-10 15:34:14 -0700207 const auto ordinal = currentPeriod == 0
208 ? 0
209 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800210 ordinals[i] = ordinal;
211 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700212 }
213
Dominik Laskowski62eff352021-12-06 09:59:41 -0800214 meanTS /= numSamples;
215 meanOrdinal /= numSamples;
216
217 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700218 vsyncTS[i] -= meanTS;
219 ordinals[i] -= meanOrdinal;
220 }
221
Dominik Laskowski62eff352021-12-06 09:59:41 -0800222 nsecs_t top = 0;
223 nsecs_t bottom = 0;
224 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700225 top += vsyncTS[i] * ordinals[i];
226 bottom += ordinals[i] * ordinals[i];
227 }
228
229 if (CC_UNLIKELY(bottom == 0)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800230 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800231 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800232 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700233 }
234
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700235 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700236 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
237
Ady Abrahamc585dba2023-11-15 18:41:35 -0800238 auto const percent = std::abs(anticipatedPeriod - idealPeriod()) * kMaxPercent / idealPeriod();
Ady Abraham92fa2f42020-02-11 15:33:56 -0800239 if (percent >= kOutlierTolerancePercent) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800240 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800241 clearTimestamps();
242 return false;
243 }
244
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800245 traceInt64If("VSP-period", anticipatedPeriod);
246 traceInt64If("VSP-intercept", intercept);
247
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700248 it->second = {anticipatedPeriod, intercept};
249
Leon Scroggins III67388622023-02-06 20:36:20 -0500250 ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64,
251 mId.value, timestamp, anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800252 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700253}
254
Ady Abraham4335afd2023-12-18 19:10:47 -0800255nsecs_t VSyncPredictor::snapToVsync(nsecs_t timePoint) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700256 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700257
Ady Abraham92fa2f42020-02-11 15:33:56 -0800258 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800259 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700260 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800261 auto const numPeriodsOut = ((timePoint - knownTimestamp) / idealPeriod()) + 1;
262 return knownTimestamp + numPeriodsOut * idealPeriod();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700263 }
264
Ady Abraham92fa2f42020-02-11 15:33:56 -0800265 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800266
267 // See b/145667109, the ordinal calculation must take into account the intercept.
268 auto const zeroPoint = oldest + intercept;
269 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700270 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
271
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800272 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800273 traceInt64If("VSP-timePoint", timePoint);
274 traceInt64If("VSP-prediction", prediction);
275
Kevin DuBois127a2d92019-12-04 13:52:52 -0800276 auto const printer = [&, slope = slope, intercept = intercept] {
277 std::stringstream str;
278 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
279 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
280 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
281 return str.str();
282 };
283
284 ALOGV("%s", printer().c_str());
285 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
286 printer().c_str());
287
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700288 return prediction;
289}
290
Ady Abraham4335afd2023-12-18 19:10:47 -0800291nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint,
Ady Abraham20024aa2024-03-05 01:32:49 +0000292 std::optional<nsecs_t> lastVsyncOpt) {
Ady Abraham4335afd2023-12-18 19:10:47 -0800293 ATRACE_CALL();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700294 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800295
Ady Abraham20024aa2024-03-05 01:32:49 +0000296 const auto now = TimePoint::fromNs(mClock->now());
297 purgeTimelines(now);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800298
Ady Abrahamee6365b2024-03-06 14:31:45 -0800299 if (lastVsyncOpt && *lastVsyncOpt > timePoint) {
300 timePoint = *lastVsyncOpt;
301 }
302
Ady Abraham77b4fb12024-03-05 17:51:53 -0800303 const auto model = getVSyncPredictionModelLocked();
304 const auto threshold = model.slope / 2;
Ady Abraham20024aa2024-03-05 01:32:49 +0000305 std::optional<TimePoint> vsyncOpt;
306 for (auto& timeline : mTimelines) {
Ady Abraham77b4fb12024-03-05 17:51:53 -0800307 vsyncOpt = timeline.nextAnticipatedVSyncTimeFrom(model, minFramePeriodLocked(),
Ady Abraham20024aa2024-03-05 01:32:49 +0000308 snapToVsync(timePoint), mMissedVsync,
Ady Abraham77b4fb12024-03-05 17:51:53 -0800309 lastVsyncOpt ? snapToVsync(*lastVsyncOpt -
310 threshold)
311 : lastVsyncOpt);
Ady Abraham20024aa2024-03-05 01:32:49 +0000312 if (vsyncOpt) {
313 break;
Ady Abrahame9883032023-11-20 17:54:54 -0800314 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000315 }
316 LOG_ALWAYS_FATAL_IF(!vsyncOpt);
Ady Abrahame9883032023-11-20 17:54:54 -0800317
Ady Abraham20024aa2024-03-05 01:32:49 +0000318 if (*vsyncOpt > mLastCommittedVsync) {
319 mLastCommittedVsync = *vsyncOpt;
320 ATRACE_FORMAT_INSTANT("mLastCommittedVsync in %.2fms",
321 float(mLastCommittedVsync.ns() - mClock->now()) / 1e6f);
Ady Abrahamace3d052022-11-17 16:25:05 -0800322 }
Ady Abrahamf34a8132023-02-13 20:49:48 -0800323
Ady Abraham20024aa2024-03-05 01:32:49 +0000324 return vsyncOpt->ns();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700325}
326
Ady Abraham0bb6a472020-10-12 10:22:13 -0700327/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700328 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800329 * 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 -0700330 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
331 * isVSyncInPhase(16.6, 30) = true
332 * isVSyncInPhase(33.3, 30) = false
333 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700334 */
Ady Abraham20024aa2024-03-05 01:32:49 +0000335bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) {
336 if (timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700337 return true;
338 }
339
Ady Abraham20024aa2024-03-05 01:32:49 +0000340 std::lock_guard lock(mMutex);
341 const auto model = getVSyncPredictionModelLocked();
342 const nsecs_t period = model.slope;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700343 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abraham20024aa2024-03-05 01:32:49 +0000344 const auto now = TimePoint::fromNs(mClock->now());
345 const auto vsync = snapToVsync(justBeforeTimePoint);
346
347 purgeTimelines(now);
348
349 for (auto& timeline : mTimelines) {
350 if (timeline.validUntil() && timeline.validUntil()->ns() > vsync) {
351 return timeline.isVSyncInPhase(model, vsync, frameRate);
352 }
353 }
354
355 // The last timeline should always be valid
356 return mTimelines.back().isVSyncInPhase(model, vsync, frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700357}
358
Ady Abrahamee6365b2024-03-06 14:31:45 -0800359void VSyncPredictor::setRenderRate(Fps renderRate, bool applyImmediately) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800360 ATRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str());
361 ALOGV("%s %s: RenderRate %s ", __func__, to_string(mId).c_str(), to_string(renderRate).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800362 std::lock_guard lock(mMutex);
Ady Abraham77b4fb12024-03-05 17:51:53 -0800363 const auto prevRenderRate = mRenderRateOpt;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800364 mRenderRateOpt = renderRate;
Ady Abraham77b4fb12024-03-05 17:51:53 -0800365 const auto renderPeriodDelta =
366 prevRenderRate ? prevRenderRate->getPeriodNsecs() - renderRate.getPeriodNsecs() : 0;
Ady Abrahamee6365b2024-03-06 14:31:45 -0800367 const bool newRenderRateIsHigher = renderPeriodDelta > renderRate.getPeriodNsecs() &&
368 mLastCommittedVsync.ns() - mClock->now() > 2 * renderRate.getPeriodNsecs();
369 if (applyImmediately || newRenderRateIsHigher) {
Ady Abraham77b4fb12024-03-05 17:51:53 -0800370 mTimelines.clear();
371 mLastCommittedVsync = TimePoint::fromNs(0);
372 } else {
373 mTimelines.back().freeze(
374 TimePoint::fromNs(mLastCommittedVsync.ns() + mIdealPeriod.ns() / 2));
375 }
376 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, renderRate);
Ady Abraham20024aa2024-03-05 01:32:49 +0000377 purgeTimelines(TimePoint::fromNs(mClock->now()));
Ady Abrahamc585dba2023-11-15 18:41:35 -0800378}
379
380void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) {
381 LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(),
382 "mode does not belong to the display");
383 ATRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str());
384 const auto timeout = modePtr->getVrrConfig()
385 ? modePtr->getVrrConfig()->notifyExpectedPresentConfig
386 : std::nullopt;
387 ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(),
388 to_string(*modePtr).c_str(),
ramindanicbd7a6d2023-12-19 16:00:30 -0800389 timeout ? std::to_string(timeout->timeoutNs).c_str() : "N/A");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800390 std::lock_guard lock(mMutex);
391
392 mDisplayModePtr = modePtr;
393 traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs());
394
395 static constexpr size_t kSizeLimit = 30;
396 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
397 mRateMap.erase(mRateMap.begin());
398 }
399
400 if (mRateMap.find(idealPeriod()) == mRateMap.end()) {
401 mRateMap[idealPeriod()] = {idealPeriod(), 0};
402 }
403
404 clearTimestamps();
Ady Abrahamace3d052022-11-17 16:25:05 -0800405}
406
Ady Abraham20024aa2024-03-05 01:32:49 +0000407Duration VSyncPredictor::ensureMinFrameDurationIsKept(TimePoint expectedPresentTime,
408 TimePoint lastConfirmedPresentTime) {
409 ATRACE_CALL();
Ady Abrahame9883032023-11-20 17:54:54 -0800410 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
411 const auto threshold = currentPeriod / 2;
412 const auto minFramePeriod = minFramePeriodLocked().ns();
413
414 auto prev = lastConfirmedPresentTime.ns();
415 for (auto& current : mPastExpectedPresentTimes) {
416 if (CC_UNLIKELY(mTraceOn)) {
417 ATRACE_FORMAT_INSTANT("current %.2f past last signaled fence",
418 static_cast<float>(current.ns() - lastConfirmedPresentTime.ns()) /
419 1e6f);
420 }
421
422 const auto minPeriodViolation = current.ns() - prev + threshold < minFramePeriod;
423 if (minPeriodViolation) {
424 ATRACE_NAME("minPeriodViolation");
425 current = TimePoint::fromNs(prev + minFramePeriod);
426 prev = current.ns();
427 } else {
428 break;
429 }
430 }
431
432 if (!mPastExpectedPresentTimes.empty()) {
433 const auto phase = Duration(mPastExpectedPresentTimes.back() - expectedPresentTime);
434 if (phase > 0ns) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000435 for (auto& timeline : mTimelines) {
436 timeline.shiftVsyncSequence(phase);
Ady Abrahame9883032023-11-20 17:54:54 -0800437 }
Ady Abraham4335afd2023-12-18 19:10:47 -0800438 mPastExpectedPresentTimes.clear();
Ady Abraham20024aa2024-03-05 01:32:49 +0000439 return phase;
Ady Abrahame9883032023-11-20 17:54:54 -0800440 }
441 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000442
443 return 0ns;
Ady Abrahame9883032023-11-20 17:54:54 -0800444}
445
446void VSyncPredictor::onFrameBegin(TimePoint expectedPresentTime,
447 TimePoint lastConfirmedPresentTime) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000448 ATRACE_NAME("VSyncPredictor::onFrameBegin");
Ady Abrahame9883032023-11-20 17:54:54 -0800449 std::lock_guard lock(mMutex);
450
451 if (!mDisplayModePtr->getVrrConfig()) return;
452
453 if (CC_UNLIKELY(mTraceOn)) {
454 ATRACE_FORMAT_INSTANT("vsync is %.2f past last signaled fence",
455 static_cast<float>(expectedPresentTime.ns() -
456 lastConfirmedPresentTime.ns()) /
457 1e6f);
458 }
Ady Abrahame9883032023-11-20 17:54:54 -0800459 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
460 const auto threshold = currentPeriod / 2;
Ady Abraham4335afd2023-12-18 19:10:47 -0800461 mPastExpectedPresentTimes.push_back(expectedPresentTime);
Ady Abrahame9883032023-11-20 17:54:54 -0800462
Ady Abrahame9883032023-11-20 17:54:54 -0800463 while (!mPastExpectedPresentTimes.empty()) {
464 const auto front = mPastExpectedPresentTimes.front().ns();
Ady Abraham4335afd2023-12-18 19:10:47 -0800465 const bool frontIsBeforeConfirmed = front < lastConfirmedPresentTime.ns() + threshold;
466 if (frontIsBeforeConfirmed) {
Ady Abrahame9883032023-11-20 17:54:54 -0800467 if (CC_UNLIKELY(mTraceOn)) {
468 ATRACE_FORMAT_INSTANT("Discarding old vsync - %.2f before last signaled fence",
Ady Abraham4335afd2023-12-18 19:10:47 -0800469 static_cast<float>(lastConfirmedPresentTime.ns() - front) /
Ady Abrahame9883032023-11-20 17:54:54 -0800470 1e6f);
471 }
472 mPastExpectedPresentTimes.pop_front();
473 } else {
474 break;
475 }
476 }
477
Ady Abraham20024aa2024-03-05 01:32:49 +0000478 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
479 if (phase > 0ns) {
480 mMissedVsync = {expectedPresentTime, minFramePeriodLocked()};
481 }
Ady Abrahame9883032023-11-20 17:54:54 -0800482}
483
484void VSyncPredictor::onFrameMissed(TimePoint expectedPresentTime) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000485 ATRACE_NAME("VSyncPredictor::onFrameMissed");
Ady Abrahame9883032023-11-20 17:54:54 -0800486
487 std::lock_guard lock(mMutex);
488 if (!mDisplayModePtr->getVrrConfig()) return;
489
490 // We don't know when the frame is going to be presented, so we assume it missed one vsync
491 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
492 const auto lastConfirmedPresentTime =
493 TimePoint::fromNs(expectedPresentTime.ns() + currentPeriod);
494
Ady Abraham20024aa2024-03-05 01:32:49 +0000495 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
496 if (phase > 0ns) {
497 mMissedVsync = {expectedPresentTime, Duration::fromNs(0)};
498 }
Ady Abrahame9883032023-11-20 17:54:54 -0800499}
500
Ady Abraham0bb6a472020-10-12 10:22:13 -0700501VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
502 std::lock_guard lock(mMutex);
Ady Abraham20024aa2024-03-05 01:32:49 +0000503 return VSyncPredictor::getVSyncPredictionModelLocked();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700504}
505
506VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800507 return mRateMap.find(idealPeriod())->second;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800508}
509
510void VSyncPredictor::clearTimestamps() {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000511 ATRACE_CALL();
512
Ady Abraham92fa2f42020-02-11 15:33:56 -0800513 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700514 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
515 if (mKnownTimestamp) {
516 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
517 } else {
518 mKnownTimestamp = maxRb;
519 }
520
Ady Abraham92fa2f42020-02-11 15:33:56 -0800521 mTimestamps.clear();
522 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700523 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000524
525 mTimelines.clear();
526 mLastCommittedVsync = TimePoint::fromNs(0);
527 mIdealPeriod = Period::fromNs(idealPeriod());
Ady Abraham77b4fb12024-03-05 17:51:53 -0800528 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700529}
530
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700531bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700532 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700533 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700534}
535
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800536void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700537 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800538 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800539 clearTimestamps();
540}
541
Ady Abraham5e7371c2020-03-24 14:47:24 -0700542void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700543 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800544 StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700545 StringAppendF(&result, "\tRefresh Rate Map:\n");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800546 for (const auto& [period, periodInterceptTuple] : mRateMap) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700547 StringAppendF(&result,
548 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abrahamc585dba2023-11-15 18:41:35 -0800549 period / 1e6f, periodInterceptTuple.slope / 1e6f,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700550 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700551 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000552 StringAppendF(&result, "\tmTimelines.size()=%zu\n", mTimelines.size());
553}
554
555void VSyncPredictor::purgeTimelines(android::TimePoint now) {
Ady Abraham77b4fb12024-03-05 17:51:53 -0800556 const auto kEnoughFramesToBreakPhase = 5;
557 if (mRenderRateOpt &&
558 mLastCommittedVsync.ns() + mRenderRateOpt->getPeriodNsecs() * kEnoughFramesToBreakPhase <
559 mClock->now()) {
560 mTimelines.clear();
561 mLastCommittedVsync = TimePoint::fromNs(0);
562 mTimelines.emplace_back(mLastCommittedVsync, mIdealPeriod, mRenderRateOpt);
563 return;
564 }
565
Ady Abraham20024aa2024-03-05 01:32:49 +0000566 while (mTimelines.size() > 1) {
567 const auto validUntilOpt = mTimelines.front().validUntil();
568 if (validUntilOpt && *validUntilOpt < now) {
569 mTimelines.pop_front();
570 } else {
571 break;
572 }
573 }
574 LOG_ALWAYS_FATAL_IF(mTimelines.empty());
575 LOG_ALWAYS_FATAL_IF(mTimelines.back().validUntil().has_value());
576}
577
Ady Abraham77b4fb12024-03-05 17:51:53 -0800578auto VSyncPredictor::VsyncTimeline::makeVsyncSequence(TimePoint knownVsync)
579 -> std::optional<VsyncSequence> {
580 if (knownVsync.ns() == 0) return std::nullopt;
581 return std::make_optional<VsyncSequence>({knownVsync.ns(), 0});
582}
583
584VSyncPredictor::VsyncTimeline::VsyncTimeline(TimePoint knownVsync, Period idealPeriod,
585 std::optional<Fps> renderRateOpt)
586 : mIdealPeriod(idealPeriod),
587 mRenderRateOpt(renderRateOpt),
588 mLastVsyncSequence(makeVsyncSequence(knownVsync)) {}
Ady Abraham20024aa2024-03-05 01:32:49 +0000589
590void VSyncPredictor::VsyncTimeline::freeze(TimePoint lastVsync) {
591 LOG_ALWAYS_FATAL_IF(mValidUntil.has_value());
592 ATRACE_FORMAT_INSTANT("renderRate %s valid for %.2f",
593 mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA",
594 float(lastVsync.ns() - TimePoint::now().ns()) / 1e6f);
595 mValidUntil = lastVsync;
596}
597
598std::optional<TimePoint> VSyncPredictor::VsyncTimeline::nextAnticipatedVSyncTimeFrom(
599 Model model, Period minFramePeriod, nsecs_t vsync, MissedVsync missedVsync,
600 std::optional<nsecs_t> lastVsyncOpt) {
601 ATRACE_FORMAT("renderRate %s", mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA");
602
Ady Abrahame54ce102024-03-04 23:18:38 +0000603 nsecs_t vsyncTime = snapToVsyncAlignedWithRenderRate(model, vsync);
Ady Abraham20024aa2024-03-05 01:32:49 +0000604 const auto threshold = model.slope / 2;
605 const auto lastFrameMissed =
606 lastVsyncOpt && std::abs(*lastVsyncOpt - missedVsync.vsync.ns()) < threshold;
Ady Abraham20024aa2024-03-05 01:32:49 +0000607 nsecs_t vsyncFixupTime = 0;
608 if (FlagManager::getInstance().vrr_config() && lastFrameMissed) {
Ady Abrahame54ce102024-03-04 23:18:38 +0000609 // If the last frame missed is the last vsync, we already shifted the timeline. Depends on
610 // whether we skipped the frame (onFrameMissed) or not (onFrameBegin) we apply a different
611 // fixup. There is no need to to shift the vsync timeline again.
Ady Abraham20024aa2024-03-05 01:32:49 +0000612 vsyncTime += missedVsync.fixup.ns();
613 ATRACE_FORMAT_INSTANT("lastFrameMissed");
614 } else {
Ady Abrahame54ce102024-03-04 23:18:38 +0000615 if (FlagManager::getInstance().vrr_config() && lastVsyncOpt) {
616 // lastVsyncOpt is based on the old timeline before we shifted it. we should correct it
617 // first before trying to use it.
Ady Abraham77b4fb12024-03-05 17:51:53 -0800618 if (mLastVsyncSequence->seq > 0) {
619 lastVsyncOpt = snapToVsyncAlignedWithRenderRate(model, *lastVsyncOpt);
620 }
Ady Abrahame54ce102024-03-04 23:18:38 +0000621 const auto vsyncDiff = vsyncTime - *lastVsyncOpt;
622 if (vsyncDiff <= minFramePeriod.ns() - threshold) {
623 vsyncFixupTime = *lastVsyncOpt + minFramePeriod.ns() - vsyncTime;
624 ATRACE_FORMAT_INSTANT("minFramePeriod violation. next in %.2f which is %.2f from "
625 "prev. "
626 "adjust by %.2f",
627 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f,
628 static_cast<float>(vsyncTime - *lastVsyncOpt) / 1e6f,
629 static_cast<float>(vsyncFixupTime) / 1e6f);
630 }
631 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000632 vsyncTime += vsyncFixupTime;
633 }
634
635 ATRACE_FORMAT_INSTANT("vsync in %.2fms", float(vsyncTime - TimePoint::now().ns()) / 1e6f);
636 if (mValidUntil && vsyncTime > mValidUntil->ns()) {
637 ATRACE_FORMAT_INSTANT("no longer valid for vsync in %.2f",
638 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f);
639 return std::nullopt;
640 }
641
Ady Abrahame54ce102024-03-04 23:18:38 +0000642 // If we needed a fixup, it means that we changed the render rate and the chosen vsync would
643 // cross minFramePeriod. In that case we need to shift the entire vsync timeline.
Ady Abraham20024aa2024-03-05 01:32:49 +0000644 if (vsyncFixupTime > 0) {
645 shiftVsyncSequence(Duration::fromNs(vsyncFixupTime));
646 }
647
648 return TimePoint::fromNs(vsyncTime);
649}
650
651auto VSyncPredictor::VsyncTimeline::getVsyncSequenceLocked(Model model, nsecs_t vsync)
652 -> VsyncSequence {
653 if (!mLastVsyncSequence) return {vsync, 0};
654
655 const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence;
656 const auto vsyncSequence = lastVsyncSequence +
657 static_cast<int64_t>(std::round((vsync - lastVsyncTime) /
658 static_cast<float>(model.slope)));
659 return {vsync, vsyncSequence};
660}
661
662nsecs_t VSyncPredictor::VsyncTimeline::snapToVsyncAlignedWithRenderRate(Model model,
663 nsecs_t vsync) {
664 // update the mLastVsyncSequence for reference point
665 mLastVsyncSequence = getVsyncSequenceLocked(model, vsync);
666
667 const auto renderRatePhase = [&]() -> int {
668 if (!mRenderRateOpt) return 0;
669 const auto divisor =
670 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod.ns()),
671 *mRenderRateOpt);
672 if (divisor <= 1) return 0;
673
674 int mod = mLastVsyncSequence->seq % divisor;
675 if (mod == 0) return 0;
676
677 // This is actually a bug fix, but guarded with vrr_config since we found it with this
678 // config
679 if (FlagManager::getInstance().vrr_config()) {
680 if (mod < 0) mod += divisor;
681 }
682
683 return divisor - mod;
684 }();
685
686 if (renderRatePhase == 0) {
687 return mLastVsyncSequence->vsyncTime;
688 }
689
690 return mLastVsyncSequence->vsyncTime + model.slope * renderRatePhase;
691}
692
693bool VSyncPredictor::VsyncTimeline::isVSyncInPhase(Model model, nsecs_t vsync, Fps frameRate) {
694 const auto getVsyncIn = [](TimePoint now, nsecs_t timePoint) -> float {
695 return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now);
696 };
697
698 Fps displayFps = mRenderRateOpt ? *mRenderRateOpt : Fps::fromPeriodNsecs(mIdealPeriod.ns());
699 const auto divisor = RefreshRateSelector::getFrameRateDivisor(displayFps, frameRate);
700 const auto now = TimePoint::now();
701
702 if (divisor <= 1) {
703 return true;
704 }
705 const auto vsyncSequence = getVsyncSequenceLocked(model, vsync);
706 ATRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64 " divisor: %zu",
707 getVsyncIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq, divisor);
708 return vsyncSequence.seq % divisor == 0;
709}
710
711void VSyncPredictor::VsyncTimeline::shiftVsyncSequence(Duration phase) {
712 if (mLastVsyncSequence) {
713 ATRACE_FORMAT_INSTANT("adjusting vsync by %.2f", static_cast<float>(phase.ns()) / 1e6f);
714 mLastVsyncSequence->vsyncTime += phase.ns();
715 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700716}
717
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700718} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800719
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100720// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800721#pragma clang diagnostic pop // ignored "-Wextra"