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  */
16 
17 // #define LOG_NDEBUG 0
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 
20 // TODO(b/129481165): remove the #pragma below and fix conversion issues
21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wextra"
23 
24 #include "RefreshRateConfigs.h"
25 #include <android-base/stringprintf.h>
26 #include <utils/Trace.h>
27 #include <chrono>
28 #include <cmath>
29 #include "../SurfaceFlingerProperties.h"
30 
31 #undef LOG_TAG
32 #define LOG_TAG "RefreshRateConfigs"
33 
34 namespace android::scheduler {
35 namespace {
formatLayerInfo(const RefreshRateConfigs::LayerRequirement & layer,float weight)36 std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) {
37     return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %s", layer.name.c_str(),
38                               RefreshRateConfigs::layerVoteTypeString(layer.vote).c_str(), weight,
39                               toString(layer.seamlessness).c_str(),
40                               to_string(layer.desiredRefreshRate).c_str());
41 }
42 
constructKnownFrameRates(const DisplayModes & modes)43 std::vector<Fps> constructKnownFrameRates(const DisplayModes& modes) {
44     std::vector<Fps> knownFrameRates = {Fps(24.0f), Fps(30.0f), Fps(45.0f), Fps(60.0f), Fps(72.0f)};
45     knownFrameRates.reserve(knownFrameRates.size() + modes.size());
46 
47     // Add all supported refresh rates to the set
48     for (const auto& mode : modes) {
49         const auto refreshRate = Fps::fromPeriodNsecs(mode->getVsyncPeriod());
50         knownFrameRates.emplace_back(refreshRate);
51     }
52 
53     // Sort and remove duplicates
54     std::sort(knownFrameRates.begin(), knownFrameRates.end(), Fps::comparesLess);
55     knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
56                                       Fps::EqualsWithMargin()),
57                           knownFrameRates.end());
58     return knownFrameRates;
59 }
60 
61 } // namespace
62 
63 using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
64 using RefreshRate = RefreshRateConfigs::RefreshRate;
65 
toString() const66 std::string RefreshRate::toString() const {
67     return base::StringPrintf("{id=%d, hwcId=%d, fps=%.2f, width=%d, height=%d group=%d}",
68                               getModeId().value(), mode->getHwcId(), getFps().getValue(),
69                               mode->getWidth(), mode->getHeight(), getModeGroup());
70 }
71 
layerVoteTypeString(LayerVoteType vote)72 std::string RefreshRateConfigs::layerVoteTypeString(LayerVoteType vote) {
73     switch (vote) {
74         case LayerVoteType::NoVote:
75             return "NoVote";
76         case LayerVoteType::Min:
77             return "Min";
78         case LayerVoteType::Max:
79             return "Max";
80         case LayerVoteType::Heuristic:
81             return "Heuristic";
82         case LayerVoteType::ExplicitDefault:
83             return "ExplicitDefault";
84         case LayerVoteType::ExplicitExactOrMultiple:
85             return "ExplicitExactOrMultiple";
86         case LayerVoteType::ExplicitExact:
87             return "ExplicitExact";
88     }
89 }
90 
toString() const91 std::string RefreshRateConfigs::Policy::toString() const {
92     return base::StringPrintf("default mode ID: %d, allowGroupSwitching = %d"
93                               ", primary range: %s, app request range: %s",
94                               defaultMode.value(), allowGroupSwitching,
95                               primaryRange.toString().c_str(), appRequestRange.toString().c_str());
96 }
97 
getDisplayFrames(nsecs_t layerPeriod,nsecs_t displayPeriod) const98 std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
99                                                                  nsecs_t displayPeriod) const {
100     auto [quotient, remainder] = std::div(layerPeriod, displayPeriod);
101     if (remainder <= MARGIN_FOR_PERIOD_CALCULATION ||
102         std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
103         quotient++;
104         remainder = 0;
105     }
106 
107     return {quotient, remainder};
108 }
109 
isVoteAllowed(const LayerRequirement & layer,const RefreshRate & refreshRate) const110 bool RefreshRateConfigs::isVoteAllowed(const LayerRequirement& layer,
111                                        const RefreshRate& refreshRate) const {
112     switch (layer.vote) {
113         case LayerVoteType::ExplicitExactOrMultiple:
114         case LayerVoteType::Heuristic:
115             if (mConfig.frameRateMultipleThreshold != 0 &&
116                 refreshRate.fps.greaterThanOrEqualWithMargin(
117                         Fps(mConfig.frameRateMultipleThreshold)) &&
118                 layer.desiredRefreshRate.lessThanWithMargin(
119                         Fps(mConfig.frameRateMultipleThreshold / 2))) {
120                 // Don't vote high refresh rates past the threshold for layers with a low desired
121                 // refresh rate. For example, desired 24 fps with 120 Hz threshold means no vote for
122                 // 120 Hz, but desired 60 fps should have a vote.
123                 return false;
124             }
125             break;
126         case LayerVoteType::ExplicitDefault:
127         case LayerVoteType::ExplicitExact:
128         case LayerVoteType::Max:
129         case LayerVoteType::Min:
130         case LayerVoteType::NoVote:
131             break;
132     }
133     return true;
134 }
135 
calculateLayerScoreLocked(const LayerRequirement & layer,const RefreshRate & refreshRate,bool isSeamlessSwitch) const136 float RefreshRateConfigs::calculateLayerScoreLocked(const LayerRequirement& layer,
137                                                     const RefreshRate& refreshRate,
138                                                     bool isSeamlessSwitch) const {
139     if (!isVoteAllowed(layer, refreshRate)) {
140         return 0;
141     }
142 
143     // Slightly prefer seamless switches.
144     constexpr float kSeamedSwitchPenalty = 0.95f;
145     const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty;
146 
147     // If the layer wants Max, give higher score to the higher refresh rate
148     if (layer.vote == LayerVoteType::Max) {
149         const auto ratio =
150                 refreshRate.fps.getValue() / mAppRequestRefreshRates.back()->fps.getValue();
151         // use ratio^2 to get a lower score the more we get further from peak
152         return ratio * ratio;
153     }
154 
155     const auto displayPeriod = refreshRate.getVsyncPeriod();
156     const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs();
157     if (layer.vote == LayerVoteType::ExplicitDefault) {
158         // Find the actual rate the layer will render, assuming
159         // that layerPeriod is the minimal time to render a frame
160         auto actualLayerPeriod = displayPeriod;
161         int multiplier = 1;
162         while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
163             multiplier++;
164             actualLayerPeriod = displayPeriod * multiplier;
165         }
166         return std::min(1.0f,
167                         static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod));
168     }
169 
170     if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
171         layer.vote == LayerVoteType::Heuristic) {
172         // Calculate how many display vsyncs we need to present a single frame for this
173         // layer
174         const auto [displayFramesQuotient, displayFramesRemainder] =
175                 getDisplayFrames(layerPeriod, displayPeriod);
176         static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1
177         if (displayFramesRemainder == 0) {
178             // Layer desired refresh rate matches the display rate.
179             return 1.0f * seamlessness;
180         }
181 
182         if (displayFramesQuotient == 0) {
183             // Layer desired refresh rate is higher than the display rate.
184             return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) *
185                     (1.0f / (MAX_FRAMES_TO_FIT + 1));
186         }
187 
188         // Layer desired refresh rate is lower than the display rate. Check how well it fits
189         // the cadence.
190         auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder));
191         int iter = 2;
192         while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
193             diff = diff - (displayPeriod - diff);
194             iter++;
195         }
196 
197         return (1.0f / iter) * seamlessness;
198     }
199 
200     if (layer.vote == LayerVoteType::ExplicitExact) {
201         const int divider = getFrameRateDivider(refreshRate.getFps(), layer.desiredRefreshRate);
202         if (mSupportsFrameRateOverride) {
203             // Since we support frame rate override, allow refresh rates which are
204             // multiples of the layer's request, as those apps would be throttled
205             // down to run at the desired refresh rate.
206             return divider > 0;
207         }
208 
209         return divider == 1;
210     }
211 
212     return 0;
213 }
214 
215 struct RefreshRateScore {
216     const RefreshRate* refreshRate;
217     float score;
218 };
219 
getBestRefreshRate(const std::vector<LayerRequirement> & layers,const GlobalSignals & globalSignals,GlobalSignals * outSignalsConsidered) const220 RefreshRate RefreshRateConfigs::getBestRefreshRate(const std::vector<LayerRequirement>& layers,
221                                                    const GlobalSignals& globalSignals,
222                                                    GlobalSignals* outSignalsConsidered) const {
223     std::lock_guard lock(mLock);
224 
225     if (auto cached = getCachedBestRefreshRate(layers, globalSignals, outSignalsConsidered)) {
226         return *cached;
227     }
228 
229     GlobalSignals signalsConsidered;
230     RefreshRate result = getBestRefreshRateLocked(layers, globalSignals, &signalsConsidered);
231     lastBestRefreshRateInvocation.emplace(
232             GetBestRefreshRateInvocation{.layerRequirements = layers,
233                                          .globalSignals = globalSignals,
234                                          .outSignalsConsidered = signalsConsidered,
235                                          .resultingBestRefreshRate = result});
236     if (outSignalsConsidered) {
237         *outSignalsConsidered = signalsConsidered;
238     }
239     return result;
240 }
241 
getCachedBestRefreshRate(const std::vector<LayerRequirement> & layers,const GlobalSignals & globalSignals,GlobalSignals * outSignalsConsidered) const242 std::optional<RefreshRate> RefreshRateConfigs::getCachedBestRefreshRate(
243         const std::vector<LayerRequirement>& layers, const GlobalSignals& globalSignals,
244         GlobalSignals* outSignalsConsidered) const {
245     const bool sameAsLastCall = lastBestRefreshRateInvocation &&
246             lastBestRefreshRateInvocation->layerRequirements == layers &&
247             lastBestRefreshRateInvocation->globalSignals == globalSignals;
248 
249     if (sameAsLastCall) {
250         if (outSignalsConsidered) {
251             *outSignalsConsidered = lastBestRefreshRateInvocation->outSignalsConsidered;
252         }
253         return lastBestRefreshRateInvocation->resultingBestRefreshRate;
254     }
255 
256     return {};
257 }
258 
getBestRefreshRateLocked(const std::vector<LayerRequirement> & layers,const GlobalSignals & globalSignals,GlobalSignals * outSignalsConsidered) const259 RefreshRate RefreshRateConfigs::getBestRefreshRateLocked(
260         const std::vector<LayerRequirement>& layers, const GlobalSignals& globalSignals,
261         GlobalSignals* outSignalsConsidered) const {
262     ATRACE_CALL();
263     ALOGV("getBestRefreshRate %zu layers", layers.size());
264 
265     if (outSignalsConsidered) *outSignalsConsidered = {};
266     const auto setTouchConsidered = [&] {
267         if (outSignalsConsidered) {
268             outSignalsConsidered->touch = true;
269         }
270     };
271 
272     const auto setIdleConsidered = [&] {
273         if (outSignalsConsidered) {
274             outSignalsConsidered->idle = true;
275         }
276     };
277 
278     int noVoteLayers = 0;
279     int minVoteLayers = 0;
280     int maxVoteLayers = 0;
281     int explicitDefaultVoteLayers = 0;
282     int explicitExactOrMultipleVoteLayers = 0;
283     int explicitExact = 0;
284     float maxExplicitWeight = 0;
285     int seamedFocusedLayers = 0;
286     for (const auto& layer : layers) {
287         switch (layer.vote) {
288             case LayerVoteType::NoVote:
289                 noVoteLayers++;
290                 break;
291             case LayerVoteType::Min:
292                 minVoteLayers++;
293                 break;
294             case LayerVoteType::Max:
295                 maxVoteLayers++;
296                 break;
297             case LayerVoteType::ExplicitDefault:
298                 explicitDefaultVoteLayers++;
299                 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
300                 break;
301             case LayerVoteType::ExplicitExactOrMultiple:
302                 explicitExactOrMultipleVoteLayers++;
303                 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
304                 break;
305             case LayerVoteType::ExplicitExact:
306                 explicitExact++;
307                 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
308                 break;
309             case LayerVoteType::Heuristic:
310                 break;
311         }
312 
313         if (layer.seamlessness == Seamlessness::SeamedAndSeamless && layer.focused) {
314             seamedFocusedLayers++;
315         }
316     }
317 
318     const bool hasExplicitVoteLayers = explicitDefaultVoteLayers > 0 ||
319             explicitExactOrMultipleVoteLayers > 0 || explicitExact > 0;
320 
321     // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
322     // selected a refresh rate to see if we should apply touch boost.
323     if (globalSignals.touch && !hasExplicitVoteLayers) {
324         ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str());
325         setTouchConsidered();
326         return getMaxRefreshRateByPolicyLocked();
327     }
328 
329     // If the primary range consists of a single refresh rate then we can only
330     // move out the of range if layers explicitly request a different refresh
331     // rate.
332     const Policy* policy = getCurrentPolicyLocked();
333     const bool primaryRangeIsSingleRate =
334             policy->primaryRange.min.equalsWithMargin(policy->primaryRange.max);
335 
336     if (!globalSignals.touch && globalSignals.idle &&
337         !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
338         ALOGV("Idle - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
339         setIdleConsidered();
340         return getMinRefreshRateByPolicyLocked();
341     }
342 
343     if (layers.empty() || noVoteLayers == layers.size()) {
344         return getMaxRefreshRateByPolicyLocked();
345     }
346 
347     // Only if all layers want Min we should return Min
348     if (noVoteLayers + minVoteLayers == layers.size()) {
349         ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
350         return getMinRefreshRateByPolicyLocked();
351     }
352 
353     // Find the best refresh rate based on score
354     std::vector<RefreshRateScore> scores;
355     scores.reserve(mAppRequestRefreshRates.size());
356 
357     for (const auto refreshRate : mAppRequestRefreshRates) {
358         scores.emplace_back(RefreshRateScore{refreshRate, 0.0f});
359     }
360 
361     const auto& defaultMode = mRefreshRates.at(policy->defaultMode);
362 
363     for (const auto& layer : layers) {
364         ALOGV("Calculating score for %s (%s, weight %.2f, desired %.2f) ", layer.name.c_str(),
365               layerVoteTypeString(layer.vote).c_str(), layer.weight,
366               layer.desiredRefreshRate.getValue());
367         if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
368             continue;
369         }
370 
371         auto weight = layer.weight;
372 
373         for (auto i = 0u; i < scores.size(); i++) {
374             const bool isSeamlessSwitch =
375                     scores[i].refreshRate->getModeGroup() == mCurrentRefreshRate->getModeGroup();
376 
377             if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) {
378                 ALOGV("%s ignores %s to avoid non-seamless switch. Current mode = %s",
379                       formatLayerInfo(layer, weight).c_str(),
380                       scores[i].refreshRate->toString().c_str(),
381                       mCurrentRefreshRate->toString().c_str());
382                 continue;
383             }
384 
385             if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch &&
386                 !layer.focused) {
387                 ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed."
388                       " Current mode = %s",
389                       formatLayerInfo(layer, weight).c_str(),
390                       scores[i].refreshRate->toString().c_str(),
391                       mCurrentRefreshRate->toString().c_str());
392                 continue;
393             }
394 
395             // Layers with default seamlessness vote for the current mode group if
396             // there are layers with seamlessness=SeamedAndSeamless and for the default
397             // mode group otherwise. In second case, if the current mode group is different
398             // from the default, this means a layer with seamlessness=SeamedAndSeamless has just
399             // disappeared.
400             const bool isInPolicyForDefault = seamedFocusedLayers > 0
401                     ? scores[i].refreshRate->getModeGroup() == mCurrentRefreshRate->getModeGroup()
402                     : scores[i].refreshRate->getModeGroup() == defaultMode->getModeGroup();
403 
404             if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault) {
405                 ALOGV("%s ignores %s. Current mode = %s", formatLayerInfo(layer, weight).c_str(),
406                       scores[i].refreshRate->toString().c_str(),
407                       mCurrentRefreshRate->toString().c_str());
408                 continue;
409             }
410 
411             bool inPrimaryRange = scores[i].refreshRate->inPolicy(policy->primaryRange.min,
412                                                                   policy->primaryRange.max);
413             if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
414                 !(layer.focused &&
415                   (layer.vote == LayerVoteType::ExplicitDefault ||
416                    layer.vote == LayerVoteType::ExplicitExact))) {
417                 // Only focused layers with ExplicitDefault frame rate settings are allowed to score
418                 // refresh rates outside the primary range.
419                 continue;
420             }
421 
422             const auto layerScore =
423                     calculateLayerScoreLocked(layer, *scores[i].refreshRate, isSeamlessSwitch);
424             ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(),
425                   scores[i].refreshRate->getName().c_str(), layerScore);
426             scores[i].score += weight * layerScore;
427         }
428     }
429 
430     // Now that we scored all the refresh rates we need to pick the one that got the highest score.
431     // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
432     // or the lower otherwise.
433     const RefreshRate* bestRefreshRate = maxVoteLayers > 0
434             ? getBestRefreshRate(scores.rbegin(), scores.rend())
435             : getBestRefreshRate(scores.begin(), scores.end());
436 
437     if (primaryRangeIsSingleRate) {
438         // If we never scored any layers, then choose the rate from the primary
439         // range instead of picking a random score from the app range.
440         if (std::all_of(scores.begin(), scores.end(),
441                         [](RefreshRateScore score) { return score.score == 0; })) {
442             ALOGV("layers not scored - choose %s",
443                   getMaxRefreshRateByPolicyLocked().getName().c_str());
444             return getMaxRefreshRateByPolicyLocked();
445         } else {
446             return *bestRefreshRate;
447         }
448     }
449 
450     // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
451     // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
452     // vote we should not change it if we get a touch event. Only apply touch boost if it will
453     // actually increase the refresh rate over the normal selection.
454     const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
455 
456     const bool touchBoostForExplicitExact = [&] {
457         if (mSupportsFrameRateOverride) {
458             // Enable touch boost if there are other layers besides exact
459             return explicitExact + noVoteLayers != layers.size();
460         } else {
461             // Enable touch boost if there are no exact layers
462             return explicitExact == 0;
463         }
464     }();
465     if (globalSignals.touch && explicitDefaultVoteLayers == 0 && touchBoostForExplicitExact &&
466         bestRefreshRate->fps.lessThanWithMargin(touchRefreshRate.fps)) {
467         setTouchConsidered();
468         ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str());
469         return touchRefreshRate;
470     }
471 
472     return *bestRefreshRate;
473 }
474 
475 std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>>
groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement> & layers)476 groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) {
477     std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid;
478     for (const auto& layer : layers) {
479         auto iter = layersByUid.emplace(layer.ownerUid,
480                                         std::vector<const RefreshRateConfigs::LayerRequirement*>());
481         auto& layersWithSameUid = iter.first->second;
482         layersWithSameUid.push_back(&layer);
483     }
484 
485     // Remove uids that can't have a frame rate override
486     for (auto iter = layersByUid.begin(); iter != layersByUid.end();) {
487         const auto& layersWithSameUid = iter->second;
488         bool skipUid = false;
489         for (const auto& layer : layersWithSameUid) {
490             if (layer->vote == RefreshRateConfigs::LayerVoteType::Max ||
491                 layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) {
492                 skipUid = true;
493                 break;
494             }
495         }
496         if (skipUid) {
497             iter = layersByUid.erase(iter);
498         } else {
499             ++iter;
500         }
501     }
502 
503     return layersByUid;
504 }
505 
initializeScoresForAllRefreshRates(const AllRefreshRatesMapType & refreshRates)506 std::vector<RefreshRateScore> initializeScoresForAllRefreshRates(
507         const AllRefreshRatesMapType& refreshRates) {
508     std::vector<RefreshRateScore> scores;
509     scores.reserve(refreshRates.size());
510     for (const auto& [ignored, refreshRate] : refreshRates) {
511         scores.emplace_back(RefreshRateScore{refreshRate.get(), 0.0f});
512     }
513     std::sort(scores.begin(), scores.end(),
514               [](const auto& a, const auto& b) { return *a.refreshRate < *b.refreshRate; });
515     return scores;
516 }
517 
getFrameRateOverrides(const std::vector<LayerRequirement> & layers,Fps displayFrameRate,bool touch) const518 RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides(
519         const std::vector<LayerRequirement>& layers, Fps displayFrameRate, bool touch) const {
520     ATRACE_CALL();
521     if (!mSupportsFrameRateOverride) return {};
522 
523     ALOGV("getFrameRateOverrides %zu layers", layers.size());
524     std::lock_guard lock(mLock);
525     std::vector<RefreshRateScore> scores = initializeScoresForAllRefreshRates(mRefreshRates);
526     std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid =
527             groupLayersByUid(layers);
528     UidToFrameRateOverride frameRateOverrides;
529     for (const auto& [uid, layersWithSameUid] : layersByUid) {
530         // Layers with ExplicitExactOrMultiple expect touch boost
531         const bool hasExplicitExactOrMultiple =
532                 std::any_of(layersWithSameUid.cbegin(), layersWithSameUid.cend(),
533                             [](const auto& layer) {
534                                 return layer->vote == LayerVoteType::ExplicitExactOrMultiple;
535                             });
536 
537         if (touch && hasExplicitExactOrMultiple) {
538             continue;
539         }
540 
541         for (auto& score : scores) {
542             score.score = 0;
543         }
544 
545         for (const auto& layer : layersWithSameUid) {
546             if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) {
547                 continue;
548             }
549 
550             LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault &&
551                                 layer->vote != LayerVoteType::ExplicitExactOrMultiple &&
552                                 layer->vote != LayerVoteType::ExplicitExact);
553             for (RefreshRateScore& score : scores) {
554                 const auto layerScore = calculateLayerScoreLocked(*layer, *score.refreshRate,
555                                                                   /*isSeamlessSwitch*/ true);
556                 score.score += layer->weight * layerScore;
557             }
558         }
559 
560         // We just care about the refresh rates which are a divider of the
561         // display refresh rate
562         auto iter =
563                 std::remove_if(scores.begin(), scores.end(), [&](const RefreshRateScore& score) {
564                     return getFrameRateDivider(displayFrameRate, score.refreshRate->getFps()) == 0;
565                 });
566         scores.erase(iter, scores.end());
567 
568         // If we never scored any layers, we don't have a preferred frame rate
569         if (std::all_of(scores.begin(), scores.end(),
570                         [](const RefreshRateScore& score) { return score.score == 0; })) {
571             continue;
572         }
573 
574         // Now that we scored all the refresh rates we need to pick the one that got the highest
575         // score.
576         const RefreshRate* bestRefreshRate = getBestRefreshRate(scores.begin(), scores.end());
577         frameRateOverrides.emplace(uid, bestRefreshRate->getFps());
578     }
579 
580     return frameRateOverrides;
581 }
582 
583 template <typename Iter>
getBestRefreshRate(Iter begin,Iter end) const584 const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
585     constexpr auto EPSILON = 0.001f;
586     const RefreshRate* bestRefreshRate = begin->refreshRate;
587     float max = begin->score;
588     for (auto i = begin; i != end; ++i) {
589         const auto [refreshRate, score] = *i;
590         ALOGV("%s scores %.2f", refreshRate->getName().c_str(), score);
591 
592         ATRACE_INT(refreshRate->getName().c_str(), round<int>(score * 100));
593 
594         if (score > max * (1 + EPSILON)) {
595             max = score;
596             bestRefreshRate = refreshRate;
597         }
598     }
599 
600     return bestRefreshRate;
601 }
602 
onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveConfigId,bool timerExpired) const603 std::optional<Fps> RefreshRateConfigs::onKernelTimerChanged(
604         std::optional<DisplayModeId> desiredActiveConfigId, bool timerExpired) const {
605     std::lock_guard lock(mLock);
606 
607     const auto& current = desiredActiveConfigId ? *mRefreshRates.at(*desiredActiveConfigId)
608                                                 : *mCurrentRefreshRate;
609     const auto& min = *mMinSupportedRefreshRate;
610 
611     if (current != min) {
612         const auto& refreshRate = timerExpired ? min : current;
613         return refreshRate.getFps();
614     }
615 
616     return {};
617 }
618 
getMinRefreshRateByPolicyLocked() const619 const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
620     for (auto refreshRate : mPrimaryRefreshRates) {
621         if (mCurrentRefreshRate->getModeGroup() == refreshRate->getModeGroup()) {
622             return *refreshRate;
623         }
624     }
625     ALOGE("Can't find min refresh rate by policy with the same mode group"
626           " as the current mode %s",
627           mCurrentRefreshRate->toString().c_str());
628     // Defaulting to the lowest refresh rate
629     return *mPrimaryRefreshRates.front();
630 }
631 
getMaxRefreshRateByPolicy() const632 RefreshRate RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
633     std::lock_guard lock(mLock);
634     return getMaxRefreshRateByPolicyLocked();
635 }
636 
getMaxRefreshRateByPolicyLocked() const637 const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
638     for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); it++) {
639         const auto& refreshRate = (**it);
640         if (mCurrentRefreshRate->getModeGroup() == refreshRate.getModeGroup()) {
641             return refreshRate;
642         }
643     }
644     ALOGE("Can't find max refresh rate by policy with the same mode group"
645           " as the current mode %s",
646           mCurrentRefreshRate->toString().c_str());
647     // Defaulting to the highest refresh rate
648     return *mPrimaryRefreshRates.back();
649 }
650 
getCurrentRefreshRate() const651 RefreshRate RefreshRateConfigs::getCurrentRefreshRate() const {
652     std::lock_guard lock(mLock);
653     return *mCurrentRefreshRate;
654 }
655 
getCurrentRefreshRateByPolicy() const656 RefreshRate RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
657     std::lock_guard lock(mLock);
658     return getCurrentRefreshRateByPolicyLocked();
659 }
660 
getCurrentRefreshRateByPolicyLocked() const661 const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
662     if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
663                   mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
664         return *mCurrentRefreshRate;
665     }
666     return *mRefreshRates.at(getCurrentPolicyLocked()->defaultMode);
667 }
668 
setCurrentModeId(DisplayModeId modeId)669 void RefreshRateConfigs::setCurrentModeId(DisplayModeId modeId) {
670     std::lock_guard lock(mLock);
671 
672     // Invalidate the cached invocation to getBestRefreshRate. This forces
673     // the refresh rate to be recomputed on the next call to getBestRefreshRate.
674     lastBestRefreshRateInvocation.reset();
675 
676     mCurrentRefreshRate = mRefreshRates.at(modeId).get();
677 }
678 
RefreshRateConfigs(const DisplayModes & modes,DisplayModeId currentModeId,Config config)679 RefreshRateConfigs::RefreshRateConfigs(const DisplayModes& modes, DisplayModeId currentModeId,
680                                        Config config)
681       : mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) {
682     updateDisplayModes(modes, currentModeId);
683 }
684 
updateDisplayModes(const DisplayModes & modes,DisplayModeId currentModeId)685 void RefreshRateConfigs::updateDisplayModes(const DisplayModes& modes,
686                                             DisplayModeId currentModeId) {
687     std::lock_guard lock(mLock);
688 
689     // The current mode should be supported
690     LOG_ALWAYS_FATAL_IF(std::none_of(modes.begin(), modes.end(), [&](DisplayModePtr mode) {
691         return mode->getId() == currentModeId;
692     }));
693 
694     // Invalidate the cached invocation to getBestRefreshRate. This forces
695     // the refresh rate to be recomputed on the next call to getBestRefreshRate.
696     lastBestRefreshRateInvocation.reset();
697 
698     mRefreshRates.clear();
699     for (const auto& mode : modes) {
700         const auto modeId = mode->getId();
701         mRefreshRates.emplace(modeId,
702                               std::make_unique<RefreshRate>(modeId, mode, mode->getFps(),
703                                                             RefreshRate::ConstructorTag(0)));
704         if (modeId == currentModeId) {
705             mCurrentRefreshRate = mRefreshRates.at(modeId).get();
706         }
707     }
708 
709     std::vector<const RefreshRate*> sortedModes;
710     getSortedRefreshRateListLocked([](const RefreshRate&) { return true; }, &sortedModes);
711     // Reset the policy because the old one may no longer be valid.
712     mDisplayManagerPolicy = {};
713     mDisplayManagerPolicy.defaultMode = currentModeId;
714     mMinSupportedRefreshRate = sortedModes.front();
715     mMaxSupportedRefreshRate = sortedModes.back();
716 
717     mSupportsFrameRateOverride = false;
718     if (mConfig.enableFrameRateOverride) {
719         for (const auto& mode1 : sortedModes) {
720             for (const auto& mode2 : sortedModes) {
721                 if (getFrameRateDivider(mode1->getFps(), mode2->getFps()) >= 2) {
722                     mSupportsFrameRateOverride = true;
723                     break;
724                 }
725             }
726         }
727     }
728 
729     constructAvailableRefreshRates();
730 }
731 
isPolicyValidLocked(const Policy & policy) const732 bool RefreshRateConfigs::isPolicyValidLocked(const Policy& policy) const {
733     // defaultMode must be a valid mode, and within the given refresh rate range.
734     auto iter = mRefreshRates.find(policy.defaultMode);
735     if (iter == mRefreshRates.end()) {
736         ALOGE("Default mode is not found.");
737         return false;
738     }
739     const RefreshRate& refreshRate = *iter->second;
740     if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
741         ALOGE("Default mode is not in the primary range.");
742         return false;
743     }
744     return policy.appRequestRange.min.lessThanOrEqualWithMargin(policy.primaryRange.min) &&
745             policy.appRequestRange.max.greaterThanOrEqualWithMargin(policy.primaryRange.max);
746 }
747 
setDisplayManagerPolicy(const Policy & policy)748 status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
749     std::lock_guard lock(mLock);
750     if (!isPolicyValidLocked(policy)) {
751         ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str());
752         return BAD_VALUE;
753     }
754     lastBestRefreshRateInvocation.reset();
755     Policy previousPolicy = *getCurrentPolicyLocked();
756     mDisplayManagerPolicy = policy;
757     if (*getCurrentPolicyLocked() == previousPolicy) {
758         return CURRENT_POLICY_UNCHANGED;
759     }
760     constructAvailableRefreshRates();
761     return NO_ERROR;
762 }
763 
setOverridePolicy(const std::optional<Policy> & policy)764 status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
765     std::lock_guard lock(mLock);
766     if (policy && !isPolicyValidLocked(*policy)) {
767         return BAD_VALUE;
768     }
769     lastBestRefreshRateInvocation.reset();
770     Policy previousPolicy = *getCurrentPolicyLocked();
771     mOverridePolicy = policy;
772     if (*getCurrentPolicyLocked() == previousPolicy) {
773         return CURRENT_POLICY_UNCHANGED;
774     }
775     constructAvailableRefreshRates();
776     return NO_ERROR;
777 }
778 
getCurrentPolicyLocked() const779 const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
780     return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
781 }
782 
getCurrentPolicy() const783 RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
784     std::lock_guard lock(mLock);
785     return *getCurrentPolicyLocked();
786 }
787 
getDisplayManagerPolicy() const788 RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
789     std::lock_guard lock(mLock);
790     return mDisplayManagerPolicy;
791 }
792 
isModeAllowed(DisplayModeId modeId) const793 bool RefreshRateConfigs::isModeAllowed(DisplayModeId modeId) const {
794     std::lock_guard lock(mLock);
795     for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
796         if (refreshRate->modeId == modeId) {
797             return true;
798         }
799     }
800     return false;
801 }
802 
getSortedRefreshRateListLocked(const std::function<bool (const RefreshRate &)> & shouldAddRefreshRate,std::vector<const RefreshRate * > * outRefreshRates)803 void RefreshRateConfigs::getSortedRefreshRateListLocked(
804         const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
805         std::vector<const RefreshRate*>* outRefreshRates) {
806     outRefreshRates->clear();
807     outRefreshRates->reserve(mRefreshRates.size());
808     for (const auto& [type, refreshRate] : mRefreshRates) {
809         if (shouldAddRefreshRate(*refreshRate)) {
810             ALOGV("getSortedRefreshRateListLocked: mode %d added to list policy",
811                   refreshRate->modeId.value());
812             outRefreshRates->push_back(refreshRate.get());
813         }
814     }
815 
816     std::sort(outRefreshRates->begin(), outRefreshRates->end(),
817               [](const auto refreshRate1, const auto refreshRate2) {
818                   if (refreshRate1->mode->getVsyncPeriod() !=
819                       refreshRate2->mode->getVsyncPeriod()) {
820                       return refreshRate1->mode->getVsyncPeriod() >
821                               refreshRate2->mode->getVsyncPeriod();
822                   } else {
823                       return refreshRate1->mode->getGroup() > refreshRate2->mode->getGroup();
824                   }
825               });
826 }
827 
constructAvailableRefreshRates()828 void RefreshRateConfigs::constructAvailableRefreshRates() {
829     // Filter modes based on current policy and sort based on vsync period
830     const Policy* policy = getCurrentPolicyLocked();
831     const auto& defaultMode = mRefreshRates.at(policy->defaultMode)->mode;
832     ALOGV("constructAvailableRefreshRates: %s ", policy->toString().c_str());
833 
834     auto filterRefreshRates =
835             [&](Fps min, Fps max, const char* listName,
836                 std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock) {
837                 getSortedRefreshRateListLocked(
838                         [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
839                             const auto& mode = refreshRate.mode;
840 
841                             return mode->getHeight() == defaultMode->getHeight() &&
842                                     mode->getWidth() == defaultMode->getWidth() &&
843                                     mode->getDpiX() == defaultMode->getDpiX() &&
844                                     mode->getDpiY() == defaultMode->getDpiY() &&
845                                     (policy->allowGroupSwitching ||
846                                      mode->getGroup() == defaultMode->getGroup()) &&
847                                     refreshRate.inPolicy(min, max);
848                         },
849                         outRefreshRates);
850 
851                 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
852                                     "No matching modes for %s range: min=%s max=%s", listName,
853                                     to_string(min).c_str(), to_string(max).c_str());
854                 auto stringifyRefreshRates = [&]() -> std::string {
855                     std::string str;
856                     for (auto refreshRate : *outRefreshRates) {
857                         base::StringAppendF(&str, "%s ", refreshRate->getName().c_str());
858                     }
859                     return str;
860                 };
861                 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
862             };
863 
864     filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
865                        &mPrimaryRefreshRates);
866     filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
867                        &mAppRequestRefreshRates);
868 }
869 
findClosestKnownFrameRate(Fps frameRate) const870 Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const {
871     if (frameRate.lessThanOrEqualWithMargin(*mKnownFrameRates.begin())) {
872         return *mKnownFrameRates.begin();
873     }
874 
875     if (frameRate.greaterThanOrEqualWithMargin(*std::prev(mKnownFrameRates.end()))) {
876         return *std::prev(mKnownFrameRates.end());
877     }
878 
879     auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
880                                        Fps::comparesLess);
881 
882     const auto distance1 = std::abs((frameRate.getValue() - lowerBound->getValue()));
883     const auto distance2 = std::abs((frameRate.getValue() - std::prev(lowerBound)->getValue()));
884     return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
885 }
886 
getIdleTimerAction() const887 RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const {
888     std::lock_guard lock(mLock);
889     const auto& deviceMin = *mMinSupportedRefreshRate;
890     const auto& minByPolicy = getMinRefreshRateByPolicyLocked();
891     const auto& maxByPolicy = getMaxRefreshRateByPolicyLocked();
892     const auto& currentPolicy = getCurrentPolicyLocked();
893 
894     // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that
895     // the min allowed refresh rate is higher than the device min, we do not want to enable the
896     // timer.
897     if (deviceMin < minByPolicy) {
898         return RefreshRateConfigs::KernelIdleTimerAction::TurnOff;
899     }
900     if (minByPolicy == maxByPolicy) {
901         // when min primary range in display manager policy is below device min turn on the timer.
902         if (currentPolicy->primaryRange.min.lessThanWithMargin(deviceMin.getFps())) {
903             return RefreshRateConfigs::KernelIdleTimerAction::TurnOn;
904         }
905         return RefreshRateConfigs::KernelIdleTimerAction::TurnOff;
906     }
907     // Turn on the timer in all other cases.
908     return RefreshRateConfigs::KernelIdleTimerAction::TurnOn;
909 }
910 
getFrameRateDivider(Fps displayFrameRate,Fps layerFrameRate)911 int RefreshRateConfigs::getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate) {
912     // This calculation needs to be in sync with the java code
913     // in DisplayManagerService.getDisplayInfoForFrameRateOverride
914     constexpr float kThreshold = 0.1f;
915     const auto numPeriods = displayFrameRate.getValue() / layerFrameRate.getValue();
916     const auto numPeriodsRounded = std::round(numPeriods);
917     if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
918         return 0;
919     }
920 
921     return static_cast<int>(numPeriodsRounded);
922 }
923 
dump(std::string & result) const924 void RefreshRateConfigs::dump(std::string& result) const {
925     std::lock_guard lock(mLock);
926     base::StringAppendF(&result, "DesiredDisplayModeSpecs (DisplayManager): %s\n\n",
927                         mDisplayManagerPolicy.toString().c_str());
928     scheduler::RefreshRateConfigs::Policy currentPolicy = *getCurrentPolicyLocked();
929     if (mOverridePolicy && currentPolicy != mDisplayManagerPolicy) {
930         base::StringAppendF(&result, "DesiredDisplayModeSpecs (Override): %s\n\n",
931                             currentPolicy.toString().c_str());
932     }
933 
934     auto mode = mCurrentRefreshRate->mode;
935     base::StringAppendF(&result, "Current mode: %s\n", mCurrentRefreshRate->toString().c_str());
936 
937     result.append("Refresh rates:\n");
938     for (const auto& [id, refreshRate] : mRefreshRates) {
939         mode = refreshRate->mode;
940         base::StringAppendF(&result, "\t%s\n", refreshRate->toString().c_str());
941     }
942 
943     base::StringAppendF(&result, "Supports Frame Rate Override: %s\n",
944                         mSupportsFrameRateOverride ? "yes" : "no");
945     result.append("\n");
946 }
947 
948 } // namespace android::scheduler
949 
950 // TODO(b/129481165): remove the #pragma below and fix conversion issues
951 #pragma clang diagnostic pop // ignored "-Wextra"
952