blob: f4bc2a10cbf3435d2d31a80ab64308d5b723075b [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 Abrahambdda8f02021-04-01 16:06:11 -070087 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
Ady Abraham8a82ba62020-01-17 12:43:17 -080088 std::lock_guard lock(mLock);
89 mLayerInfos.emplace_back(layer, std::move(info));
90}
91
Ady Abrahambdda8f02021-04-01 16:06:11 -070092void LayerHistory::deregisterLayer(Layer* layer) {
93 std::lock_guard lock(mLock);
94
95 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
96 [layer](const auto& pair) { return pair.first == layer; });
97 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
98
99 const size_t i = static_cast<size_t>(it - mLayerInfos.begin());
100 if (i < mActiveLayersEnd) {
101 mActiveLayersEnd--;
102 }
103 const size_t last = mLayerInfos.size() - 1;
104 std::swap(mLayerInfos[i], mLayerInfos[last]);
105 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(last));
106}
107
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100108void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
109 LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110 std::lock_guard lock(mLock);
111
112 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
113 [layer](const auto& pair) { return pair.first == layer; });
114 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
115
116 const auto& info = it->second;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700117 const auto layerProps = LayerInfo::LayerProps{
118 .visible = layer->isVisible(),
119 .bounds = layer->getBounds(),
120 .transform = layer->getTransform(),
121 .setFrameRateVote = layer->getFrameRateForLayerTree(),
122 .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(),
123 };
124
125 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126
127 // Activate layer if inactive.
128 if (const auto end = activeLayers().end(); it >= end) {
129 std::iter_swap(it, end);
130 mActiveLayersEnd++;
131 }
132}
133
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100134LayerHistory::Summary LayerHistory::summarize(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800135 LayerHistory::Summary summary;
136
137 std::lock_guard lock(mLock);
138
139 partitionLayers(now);
140
141 for (const auto& [layer, info] : activeLayers()) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700142 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700143 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
Ady Abrahambdda8f02021-04-01 16:06:11 -0700144 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
145 layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800146
Marin Shalamanov46084422020-10-13 12:33:42 +0200147 const auto vote = info->getRefreshRateVote(now);
Ady Abraham89089c92020-04-15 18:24:41 -0700148 // Skip NoVote layer as those don't have any requirements
Marin Shalamanov46084422020-10-13 12:33:42 +0200149 if (vote.type == LayerHistory::LayerVoteType::NoVote) {
Ady Abraham89089c92020-04-15 18:24:41 -0700150 continue;
151 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152
Ady Abraham89089c92020-04-15 18:24:41 -0700153 // Compute the layer's position on the screen
Ady Abrahambdda8f02021-04-01 16:06:11 -0700154 const Rect bounds = Rect(info->getBounds());
155 const ui::Transform transform = info->getTransform();
Ady Abraham89089c92020-04-15 18:24:41 -0700156 constexpr bool roundOutwards = true;
157 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158
Ady Abraham89089c92020-04-15 18:24:41 -0700159 const float layerArea = transformed.getWidth() * transformed.getHeight();
160 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700161 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
Ady Abraham62a0be22020-12-08 16:54:10 -0800162 vote.seamlessness, weight, layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800163
Ady Abraham89089c92020-04-15 18:24:41 -0700164 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700165 trace(*info, vote.type, vote.fps.getIntValue());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800166 }
167 }
168
169 return summary;
170}
171
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100172void LayerHistory::partitionLayers(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800173 const nsecs_t threshold = getActiveLayerThreshold(now);
174
175 // Collect expired and inactive layers after active layers.
176 size_t i = 0;
177 while (i < mActiveLayersEnd) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700178 auto& [layerUnsafe, info] = mLayerInfos[i];
179 if (isLayerActive(*info, threshold)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800180 i++;
181 // Set layer vote if set
Ady Abrahambdda8f02021-04-01 16:06:11 -0700182 const auto frameRate = info->getSetFrameRateVote();
Ady Abraham71c437d2020-01-31 15:56:57 -0800183 const auto voteType = [&]() {
184 switch (frameRate.type) {
185 case Layer::FrameRateCompatibility::Default:
186 return LayerVoteType::ExplicitDefault;
187 case Layer::FrameRateCompatibility::ExactOrMultiple:
188 return LayerVoteType::ExplicitExactOrMultiple;
189 case Layer::FrameRateCompatibility::NoVote:
190 return LayerVoteType::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800191 case Layer::FrameRateCompatibility::Exact:
192 return LayerVoteType::ExplicitExact;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800193 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800194 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700195
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100196 if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700197 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100198 info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199 } else {
200 info->resetLayerVote();
201 }
202 continue;
203 }
204
205 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abrahambdda8f02021-04-01 16:06:11 -0700206 trace(*info, LayerHistory::LayerVoteType::NoVote, 0);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800207 }
208
Ady Abraham983e5682020-05-28 16:49:18 -0700209 info->onLayerInactive(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800210 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
211 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800212}
213
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100214void LayerHistory::clear() {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800215 std::lock_guard lock(mLock);
216
217 for (const auto& [layer, info] : activeLayers()) {
Ady Abraham983e5682020-05-28 16:49:18 -0700218 info->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800220}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700221
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100222std::string LayerHistory::dump() const {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700223 std::lock_guard lock(mLock);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100224 return base::StringPrintf("LayerHistory{size=%zu, active=%zu}", mLayerInfos.size(),
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700225 mActiveLayersEnd);
226}
227
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100228} // namespace android::scheduler