blob: b7e55f4822067ea29697880811661543fc165ca4 [file] [log] [blame]
Ana Krulec4593b692019-01-11 22:07:25 -08001/*
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/RefreshRateStats.h"
25#include "mock/DisplayHardware/MockDisplay.h"
Alec Mourifb571ea2019-01-24 18:42:10 -080026#include "mock/MockTimeStats.h"
Ana Krulec4593b692019-01-11 22:07:25 -080027
28using namespace std::chrono_literals;
Alec Mourifb571ea2019-01-24 18:42:10 -080029using testing::_;
Alec Mouria2957ea2019-03-16 21:05:23 -070030using testing::AtLeast;
Ana Krulec4593b692019-01-11 22:07:25 -080031
32namespace android {
33namespace scheduler {
34
35class RefreshRateStatsTest : public testing::Test {
36protected:
37 static constexpr int CONFIG_ID_90 = 0;
38 static constexpr int CONFIG_ID_60 = 1;
39 static constexpr int64_t VSYNC_90 = 11111111;
40 static constexpr int64_t VSYNC_60 = 16666667;
41
42 RefreshRateStatsTest();
43 ~RefreshRateStatsTest();
44
45 void init(std::vector<std::shared_ptr<const HWC2::Display::Config>> configs);
46
47 std::unique_ptr<RefreshRateStats> mRefreshRateStats;
Alec Mourifb571ea2019-01-24 18:42:10 -080048 std::shared_ptr<android::mock::TimeStats> mTimeStats;
Ady Abraham1902d072019-03-01 17:18:59 -080049 std::shared_ptr<RefreshRateConfigs> mRefreshRateConfigs;
Ana Krulec4593b692019-01-11 22:07:25 -080050};
51
52RefreshRateStatsTest::RefreshRateStatsTest() {
53 const ::testing::TestInfo* const test_info =
54 ::testing::UnitTest::GetInstance()->current_test_info();
55 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
56}
57
58RefreshRateStatsTest::~RefreshRateStatsTest() {
59 const ::testing::TestInfo* const test_info =
60 ::testing::UnitTest::GetInstance()->current_test_info();
61 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
62}
63
64void RefreshRateStatsTest::init(std::vector<std::shared_ptr<const HWC2::Display::Config>> configs) {
Alec Mourifb571ea2019-01-24 18:42:10 -080065 mTimeStats = std::make_shared<android::mock::TimeStats>();
Ady Abraham1902d072019-03-01 17:18:59 -080066 mRefreshRateConfigs = std::make_shared<RefreshRateConfigs>(configs);
67 mRefreshRateStats = std::make_unique<RefreshRateStats>(mRefreshRateConfigs, mTimeStats);
Ana Krulec4593b692019-01-11 22:07:25 -080068}
69
70namespace {
71/* ------------------------------------------------------------------------
72 * Test cases
73 */
74TEST_F(RefreshRateStatsTest, canCreateAndDestroyTest) {
75 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
76 init(configs);
77
78 // There is one default config, so the refresh rates should have one item.
Alec Mouria2957ea2019-03-16 21:05:23 -070079 EXPECT_EQ(1, mRefreshRateStats->getTotalTimes().size());
Ana Krulec4593b692019-01-11 22:07:25 -080080}
81
82TEST_F(RefreshRateStatsTest, oneConfigTest) {
83 auto display = new Hwc2::mock::Display();
84
85 auto config = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
86 config.setVsyncPeriod(VSYNC_90);
87 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
88 configs.push_back(config.build());
89
90 init(configs);
91
Alec Mouria2957ea2019-03-16 21:05:23 -070092 EXPECT_CALL(*mTimeStats, recordRefreshRate(0, _)).Times(AtLeast(1));
93 EXPECT_CALL(*mTimeStats, recordRefreshRate(90, _)).Times(AtLeast(1));
Alec Mourifb571ea2019-01-24 18:42:10 -080094
Ana Krulec4593b692019-01-11 22:07:25 -080095 std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -070096 EXPECT_EQ(2, times.size());
97 EXPECT_NE(0u, times.count("ScreenOff"));
98 EXPECT_EQ(1u, times.count("90fps"));
99 EXPECT_EQ(0, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800100 // Setting up tests on mobile harness can be flaky with time passing, so testing for
101 // exact time changes can result in flaxy numbers. To avoid that remember old
102 // numbers to make sure the correct values are increasing in the next test.
103 int screenOff = times["ScreenOff"];
104 int ninety = times["90fps"];
105
106 // Screen is off by default.
107 std::this_thread::sleep_for(std::chrono::milliseconds(2));
108 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700109 EXPECT_LT(screenOff, times["ScreenOff"]);
110 EXPECT_EQ(0, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800111
112 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
Alec Mouria2957ea2019-03-16 21:05:23 -0700113 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800114 std::this_thread::sleep_for(std::chrono::milliseconds(2));
115 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700116 EXPECT_EQ(screenOff, times["ScreenOff"]);
117 EXPECT_LT(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800118
119 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
Alec Mouria2957ea2019-03-16 21:05:23 -0700120 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800121 std::this_thread::sleep_for(std::chrono::milliseconds(2));
122 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700123 EXPECT_LT(screenOff, times["ScreenOff"]);
124 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800125
126 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700127 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800128 std::this_thread::sleep_for(std::chrono::milliseconds(2));
129 times = mRefreshRateStats->getTotalTimes();
130 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
131 // does not update refresh rates that come from the config.
Alec Mouria2957ea2019-03-16 21:05:23 -0700132 EXPECT_LT(screenOff, times["ScreenOff"]);
133 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800134}
135
136TEST_F(RefreshRateStatsTest, twoConfigsTest) {
137 auto display = new Hwc2::mock::Display();
138
139 auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
140 config90.setVsyncPeriod(VSYNC_90);
141 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
142 configs.push_back(config90.build());
143
144 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
145 config60.setVsyncPeriod(VSYNC_60);
146 configs.push_back(config60.build());
147
148 init(configs);
149
Alec Mouria2957ea2019-03-16 21:05:23 -0700150 EXPECT_CALL(*mTimeStats, recordRefreshRate(0, _)).Times(AtLeast(1));
151 EXPECT_CALL(*mTimeStats, recordRefreshRate(60, _)).Times(AtLeast(1));
152 EXPECT_CALL(*mTimeStats, recordRefreshRate(90, _)).Times(AtLeast(1));
Alec Mourifb571ea2019-01-24 18:42:10 -0800153
Ana Krulec4593b692019-01-11 22:07:25 -0800154 std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700155 EXPECT_EQ(3, times.size());
156 EXPECT_NE(0u, times.count("ScreenOff"));
157 EXPECT_EQ(1u, times.count("60fps"));
158 EXPECT_EQ(0, times["60fps"]);
159 EXPECT_EQ(1u, times.count("90fps"));
160 EXPECT_EQ(0, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800161 // Setting up tests on mobile harness can be flaky with time passing, so testing for
162 // exact time changes can result in flaxy numbers. To avoid that remember old
163 // numbers to make sure the correct values are increasing in the next test.
164 int screenOff = times["ScreenOff"];
165 int sixty = times["60fps"];
166 int ninety = times["90fps"];
167
168 // Screen is off by default.
169 std::this_thread::sleep_for(std::chrono::milliseconds(2));
170 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700171 EXPECT_LT(screenOff, times["ScreenOff"]);
172 EXPECT_EQ(sixty, times["60fps"]);
173 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800174
175 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
Alec Mouria2957ea2019-03-16 21:05:23 -0700176 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800177 std::this_thread::sleep_for(std::chrono::milliseconds(2));
178 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700179 EXPECT_EQ(screenOff, times["ScreenOff"]);
180 EXPECT_EQ(sixty, times["60fps"]);
181 EXPECT_LT(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800182
183 // When power mode is normal, time for configs updates.
184 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700185 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800186 std::this_thread::sleep_for(std::chrono::milliseconds(2));
187 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700188 EXPECT_EQ(screenOff, times["ScreenOff"]);
189 EXPECT_EQ(ninety, times["90fps"]);
190 EXPECT_LT(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800191
192 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700193 sixty = mRefreshRateStats->getTotalTimes()["60fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800194 std::this_thread::sleep_for(std::chrono::milliseconds(2));
195 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700196 EXPECT_EQ(screenOff, times["ScreenOff"]);
197 EXPECT_LT(ninety, times["90fps"]);
198 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800199
200 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700201 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800202 std::this_thread::sleep_for(std::chrono::milliseconds(2));
203 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700204 EXPECT_EQ(screenOff, times["ScreenOff"]);
205 EXPECT_EQ(ninety, times["90fps"]);
206 EXPECT_LT(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800207
208 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
209 // does not update refresh rates that come from the config.
210 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
211 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700212 sixty = mRefreshRateStats->getTotalTimes()["60fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800213 std::this_thread::sleep_for(std::chrono::milliseconds(2));
214 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700215 EXPECT_LT(screenOff, times["ScreenOff"]);
216 EXPECT_EQ(ninety, times["90fps"]);
217 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800218
219 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700220 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800221 std::this_thread::sleep_for(std::chrono::milliseconds(2));
222 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700223 EXPECT_LT(screenOff, times["ScreenOff"]);
224 EXPECT_EQ(ninety, times["90fps"]);
225 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800226}
227} // namespace
228} // namespace scheduler
229} // namespace android