blob: 2f9dfeaff77518c29b7645bf46397fabada6672a [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
Ady Abraham20024aa2024-03-05 01:32:49 +000048namespace {
49nsecs_t getVsyncFixup(VSyncPredictor::Model model, Period minFramePeriod, nsecs_t vsyncTime,
50 std::optional<nsecs_t> lastVsyncOpt) {
51 const auto threshold = model.slope / 2;
52
53 if (FlagManager::getInstance().vrr_config() && lastVsyncOpt) {
54 const auto vsyncDiff = vsyncTime - *lastVsyncOpt;
55 if (vsyncDiff >= threshold && vsyncDiff <= minFramePeriod.ns() - threshold) {
56 const auto vsyncFixup = *lastVsyncOpt + minFramePeriod.ns() - vsyncTime;
57 ATRACE_FORMAT_INSTANT("minFramePeriod violation. next in %.2f which is %.2f from prev. "
58 "adjust by %.2f",
59 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f,
60 static_cast<float>(vsyncTime - *lastVsyncOpt) / 1e6f,
61 static_cast<float>(vsyncFixup) / 1e6f);
62 return vsyncFixup;
63 }
64 }
65
66 return 0;
67}
68} // namespace
69
Kevin DuBois1678e2c2019-08-22 12:26:24 -070070VSyncPredictor::~VSyncPredictor() = default;
71
Ady Abraham20024aa2024-03-05 01:32:49 +000072VSyncPredictor::VSyncPredictor(std::unique_ptr<Clock> clock, ftl::NonNull<DisplayModePtr> modePtr,
73 size_t historySize, size_t minimumSamplesForPrediction,
74 uint32_t outlierTolerancePercent)
75 : mClock(std::move(clock)),
76 mId(modePtr->getPhysicalDisplayId()),
Leon Scroggins III67388622023-02-06 20:36:20 -050077 mTraceOn(property_get_bool("debug.sf.vsp_trace", false)),
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080078 kHistorySize(historySize),
Kevin DuBois1678e2c2019-08-22 12:26:24 -070079 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
80 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
Ady Abrahamc585dba2023-11-15 18:41:35 -080081 mDisplayModePtr(modePtr) {
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -080082 resetModel();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070083}
84
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080085inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
86 if (CC_UNLIKELY(mTraceOn)) {
Leon Scroggins III67388622023-02-06 20:36:20 -050087 traceInt64(name, value);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080088 }
89}
90
Ady Abrahamd9b9a042023-01-13 11:30:58 -080091inline void VSyncPredictor::traceInt64(const char* name, int64_t value) const {
Leon Scroggins III67388622023-02-06 20:36:20 -050092 ATRACE_INT64(ftl::Concat(ftl::truncated<14>(name), " ", mId.value).c_str(), value);
Ady Abrahamd9b9a042023-01-13 11:30:58 -080093}
94
Ady Abraham9c53ee72020-07-22 21:16:18 -070095inline size_t VSyncPredictor::next(size_t i) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -080096 return (i + 1) % mTimestamps.size();
Kevin DuBois1678e2c2019-08-22 12:26:24 -070097}
98
Ady Abrahamc585dba2023-11-15 18:41:35 -080099nsecs_t VSyncPredictor::idealPeriod() const {
100 return mDisplayModePtr->getVsyncRate().getPeriodNsecs();
101}
102
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700103bool VSyncPredictor::validate(nsecs_t timestamp) const {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800104 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700105 return true;
106 }
107
Ady Abrahamc585dba2023-11-15 18:41:35 -0800108 const auto aValidTimestamp = mTimestamps[mLastTimestampIndex];
109 const auto percent =
110 (timestamp - aValidTimestamp) % idealPeriod() * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -0700111 if (percent >= kOutlierTolerancePercent &&
112 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000113 ATRACE_FORMAT_INSTANT("timestamp is not aligned with model");
Ady Abraham99ca3362021-06-17 12:28:46 -0700114 return false;
115 }
116
117 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
118 [timestamp](nsecs_t a, nsecs_t b) {
119 return std::abs(timestamp - a) < std::abs(timestamp - b);
120 });
Ady Abrahamc585dba2023-11-15 18:41:35 -0800121 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / idealPeriod();
Ady Abraham99ca3362021-06-17 12:28:46 -0700122 if (distancePercent < kOutlierTolerancePercent) {
123 // duplicate timestamp
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000124 ATRACE_FORMAT_INSTANT("duplicate timestamp");
Ady Abraham99ca3362021-06-17 12:28:46 -0700125 return false;
126 }
127 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700128}
129
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800130nsecs_t VSyncPredictor::currentPeriod() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700131 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800132 return mRateMap.find(idealPeriod())->second.slope;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -0800133}
134
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800135Period VSyncPredictor::minFramePeriod() const {
136 if (!FlagManager::getInstance().vrr_config()) {
137 return Period::fromNs(currentPeriod());
138 }
139
140 std::lock_guard lock(mMutex);
Ady Abrahame9883032023-11-20 17:54:54 -0800141 return minFramePeriodLocked();
142}
143
144Period VSyncPredictor::minFramePeriodLocked() const {
Ady Abraham3db8a3c2023-11-20 17:53:47 -0800145 const auto idealPeakRefreshPeriod = mDisplayModePtr->getPeakFps().getPeriodNsecs();
146 const auto numPeriods = static_cast<int>(std::round(static_cast<float>(idealPeakRefreshPeriod) /
147 static_cast<float>(idealPeriod())));
148 const auto slope = mRateMap.find(idealPeriod())->second.slope;
149 return Period::fromNs(slope * numPeriods);
150}
151
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800152bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000153 ATRACE_CALL();
154
Ady Abraham9c53ee72020-07-22 21:16:18 -0700155 std::lock_guard lock(mMutex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700156
157 if (!validate(timestamp)) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700158 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
Ady Abraham43a3e692020-11-13 12:43:39 -0800159 // don't insert this ts into mTimestamps ringbuffer. If we are still
160 // in the learning phase we should just clear all timestamps and start
161 // over.
162 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
Ady Abraham4c56b642021-06-08 15:03:33 -0700163 // Add the timestamp to mTimestamps before clearing it so we could
164 // update mKnownTimestamp based on the new timestamp.
165 mTimestamps.push_back(timestamp);
Ady Abraham43a3e692020-11-13 12:43:39 -0800166 clearTimestamps();
167 } else if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700168 mKnownTimestamp =
169 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
170 } else {
171 mKnownTimestamp = timestamp;
172 }
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000173 ATRACE_FORMAT_INSTANT("timestamp rejected. mKnownTimestamp was %.2fms ago",
Ady Abraham20024aa2024-03-05 01:32:49 +0000174 (mClock->now() - *mKnownTimestamp) / 1e6f);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800175 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700176 }
177
Ady Abraham92fa2f42020-02-11 15:33:56 -0800178 if (mTimestamps.size() != kHistorySize) {
179 mTimestamps.push_back(timestamp);
180 mLastTimestampIndex = next(mLastTimestampIndex);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700181 } else {
Ady Abraham92fa2f42020-02-11 15:33:56 -0800182 mLastTimestampIndex = next(mLastTimestampIndex);
183 mTimestamps[mLastTimestampIndex] = timestamp;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700184 }
185
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800186 traceInt64If("VSP-ts", timestamp);
187
Dominik Laskowski62eff352021-12-06 09:59:41 -0800188 const size_t numSamples = mTimestamps.size();
189 if (numSamples < kMinimumSamplesForPrediction) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800190 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800191 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700192 }
193
194 // This is a 'simple linear regression' calculation of Y over X, with Y being the
195 // vsync timestamps, and X being the ordinal of vsync count.
196 // The calculated slope is the vsync period.
197 // Formula for reference:
198 // Sigma_i: means sum over all timestamps.
199 // mean(variable): statistical mean of variable.
200 // X: snapped ordinal of the timestamp
201 // Y: vsync timestamp
202 //
203 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
204 // slope = -------------------------------------------
205 // Sigma_i ( X_i - mean(X) ) ^ 2
206 //
207 // intercept = mean(Y) - slope * mean(X)
208 //
Dominik Laskowski62eff352021-12-06 09:59:41 -0800209 std::vector<nsecs_t> vsyncTS(numSamples);
210 std::vector<nsecs_t> ordinals(numSamples);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700211
Dominik Laskowski62eff352021-12-06 09:59:41 -0800212 // Normalizing to the oldest timestamp cuts down on error in calculating the intercept.
213 const auto oldestTS = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Ady Abrahamc585dba2023-11-15 18:41:35 -0800214 auto it = mRateMap.find(idealPeriod());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700215 auto const currentPeriod = it->second.slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700216
Dominik Laskowski62eff352021-12-06 09:59:41 -0800217 // The mean of the ordinals must be precise for the intercept calculation, so scale them up for
218 // fixed-point arithmetic.
219 constexpr int64_t kScalingFactor = 1000;
220
221 nsecs_t meanTS = 0;
222 nsecs_t meanOrdinal = 0;
223
224 for (size_t i = 0; i < numSamples; i++) {
Dominik Laskowski62eff352021-12-06 09:59:41 -0800225 const auto timestamp = mTimestamps[i] - oldestTS;
226 vsyncTS[i] = timestamp;
227 meanTS += timestamp;
228
Rachel Lee934017e2022-08-10 15:34:14 -0700229 const auto ordinal = currentPeriod == 0
230 ? 0
231 : (vsyncTS[i] + currentPeriod / 2) / currentPeriod * kScalingFactor;
Dominik Laskowski62eff352021-12-06 09:59:41 -0800232 ordinals[i] = ordinal;
233 meanOrdinal += ordinal;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700234 }
235
Dominik Laskowski62eff352021-12-06 09:59:41 -0800236 meanTS /= numSamples;
237 meanOrdinal /= numSamples;
238
239 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700240 vsyncTS[i] -= meanTS;
241 ordinals[i] -= meanOrdinal;
242 }
243
Dominik Laskowski62eff352021-12-06 09:59:41 -0800244 nsecs_t top = 0;
245 nsecs_t bottom = 0;
246 for (size_t i = 0; i < numSamples; i++) {
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700247 top += vsyncTS[i] * ordinals[i];
248 bottom += ordinals[i] * ordinals[i];
249 }
250
251 if (CC_UNLIKELY(bottom == 0)) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800252 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800253 clearTimestamps();
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800254 return false;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700255 }
256
Kevin DuBois0049f8b2020-03-11 10:30:11 -0700257 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700258 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
259
Ady Abrahamc585dba2023-11-15 18:41:35 -0800260 auto const percent = std::abs(anticipatedPeriod - idealPeriod()) * kMaxPercent / idealPeriod();
Ady Abraham92fa2f42020-02-11 15:33:56 -0800261 if (percent >= kOutlierTolerancePercent) {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800262 it->second = {idealPeriod(), 0};
Ady Abraham92fa2f42020-02-11 15:33:56 -0800263 clearTimestamps();
264 return false;
265 }
266
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800267 traceInt64If("VSP-period", anticipatedPeriod);
268 traceInt64If("VSP-intercept", intercept);
269
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700270 it->second = {anticipatedPeriod, intercept};
271
Leon Scroggins III67388622023-02-06 20:36:20 -0500272 ALOGV("model update ts %" PRIu64 ": %" PRId64 " slope: %" PRId64 " intercept: %" PRId64,
273 mId.value, timestamp, anticipatedPeriod, intercept);
Kevin DuBois02d5ed92020-01-27 11:05:46 -0800274 return true;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700275}
276
Ady Abraham4335afd2023-12-18 19:10:47 -0800277nsecs_t VSyncPredictor::snapToVsync(nsecs_t timePoint) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700278 auto const [slope, intercept] = getVSyncPredictionModelLocked();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700279
Ady Abraham92fa2f42020-02-11 15:33:56 -0800280 if (mTimestamps.empty()) {
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800281 traceInt64("VSP-mode", 1);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700282 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
Ady Abrahamc585dba2023-11-15 18:41:35 -0800283 auto const numPeriodsOut = ((timePoint - knownTimestamp) / idealPeriod()) + 1;
284 return knownTimestamp + numPeriodsOut * idealPeriod();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700285 }
286
Ady Abraham92fa2f42020-02-11 15:33:56 -0800287 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
Kevin DuBois127a2d92019-12-04 13:52:52 -0800288
289 // See b/145667109, the ordinal calculation must take into account the intercept.
290 auto const zeroPoint = oldest + intercept;
291 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700292 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
293
Ady Abrahamd9b9a042023-01-13 11:30:58 -0800294 traceInt64("VSP-mode", 0);
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800295 traceInt64If("VSP-timePoint", timePoint);
296 traceInt64If("VSP-prediction", prediction);
297
Kevin DuBois127a2d92019-12-04 13:52:52 -0800298 auto const printer = [&, slope = slope, intercept = intercept] {
299 std::stringstream str;
300 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
301 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
302 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
303 return str.str();
304 };
305
306 ALOGV("%s", printer().c_str());
307 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
308 printer().c_str());
309
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700310 return prediction;
311}
312
Ady Abraham4335afd2023-12-18 19:10:47 -0800313nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint,
Ady Abraham20024aa2024-03-05 01:32:49 +0000314 std::optional<nsecs_t> lastVsyncOpt) {
Ady Abraham4335afd2023-12-18 19:10:47 -0800315 ATRACE_CALL();
Ady Abraham9c53ee72020-07-22 21:16:18 -0700316 std::lock_guard lock(mMutex);
Ady Abrahamace3d052022-11-17 16:25:05 -0800317
Ady Abraham20024aa2024-03-05 01:32:49 +0000318 const auto now = TimePoint::fromNs(mClock->now());
319 purgeTimelines(now);
Ady Abrahamf34a8132023-02-13 20:49:48 -0800320
Ady Abraham20024aa2024-03-05 01:32:49 +0000321 std::optional<TimePoint> vsyncOpt;
322 for (auto& timeline : mTimelines) {
323 vsyncOpt = timeline.nextAnticipatedVSyncTimeFrom(getVSyncPredictionModelLocked(),
324 minFramePeriodLocked(),
325 snapToVsync(timePoint), mMissedVsync,
326 lastVsyncOpt);
327 if (vsyncOpt) {
328 break;
Ady Abrahame9883032023-11-20 17:54:54 -0800329 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000330 }
331 LOG_ALWAYS_FATAL_IF(!vsyncOpt);
Ady Abrahame9883032023-11-20 17:54:54 -0800332
Ady Abraham20024aa2024-03-05 01:32:49 +0000333 if (*vsyncOpt > mLastCommittedVsync) {
334 mLastCommittedVsync = *vsyncOpt;
335 ATRACE_FORMAT_INSTANT("mLastCommittedVsync in %.2fms",
336 float(mLastCommittedVsync.ns() - mClock->now()) / 1e6f);
Ady Abrahamace3d052022-11-17 16:25:05 -0800337 }
Ady Abrahamf34a8132023-02-13 20:49:48 -0800338
Ady Abraham20024aa2024-03-05 01:32:49 +0000339 return vsyncOpt->ns();
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700340}
341
Ady Abraham0bb6a472020-10-12 10:22:13 -0700342/*
Ady Abraham5cc2e262021-03-25 13:09:17 -0700343 * Returns whether a given vsync timestamp is in phase with a frame rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800344 * 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 -0700345 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
346 * isVSyncInPhase(16.6, 30) = true
347 * isVSyncInPhase(33.3, 30) = false
348 * isVSyncInPhase(50.0, 30) = true
Ady Abraham0bb6a472020-10-12 10:22:13 -0700349 */
Ady Abraham20024aa2024-03-05 01:32:49 +0000350bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) {
351 if (timePoint == 0) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700352 return true;
353 }
354
Ady Abraham20024aa2024-03-05 01:32:49 +0000355 std::lock_guard lock(mMutex);
356 const auto model = getVSyncPredictionModelLocked();
357 const nsecs_t period = model.slope;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700358 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
Ady Abraham20024aa2024-03-05 01:32:49 +0000359 const auto now = TimePoint::fromNs(mClock->now());
360 const auto vsync = snapToVsync(justBeforeTimePoint);
361
362 purgeTimelines(now);
363
364 for (auto& timeline : mTimelines) {
365 if (timeline.validUntil() && timeline.validUntil()->ns() > vsync) {
366 return timeline.isVSyncInPhase(model, vsync, frameRate);
367 }
368 }
369
370 // The last timeline should always be valid
371 return mTimelines.back().isVSyncInPhase(model, vsync, frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700372}
373
Ady Abrahamc585dba2023-11-15 18:41:35 -0800374void VSyncPredictor::setRenderRate(Fps renderRate) {
375 ATRACE_FORMAT("%s %s", __func__, to_string(renderRate).c_str());
376 ALOGV("%s %s: RenderRate %s ", __func__, to_string(mId).c_str(), to_string(renderRate).c_str());
Ady Abrahamace3d052022-11-17 16:25:05 -0800377 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800378 mRenderRateOpt = renderRate;
Ady Abraham20024aa2024-03-05 01:32:49 +0000379 mTimelines.back().freeze(TimePoint::fromNs(mLastCommittedVsync.ns() + mIdealPeriod.ns() / 2));
380 mTimelines.emplace_back(mIdealPeriod, renderRate);
381 purgeTimelines(TimePoint::fromNs(mClock->now()));
Ady Abrahamc585dba2023-11-15 18:41:35 -0800382}
383
384void VSyncPredictor::setDisplayModePtr(ftl::NonNull<DisplayModePtr> modePtr) {
385 LOG_ALWAYS_FATAL_IF(mId != modePtr->getPhysicalDisplayId(),
386 "mode does not belong to the display");
387 ATRACE_FORMAT("%s %s", __func__, to_string(*modePtr).c_str());
388 const auto timeout = modePtr->getVrrConfig()
389 ? modePtr->getVrrConfig()->notifyExpectedPresentConfig
390 : std::nullopt;
391 ALOGV("%s %s: DisplayMode %s notifyExpectedPresentTimeout %s", __func__, to_string(mId).c_str(),
392 to_string(*modePtr).c_str(),
ramindanicbd7a6d2023-12-19 16:00:30 -0800393 timeout ? std::to_string(timeout->timeoutNs).c_str() : "N/A");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800394 std::lock_guard lock(mMutex);
395
396 mDisplayModePtr = modePtr;
397 traceInt64("VSP-setPeriod", modePtr->getVsyncRate().getPeriodNsecs());
398
399 static constexpr size_t kSizeLimit = 30;
400 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
401 mRateMap.erase(mRateMap.begin());
402 }
403
404 if (mRateMap.find(idealPeriod()) == mRateMap.end()) {
405 mRateMap[idealPeriod()] = {idealPeriod(), 0};
406 }
407
408 clearTimestamps();
Ady Abrahamace3d052022-11-17 16:25:05 -0800409}
410
Ady Abraham20024aa2024-03-05 01:32:49 +0000411Duration VSyncPredictor::ensureMinFrameDurationIsKept(TimePoint expectedPresentTime,
412 TimePoint lastConfirmedPresentTime) {
413 ATRACE_CALL();
Ady Abrahame9883032023-11-20 17:54:54 -0800414 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
415 const auto threshold = currentPeriod / 2;
416 const auto minFramePeriod = minFramePeriodLocked().ns();
417
418 auto prev = lastConfirmedPresentTime.ns();
419 for (auto& current : mPastExpectedPresentTimes) {
420 if (CC_UNLIKELY(mTraceOn)) {
421 ATRACE_FORMAT_INSTANT("current %.2f past last signaled fence",
422 static_cast<float>(current.ns() - lastConfirmedPresentTime.ns()) /
423 1e6f);
424 }
425
426 const auto minPeriodViolation = current.ns() - prev + threshold < minFramePeriod;
427 if (minPeriodViolation) {
428 ATRACE_NAME("minPeriodViolation");
429 current = TimePoint::fromNs(prev + minFramePeriod);
430 prev = current.ns();
431 } else {
432 break;
433 }
434 }
435
436 if (!mPastExpectedPresentTimes.empty()) {
437 const auto phase = Duration(mPastExpectedPresentTimes.back() - expectedPresentTime);
438 if (phase > 0ns) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000439 for (auto& timeline : mTimelines) {
440 timeline.shiftVsyncSequence(phase);
Ady Abrahame9883032023-11-20 17:54:54 -0800441 }
Ady Abraham4335afd2023-12-18 19:10:47 -0800442 mPastExpectedPresentTimes.clear();
Ady Abraham20024aa2024-03-05 01:32:49 +0000443 return phase;
Ady Abrahame9883032023-11-20 17:54:54 -0800444 }
445 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000446
447 return 0ns;
Ady Abrahame9883032023-11-20 17:54:54 -0800448}
449
450void VSyncPredictor::onFrameBegin(TimePoint expectedPresentTime,
451 TimePoint lastConfirmedPresentTime) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000452 ATRACE_NAME("VSyncPredictor::onFrameBegin");
Ady Abrahame9883032023-11-20 17:54:54 -0800453 std::lock_guard lock(mMutex);
454
455 if (!mDisplayModePtr->getVrrConfig()) return;
456
457 if (CC_UNLIKELY(mTraceOn)) {
458 ATRACE_FORMAT_INSTANT("vsync is %.2f past last signaled fence",
459 static_cast<float>(expectedPresentTime.ns() -
460 lastConfirmedPresentTime.ns()) /
461 1e6f);
462 }
Ady Abrahame9883032023-11-20 17:54:54 -0800463 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
464 const auto threshold = currentPeriod / 2;
Ady Abraham4335afd2023-12-18 19:10:47 -0800465 mPastExpectedPresentTimes.push_back(expectedPresentTime);
Ady Abrahame9883032023-11-20 17:54:54 -0800466
Ady Abrahame9883032023-11-20 17:54:54 -0800467 while (!mPastExpectedPresentTimes.empty()) {
468 const auto front = mPastExpectedPresentTimes.front().ns();
Ady Abraham4335afd2023-12-18 19:10:47 -0800469 const bool frontIsBeforeConfirmed = front < lastConfirmedPresentTime.ns() + threshold;
470 if (frontIsBeforeConfirmed) {
Ady Abrahame9883032023-11-20 17:54:54 -0800471 if (CC_UNLIKELY(mTraceOn)) {
472 ATRACE_FORMAT_INSTANT("Discarding old vsync - %.2f before last signaled fence",
Ady Abraham4335afd2023-12-18 19:10:47 -0800473 static_cast<float>(lastConfirmedPresentTime.ns() - front) /
Ady Abrahame9883032023-11-20 17:54:54 -0800474 1e6f);
475 }
476 mPastExpectedPresentTimes.pop_front();
477 } else {
478 break;
479 }
480 }
481
Ady Abraham20024aa2024-03-05 01:32:49 +0000482 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
483 if (phase > 0ns) {
484 mMissedVsync = {expectedPresentTime, minFramePeriodLocked()};
485 }
Ady Abrahame9883032023-11-20 17:54:54 -0800486}
487
488void VSyncPredictor::onFrameMissed(TimePoint expectedPresentTime) {
Ady Abraham20024aa2024-03-05 01:32:49 +0000489 ATRACE_NAME("VSyncPredictor::onFrameMissed");
Ady Abrahame9883032023-11-20 17:54:54 -0800490
491 std::lock_guard lock(mMutex);
492 if (!mDisplayModePtr->getVrrConfig()) return;
493
494 // We don't know when the frame is going to be presented, so we assume it missed one vsync
495 const auto currentPeriod = mRateMap.find(idealPeriod())->second.slope;
496 const auto lastConfirmedPresentTime =
497 TimePoint::fromNs(expectedPresentTime.ns() + currentPeriod);
498
Ady Abraham20024aa2024-03-05 01:32:49 +0000499 const auto phase = ensureMinFrameDurationIsKept(expectedPresentTime, lastConfirmedPresentTime);
500 if (phase > 0ns) {
501 mMissedVsync = {expectedPresentTime, Duration::fromNs(0)};
502 }
Ady Abrahame9883032023-11-20 17:54:54 -0800503}
504
Ady Abraham0bb6a472020-10-12 10:22:13 -0700505VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
506 std::lock_guard lock(mMutex);
Ady Abraham20024aa2024-03-05 01:32:49 +0000507 return VSyncPredictor::getVSyncPredictionModelLocked();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700508}
509
510VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
Ady Abrahamc585dba2023-11-15 18:41:35 -0800511 return mRateMap.find(idealPeriod())->second;
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800512}
513
514void VSyncPredictor::clearTimestamps() {
Ady Abrahamf0b2bf92023-12-13 23:36:35 +0000515 ATRACE_CALL();
516
Ady Abraham92fa2f42020-02-11 15:33:56 -0800517 if (!mTimestamps.empty()) {
Kevin DuBois241d0ee2020-06-26 17:00:15 -0700518 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
519 if (mKnownTimestamp) {
520 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
521 } else {
522 mKnownTimestamp = maxRb;
523 }
524
Ady Abraham92fa2f42020-02-11 15:33:56 -0800525 mTimestamps.clear();
526 mLastTimestampIndex = 0;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700527 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000528
529 mTimelines.clear();
530 mLastCommittedVsync = TimePoint::fromNs(0);
531 mIdealPeriod = Period::fromNs(idealPeriod());
532 mTimelines.emplace_back(mIdealPeriod, mRenderRateOpt);
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700533}
534
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700535bool VSyncPredictor::needsMoreSamples() const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700536 std::lock_guard lock(mMutex);
Kevin DuBoisb818bfa2020-07-10 14:29:36 -0700537 return mTimestamps.size() < kMinimumSamplesForPrediction;
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700538}
539
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800540void VSyncPredictor::resetModel() {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700541 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800542 mRateMap[idealPeriod()] = {idealPeriod(), 0};
Kevin DuBoisc3e9e8e2020-01-07 09:06:52 -0800543 clearTimestamps();
544}
545
Ady Abraham5e7371c2020-03-24 14:47:24 -0700546void VSyncPredictor::dump(std::string& result) const {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700547 std::lock_guard lock(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -0800548 StringAppendF(&result, "\tmDisplayModePtr=%s\n", to_string(*mDisplayModePtr).c_str());
Ady Abraham5e7371c2020-03-24 14:47:24 -0700549 StringAppendF(&result, "\tRefresh Rate Map:\n");
Ady Abrahamc585dba2023-11-15 18:41:35 -0800550 for (const auto& [period, periodInterceptTuple] : mRateMap) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700551 StringAppendF(&result,
552 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
Ady Abrahamc585dba2023-11-15 18:41:35 -0800553 period / 1e6f, periodInterceptTuple.slope / 1e6f,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700554 periodInterceptTuple.intercept);
Ady Abraham5e7371c2020-03-24 14:47:24 -0700555 }
Ady Abraham20024aa2024-03-05 01:32:49 +0000556 StringAppendF(&result, "\tmTimelines.size()=%zu\n", mTimelines.size());
557}
558
559void VSyncPredictor::purgeTimelines(android::TimePoint now) {
560 while (mTimelines.size() > 1) {
561 const auto validUntilOpt = mTimelines.front().validUntil();
562 if (validUntilOpt && *validUntilOpt < now) {
563 mTimelines.pop_front();
564 } else {
565 break;
566 }
567 }
568 LOG_ALWAYS_FATAL_IF(mTimelines.empty());
569 LOG_ALWAYS_FATAL_IF(mTimelines.back().validUntil().has_value());
570}
571
572VSyncPredictor::VsyncTimeline::VsyncTimeline(Period idealPeriod, std::optional<Fps> renderRateOpt)
573 : mIdealPeriod(idealPeriod), mRenderRateOpt(renderRateOpt) {}
574
575void VSyncPredictor::VsyncTimeline::freeze(TimePoint lastVsync) {
576 LOG_ALWAYS_FATAL_IF(mValidUntil.has_value());
577 ATRACE_FORMAT_INSTANT("renderRate %s valid for %.2f",
578 mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA",
579 float(lastVsync.ns() - TimePoint::now().ns()) / 1e6f);
580 mValidUntil = lastVsync;
581}
582
583std::optional<TimePoint> VSyncPredictor::VsyncTimeline::nextAnticipatedVSyncTimeFrom(
584 Model model, Period minFramePeriod, nsecs_t vsync, MissedVsync missedVsync,
585 std::optional<nsecs_t> lastVsyncOpt) {
586 ATRACE_FORMAT("renderRate %s", mRenderRateOpt ? to_string(*mRenderRateOpt).c_str() : "NA");
587
588 const auto threshold = model.slope / 2;
589 const auto lastFrameMissed =
590 lastVsyncOpt && std::abs(*lastVsyncOpt - missedVsync.vsync.ns()) < threshold;
591 nsecs_t vsyncTime = snapToVsyncAlignedWithRenderRate(model, vsync);
592 nsecs_t vsyncFixupTime = 0;
593 if (FlagManager::getInstance().vrr_config() && lastFrameMissed) {
594 vsyncTime += missedVsync.fixup.ns();
595 ATRACE_FORMAT_INSTANT("lastFrameMissed");
596 } else {
597 vsyncFixupTime = getVsyncFixup(model, minFramePeriod, vsyncTime, lastVsyncOpt);
598 vsyncTime += vsyncFixupTime;
599 }
600
601 ATRACE_FORMAT_INSTANT("vsync in %.2fms", float(vsyncTime - TimePoint::now().ns()) / 1e6f);
602 if (mValidUntil && vsyncTime > mValidUntil->ns()) {
603 ATRACE_FORMAT_INSTANT("no longer valid for vsync in %.2f",
604 static_cast<float>(vsyncTime - TimePoint::now().ns()) / 1e6f);
605 return std::nullopt;
606 }
607
608 if (vsyncFixupTime > 0) {
609 shiftVsyncSequence(Duration::fromNs(vsyncFixupTime));
610 }
611
612 return TimePoint::fromNs(vsyncTime);
613}
614
615auto VSyncPredictor::VsyncTimeline::getVsyncSequenceLocked(Model model, nsecs_t vsync)
616 -> VsyncSequence {
617 if (!mLastVsyncSequence) return {vsync, 0};
618
619 const auto [lastVsyncTime, lastVsyncSequence] = *mLastVsyncSequence;
620 const auto vsyncSequence = lastVsyncSequence +
621 static_cast<int64_t>(std::round((vsync - lastVsyncTime) /
622 static_cast<float>(model.slope)));
623 return {vsync, vsyncSequence};
624}
625
626nsecs_t VSyncPredictor::VsyncTimeline::snapToVsyncAlignedWithRenderRate(Model model,
627 nsecs_t vsync) {
628 // update the mLastVsyncSequence for reference point
629 mLastVsyncSequence = getVsyncSequenceLocked(model, vsync);
630
631 const auto renderRatePhase = [&]() -> int {
632 if (!mRenderRateOpt) return 0;
633 const auto divisor =
634 RefreshRateSelector::getFrameRateDivisor(Fps::fromPeriodNsecs(mIdealPeriod.ns()),
635 *mRenderRateOpt);
636 if (divisor <= 1) return 0;
637
638 int mod = mLastVsyncSequence->seq % divisor;
639 if (mod == 0) return 0;
640
641 // This is actually a bug fix, but guarded with vrr_config since we found it with this
642 // config
643 if (FlagManager::getInstance().vrr_config()) {
644 if (mod < 0) mod += divisor;
645 }
646
647 return divisor - mod;
648 }();
649
650 if (renderRatePhase == 0) {
651 return mLastVsyncSequence->vsyncTime;
652 }
653
654 return mLastVsyncSequence->vsyncTime + model.slope * renderRatePhase;
655}
656
657bool VSyncPredictor::VsyncTimeline::isVSyncInPhase(Model model, nsecs_t vsync, Fps frameRate) {
658 const auto getVsyncIn = [](TimePoint now, nsecs_t timePoint) -> float {
659 return ticks<std::milli, float>(TimePoint::fromNs(timePoint) - now);
660 };
661
662 Fps displayFps = mRenderRateOpt ? *mRenderRateOpt : Fps::fromPeriodNsecs(mIdealPeriod.ns());
663 const auto divisor = RefreshRateSelector::getFrameRateDivisor(displayFps, frameRate);
664 const auto now = TimePoint::now();
665
666 if (divisor <= 1) {
667 return true;
668 }
669 const auto vsyncSequence = getVsyncSequenceLocked(model, vsync);
670 ATRACE_FORMAT_INSTANT("vsync in: %.2f sequence: %" PRId64 " divisor: %zu",
671 getVsyncIn(now, vsyncSequence.vsyncTime), vsyncSequence.seq, divisor);
672 return vsyncSequence.seq % divisor == 0;
673}
674
675void VSyncPredictor::VsyncTimeline::shiftVsyncSequence(Duration phase) {
676 if (mLastVsyncSequence) {
677 ATRACE_FORMAT_INSTANT("adjusting vsync by %.2f", static_cast<float>(phase.ns()) / 1e6f);
678 mLastVsyncSequence->vsyncTime += phase.ns();
679 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700680}
681
Kevin DuBois1678e2c2019-08-22 12:26:24 -0700682} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800683
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100684// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski62eff352021-12-06 09:59:41 -0800685#pragma clang diagnostic pop // ignored "-Wextra"