Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [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 "DisplayHardware/HWC2.h" |
| 25 | #include "Scheduler/RefreshRateConfigs.h" |
| 26 | #include "mock/DisplayHardware/MockDisplay.h" |
| 27 | |
| 28 | using namespace std::chrono_literals; |
| 29 | using testing::_; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace scheduler { |
| 33 | |
| 34 | using RefreshRateType = RefreshRateConfigs::RefreshRateType; |
| 35 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
| 36 | |
| 37 | class RefreshRateConfigsTest : public testing::Test { |
| 38 | protected: |
| 39 | static constexpr int CONFIG_ID_60 = 0; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 40 | static constexpr hwc2_config_t HWC2_CONFIG_ID_60 = 0; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 41 | static constexpr int CONFIG_ID_90 = 1; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 42 | static constexpr hwc2_config_t HWC2_CONFIG_ID_90 = 1; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 43 | 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 54 | |
| 55 | RefreshRateConfigs mConfigs; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | RefreshRateConfigsTest::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 | |
| 64 | RefreshRateConfigsTest::~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 | |
| 70 | namespace { |
| 71 | /* ------------------------------------------------------------------------ |
| 72 | * Test cases |
| 73 | */ |
| 74 | TEST_F(RefreshRateConfigsTest, zeroDeviceConfigs_storesPowerSavingConfig) { |
| 75 | std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs; |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 76 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 77 | |
| 78 | // We always store a configuration for screen off. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 79 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 80 | 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 Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 86 | RefreshRate expectedConfig = |
| 87 | RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 88 | assertRatesEqual(expectedConfig, *powerSavingRate->second); |
| 89 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 90 | 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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 94 | |
| 95 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 96 | ASSERT_EQ(1, mConfigs.getRefreshRates().size()); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 105 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 106 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 107 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 108 | 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 Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 115 | RefreshRate expectedPowerSavingConfig = |
| 116 | RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 117 | assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second); |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 118 | RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60, HWC2_CONFIG_ID_60}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 119 | assertRatesEqual(expectedDefaultConfig, *defaultRate->second); |
| 120 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 121 | ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 122 | assertRatesEqual(expectedPowerSavingConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 123 | *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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 127 | |
| 128 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 129 | ASSERT_EQ(2, mConfigs.getRefreshRates().size()); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 141 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 142 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 143 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 144 | 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 Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 152 | RefreshRate expectedPowerSavingConfig = |
| 153 | RefreshRate{SCREEN_OFF_CONFIG_ID, "ScreenOff", 0, HWC2_SCREEN_OFF_CONFIG_ID}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 154 | assertRatesEqual(expectedPowerSavingConfig, *powerSavingRate->second); |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 155 | RefreshRate expectedDefaultConfig = RefreshRate{CONFIG_ID_60, "60fps", 60, HWC2_CONFIG_ID_60}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 156 | assertRatesEqual(expectedDefaultConfig, *defaultRate->second); |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame^] | 157 | RefreshRate expectedPerformanceConfig = |
| 158 | RefreshRate{CONFIG_ID_90, "90fps", 90, HWC2_CONFIG_ID_90}; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 159 | assertRatesEqual(expectedPerformanceConfig, *performanceRate->second); |
| 160 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 161 | ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 162 | assertRatesEqual(expectedPowerSavingConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 163 | *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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 167 | assertRatesEqual(expectedPerformanceConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 168 | *mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 169 | } |
| 170 | } // namespace |
| 171 | } // namespace scheduler |
| 172 | } // namespace android |