blob: 0563795c7935daa363142901819b4c0b26b2f008 [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
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010078LayerHistory::LayerHistory(const RefreshRateConfigs& refreshRateConfigs)
Ady Abrahamb1b9d412020-06-01 19:53:52 -070079 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010080 LayerInfo::setTraceEnabled(mTraceEnabled);
81 LayerInfo::setRefreshRateConfigs(refreshRateConfigs);
Ady Abrahamb1b9d412020-06-01 19:53:52 -070082}
83
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010084LayerHistory::~LayerHistory() = default;
Ady Abraham8a82ba62020-01-17 12:43:17 -080085
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010086void LayerHistory::registerLayer(Layer* layer, LayerVoteType type) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080087 std::lock_guard lock(mLock);
Ady Abrahambe09aad2021-05-03 18:59:38 -070088 for (const auto& info : mLayerInfos) {
89 LOG_ALWAYS_FATAL_IF(info.first == layer, "%s already registered", layer->getName().c_str());
90 }
91 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Ady Abraham8a82ba62020-01-17 12:43:17 -080092 mLayerInfos.emplace_back(layer, std::move(info));
93}
94
Ady Abrahambdda8f02021-04-01 16:06:11 -070095void LayerHistory::deregisterLayer(Layer* layer) {
96 std::lock_guard lock(mLock);
97
98 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
99 [layer](const auto& pair) { return pair.first == layer; });
Ady Abrahambe09aad2021-05-03 18:59:38 -0700100 LOG_ALWAYS_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700101
102 const size_t i = static_cast<size_t>(it - mLayerInfos.begin());
103 if (i < mActiveLayersEnd) {
104 mActiveLayersEnd--;
105 }
106 const size_t last = mLayerInfos.size() - 1;
107 std::swap(mLayerInfos[i], mLayerInfos[last]);
108 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(last));
109}
110
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100111void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
112 LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800113 std::lock_guard lock(mLock);
114
115 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
116 [layer](const auto& pair) { return pair.first == layer; });
Ady Abrahambe09aad2021-05-03 18:59:38 -0700117 if (it == mLayerInfos.end()) {
118 // Offscreen layer
119 ALOGV("LayerHistory::record: %s not registered", layer->getName().c_str());
120 return;
121 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122
123 const auto& info = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700124 const auto layerProps = LayerInfo::LayerProps{
125 .visible = layer->isVisible(),
126 .bounds = layer->getBounds(),
127 .transform = layer->getTransform(),
128 .setFrameRateVote = layer->getFrameRateForLayerTree(),
129 .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(),
130 };
131
132 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133
134 // Activate layer if inactive.
135 if (const auto end = activeLayers().end(); it >= end) {
136 std::iter_swap(it, end);
137 mActiveLayersEnd++;
138 }
139}
140
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100141LayerHistory::Summary LayerHistory::summarize(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 LayerHistory::Summary summary;
143
144 std::lock_guard lock(mLock);
145
146 partitionLayers(now);
147
148 for (const auto& [layer, info] : activeLayers()) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700149 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700150 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700151 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
152 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800153
Marin Shalamanov46084422020-10-13 12:33:42 +0200154 const auto vote = info->getRefreshRateVote(now);
Ady Abraham89089c92020-04-15 18:24:41 -0700155 // Skip NoVote layer as those don't have any requirements
Marin Shalamanov46084422020-10-13 12:33:42 +0200156 if (vote.type == LayerHistory::LayerVoteType::NoVote) {
Ady Abraham89089c92020-04-15 18:24:41 -0700157 continue;
158 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800159
Ady Abraham89089c92020-04-15 18:24:41 -0700160 // Compute the layer's position on the screen
Ady Abrahambdda8f02021-04-01 16:06:11 -0700161 const Rect bounds = Rect(info->getBounds());
162 const ui::Transform transform = info->getTransform();
Ady Abraham89089c92020-04-15 18:24:41 -0700163 constexpr bool roundOutwards = true;
164 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165
Ady Abraham89089c92020-04-15 18:24:41 -0700166 const float layerArea = transformed.getWidth() * transformed.getHeight();
167 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700168 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Ady Abraham62a0be22020-12-08 16:54:10 -0800169 vote.seamlessness, weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170
Ady Abraham89089c92020-04-15 18:24:41 -0700171 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700172 trace(*info, vote.type, vote.fps.getIntValue());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800173 }
174 }
175
176 return summary;
177}
178
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100179void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800180 const nsecs_t threshold = getActiveLayerThreshold(now);
181
182 // Collect expired and inactive layers after active layers.
183 size_t i = 0;
184 while (i < mActiveLayersEnd) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700185 auto& [layerUnsafe, info] = mLayerInfos[i];
186 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800187 i++;
188 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700189 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800190 const auto voteType = [&]() {
191 switch (frameRate.type) {
192 case Layer::FrameRateCompatibility::Default:
193 return LayerVoteType::ExplicitDefault;
194 case Layer::FrameRateCompatibility::ExactOrMultiple:
195 return LayerVoteType::ExplicitExactOrMultiple;
196 case Layer::FrameRateCompatibility::NoVote:
197 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800198 case Layer::FrameRateCompatibility::Exact:
199 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800200 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800201 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700202
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100203 if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700204 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100205 info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800206 } else {
207 info->resetLayerVote();
208 }
209 continue;
210 }
211
212 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700213 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800214 }
215
Ady Abraham983e5682020-05-28 16:49:18 -0700216 info->onLayerInactive(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800217 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
218 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219}
220
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100221void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800222 std::lock_guard lock(mLock);
223
224 for (const auto& [layer, info] : activeLayers()) {
Ady Abraham983e5682020-05-28 16:49:18 -0700225 info->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800226 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800227}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700228
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100229std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700230 std::lock_guard lock(mLock);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100231 return base::StringPrintf("LayerHistory{size=%zu, active=%zu}", mLayerInfos.size(),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700232 mActiveLayersEnd);
233}
234
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100235} // namespace android::scheduler