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 
21 #undef LOG_TAG
22 #define LOG_TAG "SchedulerUnittests"
23 
24 #include <gmock/gmock.h>
25 #include <log/log.h>
26 #include <thread>
27 
28 #include "Scheduler/PhaseOffsets.h"
29 
30 using namespace testing;
31 
32 namespace android {
33 namespace scheduler {
34 
35 class TestablePhaseOffsetsAsDurations : public impl::PhaseDurations {
36 public:
TestablePhaseOffsetsAsDurations(float currentFps,nsecs_t sfDuration,nsecs_t appDuration,nsecs_t sfEarlyDuration,nsecs_t appEarlyDuration,nsecs_t sfEarlyGlDuration,nsecs_t appEarlyGlDuration)37     TestablePhaseOffsetsAsDurations(float currentFps, nsecs_t sfDuration, nsecs_t appDuration,
38                                     nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration,
39                                     nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration)
40           : impl::PhaseDurations({60.0f, 90.0f}, currentFps, sfDuration, appDuration,
41                                  sfEarlyDuration, appEarlyDuration, sfEarlyGlDuration,
42                                  appEarlyGlDuration) {}
43 };
44 
45 class PhaseDurationTest : public testing::Test {
46 protected:
PhaseDurationTest()47     PhaseDurationTest()
48           : mPhaseDurations(60.0f, 10'500'000, 20'500'000, 16'000'000, 16'500'000, 13'500'000,
49                             21'000'000) {}
50 
51     ~PhaseDurationTest() = default;
52 
53     TestablePhaseOffsetsAsDurations mPhaseDurations;
54 };
55 
56 namespace {
57 /* ------------------------------------------------------------------------
58  * Test cases
59  */
TEST_F(PhaseDurationTest,getOffsetsForRefreshRate_60Hz)60 TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_60Hz) {
61     mPhaseDurations.setRefreshRateFps(60.0f);
62     auto currentOffsets = mPhaseDurations.getCurrentOffsets();
63     auto offsets = mPhaseDurations.getOffsetsForRefreshRate(60.0f);
64 
65     EXPECT_EQ(currentOffsets, offsets);
66     EXPECT_EQ(offsets.late.sf, 6'166'667);
67 
68     EXPECT_EQ(offsets.late.app, 2'333'334);
69 
70     EXPECT_EQ(offsets.early.sf, 666'667);
71 
72     EXPECT_EQ(offsets.early.app, 833'334);
73 
74     EXPECT_EQ(offsets.earlyGl.sf, 3'166'667);
75 
76     EXPECT_EQ(offsets.earlyGl.app, 15'500'001);
77 }
78 
TEST_F(PhaseDurationTest,getOffsetsForRefreshRate_90Hz)79 TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_90Hz) {
80     mPhaseDurations.setRefreshRateFps(90.0f);
81     auto currentOffsets = mPhaseDurations.getCurrentOffsets();
82     auto offsets = mPhaseDurations.getOffsetsForRefreshRate(90.0f);
83 
84     EXPECT_EQ(currentOffsets, offsets);
85     EXPECT_EQ(offsets.late.sf, 611'111);
86 
87     EXPECT_EQ(offsets.late.app, 2'333'333);
88 
89     EXPECT_EQ(offsets.early.sf, -4'888'889);
90 
91     EXPECT_EQ(offsets.early.app, 833'333);
92 
93     EXPECT_EQ(offsets.earlyGl.sf, -2'388'889);
94 
95     EXPECT_EQ(offsets.earlyGl.app, 9'944'444);
96 }
97 
TEST_F(PhaseDurationTest,getOffsetsForRefreshRate_DefaultOffsets)98 TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_DefaultOffsets) {
99     TestablePhaseOffsetsAsDurations phaseOffsetsWithDefaultValues(60.0f, -1, -1, -1, -1, -1, -1);
100 
101     auto validateOffsets = [](auto& offsets) {
102         EXPECT_EQ(offsets.late.sf, 1'000'000);
103 
104         EXPECT_EQ(offsets.late.app, 1'000'000);
105 
106         EXPECT_EQ(offsets.early.sf, 1'000'000);
107 
108         EXPECT_EQ(offsets.early.app, 1'000'000);
109 
110         EXPECT_EQ(offsets.earlyGl.sf, 1'000'000);
111 
112         EXPECT_EQ(offsets.earlyGl.app, 1'000'000);
113     };
114 
115     phaseOffsetsWithDefaultValues.setRefreshRateFps(90.0f);
116     auto currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets();
117     auto offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f);
118     EXPECT_EQ(currentOffsets, offsets);
119     validateOffsets(offsets);
120 
121     phaseOffsetsWithDefaultValues.setRefreshRateFps(60.0f);
122     currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets();
123     offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f);
124     EXPECT_EQ(currentOffsets, offsets);
125     validateOffsets(offsets);
126 }
127 
TEST_F(PhaseDurationTest,getOffsetsForRefreshRate_unknownRefreshRate)128 TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_unknownRefreshRate) {
129     auto offsets = mPhaseDurations.getOffsetsForRefreshRate(14.7f);
130 
131     EXPECT_EQ(offsets.late.sf, 57'527'208);
132 
133     EXPECT_EQ(offsets.late.app, 37'027'208);
134 
135     EXPECT_EQ(offsets.early.sf, 52'027'208);
136 
137     EXPECT_EQ(offsets.early.app, 35'527'208);
138 
139     EXPECT_EQ(offsets.earlyGl.sf, 54'527'208);
140 
141     EXPECT_EQ(offsets.earlyGl.app, 33'527'208);
142 }
143 
144 } // namespace
145 
146 class TestablePhaseOffsets : public impl::PhaseOffsets {
147 public:
TestablePhaseOffsets()148     TestablePhaseOffsets()
149           : impl::PhaseOffsets({60.0f, 90.0f}, 60.0f, 1'000'000, 1'000'000, {}, {}, {}, {},
150                                10'000'000) {}
151 };
152 
153 class PhaseOffsetsTest : public testing::Test {
154 protected:
155     PhaseOffsetsTest() = default;
156     ~PhaseOffsetsTest() = default;
157 
158     TestablePhaseOffsets mPhaseOffsets;
159 };
160 
161 namespace {
TEST_F(PhaseOffsetsTest,getOffsetsForRefreshRate_unknownRefreshRate)162 TEST_F(PhaseOffsetsTest, getOffsetsForRefreshRate_unknownRefreshRate) {
163     auto offsets = mPhaseOffsets.getOffsetsForRefreshRate(14.7f);
164 
165     EXPECT_EQ(offsets.late.sf, 1'000'000);
166 
167     EXPECT_EQ(offsets.late.app, 1'000'000);
168 
169     EXPECT_EQ(offsets.early.sf, 1'000'000);
170 
171     EXPECT_EQ(offsets.early.app, 1'000'000);
172 
173     EXPECT_EQ(offsets.earlyGl.sf, 1'000'000);
174 
175     EXPECT_EQ(offsets.earlyGl.app, 1'000'000);
176 }
177 
178 } // namespace
179 } // namespace scheduler
180 } // namespace android
181 
182 // TODO(b/129481165): remove the #pragma below and fix conversion issues
183 #pragma clang diagnostic pop // ignored "-Wconversion"