blob: 0efc28b71da18184951f81ac3163ea8778eede6b [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"
Ady Abraham8a82ba62020-01-17 12:43:17 -080036
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010037namespace android::scheduler {
Ady Abraham8a82ba62020-01-17 12:43:17 -080038
39namespace {
40
Ady Abrahambdda8f02021-04-01 16:06:11 -070041bool isLayerActive(const LayerInfo& info, nsecs_t threshold) {
Ady Abraham89089c92020-04-15 18:24:41 -070042 // Layers with an explicit vote are always kept active
Ady Abrahambdda8f02021-04-01 16:06:11 -070043 if (info.getSetFrameRateVote().rate.isValid()) {
Ady Abraham89089c92020-04-15 18:24:41 -070044 return true;
Ady Abraham8a82ba62020-01-17 12:43:17 -080045 }
Ady Abraham89089c92020-04-15 18:24:41 -070046
Ady Abrahambdda8f02021-04-01 16:06:11 -070047 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
Ady Abraham8a82ba62020-01-17 12:43:17 -080048}
49
50bool traceEnabled() {
51 return property_get_bool("debug.sf.layer_history_trace", false);
52}
53
54bool useFrameRatePriority() {
55 char value[PROPERTY_VALUE_MAX];
56 property_get("debug.sf.use_frame_rate_priority", value, "1");
57 return atoi(value);
58}
59
Ady Abrahambdda8f02021-04-01 16:06:11 -070060void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070061 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
62 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070063 };
64
Ady Abraham0ccd79b2020-06-10 10:11:17 -070065 traceType(LayerHistory::LayerVoteType::NoVote, 1);
66 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
67 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
68 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
Ady Abrahamdd5bfa92021-01-07 17:56:08 -080069 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
Ady Abraham0ccd79b2020-06-10 10:11:17 -070070 traceType(LayerHistory::LayerVoteType::Min, 1);
71 traceType(LayerHistory::LayerVoteType::Max, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080072
Ady Abrahambdda8f02021-04-01 16:06:11 -070073 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080074}
Ady Abraham8a82ba62020-01-17 12:43:17 -080075} // namespace
76
Ady Abraham3efa3942021-06-24 19:01:25 -070077LayerHistory::LayerHistory()
Ady Abrahamb1b9d412020-06-01 19:53:52 -070078 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010079 LayerInfo::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -070080}
81
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010082LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -080083
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010084void LayerHistory::registerLayer(Layer* layer, LayerVoteType type) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080085 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040086 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first !=
87 LayerHistory::layerStatus::NotFound,
88 "%s already registered", layer->getName().c_str());
Ady Abrahambe09aad2021-05-03 18:59:38 -070089 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040090
91 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
92 // to correct them.
93 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
Ady Abraham8a82ba62020-01-17 12:43:17 -080094}
95
Ady Abrahambdda8f02021-04-01 16:06:11 -070096void LayerHistory::deregisterLayer(Layer* layer) {
97 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -040098 if (!mActiveLayerInfos.erase(layer->getSequence())) {
99 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
100 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
101 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700102 }
Ady Abrahambdda8f02021-04-01 16:06:11 -0700103}
104
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100105void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
106 LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400108 auto id = layer->getSequence();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800109
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400110 auto [found, layerPair] = findLayer(id);
111 if (found == LayerHistory::layerStatus::NotFound) {
Ady Abrahambe09aad2021-05-03 18:59:38 -0700112 // Offscreen layer
113 ALOGV("LayerHistory::record: %s not registered", layer->getName().c_str());
114 return;
115 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800116
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400117 const auto& info = layerPair->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700118 const auto layerProps = LayerInfo::LayerProps{
119 .visible = layer->isVisible(),
120 .bounds = layer->getBounds(),
121 .transform = layer->getTransform(),
122 .setFrameRateVote = layer->getFrameRateForLayerTree(),
123 .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(),
124 };
125
126 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127
128 // Activate layer if inactive.
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400129 if (found == LayerHistory::layerStatus::LayerInInactiveMap) {
130 mActiveLayerInfos.insert(
131 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
132 mInactiveLayerInfos.erase(id);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133 }
134}
135
Ady Abraham3efa3942021-06-24 19:01:25 -0700136LayerHistory::Summary LayerHistory::summarize(const RefreshRateConfigs& refreshRateConfigs,
137 nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800138 LayerHistory::Summary summary;
139
140 std::lock_guard lock(mLock);
141
142 partitionLayers(now);
143
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400144 for (const auto& [key, value] : mActiveLayerInfos) {
145 auto& info = value.second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700146 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700147 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700148 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
149 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800150
Ady Abraham3efa3942021-06-24 19:01:25 -0700151 const auto vote = info->getRefreshRateVote(refreshRateConfigs, now);
Ady Abraham89089c92020-04-15 18:24:41 -0700152 // Skip NoVote layer as those don't have any requirements
Marin Shalamanov46084422020-10-13 12:33:42 +0200153 if (vote.type == LayerHistory::LayerVoteType::NoVote) {
Ady Abraham89089c92020-04-15 18:24:41 -0700154 continue;
155 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800156
Ady Abraham89089c92020-04-15 18:24:41 -0700157 // Compute the layer's position on the screen
Ady Abrahambdda8f02021-04-01 16:06:11 -0700158 const Rect bounds = Rect(info->getBounds());
159 const ui::Transform transform = info->getTransform();
Ady Abraham89089c92020-04-15 18:24:41 -0700160 constexpr bool roundOutwards = true;
161 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800162
Ady Abraham89089c92020-04-15 18:24:41 -0700163 const float layerArea = transformed.getWidth() * transformed.getHeight();
164 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700165 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Ady Abraham62a0be22020-12-08 16:54:10 -0800166 vote.seamlessness, weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800167
Ady Abraham89089c92020-04-15 18:24:41 -0700168 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700169 trace(*info, vote.type, vote.fps.getIntValue());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170 }
171 }
172
173 return summary;
174}
175
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100176void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800177 const nsecs_t threshold = getActiveLayerThreshold(now);
178
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400179 // iterate over inactive map
180 LayerInfos::iterator it = mInactiveLayerInfos.begin();
181 while (it != mInactiveLayerInfos.end()) {
182 auto& [layerUnsafe, info] = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700183 if (isLayerActive(*info, threshold)) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400184 // move this to the active map
185
186 mActiveLayerInfos.insert({it->first, std::move(it->second)});
187 it = mInactiveLayerInfos.erase(it);
188 } else {
189 if (CC_UNLIKELY(mTraceEnabled)) {
190 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
191 }
192 info->onLayerInactive(now);
193 it++;
194 }
195 }
196
197 // iterate over active map
198 it = mActiveLayerInfos.begin();
199 while (it != mActiveLayerInfos.end()) {
200 auto& [layerUnsafe, info] = it->second;
201 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800202 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700203 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800204 const auto voteType = [&]() {
205 switch (frameRate.type) {
206 case Layer::FrameRateCompatibility::Default:
207 return LayerVoteType::ExplicitDefault;
208 case Layer::FrameRateCompatibility::ExactOrMultiple:
209 return LayerVoteType::ExplicitExactOrMultiple;
210 case Layer::FrameRateCompatibility::NoVote:
211 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800212 case Layer::FrameRateCompatibility::Exact:
213 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800215 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700216
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100217 if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700218 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100219 info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800220 } else {
221 info->resetLayerVote();
222 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800223
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400224 it++;
225 } else {
226 if (CC_UNLIKELY(mTraceEnabled)) {
227 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
228 }
229 info->onLayerInactive(now);
230 // move this to the inactive map
231 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
232 it = mActiveLayerInfos.erase(it);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800233 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800234 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800235}
236
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100237void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400239 for (const auto& [key, value] : mActiveLayerInfos) {
240 value.second->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800241 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800242}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700243
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100244std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700245 std::lock_guard lock(mLock);
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400246 return base::StringPrintf("LayerHistory{size=%zu, active=%zu}",
247 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
248 mActiveLayerInfos.size());
249}
250
251float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
252 std::lock_guard lock(mLock);
253 auto [found, layerPair] = findLayer(id);
254 if (found != LayerHistory::layerStatus::NotFound) {
255 return layerPair->second->getFps(now).getValue();
256 }
257 return 0.f;
258}
259
260std::pair<LayerHistory::layerStatus, LayerHistory::LayerPair*> LayerHistory::findLayer(int32_t id) {
261 // the layer could be in either the active or inactive map, try both
262 auto it = mActiveLayerInfos.find(id);
263 if (it != mActiveLayerInfos.end()) {
264 return std::make_pair(LayerHistory::layerStatus::LayerInActiveMap, &(it->second));
265 }
266 it = mInactiveLayerInfos.find(id);
267 if (it != mInactiveLayerInfos.end()) {
268 return std::make_pair(LayerHistory::layerStatus::LayerInInactiveMap, &(it->second));
269 }
270 return std::make_pair(LayerHistory::layerStatus::NotFound, nullptr);
271}
272
273std::pair<LayerHistory::layerStatus, const LayerHistory::LayerPair*> LayerHistory::findLayer(
274 int32_t id) const {
275 // the layer could be in either the active or inactive map, try both
276 auto it = mActiveLayerInfos.find(id);
277 if (it != mActiveLayerInfos.end()) {
278 return std::make_pair(LayerHistory::layerStatus::LayerInActiveMap, &(it->second));
279 }
280 it = mInactiveLayerInfos.find(id);
281 if (it != mInactiveLayerInfos.end()) {
282 return std::make_pair(LayerHistory::layerStatus::LayerInInactiveMap, &(it->second));
283 }
284 return std::make_pair(LayerHistory::layerStatus::NotFound, nullptr);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700285}
286
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100287} // namespace android::scheduler