blob: 5067fe890bfe615ca42a028f20dd388dbb465d7f [file] [log] [blame]
Alec Mouri0a1cc962019-03-14 12:33:02 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
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 "DisplayHardware/HWC2.h"
25#include "Scheduler/RefreshRateConfigs.h"
26#include "mock/DisplayHardware/MockDisplay.h"
27
28using namespace std::chrono_literals;
29using testing::_;
30
31namespace android {
32namespace scheduler {
33
34using RefreshRateType = RefreshRateConfigs::RefreshRateType;
35using RefreshRate = RefreshRateConfigs::RefreshRate;
36
37class RefreshRateConfigsTest : public testing::Test {
38protected:
39 static constexpr int CONFIG_ID_60 = 0;
Ady Abraham796beb02019-04-11 15:23:07 -070040 static constexpr hwc2_config_t HWC2_CONFIG_ID_60 = 0;
Alec Mouri0a1cc962019-03-14 12:33:02 -070041 static constexpr int CONFIG_ID_90 = 1;
Ady Abraham796beb02019-04-11 15:23:07 -070042 static constexpr hwc2_config_t HWC2_CONFIG_ID_90 = 1;
Alec Mouri0a1cc962019-03-14 12:33:02 -070043 static constexpr int64_t VSYNC_60 = 16666667;
44 static constexpr int64_t VSYNC_90 = 11111111;
45
46 RefreshRateConfigsTest();
47 ~RefreshRateConfigsTest();
48
49 void assertRatesEqual(const RefreshRate& left, const RefreshRate& right) {
50 ASSERT_EQ(left.configId, right.configId);
51 ASSERT_EQ(left.name, right.name);
52 ASSERT_EQ(left.fps, right.fps);
53 }
Dominik Laskowski22488f62019-03-28 09:53:04 -070054
55 RefreshRateConfigs mConfigs;
Alec Mouri0a1cc962019-03-14 12:33:02 -070056};
57
58RefreshRateConfigsTest::RefreshRateConfigsTest() {
59 const ::testing::TestInfo* const test_info =
60 ::testing::UnitTest::GetInstance()->current_test_info();
61 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
62}
63
64RefreshRateConfigsTest::~RefreshRateConfigsTest() {
65 const ::testing::TestInfo* const test_info =
66 ::testing::UnitTest::GetInstance()->current_test_info();
67 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
68}
69
70namespace {
71/* ------------------------------------------------------------------------
72 * Test cases
73 */
74TEST_F(RefreshRateConfigsTest, zeroDeviceConfigs_storesPowerSavingConfig) {
75 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
Dominik Laskowski22488f62019-03-28 09:53:04 -070076 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -070077
78 // We always store a configuration for screen off.
Dominik Laskowski22488f62019-03-28 09:53:04 -070079 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -070080 ASSERT_EQ(1, rates.size());
81 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
82 ASSERT_NE(rates.end(), powerSavingRate);
83 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE));
84 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::DEFAULT));
85
Ady Abraham796beb02019-04-11 15:23:07 -070086 RefreshRate expectedConfig =
87 RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID};
Alec Mouri0a1cc962019-03-14 12:33:02 -070088 assertRatesEqual(expectedConfig, *powerSavingRate->second);
89
Dominik Laskowski22488f62019-03-28 09:53:04 -070090 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
91 assertRatesEqual(expectedConfig, *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
92 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
93 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
Alec Mouri0a1cc962019-03-14 12:33:02 -070094
95 // Sanity check that getRefreshRate() does not modify the underlying configs.
Dominik Laskowski22488f62019-03-28 09:53:04 -070096 ASSERT_EQ(1, mConfigs.getRefreshRates().size());
Alec Mouri0a1cc962019-03-14 12:33:02 -070097}
98
99TEST_F(RefreshRateConfigsTest, oneDeviceConfig_storesDefaultConfig) {
100 auto display = new Hwc2::mock::Display();
101 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
102 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
103 config60.setVsyncPeriod(VSYNC_60);
104 displayConfigs.push_back(config60.build());
Dominik Laskowski22488f62019-03-28 09:53:04 -0700105 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -0700106
Dominik Laskowski22488f62019-03-28 09:53:04 -0700107 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -0700108 ASSERT_EQ(2, rates.size());
109 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
110 const auto& defaultRate = rates.find(RefreshRateType::DEFAULT);
111 ASSERT_NE(rates.end(), powerSavingRate);
112 ASSERT_NE(rates.end(), defaultRate);
113 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE));
114
Ady Abraham796beb02019-04-11 15:23:07 -0700115 RefreshRate expectedPowerSavingConfig =
116 RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID};
Alec Mouri0a1cc962019-03-14 12:33:02 -0700117 assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second);
Ady Abraham796beb02019-04-11 15:23:07 -0700118 RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60, HWC2_CONFIG_ID_60};
Alec Mouri0a1cc962019-03-14 12:33:02 -0700119 assertRatesEqual(expectedDefaultConfig, *defaultRate->second);
120
Dominik Laskowski22488f62019-03-28 09:53:04 -0700121 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700122 assertRatesEqual(expectedPowerSavingConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700123 *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
124 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
125 assertRatesEqual(expectedDefaultConfig, *mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
126 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700127
128 // Sanity check that getRefreshRate() does not modify the underlying configs.
Dominik Laskowski22488f62019-03-28 09:53:04 -0700129 ASSERT_EQ(2, mConfigs.getRefreshRates().size());
Alec Mouri0a1cc962019-03-14 12:33:02 -0700130}
131
132TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesPerformanceConfig) {
133 auto display = new Hwc2::mock::Display();
134 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
135 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
136 config60.setVsyncPeriod(VSYNC_60);
137 displayConfigs.push_back(config60.build());
138 auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
139 config90.setVsyncPeriod(VSYNC_90);
140 displayConfigs.push_back(config90.build());
Dominik Laskowski22488f62019-03-28 09:53:04 -0700141 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -0700142
Dominik Laskowski22488f62019-03-28 09:53:04 -0700143 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -0700144 ASSERT_EQ(3, rates.size());
145 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
146 const auto& defaultRate = rates.find(RefreshRateType::DEFAULT);
147 const auto& performanceRate = rates.find(RefreshRateType::PERFORMANCE);
148 ASSERT_NE(rates.end(), powerSavingRate);
149 ASSERT_NE(rates.end(), defaultRate);
150 ASSERT_NE(rates.end(), performanceRate);
151
Ady Abraham796beb02019-04-11 15:23:07 -0700152 RefreshRate expectedPowerSavingConfig =
153 RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID};
Alec Mouri0a1cc962019-03-14 12:33:02 -0700154 assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second);
Ady Abraham796beb02019-04-11 15:23:07 -0700155 RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60, HWC2_CONFIG_ID_60};
Alec Mouri0a1cc962019-03-14 12:33:02 -0700156 assertRatesEqual(expectedDefaultConfig, *defaultRate->second);
Ady Abraham796beb02019-04-11 15:23:07 -0700157 RefreshRate expectedPerformanceConfig =
158 RefreshRate{CONFIG_ID_90, "90fps", 90, HWC2_CONFIG_ID_90};
Alec Mouri0a1cc962019-03-14 12:33:02 -0700159 assertRatesEqual(expectedPerformanceConfig, *performanceRate->second);
160
Dominik Laskowski22488f62019-03-28 09:53:04 -0700161 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700162 assertRatesEqual(expectedPowerSavingConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700163 *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
164 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
165 assertRatesEqual(expectedDefaultConfig, *mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
166 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700167 assertRatesEqual(expectedPerformanceConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700168 *mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700169}
170} // namespace
171} // namespace scheduler
172} // namespace android