1 /*
2  * Copyright 2022 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 #include <gtest/gtest.h>
18 #include <gui/CompositorTiming.h>
19 
20 namespace android::test {
21 namespace {
22 
23 constexpr nsecs_t kMillisecond = 1'000'000;
24 constexpr nsecs_t kVsyncPeriod = 8'333'333;
25 constexpr nsecs_t kVsyncPhase = -2'166'667;
26 constexpr nsecs_t kIdealLatency = -kVsyncPhase;
27 
28 } // namespace
29 
TEST(CompositorTimingTest,InvalidVsyncPeriod)30 TEST(CompositorTimingTest, InvalidVsyncPeriod) {
31     const nsecs_t vsyncDeadline = systemTime();
32     constexpr nsecs_t kInvalidVsyncPeriod = -1;
33 
34     const gui::CompositorTiming timing(vsyncDeadline, kInvalidVsyncPeriod, kVsyncPhase,
35                                        kIdealLatency);
36 
37     EXPECT_EQ(timing.deadline, 0);
38     EXPECT_EQ(timing.interval, gui::CompositorTiming::kDefaultVsyncPeriod);
39     EXPECT_EQ(timing.presentLatency, gui::CompositorTiming::kDefaultVsyncPeriod);
40 }
41 
TEST(CompositorTimingTest,PresentLatencySnapping)42 TEST(CompositorTimingTest, PresentLatencySnapping) {
43     for (nsecs_t presentDelay = 0, compositeTime = systemTime(); presentDelay < 10 * kVsyncPeriod;
44          presentDelay += kMillisecond, compositeTime += kVsyncPeriod) {
45         const nsecs_t presentLatency = kIdealLatency + presentDelay;
46         const nsecs_t vsyncDeadline = compositeTime + presentLatency + kVsyncPeriod;
47 
48         const gui::CompositorTiming timing(vsyncDeadline, kVsyncPeriod, kVsyncPhase,
49                                            presentLatency);
50 
51         EXPECT_EQ(timing.deadline, compositeTime + presentDelay + kVsyncPeriod);
52         EXPECT_EQ(timing.interval, kVsyncPeriod);
53 
54         // The presentDelay should be rounded to a multiple of the VSYNC period, such that the
55         // remainder (presentLatency % interval) always evaluates to the VSYNC phase offset.
56         EXPECT_GE(timing.presentLatency, kIdealLatency);
57         EXPECT_EQ(timing.presentLatency % timing.interval, kIdealLatency);
58     }
59 }
60 
61 } // namespace android::test
62