blob: 8b37c22356d8b269b5168918298693331bb4f63d [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;
40 static constexpr int CONFIG_ID_90 = 1;
41 static constexpr int64_t VSYNC_60 = 16666667;
42 static constexpr int64_t VSYNC_90 = 11111111;
43
44 RefreshRateConfigsTest();
45 ~RefreshRateConfigsTest();
46
47 void assertRatesEqual(const RefreshRate& left, const RefreshRate& right) {
48 ASSERT_EQ(left.configId, right.configId);
49 ASSERT_EQ(left.name, right.name);
50 ASSERT_EQ(left.fps, right.fps);
51 }
Dominik Laskowski22488f62019-03-28 09:53:04 -070052
53 RefreshRateConfigs mConfigs;
Alec Mouri0a1cc962019-03-14 12:33:02 -070054};
55
56RefreshRateConfigsTest::RefreshRateConfigsTest() {
57 const ::testing::TestInfo* const test_info =
58 ::testing::UnitTest::GetInstance()->current_test_info();
59 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
60}
61
62RefreshRateConfigsTest::~RefreshRateConfigsTest() {
63 const ::testing::TestInfo* const test_info =
64 ::testing::UnitTest::GetInstance()->current_test_info();
65 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
66}
67
68namespace {
69/* ------------------------------------------------------------------------
70 * Test cases
71 */
72TEST_F(RefreshRateConfigsTest, zeroDeviceConfigs_storesPowerSavingConfig) {
73 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
Dominik Laskowski22488f62019-03-28 09:53:04 -070074 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -070075
76 // We always store a configuration for screen off.
Dominik Laskowski22488f62019-03-28 09:53:04 -070077 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -070078 ASSERT_EQ(1, rates.size());
79 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
80 ASSERT_NE(rates.end(), powerSavingRate);
81 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE));
82 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::DEFAULT));
83
84 RefreshRate expectedConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0};
85 assertRatesEqual(expectedConfig, *powerSavingRate->second);
86
Dominik Laskowski22488f62019-03-28 09:53:04 -070087 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
88 assertRatesEqual(expectedConfig, *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
89 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
90 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
Alec Mouri0a1cc962019-03-14 12:33:02 -070091
92 // Sanity check that getRefreshRate() does not modify the underlying configs.
Dominik Laskowski22488f62019-03-28 09:53:04 -070093 ASSERT_EQ(1, mConfigs.getRefreshRates().size());
Alec Mouri0a1cc962019-03-14 12:33:02 -070094}
95
96TEST_F(RefreshRateConfigsTest, oneDeviceConfig_storesDefaultConfig) {
97 auto display = new Hwc2::mock::Display();
98 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
99 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
100 config60.setVsyncPeriod(VSYNC_60);
101 displayConfigs.push_back(config60.build());
Dominik Laskowski22488f62019-03-28 09:53:04 -0700102 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -0700103
Dominik Laskowski22488f62019-03-28 09:53:04 -0700104 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -0700105 ASSERT_EQ(2, rates.size());
106 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
107 const auto& defaultRate = rates.find(RefreshRateType::DEFAULT);
108 ASSERT_NE(rates.end(), powerSavingRate);
109 ASSERT_NE(rates.end(), defaultRate);
110 ASSERT_EQ(rates.end(), rates.find(RefreshRateType::PERFORMANCE));
111
112 RefreshRate expectedPowerSavingConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0};
113 assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second);
114 RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60};
115 assertRatesEqual(expectedDefaultConfig, *defaultRate->second);
116
Dominik Laskowski22488f62019-03-28 09:53:04 -0700117 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700118 assertRatesEqual(expectedPowerSavingConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700119 *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
120 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
121 assertRatesEqual(expectedDefaultConfig, *mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
122 ASSERT_FALSE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700123
124 // Sanity check that getRefreshRate() does not modify the underlying configs.
Dominik Laskowski22488f62019-03-28 09:53:04 -0700125 ASSERT_EQ(2, mConfigs.getRefreshRates().size());
Alec Mouri0a1cc962019-03-14 12:33:02 -0700126}
127
128TEST_F(RefreshRateConfigsTest, twoDeviceConfigs_storesPerformanceConfig) {
129 auto display = new Hwc2::mock::Display();
130 std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs;
131 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
132 config60.setVsyncPeriod(VSYNC_60);
133 displayConfigs.push_back(config60.build());
134 auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
135 config90.setVsyncPeriod(VSYNC_90);
136 displayConfigs.push_back(config90.build());
Dominik Laskowski22488f62019-03-28 09:53:04 -0700137 mConfigs.populate(displayConfigs);
Alec Mouri0a1cc962019-03-14 12:33:02 -0700138
Dominik Laskowski22488f62019-03-28 09:53:04 -0700139 const auto& rates = mConfigs.getRefreshRates();
Alec Mouri0a1cc962019-03-14 12:33:02 -0700140 ASSERT_EQ(3, rates.size());
141 const auto& powerSavingRate = rates.find(RefreshRateType::POWER_SAVING);
142 const auto& defaultRate = rates.find(RefreshRateType::DEFAULT);
143 const auto& performanceRate = rates.find(RefreshRateType::PERFORMANCE);
144 ASSERT_NE(rates.end(), powerSavingRate);
145 ASSERT_NE(rates.end(), defaultRate);
146 ASSERT_NE(rates.end(), performanceRate);
147
148 RefreshRate expectedPowerSavingConfig = RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0};
149 assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second);
150 RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60};
151 assertRatesEqual(expectedDefaultConfig, *defaultRate->second);
152 RefreshRate expectedPerformanceConfig = RefreshRate{CONFIG_ID_90, "90fps", 90};
153 assertRatesEqual(expectedPerformanceConfig, *performanceRate->second);
154
Dominik Laskowski22488f62019-03-28 09:53:04 -0700155 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700156 assertRatesEqual(expectedPowerSavingConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700157 *mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING));
158 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
159 assertRatesEqual(expectedDefaultConfig, *mConfigs.getRefreshRate(RefreshRateType::DEFAULT));
160 ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700161 assertRatesEqual(expectedPerformanceConfig,
Dominik Laskowski22488f62019-03-28 09:53:04 -0700162 *mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE));
Alec Mouri0a1cc962019-03-14 12:33:02 -0700163}
164} // namespace
165} // namespace scheduler
166} // namespace android