blob: ff829143d395f48f694d24f91b12685143feacdf [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>
Rachel Lee414f4082023-10-23 14:48:44 -070024#include <com_android_graphics_surfaceflinger_flags.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080025#include <cutils/properties.h>
Ady Abraham73c3df52023-01-12 18:09:31 -080026#include <gui/TraceUtils.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080027#include <utils/Log.h>
28#include <utils/Timers.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080029
30#include <algorithm>
31#include <cmath>
32#include <string>
33#include <utility>
34
35#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 Lee414f4082023-10-23 14:48:44 -070043using namespace com::android::graphics::surfaceflinger;
44
Ady Abrahambdda8f02021-04-01 16:06:11 -070045bool isLayerActive(const LayerInfo& info, nsecs_t threshold) {
Rachel Lee414f4082023-10-23 14:48:44 -070046 if (flags::misc1() && !info.isVisible()) {
47 return false;
48 }
49
50 // Layers with an explicit frame rate or frame rate category are kept active,
Rachel Leed0694bc2023-09-12 14:57:58 -070051 // but ignore NoVote.
Rachel Leece6e0042023-06-27 11:22:54 -070052 if (info.getSetFrameRateVote().isValid() && !info.getSetFrameRateVote().isNoVote()) {
Ady Abraham89089c92020-04-15 18:24:41 -070053 return true;
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 }
Ady Abraham89089c92020-04-15 18:24:41 -070055
Ady Abrahambdda8f02021-04-01 16:06:11 -070056 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
Ady Abraham8a82ba62020-01-17 12:43:17 -080057}
58
59bool traceEnabled() {
60 return property_get_bool("debug.sf.layer_history_trace", false);
61}
62
63bool useFrameRatePriority() {
64 char value[PROPERTY_VALUE_MAX];
65 property_get("debug.sf.use_frame_rate_priority", value, "1");
66 return atoi(value);
67}
68
Ady Abrahambdda8f02021-04-01 16:06:11 -070069void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070070 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
71 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070072 };
73
Ady Abraham0ccd79b2020-06-10 10:11:17 -070074 traceType(LayerHistory::LayerVoteType::NoVote, 1);
75 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
76 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
77 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
Ady Abrahamdd5bfa92021-01-07 17:56:08 -080078 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
Ady Abraham0ccd79b2020-06-10 10:11:17 -070079 traceType(LayerHistory::LayerVoteType::Min, 1);
80 traceType(LayerHistory::LayerVoteType::Max, 1);
Rachel Leece6e0042023-06-27 11:22:54 -070081 traceType(LayerHistory::LayerVoteType::ExplicitCategory, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080082
Ady Abrahambdda8f02021-04-01 16:06:11 -070083 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080084}
Andy Labrada096227e2022-06-15 16:58:11 +000085
Vishnu Nair3fbe3262023-09-29 17:07:00 -070086LayerHistory::LayerVoteType getVoteType(FrameRateCompatibility compatibility,
Andy Labrada096227e2022-06-15 16:58:11 +000087 bool contentDetectionEnabled) {
88 LayerHistory::LayerVoteType voteType;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070089 if (!contentDetectionEnabled || compatibility == FrameRateCompatibility::NoVote) {
Andy Labrada096227e2022-06-15 16:58:11 +000090 voteType = LayerHistory::LayerVoteType::NoVote;
Vishnu Nair3fbe3262023-09-29 17:07:00 -070091 } else if (compatibility == FrameRateCompatibility::Min) {
Andy Labrada096227e2022-06-15 16:58:11 +000092 voteType = LayerHistory::LayerVoteType::Min;
93 } else {
94 voteType = LayerHistory::LayerVoteType::Heuristic;
95 }
96 return voteType;
97}
98
Ady Abraham8a82ba62020-01-17 12:43:17 -080099} // namespace
100
Ady Abraham3efa3942021-06-24 19:01:25 -0700101LayerHistory::LayerHistory()
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700102 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100103 LayerInfo::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700104}
105
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100106LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107
Andy Labrada096227e2022-06-15 16:58:11 +0000108void LayerHistory::registerLayer(Layer* layer, bool contentDetectionEnabled) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800109 std::lock_guard lock(mLock);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800110 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first != LayerStatus::NotFound,
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400111 "%s already registered", layer->getName().c_str());
Andy Labrada096227e2022-06-15 16:58:11 +0000112 LayerVoteType type =
113 getVoteType(layer->getDefaultFrameRateCompatibility(), contentDetectionEnabled);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700114 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400115
116 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
117 // to correct them.
118 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800119}
120
Ady Abrahambdda8f02021-04-01 16:06:11 -0700121void LayerHistory::deregisterLayer(Layer* layer) {
122 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400123 if (!mActiveLayerInfos.erase(layer->getSequence())) {
124 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
125 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
126 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700127 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700128}
129
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000130void LayerHistory::record(int32_t id, const LayerProps& layerProps, nsecs_t presentTime,
131 nsecs_t now, LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800132 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400133 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800134 if (found == LayerStatus::NotFound) {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700135 // Offscreen layer
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000136 ALOGV("%s: %d not registered", __func__, id);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700137 return;
138 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800139
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400140 const auto& info = layerPair->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700141 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142
143 // Activate layer if inactive.
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800144 if (found == LayerStatus::LayerInInactiveMap) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400145 mActiveLayerInfos.insert(
146 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
147 mInactiveLayerInfos.erase(id);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800148 }
149}
150
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700151void LayerHistory::setDefaultFrameRateCompatibility(int32_t id,
152 FrameRateCompatibility frameRateCompatibility,
153 bool contentDetectionEnabled) {
Andy Labrada096227e2022-06-15 16:58:11 +0000154 std::lock_guard lock(mLock);
Andy Labrada096227e2022-06-15 16:58:11 +0000155
156 auto [found, layerPair] = findLayer(id);
157 if (found == LayerStatus::NotFound) {
158 // Offscreen layer
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700159 ALOGV("%s: %d not registered", __func__, id);
Andy Labrada096227e2022-06-15 16:58:11 +0000160 return;
161 }
162
163 const auto& info = layerPair->second;
Vishnu Nair80e8cfe2023-09-29 17:03:45 -0700164 info->setDefaultLayerVote(getVoteType(frameRateCompatibility, contentDetectionEnabled));
Andy Labrada096227e2022-06-15 16:58:11 +0000165}
166
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400167auto LayerHistory::summarize(const RefreshRateSelector& selector, nsecs_t now) -> Summary {
Ady Abraham73c3df52023-01-12 18:09:31 -0800168 ATRACE_CALL();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800169 Summary summary;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170
171 std::lock_guard lock(mLock);
172
173 partitionLayers(now);
174
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400175 for (const auto& [key, value] : mActiveLayerInfos) {
176 auto& info = value.second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700177 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700178 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700179 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
180 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800181
Ady Abraham73c3df52023-01-12 18:09:31 -0800182 ATRACE_FORMAT("%s", info->getName().c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700183 const auto votes = info->getRefreshRateVote(selector, now);
184 for (LayerInfo::LayerVote vote : votes) {
185 if (vote.isNoVote()) {
186 continue;
187 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800188
Rachel Leece6e0042023-06-27 11:22:54 -0700189 // Compute the layer's position on the screen
190 const Rect bounds = Rect(info->getBounds());
191 const ui::Transform transform = info->getTransform();
192 constexpr bool roundOutwards = true;
193 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194
Rachel Leece6e0042023-06-27 11:22:54 -0700195 const float layerArea = transformed.getWidth() * transformed.getHeight();
196 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
197 const std::string categoryString = vote.category == FrameRateCategory::Default
Rachel Lee94ff7992023-08-31 15:15:22 -0700198 ? ""
199 : base::StringPrintf("category=%s", ftl::enum_string(vote.category).c_str());
Rachel Leece6e0042023-06-27 11:22:54 -0700200 ATRACE_FORMAT_INSTANT("%s %s %s (%d%)", ftl::enum_string(vote.type).c_str(),
201 to_string(vote.fps).c_str(), categoryString.c_str(),
202 weight * 100);
203 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Rachel Lee67afbea2023-09-28 15:35:07 -0700204 vote.seamlessness, vote.category, vote.categorySmoothSwitchOnly,
205 weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800206
Rachel Leece6e0042023-06-27 11:22:54 -0700207 if (CC_UNLIKELY(mTraceEnabled)) {
208 trace(*info, vote.type, vote.fps.getIntValue());
209 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800210 }
211 }
212
213 return summary;
214}
215
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100216void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800217 ATRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800218 const nsecs_t threshold = getActiveLayerThreshold(now);
219
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400220 // iterate over inactive map
221 LayerInfos::iterator it = mInactiveLayerInfos.begin();
222 while (it != mInactiveLayerInfos.end()) {
223 auto& [layerUnsafe, info] = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700224 if (isLayerActive(*info, threshold)) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400225 // move this to the active map
226
227 mActiveLayerInfos.insert({it->first, std::move(it->second)});
228 it = mInactiveLayerInfos.erase(it);
229 } else {
230 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800231 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400232 }
233 info->onLayerInactive(now);
234 it++;
235 }
236 }
237
238 // iterate over active map
239 it = mActiveLayerInfos.begin();
240 while (it != mActiveLayerInfos.end()) {
241 auto& [layerUnsafe, info] = it->second;
242 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800243 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700244 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800245 const auto voteType = [&]() {
Rachel Leece6e0042023-06-27 11:22:54 -0700246 switch (frameRate.vote.type) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800247 case Layer::FrameRateCompatibility::Default:
248 return LayerVoteType::ExplicitDefault;
Andy Labrada096227e2022-06-15 16:58:11 +0000249 case Layer::FrameRateCompatibility::Min:
250 return LayerVoteType::Min;
Ady Abraham71c437d2020-01-31 15:56:57 -0800251 case Layer::FrameRateCompatibility::ExactOrMultiple:
252 return LayerVoteType::ExplicitExactOrMultiple;
253 case Layer::FrameRateCompatibility::NoVote:
254 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800255 case Layer::FrameRateCompatibility::Exact:
256 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800257 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800258 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700259
Rachel Leece6e0042023-06-27 11:22:54 -0700260 if (frameRate.isValid()) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700261 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Rachel Leece6e0042023-06-27 11:22:54 -0700262 info->setLayerVote({type, frameRate.vote.rate, frameRate.vote.seamlessness,
263 frameRate.category});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800264 } else {
265 info->resetLayerVote();
266 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800267
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400268 it++;
269 } else {
270 if (CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800271 trace(*info, LayerVoteType::NoVote, 0);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400272 }
273 info->onLayerInactive(now);
274 // move this to the inactive map
275 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
276 it = mActiveLayerInfos.erase(it);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800277 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800278 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800279}
280
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100281void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800282 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400283 for (const auto& [key, value] : mActiveLayerInfos) {
284 value.second->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800285 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800286}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700287
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100288std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700289 std::lock_guard lock(mLock);
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400290 return base::StringPrintf("{size=%zu, active=%zu}",
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400291 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
292 mActiveLayerInfos.size());
293}
294
295float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
296 std::lock_guard lock(mLock);
297 auto [found, layerPair] = findLayer(id);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800298 if (found != LayerStatus::NotFound) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400299 return layerPair->second->getFps(now).getValue();
300 }
301 return 0.f;
302}
303
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800304auto LayerHistory::findLayer(int32_t id) -> std::pair<LayerStatus, LayerPair*> {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400305 // the layer could be in either the active or inactive map, try both
306 auto it = mActiveLayerInfos.find(id);
307 if (it != mActiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800308 return {LayerStatus::LayerInActiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400309 }
310 it = mInactiveLayerInfos.find(id);
311 if (it != mInactiveLayerInfos.end()) {
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800312 return {LayerStatus::LayerInInactiveMap, &(it->second)};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400313 }
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800314 return {LayerStatus::NotFound, nullptr};
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700315}
316
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000317bool LayerHistory::isSmallDirtyArea(uint32_t dirtyArea, float threshold) const {
Arthur Hungc70bee22023-06-02 01:35:52 +0000318 const float ratio = (float)dirtyArea / mDisplayArea;
Tony Huang9ac5e6e2023-08-24 09:01:44 +0000319 const bool isSmallDirty = ratio <= threshold;
Arthur Hungc70bee22023-06-02 01:35:52 +0000320 ATRACE_FORMAT_INSTANT("small dirty=%s, ratio=%.3f", isSmallDirty ? "true" : "false", ratio);
321 return isSmallDirty;
322}
323
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100324} // namespace android::scheduler