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 #undef LOG_TAG
18 #define LOG_TAG "SchedulerUnittests"
19 
20 #include <gmock/gmock.h>
21 #include <log/log.h>
22 #include <thread>
23 
24 #include "../../Scheduler/RefreshRateConfigs.h"
25 #include "DisplayHardware/HWC2.h"
26 #include "Scheduler/RefreshRateConfigs.h"
27 #include "mock/DisplayHardware/MockDisplay.h"
28 
29 using namespace std::chrono_literals;
30 using testing::_;
31 
32 namespace android {
33 namespace scheduler {
34 
35 namespace hal = android::hardware::graphics::composer::hal;
36 
37 using RefreshRate = RefreshRateConfigs::RefreshRate;
38 using LayerVoteType = RefreshRateConfigs::LayerVoteType;
39 using LayerRequirement = RefreshRateConfigs::LayerRequirement;
40 
41 class RefreshRateConfigsTest : public testing::Test {
42 protected:
43     RefreshRateConfigsTest();
44     ~RefreshRateConfigsTest();
45 
findClosestKnownFrameRate(const RefreshRateConfigs & refreshRateConfigs,float frameRate)46     float findClosestKnownFrameRate(const RefreshRateConfigs& refreshRateConfigs, float frameRate) {
47         return refreshRateConfigs.findClosestKnownFrameRate(frameRate);
48     }
49 
getKnownFrameRate(const RefreshRateConfigs & refreshRateConfigs)50     std::vector<float> getKnownFrameRate(const RefreshRateConfigs& refreshRateConfigs) {
51         return refreshRateConfigs.mKnownFrameRates;
52     }
53 
54     // Test config IDs
55     static inline const HwcConfigIndexType HWC_CONFIG_ID_60 = HwcConfigIndexType(0);
56     static inline const HwcConfigIndexType HWC_CONFIG_ID_90 = HwcConfigIndexType(1);
57     static inline const HwcConfigIndexType HWC_CONFIG_ID_72 = HwcConfigIndexType(2);
58     static inline const HwcConfigIndexType HWC_CONFIG_ID_120 = HwcConfigIndexType(3);
59     static inline const HwcConfigIndexType HWC_CONFIG_ID_30 = HwcConfigIndexType(4);
60 
61     // Test configs
62     std::shared_ptr<const HWC2::Display::Config> mConfig60 =
63             createConfig(HWC_CONFIG_ID_60, 0, static_cast<int64_t>(1e9f / 60));
64     std::shared_ptr<const HWC2::Display::Config> mConfig90 =
65             createConfig(HWC_CONFIG_ID_90, 0, static_cast<int64_t>(1e9f / 90));
66     std::shared_ptr<const HWC2::Display::Config> mConfig90DifferentGroup =
67             createConfig(HWC_CONFIG_ID_90, 1, static_cast<int64_t>(1e9f / 90));
68     std::shared_ptr<const HWC2::Display::Config> mConfig90DifferentResolution =
69             createConfig(HWC_CONFIG_ID_90, 0, static_cast<int64_t>(1e9f / 90), 111, 222);
70     std::shared_ptr<const HWC2::Display::Config> mConfig72 =
71             createConfig(HWC_CONFIG_ID_72, 0, static_cast<int64_t>(1e9f / 72));
72     std::shared_ptr<const HWC2::Display::Config> mConfig72DifferentGroup =
73             createConfig(HWC_CONFIG_ID_72, 1, static_cast<int64_t>(1e9f / 72));
74     std::shared_ptr<const HWC2::Display::Config> mConfig120 =
75             createConfig(HWC_CONFIG_ID_120, 0, static_cast<int64_t>(1e9f / 120));
76     std::shared_ptr<const HWC2::Display::Config> mConfig120DifferentGroup =
77             createConfig(HWC_CONFIG_ID_120, 1, static_cast<int64_t>(1e9f / 120));
78     std::shared_ptr<const HWC2::Display::Config> mConfig30 =
79             createConfig(HWC_CONFIG_ID_30, 0, static_cast<int64_t>(1e9f / 30));
80 
81     // Test device configurations
82     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60OnlyConfigDevice = {mConfig60};
83     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60_90Device = {mConfig60, mConfig90};
84     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60_90DeviceWithDifferentGroups =
85             {mConfig60, mConfig90DifferentGroup};
86     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60_90DeviceWithDifferentResolutions =
87             {mConfig60, mConfig90DifferentResolution};
88     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60_72_90Device = {mConfig60,
89                                                                                  mConfig90,
90                                                                                  mConfig72};
91     std::vector<std::shared_ptr<const HWC2::Display::Config>> m60_90_72_120Device = {mConfig60,
92                                                                                      mConfig90,
93                                                                                      mConfig72,
94                                                                                      mConfig120};
95     std::vector<std::shared_ptr<const HWC2::Display::Config>> m30_60_72_90_120Device = {mConfig60,
96                                                                                         mConfig90,
97                                                                                         mConfig72,
98                                                                                         mConfig120,
99                                                                                         mConfig30};
100     std::vector<std::shared_ptr<const HWC2::Display::Config>> m30_60Device =
101             {mConfig60, mConfig90DifferentGroup, mConfig72DifferentGroup, mConfig120DifferentGroup,
102              mConfig30};
103     std::vector<std::shared_ptr<const HWC2::Display::Config>> m30_60_72_90Device =
104             {mConfig60, mConfig90, mConfig72, mConfig120DifferentGroup, mConfig30};
105     std::vector<std::shared_ptr<const HWC2::Display::Config>> m30_60_90Device =
106             {mConfig60, mConfig90, mConfig72DifferentGroup, mConfig120DifferentGroup, mConfig30};
107 
108     // Expected RefreshRate objects
109     RefreshRate mExpected60Config = {HWC_CONFIG_ID_60, mConfig60, "60fps", 60,
110                                      RefreshRate::ConstructorTag(0)};
111     RefreshRate mExpectedAlmost60Config = {HWC_CONFIG_ID_60,
112                                            createConfig(HWC_CONFIG_ID_60, 0, 16666665), "60fps", 60,
113                                            RefreshRate::ConstructorTag(0)};
114     RefreshRate mExpected90Config = {HWC_CONFIG_ID_90, mConfig90, "90fps", 90,
115                                      RefreshRate::ConstructorTag(0)};
116     RefreshRate mExpected90DifferentGroupConfig = {HWC_CONFIG_ID_90, mConfig90DifferentGroup,
117                                                    "90fps", 90, RefreshRate::ConstructorTag(0)};
118     RefreshRate mExpected90DifferentResolutionConfig = {HWC_CONFIG_ID_90,
119                                                         mConfig90DifferentResolution, "90fps", 90,
120                                                         RefreshRate::ConstructorTag(0)};
121     RefreshRate mExpected72Config = {HWC_CONFIG_ID_72, mConfig72, "72fps", 72,
122                                      RefreshRate::ConstructorTag(0)};
123     RefreshRate mExpected30Config = {HWC_CONFIG_ID_30, mConfig30, "30fps", 30,
124                                      RefreshRate::ConstructorTag(0)};
125     RefreshRate mExpected120Config = {HWC_CONFIG_ID_120, mConfig120, "120fps", 120,
126                                       RefreshRate::ConstructorTag(0)};
127 
128     Hwc2::mock::Display mDisplay;
129 
130 private:
131     std::shared_ptr<const HWC2::Display::Config> createConfig(HwcConfigIndexType configId,
132                                                               int32_t configGroup,
133                                                               int64_t vsyncPeriod,
134                                                               int32_t hight = -1,
135                                                               int32_t width = -1);
136 };
137 
138 using Builder = HWC2::Display::Config::Builder;
139 
RefreshRateConfigsTest()140 RefreshRateConfigsTest::RefreshRateConfigsTest() {
141     const ::testing::TestInfo* const test_info =
142             ::testing::UnitTest::GetInstance()->current_test_info();
143     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
144 }
145 
~RefreshRateConfigsTest()146 RefreshRateConfigsTest::~RefreshRateConfigsTest() {
147     const ::testing::TestInfo* const test_info =
148             ::testing::UnitTest::GetInstance()->current_test_info();
149     ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
150 }
151 
createConfig(HwcConfigIndexType configId,int32_t configGroup,int64_t vsyncPeriod,int32_t hight,int32_t width)152 std::shared_ptr<const HWC2::Display::Config> RefreshRateConfigsTest::createConfig(
153         HwcConfigIndexType configId, int32_t configGroup, int64_t vsyncPeriod, int32_t hight,
154         int32_t width) {
155     return HWC2::Display::Config::Builder(mDisplay, hal::HWConfigId(configId.value()))
156             .setVsyncPeriod(int32_t(vsyncPeriod))
157             .setConfigGroup(configGroup)
158             .setHeight(hight)
159             .setWidth(width)
160             .build();
161 }
162 
163 namespace {
164 /* ------------------------------------------------------------------------
165  * Test cases
166  */
TEST_F(RefreshRateConfigsTest,oneDeviceConfig_SwitchingSupported)167 TEST_F(RefreshRateConfigsTest, oneDeviceConfig_SwitchingSupported) {
168     auto refreshRateConfigs =
169             std::make_unique<RefreshRateConfigs>(m60OnlyConfigDevice,
170                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
171 }
172 
TEST_F(RefreshRateConfigsTest,invalidPolicy)173 TEST_F(RefreshRateConfigsTest, invalidPolicy) {
174     auto refreshRateConfigs =
175             std::make_unique<RefreshRateConfigs>(m60OnlyConfigDevice,
176                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
177     ASSERT_LT(refreshRateConfigs->setDisplayManagerPolicy({HwcConfigIndexType(10), {60, 60}}), 0);
178     ASSERT_LT(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {20, 40}}), 0);
179 }
180 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_storesFullRefreshRateMap)181 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesFullRefreshRateMap) {
182     auto refreshRateConfigs =
183             std::make_unique<RefreshRateConfigs>(m60_90Device,
184                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
185 
186     const auto& minRate = refreshRateConfigs->getMinRefreshRate();
187     const auto& performanceRate = refreshRateConfigs->getMaxRefreshRate();
188 
189     ASSERT_EQ(mExpected60Config, minRate);
190     ASSERT_EQ(mExpected90Config, performanceRate);
191 
192     const auto& minRateByPolicy = refreshRateConfigs->getMinRefreshRateByPolicy();
193     const auto& performanceRateByPolicy = refreshRateConfigs->getMaxRefreshRateByPolicy();
194     ASSERT_EQ(minRateByPolicy, minRate);
195     ASSERT_EQ(performanceRateByPolicy, performanceRate);
196 }
197 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_storesFullRefreshRateMap_differentGroups)198 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesFullRefreshRateMap_differentGroups) {
199     auto refreshRateConfigs =
200             std::make_unique<RefreshRateConfigs>(m60_90DeviceWithDifferentGroups,
201                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
202 
203     const auto& minRate = refreshRateConfigs->getMinRefreshRateByPolicy();
204     const auto& performanceRate = refreshRateConfigs->getMaxRefreshRate();
205     const auto& minRate60 = refreshRateConfigs->getMinRefreshRateByPolicy();
206     const auto& performanceRate60 = refreshRateConfigs->getMaxRefreshRateByPolicy();
207 
208     ASSERT_EQ(mExpected60Config, minRate);
209     ASSERT_EQ(mExpected60Config, minRate60);
210     ASSERT_EQ(mExpected60Config, performanceRate60);
211 
212     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {60, 90}}), 0);
213     refreshRateConfigs->setCurrentConfigId(HWC_CONFIG_ID_90);
214 
215     const auto& minRate90 = refreshRateConfigs->getMinRefreshRateByPolicy();
216     const auto& performanceRate90 = refreshRateConfigs->getMaxRefreshRateByPolicy();
217 
218     ASSERT_EQ(mExpected90DifferentGroupConfig, performanceRate);
219     ASSERT_EQ(mExpected90DifferentGroupConfig, minRate90);
220     ASSERT_EQ(mExpected90DifferentGroupConfig, performanceRate90);
221 }
222 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_storesFullRefreshRateMap_differentResolutions)223 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesFullRefreshRateMap_differentResolutions) {
224     auto refreshRateConfigs =
225             std::make_unique<RefreshRateConfigs>(m60_90DeviceWithDifferentResolutions,
226                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
227 
228     const auto& minRate = refreshRateConfigs->getMinRefreshRateByPolicy();
229     const auto& performanceRate = refreshRateConfigs->getMaxRefreshRate();
230     const auto& minRate60 = refreshRateConfigs->getMinRefreshRateByPolicy();
231     const auto& performanceRate60 = refreshRateConfigs->getMaxRefreshRateByPolicy();
232 
233     ASSERT_EQ(mExpected60Config, minRate);
234     ASSERT_EQ(mExpected60Config, minRate60);
235     ASSERT_EQ(mExpected60Config, performanceRate60);
236 
237     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {60, 90}}), 0);
238     refreshRateConfigs->setCurrentConfigId(HWC_CONFIG_ID_90);
239 
240     const auto& minRate90 = refreshRateConfigs->getMinRefreshRateByPolicy();
241     const auto& performanceRate90 = refreshRateConfigs->getMaxRefreshRateByPolicy();
242 
243     ASSERT_EQ(mExpected90DifferentResolutionConfig, performanceRate);
244     ASSERT_EQ(mExpected90DifferentResolutionConfig, minRate90);
245     ASSERT_EQ(mExpected90DifferentResolutionConfig, performanceRate90);
246 }
247 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_policyChange)248 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_policyChange) {
249     auto refreshRateConfigs =
250             std::make_unique<RefreshRateConfigs>(m60_90Device,
251                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
252 
253     auto& minRate = refreshRateConfigs->getMinRefreshRateByPolicy();
254     auto& performanceRate = refreshRateConfigs->getMaxRefreshRateByPolicy();
255 
256     ASSERT_EQ(mExpected60Config, minRate);
257     ASSERT_EQ(mExpected90Config, performanceRate);
258 
259     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 60}}), 0);
260 
261     auto& minRate60 = refreshRateConfigs->getMinRefreshRateByPolicy();
262     auto& performanceRate60 = refreshRateConfigs->getMaxRefreshRateByPolicy();
263     ASSERT_EQ(mExpected60Config, minRate60);
264     ASSERT_EQ(mExpected60Config, performanceRate60);
265 }
266 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_getCurrentRefreshRate)267 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_getCurrentRefreshRate) {
268     auto refreshRateConfigs =
269             std::make_unique<RefreshRateConfigs>(m60_90Device,
270                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
271     {
272         auto& current = refreshRateConfigs->getCurrentRefreshRate();
273         EXPECT_EQ(current.getConfigId(), HWC_CONFIG_ID_60);
274     }
275 
276     refreshRateConfigs->setCurrentConfigId(HWC_CONFIG_ID_90);
277     {
278         auto& current = refreshRateConfigs->getCurrentRefreshRate();
279         EXPECT_EQ(current.getConfigId(), HWC_CONFIG_ID_90);
280     }
281 
282     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {90, 90}}), 0);
283     {
284         auto& current = refreshRateConfigs->getCurrentRefreshRate();
285         EXPECT_EQ(current.getConfigId(), HWC_CONFIG_ID_90);
286     }
287 }
288 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_getRefreshRateForContent)289 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_getRefreshRateForContent) {
290     auto refreshRateConfigs =
291             std::make_unique<RefreshRateConfigs>(m60_90Device,
292                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
293 
294     const auto makeLayerRequirements = [](float refreshRate) -> std::vector<LayerRequirement> {
295         return {{"testLayer", LayerVoteType::Heuristic, refreshRate, /*weight*/ 1.0f,
296                  /*focused*/ false}};
297     };
298 
299     EXPECT_EQ(mExpected90Config,
300               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(90.0f)));
301     EXPECT_EQ(mExpected60Config,
302               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(60.0f)));
303     EXPECT_EQ(mExpected90Config,
304               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(45.0f)));
305     EXPECT_EQ(mExpected60Config,
306               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(30.0f)));
307     EXPECT_EQ(mExpected60Config,
308               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(24.0f)));
309 
310     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 60}}), 0);
311     EXPECT_EQ(mExpected60Config,
312               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(90.0f)));
313     EXPECT_EQ(mExpected60Config,
314               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(60.0f)));
315     EXPECT_EQ(mExpected60Config,
316               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(45.0f)));
317     EXPECT_EQ(mExpected60Config,
318               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(30.0f)));
319     EXPECT_EQ(mExpected60Config,
320               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(24.0f)));
321 
322     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {90, 90}}), 0);
323     EXPECT_EQ(mExpected90Config,
324               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(90.0f)));
325     EXPECT_EQ(mExpected90Config,
326               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(60.0f)));
327     EXPECT_EQ(mExpected90Config,
328               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(45.0f)));
329     EXPECT_EQ(mExpected90Config,
330               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(30.0f)));
331     EXPECT_EQ(mExpected90Config,
332               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(24.0f)));
333     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {0, 120}}), 0);
334     EXPECT_EQ(mExpected90Config,
335               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(90.0f)));
336     EXPECT_EQ(mExpected60Config,
337               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(60.0f)));
338     EXPECT_EQ(mExpected90Config,
339               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(45.0f)));
340     EXPECT_EQ(mExpected60Config,
341               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(30.0f)));
342     EXPECT_EQ(mExpected60Config,
343               refreshRateConfigs->getRefreshRateForContent(makeLayerRequirements(24.0f)));
344 }
345 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_noLayers)346 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_noLayers) {
347     auto refreshRateConfigs =
348             std::make_unique<RefreshRateConfigs>(m60_72_90Device, /*currentConfigId=*/
349                                                  HWC_CONFIG_ID_72);
350 
351     // If there are no layers we select the default frame rate, which is the max of the primary
352     // range.
353     auto layers = std::vector<LayerRequirement>{};
354     EXPECT_EQ(mExpected90Config,
355               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
356 
357     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 60}}), 0);
358     EXPECT_EQ(mExpected60Config,
359               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
360 }
361 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_60_90)362 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_60_90) {
363     auto refreshRateConfigs =
364             std::make_unique<RefreshRateConfigs>(m60_90Device,
365                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
366 
367     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
368     auto& lr = layers[0];
369 
370     lr.vote = LayerVoteType::Min;
371     lr.name = "Min";
372     EXPECT_EQ(mExpected60Config,
373               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
374 
375     lr.vote = LayerVoteType::Max;
376     lr.name = "Max";
377     EXPECT_EQ(mExpected90Config,
378               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
379 
380     lr.desiredRefreshRate = 90.0f;
381     lr.vote = LayerVoteType::Heuristic;
382     lr.name = "90Hz Heuristic";
383     EXPECT_EQ(mExpected90Config,
384               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
385 
386     lr.desiredRefreshRate = 60.0f;
387     lr.name = "60Hz Heuristic";
388     EXPECT_EQ(mExpected60Config,
389               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
390 
391     lr.desiredRefreshRate = 45.0f;
392     lr.name = "45Hz Heuristic";
393     EXPECT_EQ(mExpected90Config,
394               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
395 
396     lr.desiredRefreshRate = 30.0f;
397     lr.name = "30Hz Heuristic";
398     EXPECT_EQ(mExpected60Config,
399               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
400 
401     lr.desiredRefreshRate = 24.0f;
402     lr.name = "24Hz Heuristic";
403     EXPECT_EQ(mExpected60Config,
404               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
405 
406     lr.name = "";
407     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 60}}), 0);
408 
409     lr.vote = LayerVoteType::Min;
410     EXPECT_EQ(mExpected60Config,
411               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
412 
413     lr.vote = LayerVoteType::Max;
414     EXPECT_EQ(mExpected60Config,
415               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
416 
417     lr.desiredRefreshRate = 90.0f;
418     lr.vote = LayerVoteType::Heuristic;
419     EXPECT_EQ(mExpected60Config,
420               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
421 
422     lr.desiredRefreshRate = 60.0f;
423     EXPECT_EQ(mExpected60Config,
424               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
425 
426     lr.desiredRefreshRate = 45.0f;
427     EXPECT_EQ(mExpected60Config,
428               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
429 
430     lr.desiredRefreshRate = 30.0f;
431     EXPECT_EQ(mExpected60Config,
432               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
433 
434     lr.desiredRefreshRate = 24.0f;
435     EXPECT_EQ(mExpected60Config,
436               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
437 
438     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {90, 90}}), 0);
439 
440     lr.vote = LayerVoteType::Min;
441     EXPECT_EQ(mExpected90Config,
442               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
443 
444     lr.vote = LayerVoteType::Max;
445     EXPECT_EQ(mExpected90Config,
446               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
447 
448     lr.desiredRefreshRate = 90.0f;
449     lr.vote = LayerVoteType::Heuristic;
450     EXPECT_EQ(mExpected90Config,
451               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
452 
453     lr.desiredRefreshRate = 60.0f;
454     EXPECT_EQ(mExpected90Config,
455               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
456 
457     lr.desiredRefreshRate = 45.0f;
458     EXPECT_EQ(mExpected90Config,
459               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
460 
461     lr.desiredRefreshRate = 30.0f;
462     EXPECT_EQ(mExpected90Config,
463               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
464 
465     lr.desiredRefreshRate = 24.0f;
466     EXPECT_EQ(mExpected90Config,
467               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
468 
469     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {0, 120}}), 0);
470     lr.vote = LayerVoteType::Min;
471     EXPECT_EQ(mExpected60Config,
472               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
473 
474     lr.vote = LayerVoteType::Max;
475     EXPECT_EQ(mExpected90Config,
476               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
477 
478     lr.desiredRefreshRate = 90.0f;
479     lr.vote = LayerVoteType::Heuristic;
480     EXPECT_EQ(mExpected90Config,
481               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
482 
483     lr.desiredRefreshRate = 60.0f;
484     EXPECT_EQ(mExpected60Config,
485               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
486 
487     lr.desiredRefreshRate = 45.0f;
488     EXPECT_EQ(mExpected90Config,
489               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
490 
491     lr.desiredRefreshRate = 30.0f;
492     EXPECT_EQ(mExpected60Config,
493               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
494 
495     lr.desiredRefreshRate = 24.0f;
496     EXPECT_EQ(mExpected60Config,
497               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
498 }
499 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_60_72_90)500 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_60_72_90) {
501     auto refreshRateConfigs =
502             std::make_unique<RefreshRateConfigs>(m60_72_90Device,
503                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
504 
505     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
506     auto& lr = layers[0];
507 
508     lr.vote = LayerVoteType::Min;
509     EXPECT_EQ(mExpected60Config,
510               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
511 
512     lr.vote = LayerVoteType::Max;
513     EXPECT_EQ(mExpected90Config,
514               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
515 
516     lr.desiredRefreshRate = 90.0f;
517     lr.vote = LayerVoteType::Heuristic;
518     EXPECT_EQ(mExpected90Config,
519               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
520 
521     lr.desiredRefreshRate = 60.0f;
522     EXPECT_EQ(mExpected60Config,
523               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
524 
525     lr.desiredRefreshRate = 45.0f;
526     EXPECT_EQ(mExpected90Config,
527               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
528 
529     lr.desiredRefreshRate = 30.0f;
530     EXPECT_EQ(mExpected60Config,
531               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
532 
533     lr.desiredRefreshRate = 24.0f;
534     EXPECT_EQ(mExpected72Config,
535               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
536 }
537 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_30_60_72_90_120)538 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_30_60_72_90_120) {
539     auto refreshRateConfigs =
540             std::make_unique<RefreshRateConfigs>(m30_60_72_90_120Device,
541                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
542 
543     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
544                                                 LayerRequirement{.weight = 1.0f}};
545     auto& lr1 = layers[0];
546     auto& lr2 = layers[1];
547 
548     lr1.desiredRefreshRate = 24.0f;
549     lr1.vote = LayerVoteType::Heuristic;
550     lr2.desiredRefreshRate = 60.0f;
551     lr2.vote = LayerVoteType::Heuristic;
552     EXPECT_EQ(mExpected120Config,
553               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
554 
555     lr1.desiredRefreshRate = 24.0f;
556     lr1.vote = LayerVoteType::Heuristic;
557     lr2.desiredRefreshRate = 48.0f;
558     lr2.vote = LayerVoteType::Heuristic;
559     EXPECT_EQ(mExpected72Config,
560               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
561 
562     lr1.desiredRefreshRate = 24.0f;
563     lr1.vote = LayerVoteType::Heuristic;
564     lr2.desiredRefreshRate = 48.0f;
565     lr2.vote = LayerVoteType::Heuristic;
566     EXPECT_EQ(mExpected72Config,
567               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
568 }
569 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_30_60_90_120_DifferentTypes)570 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_30_60_90_120_DifferentTypes) {
571     auto refreshRateConfigs =
572             std::make_unique<RefreshRateConfigs>(m30_60_72_90_120Device,
573                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
574 
575     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
576                                                 LayerRequirement{.weight = 1.0f}};
577     auto& lr1 = layers[0];
578     auto& lr2 = layers[1];
579 
580     lr1.desiredRefreshRate = 24.0f;
581     lr1.vote = LayerVoteType::ExplicitDefault;
582     lr1.name = "24Hz ExplicitDefault";
583     lr2.desiredRefreshRate = 60.0f;
584     lr2.vote = LayerVoteType::Heuristic;
585     lr2.name = "60Hz Heuristic";
586     EXPECT_EQ(mExpected120Config,
587               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
588 
589     lr1.desiredRefreshRate = 24.0f;
590     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
591     lr1.name = "24Hz ExplicitExactOrMultiple";
592     lr2.desiredRefreshRate = 60.0f;
593     lr2.vote = LayerVoteType::Heuristic;
594     lr2.name = "60Hz Heuristic";
595     EXPECT_EQ(mExpected120Config,
596               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
597 
598     lr1.desiredRefreshRate = 24.0f;
599     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
600     lr1.name = "24Hz ExplicitExactOrMultiple";
601     lr2.desiredRefreshRate = 60.0f;
602     lr2.vote = LayerVoteType::ExplicitDefault;
603     lr2.name = "60Hz ExplicitDefault";
604     EXPECT_EQ(mExpected120Config,
605               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
606 
607     lr1.desiredRefreshRate = 24.0f;
608     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
609     lr1.name = "24Hz ExplicitExactOrMultiple";
610     lr2.desiredRefreshRate = 90.0f;
611     lr2.vote = LayerVoteType::Heuristic;
612     lr2.name = "90Hz Heuristic";
613     EXPECT_EQ(mExpected90Config,
614               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
615 
616     lr1.desiredRefreshRate = 24.0f;
617     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
618     lr1.name = "24Hz ExplicitExactOrMultiple";
619     lr2.desiredRefreshRate = 90.0f;
620     lr2.vote = LayerVoteType::ExplicitDefault;
621     lr2.name = "90Hz Heuristic";
622     EXPECT_EQ(mExpected72Config,
623               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
624 
625     lr1.desiredRefreshRate = 24.0f;
626     lr1.vote = LayerVoteType::ExplicitDefault;
627     lr1.name = "24Hz ExplicitDefault";
628     lr2.desiredRefreshRate = 90.0f;
629     lr2.vote = LayerVoteType::Heuristic;
630     lr2.name = "90Hz Heuristic";
631     EXPECT_EQ(mExpected90Config,
632               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
633 
634     lr1.desiredRefreshRate = 24.0f;
635     lr1.vote = LayerVoteType::Heuristic;
636     lr1.name = "24Hz Heuristic";
637     lr2.desiredRefreshRate = 90.0f;
638     lr2.vote = LayerVoteType::ExplicitDefault;
639     lr2.name = "90Hz ExplicitDefault";
640     EXPECT_EQ(mExpected72Config,
641               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
642 
643     lr1.desiredRefreshRate = 24.0f;
644     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
645     lr1.name = "24Hz ExplicitExactOrMultiple";
646     lr2.desiredRefreshRate = 90.0f;
647     lr2.vote = LayerVoteType::ExplicitDefault;
648     lr2.name = "90Hz ExplicitDefault";
649     EXPECT_EQ(mExpected72Config,
650               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
651 
652     lr1.desiredRefreshRate = 24.0f;
653     lr1.vote = LayerVoteType::ExplicitDefault;
654     lr1.name = "24Hz ExplicitDefault";
655     lr2.desiredRefreshRate = 90.0f;
656     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
657     lr2.name = "90Hz ExplicitExactOrMultiple";
658     EXPECT_EQ(mExpected90Config,
659               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
660 }
661 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_30_60)662 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_30_60) {
663     auto refreshRateConfigs =
664             std::make_unique<RefreshRateConfigs>(m30_60Device,
665                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
666 
667     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
668     auto& lr = layers[0];
669 
670     lr.vote = LayerVoteType::Min;
671     EXPECT_EQ(mExpected30Config,
672               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
673 
674     lr.vote = LayerVoteType::Max;
675     EXPECT_EQ(mExpected60Config,
676               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
677 
678     lr.desiredRefreshRate = 90.0f;
679     lr.vote = LayerVoteType::Heuristic;
680     EXPECT_EQ(mExpected60Config,
681               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
682 
683     lr.desiredRefreshRate = 60.0f;
684     EXPECT_EQ(mExpected60Config,
685               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
686 
687     lr.desiredRefreshRate = 45.0f;
688     EXPECT_EQ(mExpected60Config,
689               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
690 
691     lr.desiredRefreshRate = 30.0f;
692     EXPECT_EQ(mExpected30Config,
693               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
694 
695     lr.desiredRefreshRate = 24.0f;
696     EXPECT_EQ(mExpected60Config,
697               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
698 }
699 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_30_60_72_90)700 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_30_60_72_90) {
701     auto refreshRateConfigs =
702             std::make_unique<RefreshRateConfigs>(m30_60_72_90Device,
703                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
704 
705     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
706     auto& lr = layers[0];
707 
708     lr.vote = LayerVoteType::Min;
709     lr.name = "Min";
710     EXPECT_EQ(mExpected30Config,
711               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
712 
713     lr.vote = LayerVoteType::Max;
714     lr.name = "Max";
715     EXPECT_EQ(mExpected90Config,
716               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
717 
718     lr.desiredRefreshRate = 90.0f;
719     lr.vote = LayerVoteType::Heuristic;
720     lr.name = "90Hz Heuristic";
721     EXPECT_EQ(mExpected90Config,
722               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
723 
724     lr.desiredRefreshRate = 60.0f;
725     lr.name = "60Hz Heuristic";
726     EXPECT_EQ(mExpected60Config,
727               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
728     EXPECT_EQ(mExpected90Config,
729               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
730 
731     lr.desiredRefreshRate = 45.0f;
732     lr.name = "45Hz Heuristic";
733     EXPECT_EQ(mExpected90Config,
734               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
735     EXPECT_EQ(mExpected90Config,
736               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
737 
738     lr.desiredRefreshRate = 30.0f;
739     lr.name = "30Hz Heuristic";
740     EXPECT_EQ(mExpected30Config,
741               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
742     EXPECT_EQ(mExpected90Config,
743               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
744 
745     lr.desiredRefreshRate = 24.0f;
746     lr.name = "24Hz Heuristic";
747     EXPECT_EQ(mExpected72Config,
748               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
749     EXPECT_EQ(mExpected90Config,
750               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
751 
752     lr.desiredRefreshRate = 24.0f;
753     lr.vote = LayerVoteType::ExplicitExactOrMultiple;
754     lr.name = "24Hz ExplicitExactOrMultiple";
755     EXPECT_EQ(mExpected72Config,
756               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
757     EXPECT_EQ(mExpected90Config,
758               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
759 }
760 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_PriorityTest)761 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_PriorityTest) {
762     auto refreshRateConfigs =
763             std::make_unique<RefreshRateConfigs>(m30_60_90Device,
764                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
765 
766     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
767                                                 LayerRequirement{.weight = 1.0f}};
768     auto& lr1 = layers[0];
769     auto& lr2 = layers[1];
770 
771     lr1.vote = LayerVoteType::Min;
772     lr2.vote = LayerVoteType::Max;
773     EXPECT_EQ(mExpected90Config,
774               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
775 
776     lr1.vote = LayerVoteType::Min;
777     lr2.vote = LayerVoteType::Heuristic;
778     lr2.desiredRefreshRate = 24.0f;
779     EXPECT_EQ(mExpected60Config,
780               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
781 
782     lr1.vote = LayerVoteType::Min;
783     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
784     lr2.desiredRefreshRate = 24.0f;
785     EXPECT_EQ(mExpected60Config,
786               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
787 
788     lr1.vote = LayerVoteType::Max;
789     lr2.vote = LayerVoteType::Heuristic;
790     lr2.desiredRefreshRate = 60.0f;
791     EXPECT_EQ(mExpected90Config,
792               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
793 
794     lr1.vote = LayerVoteType::Max;
795     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
796     lr2.desiredRefreshRate = 60.0f;
797     EXPECT_EQ(mExpected90Config,
798               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
799 
800     lr1.vote = LayerVoteType::Heuristic;
801     lr1.desiredRefreshRate = 15.0f;
802     lr2.vote = LayerVoteType::Heuristic;
803     lr2.desiredRefreshRate = 45.0f;
804     EXPECT_EQ(mExpected90Config,
805               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
806 
807     lr1.vote = LayerVoteType::Heuristic;
808     lr1.desiredRefreshRate = 30.0f;
809     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
810     lr2.desiredRefreshRate = 45.0f;
811     EXPECT_EQ(mExpected90Config,
812               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
813 }
814 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_24FpsVideo)815 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_24FpsVideo) {
816     auto refreshRateConfigs =
817             std::make_unique<RefreshRateConfigs>(m60_90Device,
818                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
819 
820     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
821     auto& lr = layers[0];
822 
823     lr.vote = LayerVoteType::ExplicitExactOrMultiple;
824     for (float fps = 23.0f; fps < 25.0f; fps += 0.1f) {
825         lr.desiredRefreshRate = fps;
826         const auto& refreshRate =
827                 refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false});
828         EXPECT_EQ(mExpected60Config, refreshRate) << fps << "Hz chooses " << refreshRate.getName();
829     }
830 }
831 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_getRefreshRateForContent_Explicit)832 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_getRefreshRateForContent_Explicit) {
833     auto refreshRateConfigs =
834             std::make_unique<RefreshRateConfigs>(m60_90Device,
835                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
836 
837     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
838                                                 LayerRequirement{.weight = 1.0f}};
839     auto& lr1 = layers[0];
840     auto& lr2 = layers[1];
841 
842     lr1.vote = LayerVoteType::Heuristic;
843     lr1.desiredRefreshRate = 60.0f;
844     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
845     lr2.desiredRefreshRate = 90.0f;
846     EXPECT_EQ(mExpected90Config, refreshRateConfigs->getRefreshRateForContent(layers));
847 
848     lr1.vote = LayerVoteType::Heuristic;
849     lr1.desiredRefreshRate = 90.0f;
850     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
851     lr2.desiredRefreshRate = 60.0f;
852     EXPECT_EQ(mExpected60Config, refreshRateConfigs->getRefreshRateForContent(layers));
853 }
854 
TEST_F(RefreshRateConfigsTest,twoDeviceConfigs_getBestRefreshRate_Explicit)855 TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_getBestRefreshRate_Explicit) {
856     auto refreshRateConfigs =
857             std::make_unique<RefreshRateConfigs>(m60_90Device,
858                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
859 
860     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
861                                                 LayerRequirement{.weight = 1.0f}};
862     auto& lr1 = layers[0];
863     auto& lr2 = layers[1];
864 
865     lr1.vote = LayerVoteType::Heuristic;
866     lr1.desiredRefreshRate = 60.0f;
867     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
868     lr2.desiredRefreshRate = 90.0f;
869     EXPECT_EQ(mExpected90Config,
870               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
871 
872     lr1.vote = LayerVoteType::ExplicitDefault;
873     lr1.desiredRefreshRate = 90.0f;
874     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
875     lr2.desiredRefreshRate = 60.0f;
876     EXPECT_EQ(mExpected60Config,
877               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
878 
879     lr1.vote = LayerVoteType::Heuristic;
880     lr1.desiredRefreshRate = 90.0f;
881     lr2.vote = LayerVoteType::ExplicitExactOrMultiple;
882     lr2.desiredRefreshRate = 60.0f;
883     EXPECT_EQ(mExpected90Config,
884               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
885 }
886 
TEST_F(RefreshRateConfigsTest,testInPolicy)887 TEST_F(RefreshRateConfigsTest, testInPolicy) {
888     ASSERT_TRUE(mExpectedAlmost60Config.inPolicy(60.000004f, 60.000004f));
889     ASSERT_TRUE(mExpectedAlmost60Config.inPolicy(59.0f, 60.1f));
890     ASSERT_FALSE(mExpectedAlmost60Config.inPolicy(75.0f, 90.0f));
891     ASSERT_FALSE(mExpectedAlmost60Config.inPolicy(60.0011f, 90.0f));
892     ASSERT_FALSE(mExpectedAlmost60Config.inPolicy(50.0f, 59.998f));
893 }
894 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_75HzContent)895 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_75HzContent) {
896     auto refreshRateConfigs =
897             std::make_unique<RefreshRateConfigs>(m60_90Device,
898                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
899 
900     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
901     auto& lr = layers[0];
902 
903     lr.vote = LayerVoteType::ExplicitExactOrMultiple;
904     for (float fps = 75.0f; fps < 100.0f; fps += 0.1f) {
905         lr.desiredRefreshRate = fps;
906         const auto& refreshRate =
907                 refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false});
908         EXPECT_EQ(mExpected90Config, refreshRate) << fps << "Hz chooses " << refreshRate.getName();
909     }
910 }
911 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_Multiples)912 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_Multiples) {
913     auto refreshRateConfigs =
914             std::make_unique<RefreshRateConfigs>(m60_90Device,
915                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
916 
917     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
918                                                 LayerRequirement{.weight = 1.0f}};
919     auto& lr1 = layers[0];
920     auto& lr2 = layers[1];
921 
922     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
923     lr1.desiredRefreshRate = 60.0f;
924     lr1.name = "60Hz ExplicitExactOrMultiple";
925     lr2.vote = LayerVoteType::Heuristic;
926     lr2.desiredRefreshRate = 90.0f;
927     lr2.name = "90Hz Heuristic";
928     EXPECT_EQ(mExpected90Config,
929               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
930 
931     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
932     lr1.desiredRefreshRate = 60.0f;
933     lr1.name = "60Hz ExplicitExactOrMultiple";
934     lr2.vote = LayerVoteType::ExplicitDefault;
935     lr2.desiredRefreshRate = 90.0f;
936     lr2.name = "90Hz ExplicitDefault";
937     EXPECT_EQ(mExpected60Config,
938               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
939 
940     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
941     lr1.desiredRefreshRate = 60.0f;
942     lr1.name = "60Hz ExplicitExactOrMultiple";
943     lr2.vote = LayerVoteType::Max;
944     lr2.name = "Max";
945     EXPECT_EQ(mExpected90Config,
946               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
947 
948     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
949     lr1.desiredRefreshRate = 30.0f;
950     lr1.name = "30Hz ExplicitExactOrMultiple";
951     lr2.vote = LayerVoteType::Heuristic;
952     lr2.desiredRefreshRate = 90.0f;
953     lr2.name = "90Hz Heuristic";
954     EXPECT_EQ(mExpected90Config,
955               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
956 
957     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
958     lr1.desiredRefreshRate = 30.0f;
959     lr1.name = "30Hz ExplicitExactOrMultiple";
960     lr2.vote = LayerVoteType::Max;
961     lr2.name = "Max";
962     EXPECT_EQ(mExpected90Config,
963               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
964 }
965 
TEST_F(RefreshRateConfigsTest,scrollWhileWatching60fps_60_90)966 TEST_F(RefreshRateConfigsTest, scrollWhileWatching60fps_60_90) {
967     auto refreshRateConfigs =
968             std::make_unique<RefreshRateConfigs>(m60_90Device,
969                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
970 
971     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
972                                                 LayerRequirement{.weight = 1.0f}};
973     auto& lr1 = layers[0];
974     auto& lr2 = layers[1];
975 
976     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
977     lr1.desiredRefreshRate = 60.0f;
978     lr1.name = "60Hz ExplicitExactOrMultiple";
979     lr2.vote = LayerVoteType::NoVote;
980     lr2.name = "NoVote";
981     EXPECT_EQ(mExpected60Config,
982               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
983 
984     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
985     lr1.desiredRefreshRate = 60.0f;
986     lr1.name = "60Hz ExplicitExactOrMultiple";
987     lr2.vote = LayerVoteType::NoVote;
988     lr2.name = "NoVote";
989     EXPECT_EQ(mExpected90Config,
990               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
991 
992     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
993     lr1.desiredRefreshRate = 60.0f;
994     lr1.name = "60Hz ExplicitExactOrMultiple";
995     lr2.vote = LayerVoteType::Max;
996     lr2.name = "Max";
997     EXPECT_EQ(mExpected90Config,
998               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false}));
999 
1000     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
1001     lr1.desiredRefreshRate = 60.0f;
1002     lr1.name = "60Hz ExplicitExactOrMultiple";
1003     lr2.vote = LayerVoteType::Max;
1004     lr2.name = "Max";
1005     EXPECT_EQ(mExpected90Config,
1006               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1007 
1008     // The other layer starts to provide buffers
1009     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
1010     lr1.desiredRefreshRate = 60.0f;
1011     lr1.name = "60Hz ExplicitExactOrMultiple";
1012     lr2.vote = LayerVoteType::Heuristic;
1013     lr2.desiredRefreshRate = 90.0f;
1014     lr2.name = "90Hz Heuristic";
1015     EXPECT_EQ(mExpected90Config,
1016               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1017 }
1018 
TEST_F(RefreshRateConfigsTest,touchConsidered)1019 TEST_F(RefreshRateConfigsTest, touchConsidered) {
1020     RefreshRateConfigs::GlobalSignals consideredSignals;
1021     auto refreshRateConfigs =
1022             std::make_unique<RefreshRateConfigs>(m60_90Device,
1023                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1024 
1025     refreshRateConfigs->getBestRefreshRate({}, {.touch = false, .idle = false}, &consideredSignals);
1026     EXPECT_EQ(false, consideredSignals.touch);
1027 
1028     refreshRateConfigs->getBestRefreshRate({}, {.touch = true, .idle = false}, &consideredSignals);
1029     EXPECT_EQ(true, consideredSignals.touch);
1030 
1031     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f},
1032                                                 LayerRequirement{.weight = 1.0f}};
1033     auto& lr1 = layers[0];
1034     auto& lr2 = layers[1];
1035 
1036     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
1037     lr1.desiredRefreshRate = 60.0f;
1038     lr1.name = "60Hz ExplicitExactOrMultiple";
1039     lr2.vote = LayerVoteType::Heuristic;
1040     lr2.desiredRefreshRate = 60.0f;
1041     lr2.name = "60Hz Heuristic";
1042     refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false},
1043                                            &consideredSignals);
1044     EXPECT_EQ(true, consideredSignals.touch);
1045 
1046     lr1.vote = LayerVoteType::ExplicitDefault;
1047     lr1.desiredRefreshRate = 60.0f;
1048     lr1.name = "60Hz ExplicitExactOrMultiple";
1049     lr2.vote = LayerVoteType::Heuristic;
1050     lr2.desiredRefreshRate = 60.0f;
1051     lr2.name = "60Hz Heuristic";
1052     refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false},
1053                                            &consideredSignals);
1054     EXPECT_EQ(false, consideredSignals.touch);
1055 
1056     lr1.vote = LayerVoteType::ExplicitExactOrMultiple;
1057     lr1.desiredRefreshRate = 60.0f;
1058     lr1.name = "60Hz ExplicitExactOrMultiple";
1059     lr2.vote = LayerVoteType::Heuristic;
1060     lr2.desiredRefreshRate = 60.0f;
1061     lr2.name = "60Hz Heuristic";
1062     refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false},
1063                                            &consideredSignals);
1064     EXPECT_EQ(true, consideredSignals.touch);
1065 
1066     lr1.vote = LayerVoteType::ExplicitDefault;
1067     lr1.desiredRefreshRate = 60.0f;
1068     lr1.name = "60Hz ExplicitExactOrMultiple";
1069     lr2.vote = LayerVoteType::Heuristic;
1070     lr2.desiredRefreshRate = 60.0f;
1071     lr2.name = "60Hz Heuristic";
1072     refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = false},
1073                                            &consideredSignals);
1074     EXPECT_EQ(false, consideredSignals.touch);
1075 }
1076 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_ExplicitDefault)1077 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_ExplicitDefault) {
1078     auto refreshRateConfigs =
1079             std::make_unique<RefreshRateConfigs>(m60_90_72_120Device, /*currentConfigId=*/
1080                                                  HWC_CONFIG_ID_60);
1081 
1082     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1083     auto& lr = layers[0];
1084 
1085     // Prepare a table with the vote and the expected refresh rate
1086     const std::vector<std::pair<float, float>> testCases = {
1087             {130, 120}, {120, 120}, {119, 120}, {110, 120},
1088 
1089             {100, 90},  {90, 90},   {89, 90},
1090 
1091             {80, 72},   {73, 72},   {72, 72},   {71, 72},   {70, 72},
1092 
1093             {65, 60},   {60, 60},   {59, 60},   {58, 60},
1094 
1095             {55, 90},   {50, 90},   {45, 90},
1096 
1097             {42, 120},  {40, 120},  {39, 120},
1098 
1099             {37, 72},   {36, 72},   {35, 72},
1100 
1101             {30, 60},
1102     };
1103 
1104     for (const auto& test : testCases) {
1105         lr.vote = LayerVoteType::ExplicitDefault;
1106         lr.desiredRefreshRate = test.first;
1107 
1108         std::stringstream ss;
1109         ss << "ExplicitDefault " << test.first << " fps";
1110         lr.name = ss.str();
1111 
1112         const auto& refreshRate =
1113                 refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false});
1114         EXPECT_FLOAT_EQ(refreshRate.getFps(), test.second)
1115                 << "Expecting " << test.first << "fps => " << test.second << "Hz";
1116     }
1117 }
1118 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_withDisplayManagerRequestingSingleRate_ignoresTouchFlag)1119 TEST_F(RefreshRateConfigsTest,
1120        getBestRefreshRate_withDisplayManagerRequestingSingleRate_ignoresTouchFlag) {
1121     auto refreshRateConfigs =
1122             std::make_unique<RefreshRateConfigs>(m60_90Device,
1123                                                  /*currentConfigId=*/HWC_CONFIG_ID_90);
1124 
1125     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1126                       {HWC_CONFIG_ID_90, {90.f, 90.f}, {60.f, 90.f}}),
1127               0);
1128 
1129     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1130     auto& lr = layers[0];
1131 
1132     RefreshRateConfigs::GlobalSignals consideredSignals;
1133     lr.vote = LayerVoteType::ExplicitDefault;
1134     lr.desiredRefreshRate = 60.0f;
1135     lr.name = "60Hz ExplicitDefault";
1136     lr.focused = true;
1137     EXPECT_EQ(mExpected60Config,
1138               refreshRateConfigs->getBestRefreshRate(layers, {.touch = true, .idle = true},
1139                                                      &consideredSignals));
1140     EXPECT_EQ(false, consideredSignals.touch);
1141 }
1142 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_withDisplayManagerRequestingSingleRate_ignoresIdleFlag)1143 TEST_F(RefreshRateConfigsTest,
1144        getBestRefreshRate_withDisplayManagerRequestingSingleRate_ignoresIdleFlag) {
1145     auto refreshRateConfigs =
1146             std::make_unique<RefreshRateConfigs>(m60_90Device,
1147                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1148 
1149     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1150                       {HWC_CONFIG_ID_60, {60.f, 60.f}, {60.f, 90.f}}),
1151               0);
1152 
1153     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1154     auto& lr = layers[0];
1155 
1156     lr.vote = LayerVoteType::ExplicitDefault;
1157     lr.desiredRefreshRate = 90.0f;
1158     lr.name = "90Hz ExplicitDefault";
1159     lr.focused = true;
1160     EXPECT_EQ(mExpected90Config,
1161               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = true}));
1162 }
1163 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_withDisplayManagerRequestingSingleRate_onlySwitchesRatesForExplicitFocusedLayers)1164 TEST_F(RefreshRateConfigsTest,
1165        getBestRefreshRate_withDisplayManagerRequestingSingleRate_onlySwitchesRatesForExplicitFocusedLayers) {
1166     auto refreshRateConfigs =
1167             std::make_unique<RefreshRateConfigs>(m60_90Device,
1168                                                  /*currentConfigId=*/HWC_CONFIG_ID_90);
1169 
1170     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1171                       {HWC_CONFIG_ID_90, {90.f, 90.f}, {60.f, 90.f}}),
1172               0);
1173 
1174     RefreshRateConfigs::GlobalSignals consideredSignals;
1175     EXPECT_EQ(mExpected90Config,
1176               refreshRateConfigs->getBestRefreshRate({}, {.touch = false, .idle = false},
1177                                                      &consideredSignals));
1178     EXPECT_EQ(false, consideredSignals.touch);
1179 
1180     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1181     auto& lr = layers[0];
1182 
1183     lr.vote = LayerVoteType::ExplicitExactOrMultiple;
1184     lr.desiredRefreshRate = 60.0f;
1185     lr.name = "60Hz ExplicitExactOrMultiple";
1186     lr.focused = false;
1187     EXPECT_EQ(mExpected90Config,
1188               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1189 
1190     lr.focused = true;
1191     EXPECT_EQ(mExpected90Config,
1192               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1193 
1194     lr.vote = LayerVoteType::ExplicitDefault;
1195     lr.desiredRefreshRate = 60.0f;
1196     lr.name = "60Hz ExplicitDefault";
1197     lr.focused = false;
1198     EXPECT_EQ(mExpected90Config,
1199               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1200 
1201     lr.focused = true;
1202     EXPECT_EQ(mExpected60Config,
1203               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1204 
1205     lr.vote = LayerVoteType::Heuristic;
1206     lr.desiredRefreshRate = 60.0f;
1207     lr.name = "60Hz Heuristic";
1208     lr.focused = false;
1209     EXPECT_EQ(mExpected90Config,
1210               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1211 
1212     lr.focused = true;
1213     EXPECT_EQ(mExpected90Config,
1214               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1215 
1216     lr.vote = LayerVoteType::Max;
1217     lr.desiredRefreshRate = 60.0f;
1218     lr.name = "60Hz Max";
1219     lr.focused = false;
1220     EXPECT_EQ(mExpected90Config,
1221               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1222 
1223     lr.focused = true;
1224     EXPECT_EQ(mExpected90Config,
1225               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1226 
1227     lr.vote = LayerVoteType::Min;
1228     lr.desiredRefreshRate = 60.0f;
1229     lr.name = "60Hz Min";
1230     lr.focused = false;
1231     EXPECT_EQ(mExpected90Config,
1232               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1233 
1234     lr.focused = true;
1235     EXPECT_EQ(mExpected90Config,
1236               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false}));
1237 }
1238 
TEST_F(RefreshRateConfigsTest,groupSwitching)1239 TEST_F(RefreshRateConfigsTest, groupSwitching) {
1240     auto refreshRateConfigs =
1241             std::make_unique<RefreshRateConfigs>(m60_90DeviceWithDifferentGroups,
1242                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1243 
1244     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1245     auto& layer = layers[0];
1246     layer.vote = LayerVoteType::ExplicitDefault;
1247     layer.desiredRefreshRate = 90.0f;
1248     layer.name = "90Hz ExplicitDefault";
1249 
1250     ASSERT_EQ(HWC_CONFIG_ID_60,
1251               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false})
1252                       .getConfigId());
1253 
1254     RefreshRateConfigs::Policy policy;
1255     policy.defaultConfig = refreshRateConfigs->getCurrentPolicy().defaultConfig;
1256     policy.allowGroupSwitching = true;
1257     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(policy), 0);
1258     ASSERT_EQ(HWC_CONFIG_ID_90,
1259               refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false})
1260                       .getConfigId());
1261 }
1262 
TEST_F(RefreshRateConfigsTest,primaryVsAppRequestPolicy)1263 TEST_F(RefreshRateConfigsTest, primaryVsAppRequestPolicy) {
1264     auto refreshRateConfigs =
1265             std::make_unique<RefreshRateConfigs>(m30_60_90Device,
1266                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1267 
1268     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1269     layers[0].name = "Test layer";
1270 
1271     // Return the config ID from calling getBestRefreshRate() for a single layer with the
1272     // given voteType and fps.
1273     auto getFrameRate = [&](LayerVoteType voteType, float fps, bool touchActive = false,
1274                             bool focused = true) -> HwcConfigIndexType {
1275         layers[0].vote = voteType;
1276         layers[0].desiredRefreshRate = fps;
1277         layers[0].focused = focused;
1278         return refreshRateConfigs->getBestRefreshRate(layers, {.touch = touchActive, .idle = false})
1279                 .getConfigId();
1280     };
1281 
1282     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1283                       {HWC_CONFIG_ID_60, {30.f, 60.f}, {30.f, 90.f}}),
1284               0);
1285     EXPECT_EQ(HWC_CONFIG_ID_60,
1286               refreshRateConfigs->getBestRefreshRate({}, {.touch = false, .idle = false})
1287                       .getConfigId());
1288     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::NoVote, 90.f));
1289     EXPECT_EQ(HWC_CONFIG_ID_30, getFrameRate(LayerVoteType::Min, 90.f));
1290     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Max, 90.f));
1291     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Heuristic, 90.f));
1292     EXPECT_EQ(HWC_CONFIG_ID_90, getFrameRate(LayerVoteType::ExplicitDefault, 90.f));
1293     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::ExplicitExactOrMultiple, 90.f));
1294 
1295     // Layers not focused are not allowed to override primary config
1296     EXPECT_EQ(HWC_CONFIG_ID_60,
1297               getFrameRate(LayerVoteType::ExplicitDefault, 90.f, /*touch=*/false,
1298                            /*focused=*/false));
1299     EXPECT_EQ(HWC_CONFIG_ID_60,
1300               getFrameRate(LayerVoteType::ExplicitExactOrMultiple, 90.f, /*touch=*/false,
1301                            /*focused=*/false));
1302 
1303     // Touch boost should be restricted to the primary range.
1304     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Max, 90.f, /*touch=*/true));
1305     // When we're higher than the primary range max due to a layer frame rate setting, touch boost
1306     // shouldn't drag us back down to the primary range max.
1307     EXPECT_EQ(HWC_CONFIG_ID_90, getFrameRate(LayerVoteType::ExplicitDefault, 90.f, /*touch=*/true));
1308     EXPECT_EQ(HWC_CONFIG_ID_60,
1309               getFrameRate(LayerVoteType::ExplicitExactOrMultiple, 90.f, /*touch=*/true));
1310 
1311     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1312                       {HWC_CONFIG_ID_60, {60.f, 60.f}, {60.f, 60.f}}),
1313               0);
1314     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::NoVote, 90.f));
1315     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Min, 90.f));
1316     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Max, 90.f));
1317     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::Heuristic, 90.f));
1318     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::ExplicitDefault, 90.f));
1319     EXPECT_EQ(HWC_CONFIG_ID_60, getFrameRate(LayerVoteType::ExplicitExactOrMultiple, 90.f));
1320 }
1321 
TEST_F(RefreshRateConfigsTest,idle)1322 TEST_F(RefreshRateConfigsTest, idle) {
1323     auto refreshRateConfigs =
1324             std::make_unique<RefreshRateConfigs>(m60_90Device,
1325                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1326 
1327     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1328     layers[0].name = "Test layer";
1329 
1330     const auto getIdleFrameRate = [&](LayerVoteType voteType,
1331                                       bool touchActive) -> HwcConfigIndexType {
1332         layers[0].vote = voteType;
1333         layers[0].desiredRefreshRate = 90.f;
1334         RefreshRateConfigs::GlobalSignals consideredSignals;
1335         const auto configId =
1336                 refreshRateConfigs
1337                         ->getBestRefreshRate(layers, {.touch = touchActive, .idle = true},
1338                                              &consideredSignals)
1339                         .getConfigId();
1340         // Refresh rate will be chosen by either touch state or idle state
1341         EXPECT_EQ(!touchActive, consideredSignals.idle);
1342         return configId;
1343     };
1344 
1345     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy(
1346                       {HWC_CONFIG_ID_60, {60.f, 90.f}, {60.f, 90.f}}),
1347               0);
1348 
1349     // Idle should be lower priority than touch boost.
1350     EXPECT_EQ(HWC_CONFIG_ID_90, getIdleFrameRate(LayerVoteType::NoVote, /*touchActive=*/true));
1351     EXPECT_EQ(HWC_CONFIG_ID_90, getIdleFrameRate(LayerVoteType::Min, /*touchActive=*/true));
1352     EXPECT_EQ(HWC_CONFIG_ID_90, getIdleFrameRate(LayerVoteType::Max, /*touchActive=*/true));
1353     EXPECT_EQ(HWC_CONFIG_ID_90, getIdleFrameRate(LayerVoteType::Heuristic, /*touchActive=*/true));
1354     EXPECT_EQ(HWC_CONFIG_ID_90,
1355               getIdleFrameRate(LayerVoteType::ExplicitDefault, /*touchActive=*/true));
1356     EXPECT_EQ(HWC_CONFIG_ID_90,
1357               getIdleFrameRate(LayerVoteType::ExplicitExactOrMultiple, /*touchActive=*/true));
1358 
1359     // With no layers, idle should still be lower priority than touch boost.
1360     EXPECT_EQ(HWC_CONFIG_ID_90,
1361               refreshRateConfigs->getBestRefreshRate({}, {.touch = true, .idle = true})
1362                       .getConfigId());
1363 
1364     // Idle should be higher precedence than other layer frame rate considerations.
1365     refreshRateConfigs->setCurrentConfigId(HWC_CONFIG_ID_90);
1366     EXPECT_EQ(HWC_CONFIG_ID_60, getIdleFrameRate(LayerVoteType::NoVote, /*touchActive=*/false));
1367     EXPECT_EQ(HWC_CONFIG_ID_60, getIdleFrameRate(LayerVoteType::Min, /*touchActive=*/false));
1368     EXPECT_EQ(HWC_CONFIG_ID_60, getIdleFrameRate(LayerVoteType::Max, /*touchActive=*/false));
1369     EXPECT_EQ(HWC_CONFIG_ID_60, getIdleFrameRate(LayerVoteType::Heuristic, /*touchActive=*/false));
1370     EXPECT_EQ(HWC_CONFIG_ID_60,
1371               getIdleFrameRate(LayerVoteType::ExplicitDefault, /*touchActive=*/false));
1372     EXPECT_EQ(HWC_CONFIG_ID_60,
1373               getIdleFrameRate(LayerVoteType::ExplicitExactOrMultiple, /*touchActive=*/false));
1374 
1375     // Idle should be applied rather than the current config when there are no layers.
1376     EXPECT_EQ(HWC_CONFIG_ID_60,
1377               refreshRateConfigs->getBestRefreshRate({}, {.touch = false, .idle = true})
1378                       .getConfigId());
1379 }
1380 
TEST_F(RefreshRateConfigsTest,findClosestKnownFrameRate)1381 TEST_F(RefreshRateConfigsTest, findClosestKnownFrameRate) {
1382     auto refreshRateConfigs =
1383             std::make_unique<RefreshRateConfigs>(m60_90Device,
1384                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1385 
1386     for (float fps = 1.0f; fps <= 120.0f; fps += 0.1f) {
1387         const auto knownFrameRate = findClosestKnownFrameRate(*refreshRateConfigs, fps);
1388         float expectedFrameRate;
1389         if (fps < 26.91f) {
1390             expectedFrameRate = 24.0f;
1391         } else if (fps < 37.51f) {
1392             expectedFrameRate = 30.0f;
1393         } else if (fps < 52.51f) {
1394             expectedFrameRate = 45.0f;
1395         } else if (fps < 66.01f) {
1396             expectedFrameRate = 60.0f;
1397         } else if (fps < 81.01f) {
1398             expectedFrameRate = 72.0f;
1399         } else {
1400             expectedFrameRate = 90.0f;
1401         }
1402         EXPECT_FLOAT_EQ(expectedFrameRate, knownFrameRate)
1403                 << "findClosestKnownFrameRate(" << fps << ") = " << knownFrameRate;
1404     }
1405 }
1406 
TEST_F(RefreshRateConfigsTest,getBestRefreshRate_KnownFrameRate)1407 TEST_F(RefreshRateConfigsTest, getBestRefreshRate_KnownFrameRate) {
1408     auto refreshRateConfigs =
1409             std::make_unique<RefreshRateConfigs>(m60_90Device,
1410                                                  /*currentConfigId=*/HWC_CONFIG_ID_60);
1411 
1412     struct ExpectedRate {
1413         float rate;
1414         const RefreshRate& expected;
1415     };
1416 
1417     /* clang-format off */
1418     std::vector<ExpectedRate> knownFrameRatesExpectations = {
1419         {24.0f, mExpected60Config},
1420         {30.0f, mExpected60Config},
1421         {45.0f, mExpected90Config},
1422         {60.0f, mExpected60Config},
1423         {72.0f, mExpected90Config},
1424         {90.0f, mExpected90Config},
1425     };
1426     /* clang-format on */
1427 
1428     // Make sure the test tests all the known frame rate
1429     const auto knownFrameRateList = getKnownFrameRate(*refreshRateConfigs);
1430     const auto equal = std::equal(knownFrameRateList.begin(), knownFrameRateList.end(),
1431                                   knownFrameRatesExpectations.begin(),
1432                                   [](float a, const ExpectedRate& b) { return a == b.rate; });
1433     EXPECT_TRUE(equal);
1434 
1435     auto layers = std::vector<LayerRequirement>{LayerRequirement{.weight = 1.0f}};
1436     auto& layer = layers[0];
1437     layer.vote = LayerVoteType::Heuristic;
1438     for (const auto& expectedRate : knownFrameRatesExpectations) {
1439         layer.desiredRefreshRate = expectedRate.rate;
1440         const auto& refreshRate =
1441                 refreshRateConfigs->getBestRefreshRate(layers, {.touch = false, .idle = false});
1442         EXPECT_EQ(expectedRate.expected, refreshRate);
1443     }
1444 }
1445 
TEST_F(RefreshRateConfigsTest,testComparisonOperator)1446 TEST_F(RefreshRateConfigsTest, testComparisonOperator) {
1447     EXPECT_TRUE(mExpected60Config < mExpected90Config);
1448     EXPECT_FALSE(mExpected60Config < mExpected60Config);
1449     EXPECT_FALSE(mExpected90Config < mExpected90Config);
1450 }
1451 
TEST_F(RefreshRateConfigsTest,testKernelIdleTimerAction)1452 TEST_F(RefreshRateConfigsTest, testKernelIdleTimerAction) {
1453     using KernelIdleTimerAction = scheduler::RefreshRateConfigs::KernelIdleTimerAction;
1454 
1455     auto refreshRateConfigs =
1456             std::make_unique<RefreshRateConfigs>(m60_90Device,
1457                                                  /*currentConfigId=*/HWC_CONFIG_ID_90);
1458     // SetPolicy(60, 90), current 90Hz => TurnOn.
1459     EXPECT_EQ(KernelIdleTimerAction::TurnOn, refreshRateConfigs->getIdleTimerAction());
1460 
1461     // SetPolicy(60, 90), current 60Hz => TurnOn.
1462     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 90}}), 0);
1463     EXPECT_EQ(KernelIdleTimerAction::TurnOn, refreshRateConfigs->getIdleTimerAction());
1464 
1465     // SetPolicy(60, 60), current 60Hz => NoChange, avoid extra calls.
1466     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_60, {60, 60}}), 0);
1467     EXPECT_EQ(KernelIdleTimerAction::NoChange, refreshRateConfigs->getIdleTimerAction());
1468 
1469     // SetPolicy(90, 90), current 90Hz => TurnOff.
1470     ASSERT_GE(refreshRateConfigs->setDisplayManagerPolicy({HWC_CONFIG_ID_90, {90, 90}}), 0);
1471     EXPECT_EQ(KernelIdleTimerAction::TurnOff, refreshRateConfigs->getIdleTimerAction());
1472 }
1473 
1474 } // namespace
1475 } // namespace scheduler
1476 } // namespace android
1477