blob: 74a2ca786cdf4268d2bdd9704ae65ec9764a50c9 [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>
25#include <utils/Log.h>
26#include <utils/Timers.h>
27#include <utils/Trace.h>
28
29#include <algorithm>
30#include <cmath>
31#include <string>
32#include <utility>
33
34#include "../Layer.h"
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010035#include "LayerInfo.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070036#include "SchedulerUtils.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080037
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010038namespace android::scheduler {
Ady Abraham8a82ba62020-01-17 12:43:17 -080039
40namespace {
41
Ady Abrahambdda8f02021-04-01 16:06:11 -070042bool isLayerActive(const LayerInfo& info, nsecs_t threshold) {
Ady Abraham89089c92020-04-15 18:24:41 -070043 // Layers with an explicit vote are always kept active
Ady Abrahambdda8f02021-04-01 16:06:11 -070044 if (info.getSetFrameRateVote().rate.isValid()) {
Ady Abraham89089c92020-04-15 18:24:41 -070045 return true;
Ady Abraham8a82ba62020-01-17 12:43:17 -080046 }
Ady Abraham89089c92020-04-15 18:24:41 -070047
Ady Abrahambdda8f02021-04-01 16:06:11 -070048 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
Ady Abraham8a82ba62020-01-17 12:43:17 -080049}
50
51bool traceEnabled() {
52 return property_get_bool("debug.sf.layer_history_trace", false);
53}
54
55bool useFrameRatePriority() {
56 char value[PROPERTY_VALUE_MAX];
57 property_get("debug.sf.use_frame_rate_priority", value, "1");
58 return atoi(value);
59}
60
Ady Abrahambdda8f02021-04-01 16:06:11 -070061void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070062 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
63 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070064 };
65
Ady Abraham0ccd79b2020-06-10 10:11:17 -070066 traceType(LayerHistory::LayerVoteType::NoVote, 1);
67 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
68 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
69 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
Ady Abrahamdd5bfa92021-01-07 17:56:08 -080070 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
Ady Abraham0ccd79b2020-06-10 10:11:17 -070071 traceType(LayerHistory::LayerVoteType::Min, 1);
72 traceType(LayerHistory::LayerVoteType::Max, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080073
Ady Abrahambdda8f02021-04-01 16:06:11 -070074 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080075}
Ady Abraham8a82ba62020-01-17 12:43:17 -080076} // namespace
77
Ady Abraham3efa3942021-06-24 19:01:25 -070078LayerHistory::LayerHistory()
Ady Abrahamb1b9d412020-06-01 19:53:52 -070079 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010080 LayerInfo::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -070081}
82
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010083LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -080084
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010085void LayerHistory::registerLayer(Layer* layer, LayerVoteType type) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080086 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040087 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first !=
88 LayerHistory::layerStatus::NotFound,
89 "%s already registered", layer->getName().c_str());
Ady Abrahambe09aad2021-05-03 18:59:38 -070090 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040091
92 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
93 // to correct them.
94 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
Ady Abraham8a82ba62020-01-17 12:43:17 -080095}
96
Ady Abrahambdda8f02021-04-01 16:06:11 -070097void LayerHistory::deregisterLayer(Layer* layer) {
98 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040099 if (!mActiveLayerInfos.erase(layer->getSequence())) {
100 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
101 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
102 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700103 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700104}
105
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100106void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
107 LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400109 auto id = layer->getSequence();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400111 auto [found, layerPair] = findLayer(id);
112 if (found == LayerHistory::layerStatus::NotFound) {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700113 // Offscreen layer
114 ALOGV("LayerHistory::record: %s not registered", layer->getName().c_str());
115 return;
116 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800117
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400118 const auto& info = layerPair->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700119 const auto layerProps = LayerInfo::LayerProps{
120 .visible = layer->isVisible(),
121 .bounds = layer->getBounds(),
122 .transform = layer->getTransform(),
123 .setFrameRateVote = layer->getFrameRateForLayerTree(),
124 .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(),
125 };
126
127 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800128
129 // Activate layer if inactive.
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400130 if (found == LayerHistory::layerStatus::LayerInInactiveMap) {
131 mActiveLayerInfos.insert(
132 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
133 mInactiveLayerInfos.erase(id);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800134 }
135}
136
Ady Abraham3efa3942021-06-24 19:01:25 -0700137LayerHistory::Summary LayerHistory::summarize(const RefreshRateConfigs& refreshRateConfigs,
138 nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800139 LayerHistory::Summary summary;
140
141 std::lock_guard lock(mLock);
142
143 partitionLayers(now);
144
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400145 for (const auto& [key, value] : mActiveLayerInfos) {
146 auto& info = value.second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700147 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700148 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700149 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
150 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800151
Ady Abraham3efa3942021-06-24 19:01:25 -0700152 const auto vote = info->getRefreshRateVote(refreshRateConfigs, now);
Ady Abraham89089c92020-04-15 18:24:41 -0700153 // Skip NoVote layer as those don't have any requirements
Marin Shalamanov46084422020-10-13 12:33:42 +0200154 if (vote.type == LayerHistory::LayerVoteType::NoVote) {
Ady Abraham89089c92020-04-15 18:24:41 -0700155 continue;
156 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800157
Ady Abraham89089c92020-04-15 18:24:41 -0700158 // Compute the layer's position on the screen
Ady Abrahambdda8f02021-04-01 16:06:11 -0700159 const Rect bounds = Rect(info->getBounds());
160 const ui::Transform transform = info->getTransform();
Ady Abraham89089c92020-04-15 18:24:41 -0700161 constexpr bool roundOutwards = true;
162 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800163
Ady Abraham89089c92020-04-15 18:24:41 -0700164 const float layerArea = transformed.getWidth() * transformed.getHeight();
165 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700166 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Ady Abraham62a0be22020-12-08 16:54:10 -0800167 vote.seamlessness, weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800168
Ady Abraham89089c92020-04-15 18:24:41 -0700169 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700170 trace(*info, vote.type, vote.fps.getIntValue());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800171 }
172 }
173
174 return summary;
175}
176
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100177void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800178 const nsecs_t threshold = getActiveLayerThreshold(now);
179
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400180 // iterate over inactive map
181 LayerInfos::iterator it = mInactiveLayerInfos.begin();
182 while (it != mInactiveLayerInfos.end()) {
183 auto& [layerUnsafe, info] = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700184 if (isLayerActive(*info, threshold)) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400185 // move this to the active map
186
187 mActiveLayerInfos.insert({it->first, std::move(it->second)});
188 it = mInactiveLayerInfos.erase(it);
189 } else {
190 if (CC_UNLIKELY(mTraceEnabled)) {
191 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
192 }
193 info->onLayerInactive(now);
194 it++;
195 }
196 }
197
198 // iterate over active map
199 it = mActiveLayerInfos.begin();
200 while (it != mActiveLayerInfos.end()) {
201 auto& [layerUnsafe, info] = it->second;
202 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800203 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700204 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800205 const auto voteType = [&]() {
206 switch (frameRate.type) {
207 case Layer::FrameRateCompatibility::Default:
208 return LayerVoteType::ExplicitDefault;
209 case Layer::FrameRateCompatibility::ExactOrMultiple:
210 return LayerVoteType::ExplicitExactOrMultiple;
211 case Layer::FrameRateCompatibility::NoVote:
212 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800213 case Layer::FrameRateCompatibility::Exact:
214 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800215 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800216 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700217
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100218 if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700219 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100220 info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800221 } else {
222 info->resetLayerVote();
223 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800224
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400225 it++;
226 } else {
227 if (CC_UNLIKELY(mTraceEnabled)) {
228 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
229 }
230 info->onLayerInactive(now);
231 // move this to the inactive map
232 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
233 it = mActiveLayerInfos.erase(it);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800234 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800235 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800236}
237
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100238void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800239 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400240 for (const auto& [key, value] : mActiveLayerInfos) {
241 value.second->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800242 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800243}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700244
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100245std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700246 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400247 return base::StringPrintf("LayerHistory{size=%zu, active=%zu}",
248 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
249 mActiveLayerInfos.size());
250}
251
252float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
253 std::lock_guard lock(mLock);
254 auto [found, layerPair] = findLayer(id);
255 if (found != LayerHistory::layerStatus::NotFound) {
256 return layerPair->second->getFps(now).getValue();
257 }
258 return 0.f;
259}
260
261std::pair<LayerHistory::layerStatus, LayerHistory::LayerPair*> LayerHistory::findLayer(int32_t id) {
262 // the layer could be in either the active or inactive map, try both
263 auto it = mActiveLayerInfos.find(id);
264 if (it != mActiveLayerInfos.end()) {
265 return std::make_pair(LayerHistory::layerStatus::LayerInActiveMap, &(it->second));
266 }
267 it = mInactiveLayerInfos.find(id);
268 if (it != mInactiveLayerInfos.end()) {
269 return std::make_pair(LayerHistory::layerStatus::LayerInInactiveMap, &(it->second));
270 }
271 return std::make_pair(LayerHistory::layerStatus::NotFound, nullptr);
272}
273
274std::pair<LayerHistory::layerStatus, const LayerHistory::LayerPair*> LayerHistory::findLayer(
275 int32_t id) const {
276 // the layer could be in either the active or inactive map, try both
277 auto it = mActiveLayerInfos.find(id);
278 if (it != mActiveLayerInfos.end()) {
279 return std::make_pair(LayerHistory::layerStatus::LayerInActiveMap, &(it->second));
280 }
281 it = mInactiveLayerInfos.find(id);
282 if (it != mInactiveLayerInfos.end()) {
283 return std::make_pair(LayerHistory::layerStatus::LayerInInactiveMap, &(it->second));
284 }
285 return std::make_pair(LayerHistory::layerStatus::NotFound, nullptr);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700286}
287
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100288} // namespace android::scheduler