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 |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 61 | auto iter = min_element(mPrimaryRefreshRates.cbegin(), mPrimaryRefreshRates.cend(), |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 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) { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 74 | while (iter != mPrimaryRefreshRates.cend()) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 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; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 113 | return touchActive ? getMaxRefreshRateByPolicyLocked() |
| 114 | : getCurrentRefreshRateByPolicyLocked(); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 117 | int noVoteLayers = 0; |
| 118 | int minVoteLayers = 0; |
| 119 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 120 | int explicitDefaultVoteLayers = 0; |
| 121 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 122 | float maxExplicitWeight = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 123 | for (const auto& layer : layers) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 124 | if (layer.vote == LayerVoteType::NoVote) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 125 | noVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 126 | } else if (layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 127 | minVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 128 | } else if (layer.vote == LayerVoteType::Max) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 129 | maxVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 130 | } else if (layer.vote == LayerVoteType::ExplicitDefault) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 131 | explicitDefaultVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 132 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 133 | } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 134 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 135 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 136 | } |
| 137 | } |
| 138 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 139 | // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've |
| 140 | // selected a refresh rate to see if we should apply touch boost. |
| 141 | if (touchActive && explicitDefaultVoteLayers == 0 && explicitExactOrMultipleVoteLayers == 0) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 142 | *touchConsidered = true; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 143 | return getMaxRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Only if all layers want Min we should return Min |
| 147 | if (noVoteLayers + minVoteLayers == layers.size()) { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 148 | return getMinRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 151 | const Policy* policy = getCurrentPolicyLocked(); |
| 152 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 153 | // Find the best refresh rate based on score |
| 154 | std::vector<std::pair<const RefreshRate*, float>> scores; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 155 | scores.reserve(mAppRequestRefreshRates.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 156 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 157 | for (const auto refreshRate : mAppRequestRefreshRates) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 158 | scores.emplace_back(refreshRate, 0.0f); |
| 159 | } |
| 160 | |
| 161 | for (const auto& layer : layers) { |
Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 162 | 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] | 163 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 164 | continue; |
| 165 | } |
| 166 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 167 | auto weight = layer.weight; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 168 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 169 | for (auto i = 0u; i < scores.size(); i++) { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 170 | bool inPrimaryRange = |
| 171 | scores[i].first->inPolicy(policy->primaryRange.min, policy->primaryRange.max); |
| 172 | if (!inPrimaryRange && layer.vote != LayerVoteType::ExplicitDefault && |
| 173 | layer.vote != LayerVoteType::ExplicitExactOrMultiple) { |
| 174 | // Only layers with explicit frame rate settings are allowed to score refresh rates |
| 175 | // outside the primary range. |
| 176 | continue; |
| 177 | } |
| 178 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 179 | // If the layer wants Max, give higher score to the higher refresh rate |
| 180 | if (layer.vote == LayerVoteType::Max) { |
| 181 | const auto ratio = scores[i].first->fps / scores.back().first->fps; |
| 182 | // use ratio^2 to get a lower score the more we get further from peak |
| 183 | const auto layerScore = ratio * ratio; |
| 184 | ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight, |
| 185 | scores[i].first->name.c_str(), layerScore); |
| 186 | scores[i].second += weight * layerScore; |
| 187 | continue; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 188 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 189 | |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 190 | const auto displayPeriod = scores[i].first->hwcConfig->getVsyncPeriod(); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 191 | const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 192 | if (layer.vote == LayerVoteType::ExplicitDefault) { |
| 193 | const auto layerScore = [&]() { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 194 | // Find the actual rate the layer will render, assuming |
| 195 | // that layerPeriod is the minimal time to render a frame |
| 196 | auto actualLayerPeriod = displayPeriod; |
| 197 | int multiplier = 1; |
| 198 | while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) { |
| 199 | multiplier++; |
| 200 | actualLayerPeriod = displayPeriod * multiplier; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 201 | } |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 202 | return std::min(1.0f, |
| 203 | static_cast<float>(layerPeriod) / |
| 204 | static_cast<float>(actualLayerPeriod)); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 205 | }(); |
| 206 | |
| 207 | ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f", |
| 208 | layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), |
| 209 | layerScore); |
| 210 | scores[i].second += weight * layerScore; |
| 211 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 214 | if (layer.vote == LayerVoteType::ExplicitExactOrMultiple || |
| 215 | layer.vote == LayerVoteType::Heuristic) { |
| 216 | const auto layerScore = [&]() { |
| 217 | // Calculate how many display vsyncs we need to present a single frame for this |
| 218 | // layer |
| 219 | const auto [displayFramesQuot, displayFramesRem] = |
| 220 | getDisplayFrames(layerPeriod, displayPeriod); |
| 221 | static constexpr size_t MAX_FRAMES_TO_FIT = |
| 222 | 10; // Stop calculating when score < 0.1 |
| 223 | if (displayFramesRem == 0) { |
| 224 | // Layer desired refresh rate matches the display rate. |
| 225 | return 1.0f; |
| 226 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 227 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 228 | if (displayFramesQuot == 0) { |
| 229 | // Layer desired refresh rate is higher the display rate. |
| 230 | return (static_cast<float>(layerPeriod) / |
| 231 | static_cast<float>(displayPeriod)) * |
| 232 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
| 233 | } |
| 234 | |
| 235 | // Layer desired refresh rate is lower the display rate. Check how well it fits |
| 236 | // the cadence |
| 237 | auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem)); |
| 238 | int iter = 2; |
| 239 | while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) { |
| 240 | diff = diff - (displayPeriod - diff); |
| 241 | iter++; |
| 242 | } |
| 243 | |
| 244 | return 1.0f / iter; |
| 245 | }(); |
| 246 | ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f", |
| 247 | layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(), |
| 248 | layerScore); |
| 249 | scores[i].second += weight * layerScore; |
| 250 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 251 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 255 | // Now that we scored all the refresh rates we need to pick the one that got the highest score. |
| 256 | // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max, |
| 257 | // or the lower otherwise. |
| 258 | const RefreshRate* bestRefreshRate = maxVoteLayers > 0 |
| 259 | ? getBestRefreshRate(scores.rbegin(), scores.rend()) |
| 260 | : getBestRefreshRate(scores.begin(), scores.end()); |
| 261 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 262 | // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly |
| 263 | // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit |
| 264 | // vote we should not change it if we get a touch event. Only apply touch boost if it will |
| 265 | // actually increase the refresh rate over the normal selection. |
| 266 | const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked(); |
| 267 | if (touchActive && explicitDefaultVoteLayers == 0 && |
| 268 | bestRefreshRate->fps < touchRefreshRate.fps) { |
| 269 | *touchConsidered = true; |
| 270 | return touchRefreshRate; |
| 271 | } |
| 272 | |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 273 | return *bestRefreshRate; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | template <typename Iter> |
| 277 | const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 278 | constexpr auto EPSILON = 0.001f; |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 279 | const RefreshRate* bestRefreshRate = begin->first; |
| 280 | float max = begin->second; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 281 | for (auto i = begin; i != end; ++i) { |
| 282 | const auto [refreshRate, score] = *i; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 283 | ALOGV("%s scores %.2f", refreshRate->name.c_str(), score); |
| 284 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 285 | ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 286 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 287 | if (score > max * (1 + EPSILON)) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 288 | max = score; |
| 289 | bestRefreshRate = refreshRate; |
| 290 | } |
| 291 | } |
| 292 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 293 | return bestRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 296 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 297 | return mRefreshRates; |
| 298 | } |
| 299 | |
| 300 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 301 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 302 | return getMinRefreshRateByPolicyLocked(); |
| 303 | } |
| 304 | |
| 305 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const { |
| 306 | return *mPrimaryRefreshRates.front(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 310 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 311 | return getMaxRefreshRateByPolicyLocked(); |
| 312 | } |
| 313 | |
| 314 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const { |
| 315 | return *mPrimaryRefreshRates.back(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 319 | std::lock_guard lock(mLock); |
| 320 | return *mCurrentRefreshRate; |
| 321 | } |
| 322 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 323 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const { |
| 324 | std::lock_guard lock(mLock); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 325 | return getCurrentRefreshRateByPolicyLocked(); |
| 326 | } |
| 327 | |
| 328 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 329 | if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(), |
| 330 | mCurrentRefreshRate) != mAppRequestRefreshRates.end()) { |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 331 | return *mCurrentRefreshRate; |
| 332 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 333 | return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig); |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 336 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 337 | std::lock_guard lock(mLock); |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 338 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 341 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 342 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 343 | HwcConfigIndexType currentConfigId) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 344 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
| 345 | LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size()); |
| 346 | |
| 347 | for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) { |
| 348 | const auto& config = configs.at(static_cast<size_t>(configId.value())); |
| 349 | const float fps = 1e9f / config->getVsyncPeriod(); |
| 350 | mRefreshRates.emplace(configId, |
| 351 | std::make_unique<RefreshRate>(configId, config, |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 352 | base::StringPrintf("%.0ffps", fps), fps, |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 353 | RefreshRate::ConstructorTag(0))); |
| 354 | if (configId == currentConfigId) { |
| 355 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
| 356 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 357 | } |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 358 | |
| 359 | std::vector<const RefreshRate*> sortedConfigs; |
| 360 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
| 361 | mDisplayManagerPolicy.defaultConfig = currentConfigId; |
| 362 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 363 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 364 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 367 | bool RefreshRateConfigs::isPolicyValid(const Policy& policy) { |
| 368 | // defaultConfig must be a valid config, and within the given refresh rate range. |
| 369 | auto iter = mRefreshRates.find(policy.defaultConfig); |
| 370 | if (iter == mRefreshRates.end()) { |
| 371 | return false; |
| 372 | } |
| 373 | const RefreshRate& refreshRate = *iter->second; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 374 | if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) { |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 377 | return policy.appRequestRange.min <= policy.primaryRange.min && |
| 378 | policy.appRequestRange.max >= policy.primaryRange.max; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 382 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 383 | if (!isPolicyValid(policy)) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 384 | return BAD_VALUE; |
| 385 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 386 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 387 | mDisplayManagerPolicy = policy; |
| 388 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 389 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 390 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 391 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 392 | return NO_ERROR; |
| 393 | } |
| 394 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 395 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 396 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 397 | if (policy && !isPolicyValid(*policy)) { |
| 398 | return BAD_VALUE; |
| 399 | } |
| 400 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 401 | mOverridePolicy = policy; |
| 402 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 403 | return CURRENT_POLICY_UNCHANGED; |
| 404 | } |
| 405 | constructAvailableRefreshRates(); |
| 406 | return NO_ERROR; |
| 407 | } |
| 408 | |
| 409 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 410 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 411 | } |
| 412 | |
| 413 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 414 | std::lock_guard lock(mLock); |
| 415 | return *getCurrentPolicyLocked(); |
| 416 | } |
| 417 | |
| 418 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 419 | std::lock_guard lock(mLock); |
| 420 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 424 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 425 | for (const RefreshRate* refreshRate : mAppRequestRefreshRates) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 426 | if (refreshRate->configId == config) { |
| 427 | return true; |
| 428 | } |
| 429 | } |
| 430 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 434 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 435 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 436 | outRefreshRates->clear(); |
| 437 | outRefreshRates->reserve(mRefreshRates.size()); |
| 438 | for (const auto& [type, refreshRate] : mRefreshRates) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 439 | if (shouldAddRefreshRate(*refreshRate)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 440 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 441 | refreshRate->configId.value()); |
| 442 | outRefreshRates->push_back(refreshRate.get()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 447 | [](const auto refreshRate1, const auto refreshRate2) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 448 | if (refreshRate1->hwcConfig->getVsyncPeriod() != |
| 449 | refreshRate2->hwcConfig->getVsyncPeriod()) { |
| 450 | return refreshRate1->hwcConfig->getVsyncPeriod() > |
| 451 | refreshRate2->hwcConfig->getVsyncPeriod(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 452 | } else { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 453 | return refreshRate1->hwcConfig->getConfigGroup() > |
| 454 | refreshRate2->hwcConfig->getConfigGroup(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 455 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 456 | }); |
| 457 | } |
| 458 | |
| 459 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 460 | // Filter configs based on current policy and sort based on vsync period |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 461 | const Policy* policy = getCurrentPolicyLocked(); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 462 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 463 | ALOGV("constructAvailableRefreshRates: default %d group %d primaryRange=[%.2f %.2f]" |
| 464 | " appRequestRange=[%.2f %.2f]", |
| 465 | policy->defaultConfig.value(), defaultConfig->getConfigGroup(), policy->primaryRange.min, |
| 466 | policy->primaryRange.max, policy->appRequestRange.min, policy->appRequestRange.max); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 467 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 468 | auto filterRefreshRates = [&](float min, float max, const char* listName, |
| 469 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 470 | getSortedRefreshRateList( |
| 471 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
| 472 | const auto& hwcConfig = refreshRate.hwcConfig; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 473 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 474 | return hwcConfig->getHeight() == defaultConfig->getHeight() && |
| 475 | hwcConfig->getWidth() == defaultConfig->getWidth() && |
| 476 | hwcConfig->getDpiX() == defaultConfig->getDpiX() && |
| 477 | hwcConfig->getDpiY() == defaultConfig->getDpiY() && |
| 478 | (policy->allowGroupSwitching || |
| 479 | hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) && |
| 480 | refreshRate.inPolicy(min, max); |
| 481 | }, |
| 482 | outRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 483 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 484 | LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(), |
| 485 | "No matching configs for %s range: min=%.0f max=%.0f", listName, min, |
| 486 | max); |
| 487 | auto stringifyRefreshRates = [&]() -> std::string { |
| 488 | std::string str; |
| 489 | for (auto refreshRate : *outRefreshRates) { |
| 490 | base::StringAppendF(&str, "%s ", refreshRate->name.c_str()); |
| 491 | } |
| 492 | return str; |
| 493 | }; |
| 494 | ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str()); |
| 495 | }; |
| 496 | |
| 497 | filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary", |
| 498 | &mPrimaryRefreshRates); |
| 499 | filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request", |
| 500 | &mAppRequestRefreshRates); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 503 | } // namespace android::scheduler |