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