blob: 10f5af8cd26fe9da6c614dd63d190e5f95c7bfca [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
Alec Mouri62c8bd12019-03-22 15:55:23 -0700112 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Ana Krulec4593b692019-01-11 22:07:25 -0800113 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
Alec Mouria2957ea2019-03-16 21:05:23 -0700114 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800115 std::this_thread::sleep_for(std::chrono::milliseconds(2));
116 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700117 EXPECT_EQ(screenOff, times["ScreenOff"]);
118 EXPECT_LT(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800119
120 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
Alec Mouria2957ea2019-03-16 21:05:23 -0700121 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800122 std::this_thread::sleep_for(std::chrono::milliseconds(2));
123 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700124 EXPECT_LT(screenOff, times["ScreenOff"]);
125 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800126
127 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700128 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800129 std::this_thread::sleep_for(std::chrono::milliseconds(2));
130 times = mRefreshRateStats->getTotalTimes();
131 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
132 // does not update refresh rates that come from the config.
Alec Mouria2957ea2019-03-16 21:05:23 -0700133 EXPECT_LT(screenOff, times["ScreenOff"]);
134 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800135}
136
137TEST_F(RefreshRateStatsTest, twoConfigsTest) {
138 auto display = new Hwc2::mock::Display();
139
140 auto config90 = HWC2::Display::Config::Builder(*display, CONFIG_ID_90);
141 config90.setVsyncPeriod(VSYNC_90);
142 std::vector<std::shared_ptr<const HWC2::Display::Config>> configs;
143 configs.push_back(config90.build());
144
145 auto config60 = HWC2::Display::Config::Builder(*display, CONFIG_ID_60);
146 config60.setVsyncPeriod(VSYNC_60);
147 configs.push_back(config60.build());
148
149 init(configs);
150
Alec Mouria2957ea2019-03-16 21:05:23 -0700151 EXPECT_CALL(*mTimeStats, recordRefreshRate(0, _)).Times(AtLeast(1));
152 EXPECT_CALL(*mTimeStats, recordRefreshRate(60, _)).Times(AtLeast(1));
153 EXPECT_CALL(*mTimeStats, recordRefreshRate(90, _)).Times(AtLeast(1));
Alec Mourifb571ea2019-01-24 18:42:10 -0800154
Ana Krulec4593b692019-01-11 22:07:25 -0800155 std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700156 EXPECT_EQ(3, times.size());
157 EXPECT_NE(0u, times.count("ScreenOff"));
158 EXPECT_EQ(1u, times.count("60fps"));
159 EXPECT_EQ(0, times["60fps"]);
160 EXPECT_EQ(1u, times.count("90fps"));
161 EXPECT_EQ(0, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800162 // Setting up tests on mobile harness can be flaky with time passing, so testing for
163 // exact time changes can result in flaxy numbers. To avoid that remember old
164 // numbers to make sure the correct values are increasing in the next test.
165 int screenOff = times["ScreenOff"];
166 int sixty = times["60fps"];
167 int ninety = times["90fps"];
168
169 // Screen is off by default.
170 std::this_thread::sleep_for(std::chrono::milliseconds(2));
171 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700172 EXPECT_LT(screenOff, times["ScreenOff"]);
173 EXPECT_EQ(sixty, times["60fps"]);
174 EXPECT_EQ(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800175
Alec Mouri62c8bd12019-03-22 15:55:23 -0700176 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Ana Krulec4593b692019-01-11 22:07:25 -0800177 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL);
Alec Mouria2957ea2019-03-16 21:05:23 -0700178 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800179 std::this_thread::sleep_for(std::chrono::milliseconds(2));
180 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700181 EXPECT_EQ(screenOff, times["ScreenOff"]);
182 EXPECT_EQ(sixty, times["60fps"]);
183 EXPECT_LT(ninety, times["90fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800184
185 // When power mode is normal, time for configs updates.
186 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700187 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800188 std::this_thread::sleep_for(std::chrono::milliseconds(2));
189 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700190 EXPECT_EQ(screenOff, times["ScreenOff"]);
191 EXPECT_EQ(ninety, times["90fps"]);
192 EXPECT_LT(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800193
194 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700195 sixty = mRefreshRateStats->getTotalTimes()["60fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800196 std::this_thread::sleep_for(std::chrono::milliseconds(2));
197 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700198 EXPECT_EQ(screenOff, times["ScreenOff"]);
199 EXPECT_LT(ninety, times["90fps"]);
200 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800201
202 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700203 ninety = mRefreshRateStats->getTotalTimes()["90fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800204 std::this_thread::sleep_for(std::chrono::milliseconds(2));
205 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700206 EXPECT_EQ(screenOff, times["ScreenOff"]);
207 EXPECT_EQ(ninety, times["90fps"]);
208 EXPECT_LT(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800209
210 // Because the power mode is not HWC_POWER_MODE_NORMAL, switching the config
211 // does not update refresh rates that come from the config.
212 mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE);
213 mRefreshRateStats->setConfigMode(CONFIG_ID_90);
Alec Mouria2957ea2019-03-16 21:05:23 -0700214 sixty = mRefreshRateStats->getTotalTimes()["60fps"];
Ana Krulec4593b692019-01-11 22:07:25 -0800215 std::this_thread::sleep_for(std::chrono::milliseconds(2));
216 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700217 EXPECT_LT(screenOff, times["ScreenOff"]);
218 EXPECT_EQ(ninety, times["90fps"]);
219 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800220
221 mRefreshRateStats->setConfigMode(CONFIG_ID_60);
Alec Mouria2957ea2019-03-16 21:05:23 -0700222 screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"];
Ana Krulec4593b692019-01-11 22:07:25 -0800223 std::this_thread::sleep_for(std::chrono::milliseconds(2));
224 times = mRefreshRateStats->getTotalTimes();
Alec Mouria2957ea2019-03-16 21:05:23 -0700225 EXPECT_LT(screenOff, times["ScreenOff"]);
226 EXPECT_EQ(ninety, times["90fps"]);
227 EXPECT_EQ(sixty, times["60fps"]);
Ana Krulec4593b692019-01-11 22:07:25 -0800228}
229} // namespace
230} // namespace scheduler
231} // namespace android