1 /*
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
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 #pragma clang diagnostic ignored "-Wextra"
21
22 #undef LOG_TAG
23 #define LOG_TAG "LibSurfaceFlingerUnittests"
24 #define LOG_NDEBUG 0
25
26 #include <common/test/FlagUtils.h>
27 #include "Scheduler/VSyncPredictor.h"
28 #include "mock/DisplayHardware/MockDisplayMode.h"
29
30 #include <gmock/gmock.h>
31 #include <gtest/gtest.h>
32 #include <algorithm>
33 #include <chrono>
34 #include <optional>
35 #include <utility>
36
37 #include <com_android_graphics_surfaceflinger_flags.h>
38
39 using namespace testing;
40 using namespace std::literals;
41 using namespace com::android::graphics::surfaceflinger;
42
43 using NotifyExpectedPresentConfig =
44 ::aidl::android::hardware::graphics::composer3::VrrConfig::NotifyExpectedPresentConfig;
45
46 using android::mock::createDisplayMode;
47 using android::mock::createDisplayModeBuilder;
48 using android::mock::createVrrDisplayMode;
49
50 namespace android::scheduler {
51
52 namespace {
53 MATCHER_P2(IsCloseTo, value, tolerance, "is within tolerance") {
54 return arg <= value + tolerance && arg >= value - tolerance;
55 }
56
57 MATCHER_P(FpsMatcher, value, "equals") {
58 using fps_approx_ops::operator==;
59 return arg == value;
60 }
61
generateVsyncTimestamps(size_t count,nsecs_t period,nsecs_t bias)62 std::vector<nsecs_t> generateVsyncTimestamps(size_t count, nsecs_t period, nsecs_t bias) {
63 std::vector<nsecs_t> vsyncs(count);
64 std::generate(vsyncs.begin(), vsyncs.end(),
65 [&, n = 0]() mutable { return n++ * period + bias; });
66 return vsyncs;
67 }
68
69 constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(42u);
70
displayMode(nsecs_t period)71 ftl::NonNull<DisplayModePtr> displayMode(nsecs_t period) {
72 const int32_t kGroup = 0;
73 const auto kResolution = ui::Size(1920, 1080);
74 const auto refreshRate = Fps::fromPeriodNsecs(period);
75 return ftl::as_non_null(createDisplayMode(DisplayModeId(0), refreshRate, kGroup, kResolution,
76 DEFAULT_DISPLAY_ID));
77 }
78
79 class TestClock : public Clock {
80 public:
81 TestClock() = default;
82
now() const83 nsecs_t now() const override { return mNow; }
setNow(nsecs_t now)84 void setNow(nsecs_t now) { mNow = now; }
85
86 private:
87 nsecs_t mNow = 0;
88 };
89
90 class ClockWrapper : public Clock {
91 public:
ClockWrapper(std::shared_ptr<Clock> const & clock)92 ClockWrapper(std::shared_ptr<Clock> const& clock) : mClock(clock) {}
93
now() const94 nsecs_t now() const { return mClock->now(); }
95
96 private:
97 std::shared_ptr<Clock> const mClock;
98 };
99
100 } // namespace
101
102 struct VSyncPredictorTest : testing::Test {
103 nsecs_t mNow = 0;
104 nsecs_t mPeriod = 1000;
105 ftl::NonNull<DisplayModePtr> mMode = displayMode(mPeriod);
106 static constexpr size_t kHistorySize = 10;
107 static constexpr size_t kMinimumSamplesForPrediction = 6;
108 static constexpr size_t kOutlierTolerancePercent = 25;
109 static constexpr nsecs_t mMaxRoundingError = 100;
110
111 std::shared_ptr<TestClock> mClock{std::make_shared<TestClock>()};
112
113 VSyncPredictor tracker{std::make_unique<ClockWrapper>(mClock), mMode, kHistorySize,
114 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
115 };
116
TEST_F(VSyncPredictorTest,reportsAnticipatedPeriod)117 TEST_F(VSyncPredictorTest, reportsAnticipatedPeriod) {
118 auto model = tracker.getVSyncPredictionModel();
119
120 EXPECT_THAT(model.slope, Eq(mPeriod));
121 EXPECT_THAT(model.intercept, Eq(0));
122
123 auto const changedPeriod = 2000;
124 tracker.setDisplayModePtr(displayMode(changedPeriod));
125 model = tracker.getVSyncPredictionModel();
126 EXPECT_THAT(model.slope, Eq(changedPeriod));
127 EXPECT_THAT(model.intercept, Eq(0));
128 }
129
TEST_F(VSyncPredictorTest,reportsSamplesNeededWhenHasNoDataPoints)130 TEST_F(VSyncPredictorTest, reportsSamplesNeededWhenHasNoDataPoints) {
131 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
132 EXPECT_TRUE(tracker.needsMoreSamples());
133 tracker.addVsyncTimestamp(mNow += mPeriod);
134 }
135 EXPECT_FALSE(tracker.needsMoreSamples());
136 }
137
TEST_F(VSyncPredictorTest,reportsSamplesNeededAfterExplicitRateChange)138 TEST_F(VSyncPredictorTest, reportsSamplesNeededAfterExplicitRateChange) {
139 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
140 tracker.addVsyncTimestamp(mNow += mPeriod);
141 }
142 EXPECT_FALSE(tracker.needsMoreSamples());
143
144 auto const changedPeriod = mPeriod * 2;
145 tracker.setDisplayModePtr(displayMode(changedPeriod));
146 EXPECT_TRUE(tracker.needsMoreSamples());
147
148 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
149 EXPECT_TRUE(tracker.needsMoreSamples());
150 tracker.addVsyncTimestamp(mNow += changedPeriod);
151 }
152 EXPECT_FALSE(tracker.needsMoreSamples());
153 }
154
TEST_F(VSyncPredictorTest,transitionsToModelledPointsAfterSynthetic)155 TEST_F(VSyncPredictorTest, transitionsToModelledPointsAfterSynthetic) {
156 auto last = mNow;
157 auto const bias = 10;
158 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
159 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
160 mNow += mPeriod - bias;
161 last = mNow;
162 tracker.addVsyncTimestamp(mNow);
163 mNow += bias;
164 }
165
166 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod - bias));
167 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 100), Eq(mNow + mPeriod - bias));
168 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 990), Eq(mNow + 2 * mPeriod - bias));
169 }
170
TEST_F(VSyncPredictorTest,uponNotifiedOfInaccuracyUsesSynthetic)171 TEST_F(VSyncPredictorTest, uponNotifiedOfInaccuracyUsesSynthetic) {
172 auto const slightlyLessPeriod = mPeriod - 10;
173 auto const changedPeriod = mPeriod - 1;
174 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
175 tracker.addVsyncTimestamp(mNow += slightlyLessPeriod);
176 }
177
178 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + slightlyLessPeriod));
179 tracker.setDisplayModePtr(displayMode(changedPeriod));
180 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + changedPeriod));
181 }
182
183 // b/159882858
TEST_F(VSyncPredictorTest,updatesTimebaseForSyntheticAfterIdleTime)184 TEST_F(VSyncPredictorTest, updatesTimebaseForSyntheticAfterIdleTime) {
185 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
186 EXPECT_TRUE(tracker.addVsyncTimestamp(mNow += mPeriod));
187 }
188
189 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
190
191 auto const halfPeriod = mPeriod >> 2;
192 nsecs_t relativelyLongGapWithDrift = mPeriod * 100 + halfPeriod;
193
194 EXPECT_FALSE(tracker.addVsyncTimestamp(mNow += relativelyLongGapWithDrift));
195
196 tracker.resetModel();
197 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
198 }
199
TEST_F(VSyncPredictorTest,uponBadVsyncWillSwitchToSyntheticWhileRecalibrating)200 TEST_F(VSyncPredictorTest, uponBadVsyncWillSwitchToSyntheticWhileRecalibrating) {
201 auto const slightlyMorePeriod = mPeriod + 10;
202 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
203 EXPECT_TRUE(tracker.addVsyncTimestamp(mNow += slightlyMorePeriod));
204 }
205
206 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + slightlyMorePeriod));
207
208 auto const halfPeriod = mPeriod >> 2;
209 EXPECT_FALSE(tracker.addVsyncTimestamp(mNow += halfPeriod));
210
211 tracker.resetModel();
212 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
213 }
214
TEST_F(VSyncPredictorTest,adaptsToFenceTimelines_60hzHighVariance)215 TEST_F(VSyncPredictorTest, adaptsToFenceTimelines_60hzHighVariance) {
216 // these are precomputed simulated 16.6s vsyncs with uniform distribution +/- 1.6ms error
217 std::vector<nsecs_t> const simulatedVsyncs{
218 15492949, 32325658, 49534984, 67496129, 84652891,
219 100332564, 117737004, 132125931, 149291099, 165199602,
220 };
221 auto constexpr idealPeriod = 16600000;
222 auto constexpr expectedPeriod = 16639242;
223 auto constexpr expectedIntercept = 1049341;
224
225 tracker.setDisplayModePtr(displayMode(idealPeriod));
226 for (auto const& timestamp : simulatedVsyncs) {
227 tracker.addVsyncTimestamp(timestamp);
228 }
229 auto [slope, intercept] = tracker.getVSyncPredictionModel();
230 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
231 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
232 }
233
TEST_F(VSyncPredictorTest,adaptsToFenceTimelines_90hzLowVariance)234 TEST_F(VSyncPredictorTest, adaptsToFenceTimelines_90hzLowVariance) {
235 // these are precomputed simulated 11.1 vsyncs with uniform distribution +/- 1ms error
236 std::vector<nsecs_t> const simulatedVsyncs{
237 11167047, 22603464, 32538479, 44938134, 56321268,
238 66730346, 78062637, 88171429, 99707843, 111397621,
239 };
240 auto idealPeriod = 11110000;
241 auto expectedPeriod = 11089413;
242 auto expectedIntercept = 94421;
243
244 tracker.setDisplayModePtr(displayMode(idealPeriod));
245 for (auto const& timestamp : simulatedVsyncs) {
246 tracker.addVsyncTimestamp(timestamp);
247 }
248 auto [slope, intercept] = tracker.getVSyncPredictionModel();
249 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
250 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
251 }
252
TEST_F(VSyncPredictorTest,adaptsToFenceTimelinesDiscontinuous_22hzLowVariance)253 TEST_F(VSyncPredictorTest, adaptsToFenceTimelinesDiscontinuous_22hzLowVariance) {
254 // these are 11.1s vsyncs with low variance, randomly computed, between -1 and 1ms
255 std::vector<nsecs_t> const simulatedVsyncs{
256 45259463, // 0
257 91511026, // 1
258 136307650, // 2
259 1864501714, // 40
260 1908641034, // 41
261 1955278544, // 42
262 4590180096, // 100
263 4681594994, // 102
264 5499224734, // 120
265 5591378272, // 122
266 };
267 auto idealPeriod = 45454545;
268 auto expectedPeriod = 45450152;
269 auto expectedIntercept = 469647;
270
271 tracker.setDisplayModePtr(displayMode(idealPeriod));
272 for (auto const& timestamp : simulatedVsyncs) {
273 tracker.addVsyncTimestamp(timestamp);
274 }
275 auto [slope, intercept] = tracker.getVSyncPredictionModel();
276 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
277 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
278 }
279
TEST_F(VSyncPredictorTest,againstOutliersDiscontinuous_500hzLowVariance)280 TEST_F(VSyncPredictorTest, againstOutliersDiscontinuous_500hzLowVariance) {
281 std::vector<nsecs_t> const simulatedVsyncs{
282 1992548, // 0
283 4078038, // 1
284 6165794, // 2
285 7958171, // 3
286 10193537, // 4
287 2401840200, // 1200
288 2403000000, // an outlier that should be excluded (1201 and a half)
289 2405803629, // 1202
290 2408028599, // 1203
291 2410121051, // 1204
292 };
293 auto idealPeriod = 2000000;
294 auto expectedPeriod = 1999892;
295 auto expectedIntercept = 86342;
296
297 tracker.setDisplayModePtr(displayMode(idealPeriod));
298 for (auto const& timestamp : simulatedVsyncs) {
299 tracker.addVsyncTimestamp(timestamp);
300 }
301
302 auto [slope, intercept] = tracker.getVSyncPredictionModel();
303 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
304 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
305 }
306
TEST_F(VSyncPredictorTest,handlesVsyncChange)307 TEST_F(VSyncPredictorTest, handlesVsyncChange) {
308 auto const fastPeriod = 100;
309 auto const fastTimeBase = 100;
310 auto const slowPeriod = 400;
311 auto const slowTimeBase = 800;
312 auto const simulatedVsyncsFast =
313 generateVsyncTimestamps(kMinimumSamplesForPrediction, fastPeriod, fastTimeBase);
314 auto const simulatedVsyncsSlow =
315 generateVsyncTimestamps(kMinimumSamplesForPrediction, slowPeriod, slowTimeBase);
316
317 tracker.setDisplayModePtr(displayMode(fastPeriod));
318 for (auto const& timestamp : simulatedVsyncsFast) {
319 tracker.addVsyncTimestamp(timestamp);
320 }
321
322 auto const mMaxRoundingError = 100;
323 auto model = tracker.getVSyncPredictionModel();
324 EXPECT_THAT(model.slope, IsCloseTo(fastPeriod, mMaxRoundingError));
325 EXPECT_THAT(model.intercept, IsCloseTo(0, mMaxRoundingError));
326
327 tracker.setDisplayModePtr(displayMode(slowPeriod));
328 for (auto const& timestamp : simulatedVsyncsSlow) {
329 tracker.addVsyncTimestamp(timestamp);
330 }
331 model = tracker.getVSyncPredictionModel();
332 EXPECT_THAT(model.slope, IsCloseTo(slowPeriod, mMaxRoundingError));
333 EXPECT_THAT(model.intercept, IsCloseTo(0, mMaxRoundingError));
334 }
335
TEST_F(VSyncPredictorTest,willBeAccurateUsingPriorResultsForRate)336 TEST_F(VSyncPredictorTest, willBeAccurateUsingPriorResultsForRate) {
337 auto const fastPeriod = 101000;
338 auto const fastTimeBase = fastPeriod - 500;
339 auto const fastPeriod2 = 99000;
340
341 auto const slowPeriod = 400000;
342 auto const slowTimeBase = 800000 - 201;
343 auto const simulatedVsyncsFast =
344 generateVsyncTimestamps(kMinimumSamplesForPrediction, fastPeriod, fastTimeBase);
345 auto const simulatedVsyncsSlow =
346 generateVsyncTimestamps(kMinimumSamplesForPrediction, slowPeriod, slowTimeBase);
347 auto const simulatedVsyncsFast2 =
348 generateVsyncTimestamps(kMinimumSamplesForPrediction, fastPeriod2, fastTimeBase);
349
350 auto idealPeriod = 100000;
351 tracker.setDisplayModePtr(displayMode(idealPeriod));
352 for (auto const& timestamp : simulatedVsyncsFast) {
353 tracker.addVsyncTimestamp(timestamp);
354 }
355 auto model = tracker.getVSyncPredictionModel();
356 EXPECT_THAT(model.slope, Eq(fastPeriod));
357 EXPECT_THAT(model.intercept, Eq(0));
358
359 tracker.setDisplayModePtr(displayMode(slowPeriod));
360 for (auto const& timestamp : simulatedVsyncsSlow) {
361 tracker.addVsyncTimestamp(timestamp);
362 }
363
364 // we had a model for 100ns mPeriod before, use that until the new samples are
365 // sufficiently built up
366 tracker.setDisplayModePtr(displayMode(idealPeriod));
367 model = tracker.getVSyncPredictionModel();
368 EXPECT_THAT(model.slope, Eq(fastPeriod));
369 EXPECT_THAT(model.intercept, Eq(0));
370
371 for (auto const& timestamp : simulatedVsyncsFast2) {
372 tracker.addVsyncTimestamp(timestamp);
373 }
374 model = tracker.getVSyncPredictionModel();
375 EXPECT_THAT(model.slope, Eq(fastPeriod2));
376 EXPECT_THAT(model.intercept, Eq(0));
377 }
378
TEST_F(VSyncPredictorTest,idealModelPredictionsBeforeRegressionModelIsBuilt)379 TEST_F(VSyncPredictorTest, idealModelPredictionsBeforeRegressionModelIsBuilt) {
380 auto const simulatedVsyncs =
381 generateVsyncTimestamps(kMinimumSamplesForPrediction + 1, mPeriod, 0);
382 nsecs_t const mNow = 0;
383 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mPeriod));
384
385 nsecs_t const aBitOfTime = 422;
386
387 for (auto i = 0; i < kMinimumSamplesForPrediction; i++) {
388 tracker.addVsyncTimestamp(simulatedVsyncs[i]);
389 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(simulatedVsyncs[i] + aBitOfTime),
390 Eq(mPeriod + simulatedVsyncs[i]));
391 }
392
393 for (auto i = kMinimumSamplesForPrediction; i < simulatedVsyncs.size(); i++) {
394 tracker.addVsyncTimestamp(simulatedVsyncs[i]);
395 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(simulatedVsyncs[i] + aBitOfTime),
396 Eq(mPeriod + simulatedVsyncs[i]));
397 }
398 }
399
400 // See b/145667109, and comment in prod code under test.
TEST_F(VSyncPredictorTest,doesNotPredictBeforeTimePointWithHigherIntercept)401 TEST_F(VSyncPredictorTest, doesNotPredictBeforeTimePointWithHigherIntercept) {
402 std::vector<nsecs_t> const simulatedVsyncs{
403 158929578733000,
404 158929306806205, // oldest TS in ringbuffer
405 158929650879052,
406 158929661969209,
407 158929684198847,
408 158929695268171,
409 158929706370359,
410 };
411 auto const idealPeriod = 11111111;
412 auto const expectedPeriod = 11113919;
413 auto const expectedIntercept = -1195945;
414
415 tracker.setDisplayModePtr(displayMode(idealPeriod));
416 for (auto const& timestamp : simulatedVsyncs) {
417 tracker.addVsyncTimestamp(timestamp);
418 }
419
420 auto [slope, intercept] = tracker.getVSyncPredictionModel();
421 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
422 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
423
424 // (timePoint - oldestTS) % expectedPeriod works out to be: 395334
425 // (timePoint - oldestTS) / expectedPeriod works out to be: 38.96
426 // so failure to account for the offset will floor the ordinal to 38, which was in the past.
427 auto const timePoint = 158929728723871;
428 auto const prediction = tracker.nextAnticipatedVSyncTimeFrom(timePoint);
429 EXPECT_THAT(prediction, Ge(timePoint));
430 }
431
432 // See b/151146131
TEST_F(VSyncPredictorTest,hasEnoughPrecision)433 TEST_F(VSyncPredictorTest, hasEnoughPrecision) {
434 const auto mode = displayMode(mPeriod);
435 VSyncPredictor tracker{std::make_unique<ClockWrapper>(mClock), mode, 20,
436 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
437 std::vector<nsecs_t> const simulatedVsyncs{840873348817, 840890049444, 840906762675,
438 840923581635, 840940161584, 840956868096,
439 840973702473, 840990256277, 841007116851,
440 841023722530, 841040452167, 841057073002,
441 841073800920, 841090474360, 841107278632,
442 841123898634, 841140750875, 841157287127,
443 841591357014, 840856664232
444
445 };
446 auto const idealPeriod = 16666666;
447 auto const expectedPeriod = 16698426;
448 auto const expectedIntercept = 58055;
449
450 tracker.setDisplayModePtr(displayMode(idealPeriod));
451 for (auto const& timestamp : simulatedVsyncs) {
452 tracker.addVsyncTimestamp(timestamp);
453 }
454
455 auto [slope, intercept] = tracker.getVSyncPredictionModel();
456 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
457 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
458 }
459
TEST_F(VSyncPredictorTest,resetsWhenInstructed)460 TEST_F(VSyncPredictorTest, resetsWhenInstructed) {
461 auto const idealPeriod = 10000;
462 auto const realPeriod = 10500;
463 tracker.setDisplayModePtr(displayMode(idealPeriod));
464 for (auto i = 0; i < kMinimumSamplesForPrediction; i++) {
465 tracker.addVsyncTimestamp(i * realPeriod);
466 }
467
468 EXPECT_THAT(tracker.getVSyncPredictionModel().slope, IsCloseTo(realPeriod, mMaxRoundingError));
469 tracker.resetModel();
470 EXPECT_THAT(tracker.getVSyncPredictionModel().slope, IsCloseTo(idealPeriod, mMaxRoundingError));
471 }
472
TEST_F(VSyncPredictorTest,slopeAlwaysValid)473 TEST_F(VSyncPredictorTest, slopeAlwaysValid) {
474 constexpr auto kNumVsyncs = 100;
475 auto invalidPeriod = mPeriod;
476 auto now = 0;
477 for (int i = 0; i < kNumVsyncs; i++) {
478 tracker.addVsyncTimestamp(now);
479 now += invalidPeriod;
480 invalidPeriod *= 0.9f;
481
482 auto [slope, intercept] = tracker.getVSyncPredictionModel();
483 EXPECT_THAT(slope, IsCloseTo(mPeriod, mPeriod * kOutlierTolerancePercent / 100.f));
484
485 // When VsyncPredictor returns the period it means that it doesn't know how to predict and
486 // it needs to get more samples
487 if (slope == mPeriod && intercept == 0) {
488 EXPECT_TRUE(tracker.needsMoreSamples());
489 }
490 }
491 }
492
operator ""_years(unsigned long long years)493 constexpr nsecs_t operator""_years(unsigned long long years) noexcept {
494 using namespace std::chrono_literals;
495 return years * 365 * 24 * 3600 *
496 std::chrono::duration_cast<std::chrono::nanoseconds>(1s).count();
497 }
TEST_F(VSyncPredictorTest,aPhoneThatHasBeenAroundAWhileCanStillComputePeriod)498 TEST_F(VSyncPredictorTest, aPhoneThatHasBeenAroundAWhileCanStillComputePeriod) {
499 constexpr nsecs_t timeBase = 100_years;
500
501 for (auto i = 0; i < kHistorySize; i++) {
502 tracker.addVsyncTimestamp(timeBase + i * mPeriod);
503 }
504 auto [slope, intercept] = tracker.getVSyncPredictionModel();
505 EXPECT_THAT(slope, IsCloseTo(mPeriod, mMaxRoundingError));
506 EXPECT_THAT(intercept, Eq(0));
507 }
508
TEST_F(VSyncPredictorTest,isVSyncInPhase)509 TEST_F(VSyncPredictorTest, isVSyncInPhase) {
510 auto last = mNow;
511 auto const bias = 10;
512 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
513 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
514 mNow += mPeriod - bias;
515 last = mNow;
516 tracker.addVsyncTimestamp(mNow);
517 mNow += bias;
518 }
519
520 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod - bias));
521 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 100), Eq(mNow + mPeriod - bias));
522 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 990), Eq(mNow + 2 * mPeriod - bias));
523
524 const auto maxDivisor = 5;
525 const auto maxPeriods = 15;
526 for (int divisor = 1; divisor < maxDivisor; divisor++) {
527 for (int i = 0; i < maxPeriods; i++) {
528 const bool expectedInPhase = ((kMinimumSamplesForPrediction - 1 + i) % divisor) == 0;
529 EXPECT_THAT(expectedInPhase,
530 tracker.isVSyncInPhase(mNow + i * mPeriod - bias,
531 Fps::fromPeriodNsecs(divisor * mPeriod)))
532 << "vsync at " << mNow + (i + 1) * mPeriod - bias << " is "
533 << (expectedInPhase ? "not " : "") << "in phase for divisor " << divisor;
534 }
535 }
536 }
537
TEST_F(VSyncPredictorTest,isVSyncInPhaseWithRenderRate)538 TEST_F(VSyncPredictorTest, isVSyncInPhaseWithRenderRate) {
539 SET_FLAG_FOR_TEST(flags::vrr_bugfix_24q4, true);
540 auto last = mNow;
541 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
542 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
543 mNow += mPeriod;
544 last = mNow;
545 tracker.addVsyncTimestamp(mNow);
546 }
547
548 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
549 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + mPeriod), Eq(mNow + 2 * mPeriod));
550
551 const auto renderRateFps = Fps::fromPeriodNsecs(mPeriod * 2);
552 tracker.setRenderRate(renderRateFps, /*applyImmediately*/ true);
553
554 EXPECT_FALSE(tracker.isVSyncInPhase(mNow, renderRateFps));
555 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + mPeriod, renderRateFps));
556 EXPECT_FALSE(tracker.isVSyncInPhase(mNow + 2 * mPeriod, renderRateFps));
557 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + 3 * mPeriod, renderRateFps));
558 }
559
TEST_F(VSyncPredictorTest,isVSyncInPhaseForDivisors)560 TEST_F(VSyncPredictorTest, isVSyncInPhaseForDivisors) {
561 auto last = mNow;
562 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
563 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
564 mNow += mPeriod;
565 last = mNow;
566 tracker.addVsyncTimestamp(mNow);
567 }
568
569 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
570
571 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + 1 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 2)));
572 EXPECT_FALSE(tracker.isVSyncInPhase(mNow + 2 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 2)));
573 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + 3 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 2)));
574
575 EXPECT_FALSE(tracker.isVSyncInPhase(mNow + 5 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 4)));
576 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + 3 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 4)));
577 EXPECT_FALSE(tracker.isVSyncInPhase(mNow + 4 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 4)));
578 EXPECT_FALSE(tracker.isVSyncInPhase(mNow + 6 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 4)));
579 EXPECT_TRUE(tracker.isVSyncInPhase(mNow + 7 * mPeriod, Fps::fromPeriodNsecs(mPeriod * 4)));
580 }
581
TEST_F(VSyncPredictorTest,inconsistentVsyncValueIsFlushedEventually)582 TEST_F(VSyncPredictorTest, inconsistentVsyncValueIsFlushedEventually) {
583 EXPECT_TRUE(tracker.addVsyncTimestamp(600));
584 EXPECT_TRUE(tracker.needsMoreSamples());
585
586 EXPECT_FALSE(tracker.addVsyncTimestamp(mNow += mPeriod));
587
588 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
589 EXPECT_TRUE(tracker.needsMoreSamples());
590 EXPECT_TRUE(tracker.addVsyncTimestamp(mNow += mPeriod));
591 }
592
593 EXPECT_FALSE(tracker.needsMoreSamples());
594 }
595
TEST_F(VSyncPredictorTest,knownVsyncIsUpdated)596 TEST_F(VSyncPredictorTest, knownVsyncIsUpdated) {
597 EXPECT_TRUE(tracker.addVsyncTimestamp(600));
598 EXPECT_TRUE(tracker.needsMoreSamples());
599 EXPECT_EQ(600, tracker.nextAnticipatedVSyncTimeFrom(mNow));
600
601 EXPECT_FALSE(tracker.addVsyncTimestamp(mNow += mPeriod));
602 EXPECT_EQ(mNow + 1000, tracker.nextAnticipatedVSyncTimeFrom(mNow));
603
604 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
605 EXPECT_TRUE(tracker.needsMoreSamples());
606 EXPECT_TRUE(tracker.addVsyncTimestamp(mNow += mPeriod));
607 EXPECT_EQ(mNow + 1000, tracker.nextAnticipatedVSyncTimeFrom(mNow));
608 }
609
610 EXPECT_FALSE(tracker.needsMoreSamples());
611 EXPECT_EQ(mNow + 1000, tracker.nextAnticipatedVSyncTimeFrom(mNow));
612 }
613
TEST_F(VSyncPredictorTest,robustToDuplicateTimestamps_60hzRealTraceData)614 TEST_F(VSyncPredictorTest, robustToDuplicateTimestamps_60hzRealTraceData) {
615 // these are real vsync timestamps from b/190331974 which caused vsync predictor
616 // period to spike to 18ms due to very close timestamps
617 std::vector<nsecs_t> const simulatedVsyncs{
618 198353408177, 198370074844, 198371400000, 198374274000, 198390941000, 198407565000,
619 198540887994, 198607538588, 198624218276, 198657655939, 198674224176, 198690880955,
620 198724204319, 198740988133, 198758166681, 198790869196, 198824205052, 198840871678,
621 198857715631, 198890885797, 198924199640, 198940873834, 198974204401,
622 };
623 auto constexpr idealPeriod = 16'666'666;
624 auto constexpr expectedPeriod = 16'644'742;
625 auto constexpr expectedIntercept = 125'626;
626
627 tracker.setDisplayModePtr(displayMode(idealPeriod));
628 for (auto const& timestamp : simulatedVsyncs) {
629 tracker.addVsyncTimestamp(timestamp);
630 }
631 auto [slope, intercept] = tracker.getVSyncPredictionModel();
632 EXPECT_THAT(slope, IsCloseTo(expectedPeriod, mMaxRoundingError));
633 EXPECT_THAT(intercept, IsCloseTo(expectedIntercept, mMaxRoundingError));
634 }
635
TEST_F(VSyncPredictorTest,setRenderRateIsRespected)636 TEST_F(VSyncPredictorTest, setRenderRateIsRespected) {
637 auto last = mNow;
638 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
639 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
640 mNow += mPeriod;
641 last = mNow;
642 tracker.addVsyncTimestamp(mNow);
643 }
644
645 tracker.setRenderRate(Fps::fromPeriodNsecs(3 * mPeriod), /*applyImmediately*/ false);
646
647 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + 3 * mPeriod));
648 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 100), Eq(mNow + 3 * mPeriod));
649 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 1100), Eq(mNow + 3 * mPeriod));
650 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 2100), Eq(mNow + 3 * mPeriod));
651 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 3100), Eq(mNow + 6 * mPeriod));
652 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 4100), Eq(mNow + 6 * mPeriod));
653 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 5100), Eq(mNow + 6 * mPeriod));
654 }
655
TEST_F(VSyncPredictorTest,setRenderRateIsIgnoredIfNotDivisor)656 TEST_F(VSyncPredictorTest, setRenderRateIsIgnoredIfNotDivisor) {
657 auto last = mNow;
658 for (auto i = 0u; i < kMinimumSamplesForPrediction; i++) {
659 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(last + mPeriod));
660 mNow += mPeriod;
661 last = mNow;
662 tracker.addVsyncTimestamp(mNow);
663 }
664
665 tracker.setRenderRate(Fps::fromPeriodNsecs(3.5f * mPeriod), /*applyImmediately*/ false);
666
667 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow), Eq(mNow + mPeriod));
668 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 100), Eq(mNow + mPeriod));
669 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 1100), Eq(mNow + 2 * mPeriod));
670 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 2100), Eq(mNow + 3 * mPeriod));
671 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 3100), Eq(mNow + 4 * mPeriod));
672 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 4100), Eq(mNow + 5 * mPeriod));
673 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(mNow + 5100), Eq(mNow + 6 * mPeriod));
674 }
675
TEST_F(VSyncPredictorTest,setRenderRateHighIsAppliedImmediately)676 TEST_F(VSyncPredictorTest, setRenderRateHighIsAppliedImmediately) {
677 SET_FLAG_FOR_TEST(flags::vrr_config, true);
678
679 const int32_t kGroup = 0;
680 const auto kResolution = ui::Size(1920, 1080);
681 const auto vsyncRate = Fps::fromPeriodNsecs(500);
682 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
683 hal::VrrConfig vrrConfig;
684 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
685 const ftl::NonNull<DisplayModePtr> kMode =
686 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), vsyncRate, kGroup,
687 kResolution, DEFAULT_DISPLAY_ID)
688 .setVrrConfig(std::move(vrrConfig))
689 .build());
690
691 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
692 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
693
694 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(1000), /*applyImmediately*/ false);
695 vrrTracker.addVsyncTimestamp(0);
696 EXPECT_EQ(1000, vrrTracker.nextAnticipatedVSyncTimeFrom(700));
697 EXPECT_EQ(2000, vrrTracker.nextAnticipatedVSyncTimeFrom(1000, 1000));
698
699 // commit to a vsync in the future
700 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
701
702 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(2000), /*applyImmediately*/ false);
703 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
704 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
705 EXPECT_EQ(8000, vrrTracker.nextAnticipatedVSyncTimeFrom(6000, 6000));
706
707 EXPECT_EQ(12000, vrrTracker.nextAnticipatedVSyncTimeFrom(10000, 10000));
708
709 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(3500), /*applyImmediately*/ false);
710 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
711 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
712 EXPECT_EQ(8000, vrrTracker.nextAnticipatedVSyncTimeFrom(6000, 6000));
713 EXPECT_EQ(10000, vrrTracker.nextAnticipatedVSyncTimeFrom(8000, 8000));
714 EXPECT_EQ(12000, vrrTracker.nextAnticipatedVSyncTimeFrom(10000, 10000));
715 EXPECT_EQ(15500, vrrTracker.nextAnticipatedVSyncTimeFrom(12000, 12000));
716 EXPECT_EQ(19000, vrrTracker.nextAnticipatedVSyncTimeFrom(15500, 15500));
717
718 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(2500), /*applyImmediately*/ false);
719 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
720 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
721 EXPECT_EQ(8000, vrrTracker.nextAnticipatedVSyncTimeFrom(6000, 6000));
722 EXPECT_EQ(10000, vrrTracker.nextAnticipatedVSyncTimeFrom(8000, 8000));
723 EXPECT_EQ(12000, vrrTracker.nextAnticipatedVSyncTimeFrom(10000, 10000));
724 EXPECT_EQ(15500, vrrTracker.nextAnticipatedVSyncTimeFrom(12000, 12000));
725 EXPECT_EQ(19000, vrrTracker.nextAnticipatedVSyncTimeFrom(15500, 15500));
726 EXPECT_EQ(21500, vrrTracker.nextAnticipatedVSyncTimeFrom(19000, 19000));
727
728 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(1000), /*applyImmediately*/ false);
729 EXPECT_EQ(5500, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
730 EXPECT_EQ(6500, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
731 EXPECT_EQ(7500, vrrTracker.nextAnticipatedVSyncTimeFrom(6000, 6000));
732 EXPECT_EQ(9500, vrrTracker.nextAnticipatedVSyncTimeFrom(8000, 8000));
733 EXPECT_EQ(11500, vrrTracker.nextAnticipatedVSyncTimeFrom(10000, 10000));
734 EXPECT_EQ(13500, vrrTracker.nextAnticipatedVSyncTimeFrom(12000, 12000));
735 EXPECT_EQ(16500, vrrTracker.nextAnticipatedVSyncTimeFrom(15500, 15500));
736 EXPECT_EQ(20500, vrrTracker.nextAnticipatedVSyncTimeFrom(19000, 19000));
737
738 // matches the previous cadence
739 EXPECT_EQ(21500, vrrTracker.nextAnticipatedVSyncTimeFrom(20500, 20500));
740 }
741
TEST_F(VSyncPredictorTest,minFramePeriodDoesntApplyWhenSameWithRefreshRate)742 TEST_F(VSyncPredictorTest, minFramePeriodDoesntApplyWhenSameWithRefreshRate) {
743 SET_FLAG_FOR_TEST(flags::vrr_config, true);
744
745 const int32_t kGroup = 0;
746 const auto kResolution = ui::Size(1920, 1080);
747 const auto vsyncRate = Fps::fromPeriodNsecs(1000);
748 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
749 hal::VrrConfig vrrConfig;
750 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
751 const ftl::NonNull<DisplayModePtr> kMode =
752 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), vsyncRate, kGroup,
753 kResolution, DEFAULT_DISPLAY_ID)
754 .setVrrConfig(std::move(vrrConfig))
755 .build());
756
757 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
758 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
759
760 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(1000), /*applyImmediately*/ false);
761 vrrTracker.addVsyncTimestamp(0);
762 EXPECT_EQ(1000, vrrTracker.nextAnticipatedVSyncTimeFrom(700));
763 EXPECT_EQ(2000, vrrTracker.nextAnticipatedVSyncTimeFrom(1000, 1000));
764
765 // Assume that the last vsync is wrong due to a vsync drift. It shouldn't matter.
766 EXPECT_EQ(2000, vrrTracker.nextAnticipatedVSyncTimeFrom(1000, 1700));
767 }
768
TEST_F(VSyncPredictorTest,setRenderRateExplicitAppliedImmediately)769 TEST_F(VSyncPredictorTest, setRenderRateExplicitAppliedImmediately) {
770 SET_FLAG_FOR_TEST(flags::vrr_config, true);
771
772 const int32_t kGroup = 0;
773 const auto kResolution = ui::Size(1920, 1080);
774 const auto vsyncRate = Fps::fromPeriodNsecs(500);
775 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
776 hal::VrrConfig vrrConfig;
777 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
778 const ftl::NonNull<DisplayModePtr> kMode =
779 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), vsyncRate, kGroup,
780 kResolution, DEFAULT_DISPLAY_ID)
781 .setVrrConfig(std::move(vrrConfig))
782 .build());
783
784 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
785 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
786
787 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(1000), /*applyImmediately*/ false);
788 vrrTracker.addVsyncTimestamp(0);
789 EXPECT_EQ(1000, vrrTracker.nextAnticipatedVSyncTimeFrom(700));
790 EXPECT_EQ(2000, vrrTracker.nextAnticipatedVSyncTimeFrom(1000, 1000));
791
792 // commit to a vsync in the future
793 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 2000));
794
795 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(2000), /*applyImmediately*/ true);
796 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4000));
797 EXPECT_EQ(7000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
798 EXPECT_EQ(9000, vrrTracker.nextAnticipatedVSyncTimeFrom(7000, 7000));
799 }
800
TEST_F(VSyncPredictorTest,selectsClosestVsyncAfterInactivity)801 TEST_F(VSyncPredictorTest, selectsClosestVsyncAfterInactivity) {
802 SET_FLAG_FOR_TEST(flags::vrr_config, true);
803
804 const int32_t kGroup = 0;
805 const auto kResolution = ui::Size(1920, 1080);
806 const auto vsyncRate = Fps::fromPeriodNsecs(500);
807 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
808 hal::VrrConfig vrrConfig;
809 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
810 const ftl::NonNull<DisplayModePtr> kMode =
811 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), vsyncRate, kGroup,
812 kResolution, DEFAULT_DISPLAY_ID)
813 .setVrrConfig(std::move(vrrConfig))
814 .build());
815
816 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
817 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
818
819 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(5000), /*applyImmediately*/ false);
820 vrrTracker.addVsyncTimestamp(0);
821 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4700));
822 EXPECT_EQ(10000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
823
824 mClock->setNow(50000);
825 EXPECT_EQ(50500, vrrTracker.nextAnticipatedVSyncTimeFrom(50000, 10000));
826 }
827
TEST_F(VSyncPredictorTest,returnsCorrectVsyncWhenLastIsNot)828 TEST_F(VSyncPredictorTest, returnsCorrectVsyncWhenLastIsNot) {
829 SET_FLAG_FOR_TEST(flags::vrr_config, true);
830
831 const int32_t kGroup = 0;
832 const auto kResolution = ui::Size(1920, 1080);
833 const auto vsyncRate = Fps::fromPeriodNsecs(500);
834 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
835 hal::VrrConfig vrrConfig;
836 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
837 const ftl::NonNull<DisplayModePtr> kMode =
838 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), vsyncRate, kGroup,
839 kResolution, DEFAULT_DISPLAY_ID)
840 .setVrrConfig(std::move(vrrConfig))
841 .build());
842
843 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
844 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
845
846 vrrTracker.setRenderRate(Fps::fromPeriodNsecs(1000), /*applyImmediately*/ false);
847 vrrTracker.addVsyncTimestamp(0);
848 EXPECT_EQ(2500, vrrTracker.nextAnticipatedVSyncTimeFrom(1234, 1234));
849 }
850
TEST_F(VSyncPredictorTest,adjustsVrrTimeline)851 TEST_F(VSyncPredictorTest, adjustsVrrTimeline) {
852 SET_FLAG_FOR_TEST(flags::vrr_config, true);
853
854 const int32_t kGroup = 0;
855 const auto kResolution = ui::Size(1920, 1080);
856 const auto refreshRate = Fps::fromPeriodNsecs(500);
857 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
858 hal::VrrConfig vrrConfig;
859 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
860 const ftl::NonNull<DisplayModePtr> kMode =
861 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), refreshRate, kGroup,
862 kResolution, DEFAULT_DISPLAY_ID)
863 .setVrrConfig(std::move(vrrConfig))
864 .build());
865
866 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
867 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
868
869 vrrTracker.setRenderRate(minFrameRate, /*applyImmediately*/ false);
870 vrrTracker.addVsyncTimestamp(0);
871 EXPECT_EQ(1000, vrrTracker.nextAnticipatedVSyncTimeFrom(700));
872 EXPECT_EQ(2000, vrrTracker.nextAnticipatedVSyncTimeFrom(1000));
873
874 vrrTracker.onFrameBegin(TimePoint::fromNs(2000), TimePoint::fromNs(1500));
875 EXPECT_EQ(3500, vrrTracker.nextAnticipatedVSyncTimeFrom(2000, 2000));
876 EXPECT_EQ(4500, vrrTracker.nextAnticipatedVSyncTimeFrom(3500, 3500));
877
878 // Miss when starting 4500 and expect the next vsync will be at 5000 (next one)
879 vrrTracker.onFrameBegin(TimePoint::fromNs(3500), TimePoint::fromNs(2500));
880 vrrTracker.onFrameMissed(TimePoint::fromNs(4500));
881 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4500, 4500));
882 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
883
884 vrrTracker.onFrameBegin(TimePoint::fromNs(7000), TimePoint::fromNs(6500));
885 EXPECT_EQ(10500, vrrTracker.nextAnticipatedVSyncTimeFrom(9000, 7000));
886 }
887
TEST_F(VSyncPredictorTest,adjustsVrrTimelineTwoClients)888 TEST_F(VSyncPredictorTest, adjustsVrrTimelineTwoClients) {
889 SET_FLAG_FOR_TEST(flags::vrr_config, true);
890
891 const int32_t kGroup = 0;
892 const auto kResolution = ui::Size(1920, 1080);
893 const auto refreshRate = Fps::fromPeriodNsecs(500);
894 const auto minFrameRate = Fps::fromPeriodNsecs(1000);
895 hal::VrrConfig vrrConfig;
896 vrrConfig.minFrameIntervalNs = minFrameRate.getPeriodNsecs();
897 const ftl::NonNull<DisplayModePtr> kMode =
898 ftl::as_non_null(createDisplayModeBuilder(DisplayModeId(0), refreshRate, kGroup,
899 kResolution, DEFAULT_DISPLAY_ID)
900 .setVrrConfig(std::move(vrrConfig))
901 .build());
902
903 VSyncPredictor vrrTracker{std::make_unique<ClockWrapper>(mClock), kMode, kHistorySize,
904 kMinimumSamplesForPrediction, kOutlierTolerancePercent};
905
906 vrrTracker.setRenderRate(minFrameRate, /*applyImmediately*/ false);
907 vrrTracker.addVsyncTimestamp(0);
908
909 // App runs ahead
910 EXPECT_EQ(3000, vrrTracker.nextAnticipatedVSyncTimeFrom(2700));
911 EXPECT_EQ(4000, vrrTracker.nextAnticipatedVSyncTimeFrom(3000, 3000));
912 EXPECT_EQ(5000, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
913
914 // SF starts to catch up
915 EXPECT_EQ(3000, vrrTracker.nextAnticipatedVSyncTimeFrom(2700));
916 vrrTracker.onFrameBegin(TimePoint::fromNs(3000), TimePoint::fromNs(0));
917
918 // SF misses last frame (3000) and observes that when committing (4000)
919 EXPECT_EQ(6000, vrrTracker.nextAnticipatedVSyncTimeFrom(5000, 5000));
920 EXPECT_EQ(4000, vrrTracker.nextAnticipatedVSyncTimeFrom(3700));
921 vrrTracker.onFrameMissed(TimePoint::fromNs(4000));
922
923 // SF wakes up again instead of the (4000) missed frame
924 EXPECT_EQ(4500, vrrTracker.nextAnticipatedVSyncTimeFrom(4000, 4000));
925 vrrTracker.onFrameBegin(TimePoint::fromNs(4500), TimePoint::fromNs(4500));
926
927 // Timeline shifted. The app needs to get the next frame at (7500) as its last frame (6500) will
928 // be presented at (7500)
929 EXPECT_EQ(7500, vrrTracker.nextAnticipatedVSyncTimeFrom(6000, 6000));
930 EXPECT_EQ(5500, vrrTracker.nextAnticipatedVSyncTimeFrom(4500, 4500));
931 vrrTracker.onFrameBegin(TimePoint::fromNs(5500), TimePoint::fromNs(4500));
932
933 EXPECT_EQ(8500, vrrTracker.nextAnticipatedVSyncTimeFrom(7500, 7500));
934 EXPECT_EQ(6500, vrrTracker.nextAnticipatedVSyncTimeFrom(5500, 5500));
935 vrrTracker.onFrameBegin(TimePoint::fromNs(6500), TimePoint::fromNs(5500));
936 }
937
TEST_F(VSyncPredictorTest,renderRateIsPreservedForCommittedVsyncs)938 TEST_F(VSyncPredictorTest, renderRateIsPreservedForCommittedVsyncs) {
939 tracker.addVsyncTimestamp(1000);
940
941 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
942 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(5001), Eq(6000));
943 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(6001), Eq(7000));
944
945 tracker.setRenderRate(Fps::fromPeriodNsecs(2000), /*applyImmediately*/ false);
946 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
947 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(5001), Eq(6000));
948 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(6001), Eq(7000));
949 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(7001), Eq(9000));
950 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(8001), Eq(9000));
951 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(9001), Eq(11000));
952 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(10001), Eq(11000));
953
954 tracker.setRenderRate(Fps::fromPeriodNsecs(3000), /*applyImmediately*/ false);
955 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
956 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(5001), Eq(6000));
957 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(6001), Eq(7000));
958 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(7001), Eq(9000));
959 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(8001), Eq(9000));
960 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(9001), Eq(11000));
961 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(10001), Eq(11000));
962 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(11001), Eq(14000));
963 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(12001), Eq(14000));
964
965 // Check the purge logic works
966 mClock->setNow(20000);
967 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(2000));
968 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(5001), Eq(8000));
969 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(6001), Eq(8000));
970 }
971
972 // b/329310308
TEST_F(VSyncPredictorTest,renderRateChangeAfterAppliedImmediately)973 TEST_F(VSyncPredictorTest, renderRateChangeAfterAppliedImmediately) {
974 tracker.addVsyncTimestamp(1000);
975
976 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
977 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1001), Eq(2000));
978 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(2001), Eq(3000));
979
980 tracker.setRenderRate(Fps::fromPeriodNsecs(2000), /*applyImmediately*/ true);
981 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
982 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1001), Eq(3000));
983 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(3001), Eq(5000));
984
985 tracker.setRenderRate(Fps::fromPeriodNsecs(4000), /*applyImmediately*/ false);
986 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1), Eq(1000));
987 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(1001), Eq(3000));
988 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(3001), Eq(5000));
989 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(5001), Eq(9000));
990 EXPECT_THAT(tracker.nextAnticipatedVSyncTimeFrom(9001), Eq(13000));
991 }
992
993 } // namespace android::scheduler
994
995 // TODO(b/129481165): remove the #pragma below and fix conversion issues
996 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
997