blob: a819b7979ffa77409d9d024643b7db6496b03cdf [file] [log] [blame]
Ady Abraham8a82ba62020-01-17 12:43:17 -08001/*
2 * Copyright 2020 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#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010018#define LOG_TAG "LayerHistory"
Ady Abraham8a82ba62020-01-17 12:43:17 -080019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21#include "LayerHistory.h"
22
Dominik Laskowski983f2b52020-06-25 16:54:06 -070023#include <android-base/stringprintf.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080024#include <cutils/properties.h>
Ady Abraham73c3df52023-01-12 18:09:31 -080025#include <gui/TraceUtils.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080026#include <utils/Log.h>
27#include <utils/Timers.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080028
29#include <algorithm>
30#include <cmath>
31#include <string>
32#include <utility>
33
Alec Mouri9b133ca2023-11-14 19:00:01 +000034#include <common/FlagManager.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080035#include "../Layer.h"
Rachel Lee2248f522023-01-27 16:45:23 -080036#include "EventThread.h"
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010037#include "LayerInfo.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080038
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010039namespace android::scheduler {
Ady Abraham8a82ba62020-01-17 12:43:17 -080040
41namespace {
42
Rachel Lee45681982024-03-14 18:40:15 -070043bool isLayerActive(const LayerInfo& info, nsecs_t threshold, bool isVrrDevice) {
Ady Abrahamd6d80162023-10-23 12:57:41 -070044 if (FlagManager::getInstance().misc1() && !info.isVisible()) {
Rachel Lee414f4082023-10-23 14:48:44 -070045 return false;
46 }
47
48 // Layers with an explicit frame rate or frame rate category are kept active,
Rachel Leed0694bc2023-09-12 14:57:58 -070049 // but ignore NoVote.
Rachel Lee45681982024-03-14 18:40:15 -070050 const auto frameRate = info.getSetFrameRateVote();
51 if (frameRate.isValid() && !frameRate.isNoVote() && frameRate.isVoteValidForMrr(isVrrDevice)) {
Ady Abraham89089c92020-04-15 18:24:41 -070052 return true;
Ady Abraham8a82ba62020-01-17 12:43:17 -080053 }
Ady Abraham89089c92020-04-15 18:24:41 -070054
Alec Mouri89f5d4e2023-10-20 17:12:49 +000055 // Make all front buffered layers active
56 if (FlagManager::getInstance().vrr_config() && info.isFrontBuffered() && info.isVisible()) {
57 return true;
58 }
59
Ady Abrahambdda8f02021-04-01 16:06:11 -070060 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
Ady Abraham8a82ba62020-01-17 12:43:17 -080061}
62
63bool traceEnabled() {
64 return property_get_bool("debug.sf.layer_history_trace", false);
65}
66
67bool useFrameRatePriority() {
68 char value[PROPERTY_VALUE_MAX];
69 property_get("debug.sf.use_frame_rate_priority", value, "1");
70 return atoi(value);
71}
72
Ady Abrahambdda8f02021-04-01 16:06:11 -070073void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070074 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
75 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070076 };
77
Ady Abraham0ccd79b2020-06-10 10:11:17 -070078 traceType(LayerHistory::LayerVoteType::NoVote, 1);
79 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
80 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
81 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
Ady Abrahamdd5bfa92021-01-07 17:56:08 -080082 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
Ady Abraham0ccd79b2020-06-10 10:11:17 -070083 traceType(LayerHistory::LayerVoteType::Min, 1);
84 traceType(LayerHistory::LayerVoteType::Max, 1);
Rachel Leece6e0042023-06-27 11:22:54 -070085 traceType(LayerHistory::LayerVoteType::ExplicitCategory, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080086
Ady Abrahambdda8f02021-04-01 16:06:11 -070087 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080088}
Andy Labrada096227e2022-06-15 16:58:11 +000089
Vishnu Nair3fbe3262023-09-29 17:07:00 -070090LayerHistory::LayerVoteType getVoteType(FrameRateCompatibility compatibility,
Andy Labrada096227e2022-06-15 16:58:11 +000091 bool contentDetectionEnabled) {
92 LayerHistory::LayerVoteType voteType;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070093 if (!contentDetectionEnabled || compatibility == FrameRateCompatibility::NoVote) {
Andy Labrada096227e2022-06-15 16:58:11 +000094 voteType = LayerHistory::LayerVoteType::NoVote;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070095 } else if (compatibility == FrameRateCompatibility::Min) {
Andy Labrada096227e2022-06-15 16:58:11 +000096 voteType = LayerHistory::LayerVoteType::Min;
97 } else {
98 voteType = LayerHistory::LayerVoteType::Heuristic;
99 }
100 return voteType;
101}
102
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103} // namespace
104
Ady Abraham3efa3942021-06-24 19:01:25 -0700105LayerHistory::LayerHistory()
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700106 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100107 LayerInfo::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700108}
109
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100110LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800111
Andy Labrada096227e2022-06-15 16:58:11 +0000112void LayerHistory::registerLayer(Layer* layer, bool contentDetectionEnabled) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800113 std::lock_guard lock(mLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800114 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first != LayerStatus::NotFound,
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400115 "%s already registered", layer->getName().c_str());
Andy Labrada096227e2022-06-15 16:58:11 +0000116 LayerVoteType type =
117 getVoteType(layer->getDefaultFrameRateCompatibility(), contentDetectionEnabled);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700118 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400119
120 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
121 // to correct them.
122 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123}
124
Ady Abrahambdda8f02021-04-01 16:06:11 -0700125void LayerHistory::deregisterLayer(Layer* layer) {
126 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400127 if (!mActiveLayerInfos.erase(layer->getSequence())) {
128 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
129 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
130 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700131 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700132}
133
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000134void LayerHistory::record(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
135 nsecs_t now, LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800136 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400137 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800138 if (found == LayerStatus::NotFound) {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700139 // Offscreen layer
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000140 ALOGV("%s: %d not registered", __func__, id);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700141 return;
142 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800143
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400144 const auto& info = layerPair->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700145 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800146
147 // Activate layer if inactive.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800148 if (found == LayerStatus::LayerInInactiveMap) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400149 mActiveLayerInfos.insert(
150 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
151 mInactiveLayerInfos.erase(id);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152 }
153}
154
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700155void LayerHistory::setDefaultFrameRateCompatibility(int32_t id,
156 FrameRateCompatibility frameRateCompatibility,
157 bool contentDetectionEnabled) {
Andy Labrada096227e2022-06-15 16:58:11 +0000158 std::lock_guard lock(mLock);
Andy Labrada096227e2022-06-15 16:58:11 +0000159
160 auto [found, layerPair] = findLayer(id);
161 if (found == LayerStatus::NotFound) {
162 // Offscreen layer
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700163 ALOGV("%s: %d not registered", __func__, id);
Andy Labrada096227e2022-06-15 16:58:11 +0000164 return;
165 }
166
167 const auto& info = layerPair->second;
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700168 info->setDefaultLayerVote(getVoteType(frameRateCompatibility, contentDetectionEnabled));
Andy Labrada096227e2022-06-15 16:58:11 +0000169}
170
Vishnu Nair41376b62023-11-08 05:08:58 -0800171void LayerHistory::setLayerProperties(int32_t id, const LayerProps& properties) {
172 std::lock_guard lock(mLock);
173
174 auto [found, layerPair] = findLayer(id);
175 if (found == LayerStatus::NotFound) {
176 // Offscreen layer
177 ALOGV("%s: %d not registered", __func__, id);
178 return;
179 }
180
181 const auto& info = layerPair->second;
182 info->setProperties(properties);
183
184 // Activate layer if inactive and visible.
185 if (found == LayerStatus::LayerInInactiveMap && info->isVisible()) {
186 mActiveLayerInfos.insert(
187 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
188 mInactiveLayerInfos.erase(id);
189 }
190}
191
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400192auto LayerHistory::summarize(const RefreshRateSelector& selector, nsecs_t now) -> Summary {
Ady Abraham73c3df52023-01-12 18:09:31 -0800193 ATRACE_CALL();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800194 Summary summary;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800195
196 std::lock_guard lock(mLock);
197
Rachel Lee45681982024-03-14 18:40:15 -0700198 partitionLayers(now, selector.isVrrDevice());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400200 for (const auto& [key, value] : mActiveLayerInfos) {
201 auto& info = value.second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700202 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700203 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700204 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
205 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800206
Ady Abraham73c3df52023-01-12 18:09:31 -0800207 ATRACE_FORMAT("%s", info->getName().c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700208 const auto votes = info->getRefreshRateVote(selector, now);
209 for (LayerInfo::LayerVote vote : votes) {
210 if (vote.isNoVote()) {
211 continue;
212 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800213
Rachel Leece6e0042023-06-27 11:22:54 -0700214 // Compute the layer's position on the screen
215 const Rect bounds = Rect(info->getBounds());
216 const ui::Transform transform = info->getTransform();
217 constexpr bool roundOutwards = true;
218 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219
Rachel Leece6e0042023-06-27 11:22:54 -0700220 const float layerArea = transformed.getWidth() * transformed.getHeight();
221 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
222 const std::string categoryString = vote.category == FrameRateCategory::Default
Rachel Lee94ff7992023-08-31 15:15:22 -0700223 ? ""
224 : base::StringPrintf("category=%s", ftl::enum_string(vote.category).c_str());
Ady Abrahamf380cf92024-02-06 18:37:44 -0800225 ATRACE_FORMAT_INSTANT("%s %s %s (%.2f)", ftl::enum_string(vote.type).c_str(),
226 to_string(vote.fps).c_str(), categoryString.c_str(), weight);
Rachel Leece6e0042023-06-27 11:22:54 -0700227 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Rachel Lee67afbea2023-09-28 15:35:07 -0700228 vote.seamlessness, vote.category, vote.categorySmoothSwitchOnly,
229 weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800230
Rachel Leece6e0042023-06-27 11:22:54 -0700231 if (CC_UNLIKELY(mTraceEnabled)) {
232 trace(*info, vote.type, vote.fps.getIntValue());
233 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800234 }
235 }
236
237 return summary;
238}
239
Rachel Lee45681982024-03-14 18:40:15 -0700240void LayerHistory::partitionLayers(nsecs_t now, bool isVrrDevice) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800241 ATRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800242 const nsecs_t threshold = getActiveLayerThreshold(now);
243
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400244 // iterate over inactive map
245 LayerInfos::iterator it = mInactiveLayerInfos.begin();
246 while (it != mInactiveLayerInfos.end()) {
247 auto& [layerUnsafe, info] = it->second;
Rachel Lee45681982024-03-14 18:40:15 -0700248 if (isLayerActive(*info, threshold, isVrrDevice)) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400249 // move this to the active map
250
251 mActiveLayerInfos.insert({it->first, std::move(it->second)});
252 it = mInactiveLayerInfos.erase(it);
253 } else {
254 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800255 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400256 }
257 info->onLayerInactive(now);
258 it++;
259 }
260 }
261
262 // iterate over active map
263 it = mActiveLayerInfos.begin();
264 while (it != mActiveLayerInfos.end()) {
265 auto& [layerUnsafe, info] = it->second;
Rachel Lee45681982024-03-14 18:40:15 -0700266 if (isLayerActive(*info, threshold, isVrrDevice)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800267 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700268 const auto frameRate = info->getSetFrameRateVote();
Andy Yu8c2703d2023-11-03 11:22:46 -0700269
Ady Abraham71c437d2020-01-31 15:56:57 -0800270 const auto voteType = [&]() {
Rachel Leece6e0042023-06-27 11:22:54 -0700271 switch (frameRate.vote.type) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800272 case Layer::FrameRateCompatibility::Default:
273 return LayerVoteType::ExplicitDefault;
Andy Labrada096227e2022-06-15 16:58:11 +0000274 case Layer::FrameRateCompatibility::Min:
275 return LayerVoteType::Min;
Ady Abraham71c437d2020-01-31 15:56:57 -0800276 case Layer::FrameRateCompatibility::ExactOrMultiple:
277 return LayerVoteType::ExplicitExactOrMultiple;
278 case Layer::FrameRateCompatibility::NoVote:
279 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800280 case Layer::FrameRateCompatibility::Exact:
281 return LayerVoteType::ExplicitExact;
Rachel Leee5514a72023-10-25 16:20:29 -0700282 case Layer::FrameRateCompatibility::Gte:
Rachel Lee8f2eea42024-04-19 15:15:48 -0700283 if (isVrrDevice) {
284 return LayerVoteType::ExplicitGte;
285 } else {
286 // For MRR, treat GTE votes as Max because it is used for animations and
287 // scroll. MRR cannot change frame rate without jank, so it should
288 // prefer smoothness.
289 return LayerVoteType::Max;
290 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800291 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800292 }();
Rachel Lee8f2eea42024-04-19 15:15:48 -0700293 const bool isValuelessVote = voteType == LayerVoteType::NoVote ||
294 voteType == LayerVoteType::Min || voteType == LayerVoteType::Max;
Ady Abraham5dde5972020-05-13 10:50:54 -0700295
Andy Yu8c2703d2023-11-03 11:22:46 -0700296 if (FlagManager::getInstance().game_default_frame_rate()) {
297 // Determine the layer frame rate considering the following priorities:
298 // 1. Game mode intervention frame rate override
299 // 2. setFrameRate vote
300 // 3. Game default frame rate override
301
302 const auto& [gameModeFrameRateOverride, gameDefaultFrameRateOverride] =
303 getGameFrameRateOverrideLocked(info->getOwnerUid());
304
305 const auto gameFrameRateOverrideVoteType =
306 info->isVisible() ? LayerVoteType::ExplicitDefault : LayerVoteType::NoVote;
307
308 const auto setFrameRateVoteType =
309 info->isVisible() ? voteType : LayerVoteType::NoVote;
310
311 if (gameModeFrameRateOverride.isValid()) {
312 info->setLayerVote({gameFrameRateOverrideVoteType, gameModeFrameRateOverride});
Andy Yu0dca4862024-02-26 15:01:05 -0800313 ATRACE_FORMAT_INSTANT("GameModeFrameRateOverride");
314 if (CC_UNLIKELY(mTraceEnabled)) {
315 trace(*info, gameFrameRateOverrideVoteType,
316 gameModeFrameRateOverride.getIntValue());
317 }
Rachel Lee45681982024-03-14 18:40:15 -0700318 } else if (frameRate.isValid() && frameRate.isVoteValidForMrr(isVrrDevice)) {
Rachel Lee8f2eea42024-04-19 15:15:48 -0700319 info->setLayerVote({setFrameRateVoteType,
320 isValuelessVote ? 0_Hz : frameRate.vote.rate,
Andy Yu8c2703d2023-11-03 11:22:46 -0700321 frameRate.vote.seamlessness, frameRate.category});
Andy Yu0dca4862024-02-26 15:01:05 -0800322 if (CC_UNLIKELY(mTraceEnabled)) {
323 trace(*info, gameFrameRateOverrideVoteType,
324 frameRate.vote.rate.getIntValue());
325 }
Andy Yu8c2703d2023-11-03 11:22:46 -0700326 } else if (gameDefaultFrameRateOverride.isValid()) {
327 info->setLayerVote(
328 {gameFrameRateOverrideVoteType, gameDefaultFrameRateOverride});
Andy Yu0dca4862024-02-26 15:01:05 -0800329 ATRACE_FORMAT_INSTANT("GameDefaultFrameRateOverride");
330 if (CC_UNLIKELY(mTraceEnabled)) {
331 trace(*info, gameFrameRateOverrideVoteType,
332 gameDefaultFrameRateOverride.getIntValue());
333 }
Andy Yu8c2703d2023-11-03 11:22:46 -0700334 } else {
Rachel Lee45681982024-03-14 18:40:15 -0700335 if (frameRate.isValid() && !frameRate.isVoteValidForMrr(isVrrDevice)) {
336 ATRACE_FORMAT_INSTANT("Reset layer to ignore explicit vote on MRR %s: %s "
337 "%s %s",
338 info->getName().c_str(),
339 ftl::enum_string(frameRate.vote.type).c_str(),
340 to_string(frameRate.vote.rate).c_str(),
341 ftl::enum_string(frameRate.category).c_str());
342 }
Andy Yu8c2703d2023-11-03 11:22:46 -0700343 info->resetLayerVote();
344 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800345 } else {
Rachel Lee45681982024-03-14 18:40:15 -0700346 if (frameRate.isValid() && frameRate.isVoteValidForMrr(isVrrDevice)) {
Andy Yu8c2703d2023-11-03 11:22:46 -0700347 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Rachel Lee8f2eea42024-04-19 15:15:48 -0700348 info->setLayerVote({type, isValuelessVote ? 0_Hz : frameRate.vote.rate,
349 frameRate.vote.seamlessness, frameRate.category});
Andy Yu8c2703d2023-11-03 11:22:46 -0700350 } else {
Rachel Lee45681982024-03-14 18:40:15 -0700351 if (!frameRate.isVoteValidForMrr(isVrrDevice)) {
352 ATRACE_FORMAT_INSTANT("Reset layer to ignore explicit vote on MRR %s: %s "
353 "%s %s",
354 info->getName().c_str(),
355 ftl::enum_string(frameRate.vote.type).c_str(),
356 to_string(frameRate.vote.rate).c_str(),
357 ftl::enum_string(frameRate.category).c_str());
358 }
Andy Yu8c2703d2023-11-03 11:22:46 -0700359 info->resetLayerVote();
360 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800361 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800362
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400363 it++;
364 } else {
365 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800366 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400367 }
368 info->onLayerInactive(now);
369 // move this to the inactive map
370 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
371 it = mActiveLayerInfos.erase(it);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800372 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800373 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800374}
375
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100376void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800377 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400378 for (const auto& [key, value] : mActiveLayerInfos) {
379 value.second->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800380 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800381}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700382
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100383std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700384 std::lock_guard lock(mLock);
Andy Yu0dca4862024-02-26 15:01:05 -0800385 return base::StringPrintf("{size=%zu, active=%zu}\n\tGameFrameRateOverrides=\n\t\t%s",
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400386 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
Andy Yu0dca4862024-02-26 15:01:05 -0800387 mActiveLayerInfos.size(), dumpGameFrameRateOverridesLocked().c_str());
388}
389
390std::string LayerHistory::dumpGameFrameRateOverridesLocked() const {
391 std::string overridesString = "(uid, gameModeOverride, gameDefaultOverride)=";
392 for (auto it = mGameFrameRateOverride.begin(); it != mGameFrameRateOverride.end(); ++it) {
393 base::StringAppendF(&overridesString, "{%u, %d %d} ", it->first,
394 it->second.first.getIntValue(), it->second.second.getIntValue());
395 }
396 return overridesString;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400397}
398
399float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
400 std::lock_guard lock(mLock);
401 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800402 if (found != LayerStatus::NotFound) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400403 return layerPair->second->getFps(now).getValue();
404 }
405 return 0.f;
406}
407
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800408auto LayerHistory::findLayer(int32_t id) -> std::pair<LayerStatus, LayerPair*> {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400409 // the layer could be in either the active or inactive map, try both
410 auto it = mActiveLayerInfos.find(id);
411 if (it != mActiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800412 return {LayerStatus::LayerInActiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400413 }
414 it = mInactiveLayerInfos.find(id);
415 if (it != mInactiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800416 return {LayerStatus::LayerInInactiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400417 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800418 return {LayerStatus::NotFound, nullptr};
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700419}
420
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000421bool LayerHistory::isSmallDirtyArea(uint32_t dirtyArea, float threshold) const {
Arthur Hungc70bee22023-06-02 01:35:52 +0000422 const float ratio = (float)dirtyArea / mDisplayArea;
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000423 const bool isSmallDirty = ratio <= threshold;
Arthur Hungc70bee22023-06-02 01:35:52 +0000424 ATRACE_FORMAT_INSTANT("small dirty=%s, ratio=%.3f", isSmallDirty ? "true" : "false", ratio);
425 return isSmallDirty;
426}
427
Andy Yu8c2703d2023-11-03 11:22:46 -0700428void LayerHistory::updateGameModeFrameRateOverride(FrameRateOverride frameRateOverride) {
429 const uid_t uid = frameRateOverride.uid;
430 std::lock_guard lock(mLock);
431 if (frameRateOverride.frameRateHz != 0.f) {
432 mGameFrameRateOverride[uid].first = Fps::fromValue(frameRateOverride.frameRateHz);
433 } else {
434 if (mGameFrameRateOverride[uid].second.getValue() == 0.f) {
435 mGameFrameRateOverride.erase(uid);
436 } else {
437 mGameFrameRateOverride[uid].first = Fps();
438 }
439 }
440}
441
442void LayerHistory::updateGameDefaultFrameRateOverride(FrameRateOverride frameRateOverride) {
443 const uid_t uid = frameRateOverride.uid;
444 std::lock_guard lock(mLock);
445 if (frameRateOverride.frameRateHz != 0.f) {
446 mGameFrameRateOverride[uid].second = Fps::fromValue(frameRateOverride.frameRateHz);
447 } else {
448 if (mGameFrameRateOverride[uid].first.getValue() == 0.f) {
449 mGameFrameRateOverride.erase(uid);
450 } else {
451 mGameFrameRateOverride[uid].second = Fps();
452 }
453 }
454}
455
456std::pair<Fps, Fps> LayerHistory::getGameFrameRateOverride(uid_t uid) const {
457 if (!FlagManager::getInstance().game_default_frame_rate()) {
458 return std::pair<Fps, Fps>();
459 }
460
461 std::lock_guard lock(mLock);
462
463 return getGameFrameRateOverrideLocked(uid);
464}
465
466std::pair<Fps, Fps> LayerHistory::getGameFrameRateOverrideLocked(uid_t uid) const {
467 if (!FlagManager::getInstance().game_default_frame_rate()) {
468 return std::pair<Fps, Fps>();
469 }
470
471 const auto it = mGameFrameRateOverride.find(uid);
472
473 if (it == mGameFrameRateOverride.end()) {
474 return std::pair<Fps, Fps>(Fps(), Fps());
475 }
476
477 return it->second;
478}
479
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100480} // namespace android::scheduler