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) { |
| 56 | contentFramerate = round<int>(mMaxSupportedRefreshRate->fps); |
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 | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 180 | const auto displayPeriod = scores[i].first->vsyncPeriod; |
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 | |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 312 | RefreshRateConfigs::RefreshRateConfigs(const std::vector<InputConfig>& configs, |
| 313 | HwcConfigIndexType currentHwcConfig) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 314 | init(configs, currentHwcConfig); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 318 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 319 | HwcConfigIndexType currentConfigId) { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 320 | std::vector<InputConfig> inputConfigs; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 321 | for (size_t configId = 0; configId < configs.size(); ++configId) { |
| 322 | auto configGroup = HwcConfigGroupType(configs[configId]->getConfigGroup()); |
| 323 | inputConfigs.push_back({HwcConfigIndexType(static_cast<int>(configId)), configGroup, |
| 324 | configs[configId]->getVsyncPeriod()}); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 325 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 326 | init(inputConfigs, currentConfigId); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 329 | bool RefreshRateConfigs::isPolicyValid(const Policy& policy) { |
| 330 | // defaultConfig must be a valid config, and within the given refresh rate range. |
| 331 | auto iter = mRefreshRates.find(policy.defaultConfig); |
| 332 | if (iter == mRefreshRates.end()) { |
| 333 | return false; |
| 334 | } |
| 335 | const RefreshRate& refreshRate = *iter->second; |
| 336 | if (!refreshRate.inPolicy(policy.minRefreshRate, policy.maxRefreshRate)) { |
| 337 | return false; |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 343 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 344 | if (!isPolicyValid(policy)) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 345 | return BAD_VALUE; |
| 346 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 347 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 348 | mDisplayManagerPolicy = policy; |
| 349 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 350 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 351 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 352 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 353 | return NO_ERROR; |
| 354 | } |
| 355 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 356 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 357 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 358 | if (policy && !isPolicyValid(*policy)) { |
| 359 | return BAD_VALUE; |
| 360 | } |
| 361 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 362 | mOverridePolicy = policy; |
| 363 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 364 | return CURRENT_POLICY_UNCHANGED; |
| 365 | } |
| 366 | constructAvailableRefreshRates(); |
| 367 | return NO_ERROR; |
| 368 | } |
| 369 | |
| 370 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 371 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 372 | } |
| 373 | |
| 374 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 375 | std::lock_guard lock(mLock); |
| 376 | return *getCurrentPolicyLocked(); |
| 377 | } |
| 378 | |
| 379 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 380 | std::lock_guard lock(mLock); |
| 381 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 385 | std::lock_guard lock(mLock); |
| 386 | for (const RefreshRate* refreshRate : mAvailableRefreshRates) { |
| 387 | if (refreshRate->configId == config) { |
| 388 | return true; |
| 389 | } |
| 390 | } |
| 391 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 395 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 396 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 397 | outRefreshRates->clear(); |
| 398 | outRefreshRates->reserve(mRefreshRates.size()); |
| 399 | for (const auto& [type, refreshRate] : mRefreshRates) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 400 | if (shouldAddRefreshRate(*refreshRate)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 401 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 402 | refreshRate->configId.value()); |
| 403 | outRefreshRates->push_back(refreshRate.get()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 408 | [](const auto refreshRate1, const auto refreshRate2) { |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 409 | if (refreshRate1->vsyncPeriod != refreshRate2->vsyncPeriod) { |
| 410 | return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod; |
| 411 | } else { |
| 412 | return refreshRate1->configGroup > refreshRate2->configGroup; |
| 413 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 414 | }); |
| 415 | } |
| 416 | |
| 417 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 418 | // Filter configs based on current policy and sort based on vsync period |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 419 | const Policy* policy = getCurrentPolicyLocked(); |
| 420 | HwcConfigGroupType group = mRefreshRates.at(policy->defaultConfig)->configGroup; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 421 | ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f", |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 422 | policy->defaultConfig.value(), group.value(), policy->minRefreshRate, |
| 423 | policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 424 | getSortedRefreshRateList( |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 425 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 426 | return (policy->allowGroupSwitching || refreshRate.configGroup == group) && |
| 427 | refreshRate.inPolicy(policy->minRefreshRate, policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 428 | }, |
| 429 | &mAvailableRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 430 | |
| 431 | std::string availableRefreshRates; |
| 432 | for (const auto& refreshRate : mAvailableRefreshRates) { |
| 433 | base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str()); |
| 434 | } |
| 435 | |
| 436 | ALOGV("Available refresh rates: %s", availableRefreshRates.c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 437 | LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(), |
| 438 | "No compatible display configs for default=%d min=%.0f max=%.0f", |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 439 | policy->defaultConfig.value(), policy->minRefreshRate, |
| 440 | policy->maxRefreshRate); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor |
| 444 | void RefreshRateConfigs::init(const std::vector<InputConfig>& configs, |
| 445 | HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS { |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 446 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 447 | LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size()); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 448 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 449 | for (const auto& config : configs) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 450 | const float fps = 1e9f / config.vsyncPeriod; |
| 451 | mRefreshRates.emplace(config.configId, |
| 452 | std::make_unique<RefreshRate>(config.configId, config.vsyncPeriod, |
| 453 | config.configGroup, |
| 454 | base::StringPrintf("%2.ffps", fps), |
| 455 | fps)); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 456 | if (config.configId == currentHwcConfig) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 457 | mCurrentRefreshRate = mRefreshRates.at(config.configId).get(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 458 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 461 | std::vector<const RefreshRate*> sortedConfigs; |
| 462 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame^] | 463 | mDisplayManagerPolicy.defaultConfig = currentHwcConfig; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 464 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 465 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 466 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 469 | } // namespace android::scheduler |