Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 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/RefreshRateStats.h" |
| 25 | #include "mock/DisplayHardware/MockDisplay.h" |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 26 | #include "mock/MockTimeStats.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace std::chrono_literals; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 29 | using testing::_; |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 30 | using testing::AtLeast; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace scheduler { |
| 34 | |
| 35 | class RefreshRateStatsTest : public testing::Test { |
| 36 | protected: |
| 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 Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 48 | std::shared_ptr<android::mock::TimeStats> mTimeStats; |
Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 49 | std::shared_ptr<RefreshRateConfigs> mRefreshRateConfigs; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | RefreshRateStatsTest::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 | |
| 58 | RefreshRateStatsTest::~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 | |
| 64 | void RefreshRateStatsTest::init(std::vector<std::shared_ptr<const HWC2::Display::Config>> configs) { |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 65 | mTimeStats = std::make_shared<android::mock::TimeStats>(); |
Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 66 | mRefreshRateConfigs = std::make_shared<RefreshRateConfigs>(configs); |
| 67 | mRefreshRateStats = std::make_unique<RefreshRateStats>(mRefreshRateConfigs, mTimeStats); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | namespace { |
| 71 | /* ------------------------------------------------------------------------ |
| 72 | * Test cases |
| 73 | */ |
| 74 | TEST_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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 79 | EXPECT_EQ(1, mRefreshRateStats->getTotalTimes().size()); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST_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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 92 | EXPECT_CALL(*mTimeStats, recordRefreshRate(0, _)).Times(AtLeast(1)); |
| 93 | EXPECT_CALL(*mTimeStats, recordRefreshRate(90, _)).Times(AtLeast(1)); |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 94 | |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 95 | std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 96 | 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 Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 100 | // 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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 109 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 110 | EXPECT_EQ(0, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 111 | |
Alec Mouri | 62c8bd1 | 2019-03-22 15:55:23 -0700 | [diff] [blame] | 112 | mRefreshRateStats->setConfigMode(CONFIG_ID_90); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 113 | mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 114 | screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 115 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 116 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 117 | EXPECT_EQ(screenOff, times["ScreenOff"]); |
| 118 | EXPECT_LT(ninety, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 119 | |
| 120 | mRefreshRateStats->setPowerMode(HWC_POWER_MODE_DOZE); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 121 | ninety = mRefreshRateStats->getTotalTimes()["90fps"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 122 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 123 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 124 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 125 | EXPECT_EQ(ninety, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 126 | |
| 127 | mRefreshRateStats->setConfigMode(CONFIG_ID_90); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 128 | screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 129 | 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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 133 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 134 | EXPECT_EQ(ninety, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 151 | 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 Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 154 | |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 155 | std::unordered_map<std::string, int64_t> times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 156 | 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 Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 162 | // 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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 172 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 173 | EXPECT_EQ(sixty, times["60fps"]); |
| 174 | EXPECT_EQ(ninety, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 175 | |
Alec Mouri | 62c8bd1 | 2019-03-22 15:55:23 -0700 | [diff] [blame] | 176 | mRefreshRateStats->setConfigMode(CONFIG_ID_90); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 177 | mRefreshRateStats->setPowerMode(HWC_POWER_MODE_NORMAL); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 178 | screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 179 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 180 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 181 | EXPECT_EQ(screenOff, times["ScreenOff"]); |
| 182 | EXPECT_EQ(sixty, times["60fps"]); |
| 183 | EXPECT_LT(ninety, times["90fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 184 | |
| 185 | // When power mode is normal, time for configs updates. |
| 186 | mRefreshRateStats->setConfigMode(CONFIG_ID_60); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 187 | ninety = mRefreshRateStats->getTotalTimes()["90fps"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 188 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 189 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 190 | EXPECT_EQ(screenOff, times["ScreenOff"]); |
| 191 | EXPECT_EQ(ninety, times["90fps"]); |
| 192 | EXPECT_LT(sixty, times["60fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 193 | |
| 194 | mRefreshRateStats->setConfigMode(CONFIG_ID_90); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 195 | sixty = mRefreshRateStats->getTotalTimes()["60fps"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 196 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 197 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 198 | EXPECT_EQ(screenOff, times["ScreenOff"]); |
| 199 | EXPECT_LT(ninety, times["90fps"]); |
| 200 | EXPECT_EQ(sixty, times["60fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 201 | |
| 202 | mRefreshRateStats->setConfigMode(CONFIG_ID_60); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 203 | ninety = mRefreshRateStats->getTotalTimes()["90fps"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 204 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 205 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 206 | EXPECT_EQ(screenOff, times["ScreenOff"]); |
| 207 | EXPECT_EQ(ninety, times["90fps"]); |
| 208 | EXPECT_LT(sixty, times["60fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 209 | |
| 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 Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 214 | sixty = mRefreshRateStats->getTotalTimes()["60fps"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 215 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 216 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 217 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 218 | EXPECT_EQ(ninety, times["90fps"]); |
| 219 | EXPECT_EQ(sixty, times["60fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 220 | |
| 221 | mRefreshRateStats->setConfigMode(CONFIG_ID_60); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 222 | screenOff = mRefreshRateStats->getTotalTimes()["ScreenOff"]; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 223 | std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
| 224 | times = mRefreshRateStats->getTotalTimes(); |
Alec Mouri | a2957ea | 2019-03-16 21:05:23 -0700 | [diff] [blame] | 225 | EXPECT_LT(screenOff, times["ScreenOff"]); |
| 226 | EXPECT_EQ(ninety, times["90fps"]); |
| 227 | EXPECT_EQ(sixty, times["60fps"]); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 228 | } |
| 229 | } // namespace |
| 230 | } // namespace scheduler |
| 231 | } // namespace android |