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 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 26 | #undef LOG_TAG |
| 27 | #define LOG_TAG "RefreshRateConfigs" |
| 28 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 29 | namespace android::scheduler { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 30 | |
| 31 | using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 32 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 33 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 34 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContent( |
| 35 | const std::vector<LayerRequirement>& layers) const { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 36 | std::lock_guard lock(mLock); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 37 | int contentFramerate = 0; |
| 38 | int explicitContentFramerate = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 39 | for (const auto& layer : layers) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 40 | const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 41 | if (layer.vote == LayerVoteType::ExplicitDefault || |
| 42 | layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 43 | if (desiredRefreshRateRound > explicitContentFramerate) { |
| 44 | explicitContentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 45 | } |
| 46 | } else { |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 47 | if (desiredRefreshRateRound > contentFramerate) { |
| 48 | contentFramerate = desiredRefreshRateRound; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 53 | if (explicitContentFramerate != 0) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 54 | contentFramerate = explicitContentFramerate; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 55 | } else if (contentFramerate == 0) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 56 | contentFramerate = round<int>(mMaxSupportedRefreshRate->getFps()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 57 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 58 | ATRACE_INT("ContentFPS", contentFramerate); |
| 59 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 60 | // Find the appropriate refresh rate with minimal error |
| 61 | auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(), |
| 62 | [contentFramerate](const auto& lhs, const auto& rhs) -> bool { |
| 63 | return std::abs(lhs->fps - contentFramerate) < |
| 64 | std::abs(rhs->fps - contentFramerate); |
| 65 | }); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 66 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 67 | // Some content aligns better on higher refresh rate. For example for 45fps we should choose |
| 68 | // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't |
| 69 | // align well with both |
| 70 | const RefreshRate* bestSoFar = *iter; |
| 71 | constexpr float MARGIN = 0.05f; |
| 72 | float ratio = (*iter)->fps / contentFramerate; |
| 73 | if (std::abs(std::round(ratio) - ratio) > MARGIN) { |
| 74 | while (iter != mAvailableRefreshRates.cend()) { |
| 75 | ratio = (*iter)->fps / contentFramerate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 76 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 77 | if (std::abs(std::round(ratio) - ratio) <= MARGIN) { |
| 78 | bestSoFar = *iter; |
| 79 | break; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 80 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 81 | ++iter; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 85 | return *bestSoFar; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 88 | std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod, |
| 89 | nsecs_t displayPeriod) const { |
| 90 | auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod); |
| 91 | if (displayFramesRem <= MARGIN_FOR_PERIOD_CALCULATION || |
| 92 | std::abs(displayFramesRem - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) { |
| 93 | displayFramesQuot++; |
| 94 | displayFramesRem = 0; |
| 95 | } |
| 96 | |
| 97 | return {displayFramesQuot, displayFramesRem}; |
| 98 | } |
| 99 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 100 | const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2( |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 101 | const std::vector<LayerRequirement>& layers, bool touchActive, |
| 102 | bool* touchConsidered) const { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 103 | ATRACE_CALL(); |
| 104 | ALOGV("getRefreshRateForContent %zu layers", layers.size()); |
| 105 | |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 106 | *touchConsidered = false; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 107 | std::lock_guard lock(mLock); |
| 108 | |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 109 | // If there are not layers, there is not content detection, so return the current |
| 110 | // refresh rate. |
| 111 | if (layers.empty()) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 112 | *touchConsidered = touchActive; |
| 113 | return touchActive ? *mAvailableRefreshRates.back() : getCurrentRefreshRateByPolicyLocked(); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 116 | int noVoteLayers = 0; |
| 117 | int minVoteLayers = 0; |
| 118 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 119 | int explicitDefaultVoteLayers = 0; |
| 120 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 121 | float maxExplicitWeight = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 122 | for (const auto& layer : layers) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 123 | if (layer.vote == LayerVoteType::NoVote) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 124 | noVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 125 | } else if (layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 126 | minVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 127 | } else if (layer.vote == LayerVoteType::Max) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 128 | maxVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 129 | } else if (layer.vote == LayerVoteType::ExplicitDefault) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 130 | explicitDefaultVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 131 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 132 | } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 133 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 134 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Consider the touch event if there are no ExplicitDefault layers. |
| 139 | // ExplicitDefault are mostly interactive (as opposed to ExplicitExactOrMultiple) |
| 140 | // and therefore if those posted an explicit vote we should not change it |
| 141 | // if get get a touch event. |
| 142 | if (touchActive && explicitDefaultVoteLayers == 0) { |
| 143 | *touchConsidered = true; |
| 144 | return *mAvailableRefreshRates.back(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Only if all layers want Min we should return Min |
| 148 | if (noVoteLayers + minVoteLayers == layers.size()) { |
| 149 | return *mAvailableRefreshRates.front(); |
| 150 | } |
| 151 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 152 | // Find the best refresh rate based on score |
| 153 | std::vector<std::pair<const RefreshRate*, float>> scores; |
| 154 | scores.reserve(mAvailableRefreshRates.size()); |
| 155 | |
| 156 | for (const auto refreshRate : mAvailableRefreshRates) { |
| 157 | scores.emplace_back(refreshRate, 0.0f); |
| 158 | } |
| 159 | |
| 160 | for (const auto& layer : layers) { |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 161 | ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 162 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 166 | auto weight = layer.weight; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 167 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 168 | for (auto i = 0u; i < scores.size(); i++) { |
| 169 | // If the layer wants Max, give higher score to the higher refresh rate |
| 170 | if (layer.vote == LayerVoteType::Max) { |
| 171 | const auto ratio = scores[i].first->fps / scores.back().first->fps; |
| 172 | // use ratio^2 to get a lower score the more we get further from peak |
| 173 | const auto layerScore = ratio * ratio; |
| 174 | ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight, |
| 175 | scores[i].first->name.c_str(), layerScore); |
| 176 | scores[i].second += weight * layerScore; |
| 177 | continue; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 178 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 179 | |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 180 | const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod(); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 181 | const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 182 | if (layer.vote == LayerVoteType::ExplicitDefault) { |
| 183 | const auto layerScore = [&]() { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 184 | // Find the actual rate the layer will render, assuming |
| 185 | // that layerPeriod is the minimal time to render a frame |
| 186 | auto actualLayerPeriod = displayPeriod; |
| 187 | int multiplier = 1; |
| 188 | while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) { |
| 189 | multiplier++; |
| 190 | actualLayerPeriod = displayPeriod * multiplier; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 191 | } |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 192 | return std::min(1.0f, |
| 193 | static_cast<float>(layerPeriod) / |
| 194 | static_cast<float>(actualLayerPeriod)); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 195 | }(); |
| 196 | |
| 197 | ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f", |
| 198 | layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), |
| 199 | layerScore); |
| 200 | scores[i].second += weight * layerScore; |
| 201 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 204 | if (layer.vote == LayerVoteType::ExplicitExactOrMultiple || |
| 205 | layer.vote == LayerVoteType::Heuristic) { |
| 206 | const auto layerScore = [&]() { |
| 207 | // Calculate how many display vsyncs we need to present a single frame for this |
| 208 | // layer |
| 209 | const auto [displayFramesQuot, displayFramesRem] = |
| 210 | getDisplayFrames(layerPeriod, displayPeriod); |
| 211 | static constexpr size_t MAX_FRAMES_TO_FIT = |
| 212 | 10; // Stop calculating when score < 0.1 |
| 213 | if (displayFramesRem == 0) { |
| 214 | // Layer desired refresh rate matches the display rate. |
| 215 | return 1.0f; |
| 216 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 217 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 218 | if (displayFramesQuot == 0) { |
| 219 | // Layer desired refresh rate is higher the display rate. |
| 220 | return (static_cast<float>(layerPeriod) / |
| 221 | static_cast<float>(displayPeriod)) * |
| 222 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
| 223 | } |
| 224 | |
| 225 | // Layer desired refresh rate is lower the display rate. Check how well it fits |
| 226 | // the cadence |
| 227 | auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem)); |
| 228 | int iter = 2; |
| 229 | while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) { |
| 230 | diff = diff - (displayPeriod - diff); |
| 231 | iter++; |
| 232 | } |
| 233 | |
| 234 | return 1.0f / iter; |
| 235 | }(); |
| 236 | ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f", |
| 237 | layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), |
| 238 | layerScore); |
| 239 | scores[i].second += weight * layerScore; |
| 240 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 241 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 245 | // Now that we scored all the refresh rates we need to pick the one that got the highest score. |
| 246 | // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max, |
| 247 | // or the lower otherwise. |
| 248 | const RefreshRate* bestRefreshRate = maxVoteLayers > 0 |
| 249 | ? getBestRefreshRate(scores.rbegin(), scores.rend()) |
| 250 | : getBestRefreshRate(scores.begin(), scores.end()); |
| 251 | |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 252 | return *bestRefreshRate; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | template <typename Iter> |
| 256 | const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 257 | constexpr auto EPSILON = 0.001f; |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 258 | const RefreshRate* bestRefreshRate = begin->first; |
| 259 | float max = begin->second; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 260 | for (auto i = begin; i != end; ++i) { |
| 261 | const auto [refreshRate, score] = *i; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 262 | ALOGV("%s scores %.2f", refreshRate->name.c_str(), score); |
| 263 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 264 | ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 265 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 266 | if (score > max * (1 + EPSILON)) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 267 | max = score; |
| 268 | bestRefreshRate = refreshRate; |
| 269 | } |
| 270 | } |
| 271 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 272 | return bestRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 275 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 276 | return mRefreshRates; |
| 277 | } |
| 278 | |
| 279 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 280 | std::lock_guard lock(mLock); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 281 | return *mAvailableRefreshRates.front(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 285 | std::lock_guard lock(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 286 | return *mAvailableRefreshRates.back(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 290 | std::lock_guard lock(mLock); |
| 291 | return *mCurrentRefreshRate; |
| 292 | } |
| 293 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 294 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const { |
| 295 | std::lock_guard lock(mLock); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 296 | return getCurrentRefreshRateByPolicyLocked(); |
| 297 | } |
| 298 | |
| 299 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const { |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 300 | if (std::find(mAvailableRefreshRates.begin(), mAvailableRefreshRates.end(), |
| 301 | mCurrentRefreshRate) != mAvailableRefreshRates.end()) { |
| 302 | return *mCurrentRefreshRate; |
| 303 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 304 | return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig); |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 307 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 308 | std::lock_guard lock(mLock); |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 309 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 312 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 313 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 314 | HwcConfigIndexType currentConfigId) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 315 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
| 316 | LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size()); |
| 317 | |
| 318 | for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) { |
| 319 | const auto& config = configs.at(static_cast<size_t>(configId.value())); |
| 320 | const float fps = 1e9f / config->getVsyncPeriod(); |
| 321 | mRefreshRates.emplace(configId, |
| 322 | std::make_unique<RefreshRate>(configId, config, |
| 323 | base::StringPrintf("%2.ffps", fps), fps, |
| 324 | RefreshRate::ConstructorTag(0))); |
| 325 | if (configId == currentConfigId) { |
| 326 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
| 327 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 328 | } |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 329 | |
| 330 | std::vector<const RefreshRate*> sortedConfigs; |
| 331 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
| 332 | mDisplayManagerPolicy.defaultConfig = currentConfigId; |
| 333 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 334 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 335 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 338 | bool RefreshRateConfigs::isPolicyValid(const Policy& policy) { |
| 339 | // defaultConfig must be a valid config, and within the given refresh rate range. |
| 340 | auto iter = mRefreshRates.find(policy.defaultConfig); |
| 341 | if (iter == mRefreshRates.end()) { |
| 342 | return false; |
| 343 | } |
| 344 | const RefreshRate& refreshRate = *iter->second; |
| 345 | if (!refreshRate.inPolicy(policy.minRefreshRate, policy.maxRefreshRate)) { |
| 346 | return false; |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 352 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 353 | if (!isPolicyValid(policy)) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 354 | return BAD_VALUE; |
| 355 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 356 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 357 | mDisplayManagerPolicy = policy; |
| 358 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 359 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 360 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 361 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 362 | return NO_ERROR; |
| 363 | } |
| 364 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 365 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 366 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 367 | if (policy && !isPolicyValid(*policy)) { |
| 368 | return BAD_VALUE; |
| 369 | } |
| 370 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 371 | mOverridePolicy = policy; |
| 372 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 373 | return CURRENT_POLICY_UNCHANGED; |
| 374 | } |
| 375 | constructAvailableRefreshRates(); |
| 376 | return NO_ERROR; |
| 377 | } |
| 378 | |
| 379 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 380 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 381 | } |
| 382 | |
| 383 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 384 | std::lock_guard lock(mLock); |
| 385 | return *getCurrentPolicyLocked(); |
| 386 | } |
| 387 | |
| 388 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 389 | std::lock_guard lock(mLock); |
| 390 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 394 | std::lock_guard lock(mLock); |
| 395 | for (const RefreshRate* refreshRate : mAvailableRefreshRates) { |
| 396 | if (refreshRate->configId == config) { |
| 397 | return true; |
| 398 | } |
| 399 | } |
| 400 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 404 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 405 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 406 | outRefreshRates->clear(); |
| 407 | outRefreshRates->reserve(mRefreshRates.size()); |
| 408 | for (const auto& [type, refreshRate] : mRefreshRates) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 409 | if (shouldAddRefreshRate(*refreshRate)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 410 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 411 | refreshRate->configId.value()); |
| 412 | outRefreshRates->push_back(refreshRate.get()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 417 | [](const auto refreshRate1, const auto refreshRate2) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 418 | if (refreshRate1->hwcConfig->getVsyncPeriod() != |
| 419 | refreshRate2->hwcConfig->getVsyncPeriod()) { |
| 420 | return refreshRate1->hwcConfig->getVsyncPeriod() > |
| 421 | refreshRate2->hwcConfig->getVsyncPeriod(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 422 | } else { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 423 | return refreshRate1->hwcConfig->getConfigGroup() > |
| 424 | refreshRate2->hwcConfig->getConfigGroup(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 425 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 426 | }); |
| 427 | } |
| 428 | |
| 429 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 430 | // Filter configs based on current policy and sort based on vsync period |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 431 | const Policy* policy = getCurrentPolicyLocked(); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 432 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 433 | ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f", |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 434 | policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->minRefreshRate, |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 435 | policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 436 | getSortedRefreshRateList( |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 437 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 438 | const auto& hwcConfig = refreshRate.hwcConfig; |
| 439 | |
| 440 | return hwcConfig->getHeight() == defaultConfig->getHeight() && |
| 441 | hwcConfig->getWidth() == defaultConfig->getWidth() && |
| 442 | hwcConfig->getDpiX() == defaultConfig->getDpiX() && |
| 443 | hwcConfig->getDpiY() == defaultConfig->getDpiY() && |
| 444 | (policy->allowGroupSwitching || |
| 445 | hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) && |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 446 | refreshRate.inPolicy(policy->minRefreshRate, policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 447 | }, |
| 448 | &mAvailableRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 449 | |
| 450 | std::string availableRefreshRates; |
| 451 | for (const auto& refreshRate : mAvailableRefreshRates) { |
| 452 | base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str()); |
| 453 | } |
| 454 | |
| 455 | ALOGV("Available refresh rates: %s", availableRefreshRates.c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 456 | LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(), |
| 457 | "No compatible display configs for default=%d min=%.0f max=%.0f", |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 458 | policy->defaultConfig.value(), policy->minRefreshRate, |
| 459 | policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 462 | } // namespace android::scheduler |