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 | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 20 | #include "RefreshRateConfigs.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
| 22 | #include <utils/Trace.h> |
| 23 | #include <chrono> |
| 24 | #include <cmath> |
| 25 | |
| 26 | using namespace std::chrono_literals; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android::scheduler { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 29 | |
| 30 | using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 31 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 32 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 33 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContent( |
| 34 | const std::vector<LayerRequirement>& layers) const { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 35 | std::lock_guard lock(mLock); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 36 | int contentFramerate = 0; |
| 37 | int explicitContentFramerate = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 38 | for (const auto& layer : layers) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 39 | const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 40 | if (layer.vote == LayerVoteType::ExplicitDefault || |
| 41 | layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 42 | if (desiredRefreshRateRound > explicitContentFramerate) { |
| 43 | explicitContentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 44 | } |
| 45 | } else { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 46 | if (desiredRefreshRateRound > contentFramerate) { |
| 47 | contentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 52 | if (explicitContentFramerate != 0) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 53 | contentFramerate = explicitContentFramerate; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 54 | } else if (contentFramerate == 0) { |
| 55 | contentFramerate = round<int>(mMaxSupportedRefreshRate->fps); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 56 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 57 | ATRACE_INT("ContentFPS", contentFramerate); |
| 58 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 59 | // Find the appropriate refresh rate with minimal error |
| 60 | auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(), |
| 61 | [contentFramerate](const auto& lhs, const auto& rhs) -> bool { |
| 62 | return std::abs(lhs->fps - contentFramerate) < |
| 63 | std::abs(rhs->fps - contentFramerate); |
| 64 | }); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 65 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 66 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 67 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 68 | // align well with both |
| 69 | const RefreshRate* bestSoFar = *iter; |
| 70 | constexpr float MARGIN = 0.05f; |
| 71 | float ratio = (*iter)->fps / contentFramerate; |
| 72 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
| 73 | while (iter != mAvailableRefreshRates.cend()) { |
| 74 | ratio = (*iter)->fps / contentFramerate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 75 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 76 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 77 | bestSoFar = *iter; |
| 78 | break; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 79 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 80 | ++iter; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 84 | return *bestSoFar; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2( |
| 88 | const std::vector<LayerRequirement>& layers) const { |
| 89 | constexpr nsecs_t MARGIN = std::chrono::nanoseconds(800us).count(); |
| 90 | ATRACE_CALL(); |
| 91 | ALOGV("getRefreshRateForContent %zu layers", layers.size()); |
| 92 | |
| 93 | std::lock_guard lock(mLock); |
| 94 | |
| 95 | int noVoteLayers = 0; |
| 96 | int minVoteLayers = 0; |
| 97 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 98 | int explicitDefaultVoteLayers = 0; |
| 99 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 100 | for (const auto& layer : layers) { |
| 101 | if (layer.vote == LayerVoteType::NoVote) |
| 102 | noVoteLayers++; |
| 103 | else if (layer.vote == LayerVoteType::Min) |
| 104 | minVoteLayers++; |
| 105 | else if (layer.vote == LayerVoteType::Max) |
| 106 | maxVoteLayers++; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 107 | else if (layer.vote == LayerVoteType::ExplicitDefault) |
| 108 | explicitDefaultVoteLayers++; |
| 109 | else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) |
| 110 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Only if all layers want Min we should return Min |
| 114 | if (noVoteLayers + minVoteLayers == layers.size()) { |
| 115 | return *mAvailableRefreshRates.front(); |
| 116 | } |
| 117 | |
| 118 | // If we have some Max layers and no Explicit we should return Max |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 119 | if (maxVoteLayers > 0 && explicitDefaultVoteLayers + explicitExactOrMultipleVoteLayers == 0) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 120 | return *mAvailableRefreshRates.back(); |
| 121 | } |
| 122 | |
| 123 | // Find the best refresh rate based on score |
| 124 | std::vector<std::pair<const RefreshRate*, float>> scores; |
| 125 | scores.reserve(mAvailableRefreshRates.size()); |
| 126 | |
| 127 | for (const auto refreshRate : mAvailableRefreshRates) { |
| 128 | scores.emplace_back(refreshRate, 0.0f); |
| 129 | } |
| 130 | |
| 131 | for (const auto& layer : layers) { |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 132 | ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 133 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min || |
| 134 | layer.vote == LayerVoteType::Max) { |
| 135 | continue; |
| 136 | } |
| 137 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 138 | // Adjust the weight in case we have explicit layers. The priority is: |
| 139 | // - ExplicitExactOrMultiple |
| 140 | // - ExplicitDefault |
| 141 | // - Heuristic |
| 142 | auto weight = layer.weight; |
| 143 | if (explicitExactOrMultipleVoteLayers + explicitDefaultVoteLayers > 0) { |
| 144 | if (layer.vote == LayerVoteType::Heuristic) { |
| 145 | weight /= 2.f; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (explicitExactOrMultipleVoteLayers > 0) { |
| 150 | if (layer.vote == LayerVoteType::Heuristic || |
| 151 | layer.vote == LayerVoteType::ExplicitDefault) { |
| 152 | weight /= 2.f; |
| 153 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | for (auto& [refreshRate, overallScore] : scores) { |
| 157 | const auto displayPeriod = refreshRate->vsyncPeriod; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 158 | const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 159 | |
| 160 | // Calculate how many display vsyncs we need to present a single frame for this layer |
| 161 | auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod); |
| 162 | if (displayFramesRem <= MARGIN || |
| 163 | std::abs(displayFramesRem - displayPeriod) <= MARGIN) { |
| 164 | displayFramesQuot++; |
| 165 | displayFramesRem = 0; |
| 166 | } |
| 167 | |
| 168 | float layerScore; |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 169 | static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1 |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 170 | if (displayFramesRem == 0) { |
| 171 | // Layer desired refresh rate matches the display rate. |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 172 | layerScore = weight * 1.0f; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 173 | } else if (displayFramesQuot == 0) { |
| 174 | // Layer desired refresh rate is higher the display rate. |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 175 | layerScore = weight * |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 176 | (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) * |
| 177 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 178 | } else { |
| 179 | // Layer desired refresh rate is lower the display rate. Check how well it fits the |
| 180 | // cadence |
| 181 | auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem)); |
| 182 | int iter = 2; |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 183 | while (diff > MARGIN && iter < MAX_FRAMES_TO_FIT) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 184 | diff = diff - (displayPeriod - diff); |
| 185 | iter++; |
| 186 | } |
| 187 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 188 | layerScore = weight * (1.0f / iter); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 191 | ALOGV("%s (weight %.2f) %.2fHz gives %s score of %.2f", layer.name.c_str(), weight, |
| 192 | 1e9f / layerPeriod, refreshRate->name.c_str(), layerScore); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 193 | overallScore += layerScore; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | float max = 0; |
| 198 | const RefreshRate* bestRefreshRate = nullptr; |
| 199 | for (const auto [refreshRate, score] : scores) { |
| 200 | ALOGV("%s scores %.2f", refreshRate->name.c_str(), score); |
| 201 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 202 | ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 203 | |
| 204 | if (score > max) { |
| 205 | max = score; |
| 206 | bestRefreshRate = refreshRate; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return bestRefreshRate == nullptr ? *mCurrentRefreshRate : *bestRefreshRate; |
| 211 | } |
| 212 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 213 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 214 | return mRefreshRates; |
| 215 | } |
| 216 | |
| 217 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 218 | std::lock_guard lock(mLock); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 219 | return *mAvailableRefreshRates.front(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 223 | std::lock_guard lock(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 224 | return *mAvailableRefreshRates.back(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 228 | std::lock_guard lock(mLock); |
| 229 | return *mCurrentRefreshRate; |
| 230 | } |
| 231 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame^] | 232 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const { |
| 233 | std::lock_guard lock(mLock); |
| 234 | if (std::find(mAvailableRefreshRates.begin(), mAvailableRefreshRates.end(), |
| 235 | mCurrentRefreshRate) != mAvailableRefreshRates.end()) { |
| 236 | return *mCurrentRefreshRate; |
| 237 | } |
| 238 | return mRefreshRates.at(mDefaultConfig); |
| 239 | } |
| 240 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 241 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 242 | std::lock_guard lock(mLock); |
| 243 | mCurrentRefreshRate = &mRefreshRates.at(configId); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 246 | RefreshRateConfigs::RefreshRateConfigs(const std::vector<InputConfig>& configs, |
| 247 | HwcConfigIndexType currentHwcConfig) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 248 | init(configs, currentHwcConfig); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 252 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 253 | HwcConfigIndexType currentConfigId) { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 254 | std::vector<InputConfig> inputConfigs; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 255 | for (size_t configId = 0; configId < configs.size(); ++configId) { |
| 256 | auto configGroup = HwcConfigGroupType(configs[configId]->getConfigGroup()); |
| 257 | inputConfigs.push_back({HwcConfigIndexType(static_cast<int>(configId)), configGroup, |
| 258 | configs[configId]->getVsyncPeriod()}); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 259 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 260 | init(inputConfigs, currentConfigId); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 263 | status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate, |
| 264 | float maxRefreshRate, bool* outPolicyChanged) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 265 | std::lock_guard lock(mLock); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 266 | bool policyChanged = defaultConfigId != mDefaultConfig || |
| 267 | minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps; |
| 268 | if (outPolicyChanged) { |
| 269 | *outPolicyChanged = policyChanged; |
| 270 | } |
| 271 | if (!policyChanged) { |
| 272 | return NO_ERROR; |
| 273 | } |
| 274 | // defaultConfigId must be a valid config ID, and within the given refresh rate range. |
| 275 | if (mRefreshRates.count(defaultConfigId) == 0) { |
| 276 | return BAD_VALUE; |
| 277 | } |
| 278 | const RefreshRate& refreshRate = mRefreshRates.at(defaultConfigId); |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 279 | if (!refreshRate.inPolicy(minRefreshRate, maxRefreshRate)) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 280 | return BAD_VALUE; |
| 281 | } |
| 282 | mDefaultConfig = defaultConfigId; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 283 | mMinRefreshRateFps = minRefreshRate; |
| 284 | mMaxRefreshRateFps = maxRefreshRate; |
| 285 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 286 | return NO_ERROR; |
| 287 | } |
| 288 | |
| 289 | void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate, |
| 290 | float* maxRefreshRate) const { |
| 291 | std::lock_guard lock(mLock); |
| 292 | *defaultConfigId = mDefaultConfig; |
| 293 | *minRefreshRate = mMinRefreshRateFps; |
| 294 | *maxRefreshRate = mMaxRefreshRateFps; |
| 295 | } |
| 296 | |
| 297 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 298 | std::lock_guard lock(mLock); |
| 299 | for (const RefreshRate* refreshRate : mAvailableRefreshRates) { |
| 300 | if (refreshRate->configId == config) { |
| 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 308 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 309 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 310 | outRefreshRates->clear(); |
| 311 | outRefreshRates->reserve(mRefreshRates.size()); |
| 312 | for (const auto& [type, refreshRate] : mRefreshRates) { |
| 313 | if (shouldAddRefreshRate(refreshRate)) { |
| 314 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
| 315 | refreshRate.configId.value()); |
| 316 | outRefreshRates->push_back(&refreshRate); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 321 | [](const auto refreshRate1, const auto refreshRate2) { |
| 322 | return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod; |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 327 | // Filter configs based on current policy and sort based on vsync period |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 328 | HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig).configGroup; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 329 | ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f", |
| 330 | mDefaultConfig.value(), group.value(), mMinRefreshRateFps, mMaxRefreshRateFps); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 331 | getSortedRefreshRateList( |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 332 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 333 | return refreshRate.configGroup == group && |
| 334 | refreshRate.inPolicy(mMinRefreshRateFps, mMaxRefreshRateFps); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 335 | }, |
| 336 | &mAvailableRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 337 | |
| 338 | std::string availableRefreshRates; |
| 339 | for (const auto& refreshRate : mAvailableRefreshRates) { |
| 340 | base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str()); |
| 341 | } |
| 342 | |
| 343 | ALOGV("Available refresh rates: %s", availableRefreshRates.c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 344 | LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(), |
| 345 | "No compatible display configs for default=%d min=%.0f max=%.0f", |
| 346 | mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor |
| 350 | void RefreshRateConfigs::init(const std::vector<InputConfig>& configs, |
| 351 | HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 352 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 353 | LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size()); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 354 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 355 | auto buildRefreshRate = [&](InputConfig config) -> RefreshRate { |
| 356 | const float fps = 1e9f / config.vsyncPeriod; |
| 357 | return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup, |
| 358 | base::StringPrintf("%2.ffps", fps), fps); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 359 | }; |
| 360 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 361 | for (const auto& config : configs) { |
| 362 | mRefreshRates.emplace(config.configId, buildRefreshRate(config)); |
| 363 | if (config.configId == currentHwcConfig) { |
| 364 | mCurrentRefreshRate = &mRefreshRates.at(config.configId); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 365 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 368 | std::vector<const RefreshRate*> sortedConfigs; |
| 369 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 370 | mDefaultConfig = currentHwcConfig; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 371 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 372 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 373 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 376 | } // namespace android::scheduler |