Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -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 | */ |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 16 | |
| 17 | // #define LOG_NDEBUG 0 |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 18 | #include "RefreshRateConfigs.h" |
| 19 | |
| 20 | namespace android::scheduler { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 21 | |
| 22 | using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 23 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 24 | |
| 25 | // Returns the refresh rate map. This map won't be modified at runtime, so it's safe to access |
| 26 | // from multiple threads. This can only be called if refreshRateSwitching() returns true. |
| 27 | // TODO(b/122916473): Get this information from configs prepared by vendors, instead of |
| 28 | // baking them in. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 29 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(float contentFramerate) const { |
| 30 | std::lock_guard lock(mLock); |
| 31 | // Find the appropriate refresh rate with minimal error |
| 32 | auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(), |
| 33 | [contentFramerate](const auto& lhs, const auto& rhs) -> bool { |
| 34 | return std::abs(lhs->fps - contentFramerate) < |
| 35 | std::abs(rhs->fps - contentFramerate); |
| 36 | }); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 37 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 38 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 39 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 40 | // align well with both |
| 41 | const RefreshRate* bestSoFar = *iter; |
| 42 | constexpr float MARGIN = 0.05f; |
| 43 | float ratio = (*iter)->fps / contentFramerate; |
| 44 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
| 45 | while (iter != mAvailableRefreshRates.cend()) { |
| 46 | ratio = (*iter)->fps / contentFramerate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 47 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 48 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 49 | bestSoFar = *iter; |
| 50 | break; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 51 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 52 | ++iter; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 56 | return *bestSoFar; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 59 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 60 | return mRefreshRates; |
| 61 | } |
| 62 | |
| 63 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 64 | std::lock_guard lock(mLock); |
| 65 | if (!mRefreshRateSwitching) { |
| 66 | return *mCurrentRefreshRate; |
| 67 | } else { |
| 68 | return *mAvailableRefreshRates.front(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 73 | std::lock_guard lock(mLock); |
| 74 | if (!mRefreshRateSwitching) { |
| 75 | return *mCurrentRefreshRate; |
| 76 | } else { |
| 77 | return *mAvailableRefreshRates.back(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 82 | std::lock_guard lock(mLock); |
| 83 | return *mCurrentRefreshRate; |
| 84 | } |
| 85 | |
| 86 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 87 | std::lock_guard lock(mLock); |
| 88 | mCurrentRefreshRate = &mRefreshRates.at(configId); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | RefreshRateConfigs::RefreshRateConfigs(bool refreshRateSwitching, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 92 | const std::vector<InputConfig>& configs, |
| 93 | HwcConfigIndexType currentHwcConfig) |
| 94 | : mRefreshRateSwitching(refreshRateSwitching) { |
| 95 | init(configs, currentHwcConfig); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | RefreshRateConfigs::RefreshRateConfigs( |
| 99 | bool refreshRateSwitching, |
| 100 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 101 | HwcConfigIndexType currentConfigId) |
| 102 | : mRefreshRateSwitching(refreshRateSwitching) { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 103 | std::vector<InputConfig> inputConfigs; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 104 | for (auto configId = HwcConfigIndexType(0); configId < HwcConfigIndexType(configs.size()); |
| 105 | ++configId) { |
| 106 | auto configGroup = HwcConfigGroupType(configs[configId.value()]->getConfigGroup()); |
| 107 | inputConfigs.push_back( |
| 108 | {configId, configGroup, configs[configId.value()]->getVsyncPeriod()}); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 109 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 110 | init(inputConfigs, currentConfigId); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 113 | void RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate, |
| 114 | float maxRefreshRate) { |
| 115 | std::lock_guard lock(mLock); |
| 116 | mCurrentGroupId = mRefreshRates.at(defaultConfigId).configGroup; |
| 117 | mMinRefreshRateFps = minRefreshRate; |
| 118 | mMaxRefreshRateFps = maxRefreshRate; |
| 119 | constructAvailableRefreshRates(); |
| 120 | } |
| 121 | |
| 122 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 123 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 124 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 125 | outRefreshRates->clear(); |
| 126 | outRefreshRates->reserve(mRefreshRates.size()); |
| 127 | for (const auto& [type, refreshRate] : mRefreshRates) { |
| 128 | if (shouldAddRefreshRate(refreshRate)) { |
| 129 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
| 130 | refreshRate.configId.value()); |
| 131 | outRefreshRates->push_back(&refreshRate); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 136 | [](const auto refreshRate1, const auto refreshRate2) { |
| 137 | return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod; |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 142 | // Filter configs based on current policy and sort based on vsync period |
| 143 | ALOGV("constructRefreshRateMap: group %d min %.2f max %.2f", mCurrentGroupId.value(), |
| 144 | mMinRefreshRateFps, mMaxRefreshRateFps); |
| 145 | getSortedRefreshRateList( |
| 146 | [this](const RefreshRate& refreshRate) REQUIRES(mLock) { |
| 147 | return refreshRate.configGroup == mCurrentGroupId && |
| 148 | refreshRate.fps >= mMinRefreshRateFps && |
| 149 | refreshRate.fps <= mMaxRefreshRateFps; |
| 150 | }, |
| 151 | &mAvailableRefreshRates); |
| 152 | } |
| 153 | |
| 154 | // NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor |
| 155 | void RefreshRateConfigs::init(const std::vector<InputConfig>& configs, |
| 156 | HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 157 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 158 | LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size()); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 159 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 160 | auto buildRefreshRate = [&](InputConfig config) -> RefreshRate { |
| 161 | const float fps = 1e9f / config.vsyncPeriod; |
| 162 | return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup, |
| 163 | base::StringPrintf("%2.ffps", fps), fps); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 166 | for (const auto& config : configs) { |
| 167 | mRefreshRates.emplace(config.configId, buildRefreshRate(config)); |
| 168 | if (config.configId == currentHwcConfig) { |
| 169 | mCurrentRefreshRate = &mRefreshRates.at(config.configId); |
| 170 | mCurrentGroupId = config.configGroup; |
| 171 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 174 | std::vector<const RefreshRate*> sortedConfigs; |
| 175 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
| 176 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 177 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 178 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame^] | 181 | } // namespace android::scheduler |