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; |
| 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 52 | |
| 53 | RefreshRateConfigs mConfigs; |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | RefreshRateConfigsTest::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 | |
| 62 | RefreshRateConfigsTest::~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 | |
| 68 | namespace { |
| 69 | /* ------------------------------------------------------------------------ |
| 70 | * Test cases |
| 71 | */ |
| 72 | TEST_F(RefreshRateConfigsTest, zeroDeviceConfigs_storesPowerSavingConfig) { |
| 73 | std::vector<std::shared_ptr<const HWC2::Display::Config>> displayConfigs; |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 74 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 75 | |
| 76 | // We always store a configuration for screen off. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 77 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 78 | 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 87 | 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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 91 | |
| 92 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 93 | ASSERT_EQ(1, mConfigs.getRefreshRates().size()); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | TEST_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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 102 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 103 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 104 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 105 | 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 117 | ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 118 | assertRatesEqual(expectedPowerSavingConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 119 | *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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 123 | |
| 124 | // Sanity check that getRefreshRate() does not modify the underlying configs. |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 125 | ASSERT_EQ(2, mConfigs.getRefreshRates().size()); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 137 | mConfigs.populate(displayConfigs); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 138 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 139 | const auto& rates = mConfigs.getRefreshRates(); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 140 | 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 Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 155 | ASSERT_TRUE(mConfigs.getRefreshRate(RefreshRateType::POWER_SAVING)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 156 | assertRatesEqual(expectedPowerSavingConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 157 | *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 Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 161 | assertRatesEqual(expectedPerformanceConfig, |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame^] | 162 | *mConfigs.getRefreshRate(RefreshRateType::PERFORMANCE)); |
Alec Mouri | 0a1cc96 | 2019-03-14 12:33:02 -0700 | [diff] [blame] | 163 | } |
| 164 | } // namespace |
| 165 | } // namespace scheduler |
| 166 | } // namespace android |