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