blob: 21bbb08c1c016693b3b5dd3a19df59072c85fd4b [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
34#include "../Layer.h"
Rachel Lee2248f522023-01-27 16:45:23 -080035#include "EventThread.h"
Ady Abrahamd6d80162023-10-23 12:57:41 -070036#include "FlagManager.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
Ady Abrahambdda8f02021-04-01 16:06:11 -070043bool isLayerActive(const LayerInfo& info, nsecs_t threshold) {
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 Leece6e0042023-06-27 11:22:54 -070050 if (info.getSetFrameRateVote().isValid() && !info.getSetFrameRateVote().isNoVote()) {
Ady Abraham89089c92020-04-15 18:24:41 -070051 return true;
Ady Abraham8a82ba62020-01-17 12:43:17 -080052 }
Ady Abraham89089c92020-04-15 18:24:41 -070053
Ady Abrahambdda8f02021-04-01 16:06:11 -070054 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
Ady Abraham8a82ba62020-01-17 12:43:17 -080055}
56
57bool traceEnabled() {
58 return property_get_bool("debug.sf.layer_history_trace", false);
59}
60
61bool useFrameRatePriority() {
62 char value[PROPERTY_VALUE_MAX];
63 property_get("debug.sf.use_frame_rate_priority", value, "1");
64 return atoi(value);
65}
66
Ady Abrahambdda8f02021-04-01 16:06:11 -070067void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070068 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
69 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070070 };
71
Ady Abraham0ccd79b2020-06-10 10:11:17 -070072 traceType(LayerHistory::LayerVoteType::NoVote, 1);
73 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
74 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
75 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
Ady Abrahamdd5bfa92021-01-07 17:56:08 -080076 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
Ady Abraham0ccd79b2020-06-10 10:11:17 -070077 traceType(LayerHistory::LayerVoteType::Min, 1);
78 traceType(LayerHistory::LayerVoteType::Max, 1);
Rachel Leece6e0042023-06-27 11:22:54 -070079 traceType(LayerHistory::LayerVoteType::ExplicitCategory, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080080
Ady Abrahambdda8f02021-04-01 16:06:11 -070081 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080082}
Andy Labrada096227e2022-06-15 16:58:11 +000083
Vishnu Nair3fbe3262023-09-29 17:07:00 -070084LayerHistory::LayerVoteType getVoteType(FrameRateCompatibility compatibility,
Andy Labrada096227e2022-06-15 16:58:11 +000085 bool contentDetectionEnabled) {
86 LayerHistory::LayerVoteType voteType;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070087 if (!contentDetectionEnabled || compatibility == FrameRateCompatibility::NoVote) {
Andy Labrada096227e2022-06-15 16:58:11 +000088 voteType = LayerHistory::LayerVoteType::NoVote;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070089 } else if (compatibility == FrameRateCompatibility::Min) {
Andy Labrada096227e2022-06-15 16:58:11 +000090 voteType = LayerHistory::LayerVoteType::Min;
91 } else {
92 voteType = LayerHistory::LayerVoteType::Heuristic;
93 }
94 return voteType;
95}
96
Ady Abraham8a82ba62020-01-17 12:43:17 -080097} // namespace
98
Ady Abraham3efa3942021-06-24 19:01:25 -070099LayerHistory::LayerHistory()
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700100 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100101 LayerInfo::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700102}
103
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100104LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800105
Andy Labrada096227e2022-06-15 16:58:11 +0000106void LayerHistory::registerLayer(Layer* layer, bool contentDetectionEnabled) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 std::lock_guard lock(mLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800108 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first != LayerStatus::NotFound,
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400109 "%s already registered", layer->getName().c_str());
Andy Labrada096227e2022-06-15 16:58:11 +0000110 LayerVoteType type =
111 getVoteType(layer->getDefaultFrameRateCompatibility(), contentDetectionEnabled);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700112 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400113
114 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
115 // to correct them.
116 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800117}
118
Ady Abrahambdda8f02021-04-01 16:06:11 -0700119void LayerHistory::deregisterLayer(Layer* layer) {
120 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400121 if (!mActiveLayerInfos.erase(layer->getSequence())) {
122 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
123 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
124 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700125 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700126}
127
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000128void LayerHistory::record(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
129 nsecs_t now, LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800130 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400131 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800132 if (found == LayerStatus::NotFound) {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700133 // Offscreen layer
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000134 ALOGV("%s: %d not registered", __func__, id);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700135 return;
136 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800137
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400138 const auto& info = layerPair->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700139 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800140
141 // Activate layer if inactive.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800142 if (found == LayerStatus::LayerInInactiveMap) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400143 mActiveLayerInfos.insert(
144 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
145 mInactiveLayerInfos.erase(id);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800146 }
147}
148
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700149void LayerHistory::setDefaultFrameRateCompatibility(int32_t id,
150 FrameRateCompatibility frameRateCompatibility,
151 bool contentDetectionEnabled) {
Andy Labrada096227e2022-06-15 16:58:11 +0000152 std::lock_guard lock(mLock);
Andy Labrada096227e2022-06-15 16:58:11 +0000153
154 auto [found, layerPair] = findLayer(id);
155 if (found == LayerStatus::NotFound) {
156 // Offscreen layer
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700157 ALOGV("%s: %d not registered", __func__, id);
Andy Labrada096227e2022-06-15 16:58:11 +0000158 return;
159 }
160
161 const auto& info = layerPair->second;
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700162 info->setDefaultLayerVote(getVoteType(frameRateCompatibility, contentDetectionEnabled));
Andy Labrada096227e2022-06-15 16:58:11 +0000163}
164
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400165auto LayerHistory::summarize(const RefreshRateSelector& selector, nsecs_t now) -> Summary {
Ady Abraham73c3df52023-01-12 18:09:31 -0800166 ATRACE_CALL();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800167 Summary summary;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800168
169 std::lock_guard lock(mLock);
170
171 partitionLayers(now);
172
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400173 for (const auto& [key, value] : mActiveLayerInfos) {
174 auto& info = value.second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700175 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700176 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700177 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
178 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800179
Ady Abraham73c3df52023-01-12 18:09:31 -0800180 ATRACE_FORMAT("%s", info->getName().c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700181 const auto votes = info->getRefreshRateVote(selector, now);
182 for (LayerInfo::LayerVote vote : votes) {
183 if (vote.isNoVote()) {
184 continue;
185 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800186
Rachel Leece6e0042023-06-27 11:22:54 -0700187 // Compute the layer's position on the screen
188 const Rect bounds = Rect(info->getBounds());
189 const ui::Transform transform = info->getTransform();
190 constexpr bool roundOutwards = true;
191 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192
Rachel Leece6e0042023-06-27 11:22:54 -0700193 const float layerArea = transformed.getWidth() * transformed.getHeight();
194 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
195 const std::string categoryString = vote.category == FrameRateCategory::Default
Rachel Lee94ff7992023-08-31 15:15:22 -0700196 ? ""
197 : base::StringPrintf("category=%s", ftl::enum_string(vote.category).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700198 ATRACE_FORMAT_INSTANT("%s %s %s (%d%)", ftl::enum_string(vote.type).c_str(),
199 to_string(vote.fps).c_str(), categoryString.c_str(),
200 weight * 100);
201 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Rachel Lee67afbea2023-09-28 15:35:07 -0700202 vote.seamlessness, vote.category, vote.categorySmoothSwitchOnly,
203 weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800204
Rachel Leece6e0042023-06-27 11:22:54 -0700205 if (CC_UNLIKELY(mTraceEnabled)) {
206 trace(*info, vote.type, vote.fps.getIntValue());
207 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800208 }
209 }
210
211 return summary;
212}
213
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100214void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800215 ATRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800216 const nsecs_t threshold = getActiveLayerThreshold(now);
217
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400218 // iterate over inactive map
219 LayerInfos::iterator it = mInactiveLayerInfos.begin();
220 while (it != mInactiveLayerInfos.end()) {
221 auto& [layerUnsafe, info] = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700222 if (isLayerActive(*info, threshold)) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400223 // move this to the active map
224
225 mActiveLayerInfos.insert({it->first, std::move(it->second)});
226 it = mInactiveLayerInfos.erase(it);
227 } else {
228 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800229 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400230 }
231 info->onLayerInactive(now);
232 it++;
233 }
234 }
235
236 // iterate over active map
237 it = mActiveLayerInfos.begin();
238 while (it != mActiveLayerInfos.end()) {
239 auto& [layerUnsafe, info] = it->second;
240 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800241 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700242 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800243 const auto voteType = [&]() {
Rachel Leece6e0042023-06-27 11:22:54 -0700244 switch (frameRate.vote.type) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800245 case Layer::FrameRateCompatibility::Default:
246 return LayerVoteType::ExplicitDefault;
Andy Labrada096227e2022-06-15 16:58:11 +0000247 case Layer::FrameRateCompatibility::Min:
248 return LayerVoteType::Min;
Ady Abraham71c437d2020-01-31 15:56:57 -0800249 case Layer::FrameRateCompatibility::ExactOrMultiple:
250 return LayerVoteType::ExplicitExactOrMultiple;
251 case Layer::FrameRateCompatibility::NoVote:
252 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800253 case Layer::FrameRateCompatibility::Exact:
254 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800255 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800256 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700257
Rachel Leece6e0042023-06-27 11:22:54 -0700258 if (frameRate.isValid()) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700259 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Rachel Leece6e0042023-06-27 11:22:54 -0700260 info->setLayerVote({type, frameRate.vote.rate, frameRate.vote.seamlessness,
261 frameRate.category});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800262 } else {
263 info->resetLayerVote();
264 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800265
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400266 it++;
267 } else {
268 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800269 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400270 }
271 info->onLayerInactive(now);
272 // move this to the inactive map
273 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
274 it = mActiveLayerInfos.erase(it);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800275 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800276 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800277}
278
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100279void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800280 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400281 for (const auto& [key, value] : mActiveLayerInfos) {
282 value.second->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800283 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800284}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700285
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100286std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700287 std::lock_guard lock(mLock);
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400288 return base::StringPrintf("{size=%zu, active=%zu}",
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400289 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
290 mActiveLayerInfos.size());
291}
292
293float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
294 std::lock_guard lock(mLock);
295 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800296 if (found != LayerStatus::NotFound) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400297 return layerPair->second->getFps(now).getValue();
298 }
299 return 0.f;
300}
301
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800302auto LayerHistory::findLayer(int32_t id) -> std::pair<LayerStatus, LayerPair*> {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400303 // the layer could be in either the active or inactive map, try both
304 auto it = mActiveLayerInfos.find(id);
305 if (it != mActiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800306 return {LayerStatus::LayerInActiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400307 }
308 it = mInactiveLayerInfos.find(id);
309 if (it != mInactiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800310 return {LayerStatus::LayerInInactiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400311 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800312 return {LayerStatus::NotFound, nullptr};
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700313}
314
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000315bool LayerHistory::isSmallDirtyArea(uint32_t dirtyArea, float threshold) const {
Arthur Hungc70bee22023-06-02 01:35:52 +0000316 const float ratio = (float)dirtyArea / mDisplayArea;
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000317 const bool isSmallDirty = ratio <= threshold;
Arthur Hungc70bee22023-06-02 01:35:52 +0000318 ATRACE_FORMAT_INSTANT("small dirty=%s, ratio=%.3f", isSmallDirty ? "true" : "false", ratio);
319 return isSmallDirty;
320}
321
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100322} // namespace android::scheduler