blob: a63ccc1df0eecf40246eeaa60484963b94e74d24 [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
18#define LOG_TAG "LayerHistoryV2"
19#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"
Ady Abraham8a82ba62020-01-17 12:43:17 -080035#include "LayerInfoV2.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070036#include "SchedulerUtils.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080037
38namespace android::scheduler::impl {
39
40namespace {
41
42bool isLayerActive(const Layer& layer, const LayerInfoV2& info, nsecs_t threshold) {
Ady Abraham89089c92020-04-15 18:24:41 -070043 // Layers with an explicit vote are always kept active
Ady Abraham60e42ea2020-03-09 19:17:31 -070044 if (layer.getFrameRateForLayerTree().rate > 0) {
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 Abraham8a82ba62020-01-17 12:43:17 -080048 return layer.isVisible() && info.getLastUpdatedTime() >= threshold;
49}
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 Abraham0ccd79b2020-06-10 10:11:17 -070061void trace(const wp<Layer>& weak, const LayerInfoV2& info, LayerHistory::LayerVoteType type,
62 int fps) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080063 const auto layer = weak.promote();
64 if (!layer) return;
65
Ady Abraham0ccd79b2020-06-10 10:11:17 -070066 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
67 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
Ady Abrahama6b676e2020-05-27 14:29:09 -070068 };
69
Ady Abraham0ccd79b2020-06-10 10:11:17 -070070 traceType(LayerHistory::LayerVoteType::NoVote, 1);
71 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
72 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
73 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
74 traceType(LayerHistory::LayerVoteType::Min, 1);
75 traceType(LayerHistory::LayerVoteType::Max, 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -080076
Ady Abrahama6b676e2020-05-27 14:29:09 -070077 ALOGD("%s: %s @ %d Hz", __FUNCTION__, layer->getName().c_str(), fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080078}
Ady Abraham8a82ba62020-01-17 12:43:17 -080079} // namespace
80
Ady Abrahamb1b9d412020-06-01 19:53:52 -070081LayerHistoryV2::LayerHistoryV2(const scheduler::RefreshRateConfigs& refreshRateConfigs)
82 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -070083 LayerInfoV2::setTraceEnabled(mTraceEnabled);
Ady Abrahamb1b9d412020-06-01 19:53:52 -070084 LayerInfoV2::setRefreshRateConfigs(refreshRateConfigs);
85}
86
Ady Abraham8a82ba62020-01-17 12:43:17 -080087LayerHistoryV2::~LayerHistoryV2() = default;
88
89void LayerHistoryV2::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate,
90 LayerVoteType type) {
91 const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate);
Ady Abrahama6b676e2020-05-27 14:29:09 -070092 auto info = std::make_unique<LayerInfoV2>(layer->getName(), highRefreshRatePeriod, type);
Ady Abraham8a82ba62020-01-17 12:43:17 -080093 std::lock_guard lock(mLock);
94 mLayerInfos.emplace_back(layer, std::move(info));
95}
96
Ady Abraham5def7332020-05-29 16:13:47 -070097void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
98 LayerUpdateType updateType) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080099 std::lock_guard lock(mLock);
100
101 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
102 [layer](const auto& pair) { return pair.first == layer; });
103 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
104
105 const auto& info = it->second;
Ady Abraham5def7332020-05-29 16:13:47 -0700106 info->setLastPresentTime(presentTime, now, updateType, mConfigChangePending);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107
108 // Activate layer if inactive.
109 if (const auto end = activeLayers().end(); it >= end) {
110 std::iter_swap(it, end);
111 mActiveLayersEnd++;
112 }
113}
114
115LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) {
116 LayerHistory::Summary summary;
117
118 std::lock_guard lock(mLock);
119
120 partitionLayers(now);
121
122 for (const auto& [layer, info] : activeLayers()) {
123 const auto strong = layer.promote();
124 if (!strong) {
125 continue;
126 }
127
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700128 const auto frameRateSelectionPriority = strong->getFrameRateSelectionPriority();
129 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
130 ALOGV("%s has priority: %d %s focused", strong->getName().c_str(),
131 frameRateSelectionPriority, layerFocused ? "" : "not");
Ana Krulec3803b8d2020-02-03 16:35:46 -0800132
Marin Shalamanov46084422020-10-13 12:33:42 +0200133 const auto vote = info->getRefreshRateVote(now);
Ady Abraham89089c92020-04-15 18:24:41 -0700134 // Skip NoVote layer as those don't have any requirements
Marin Shalamanov46084422020-10-13 12:33:42 +0200135 if (vote.type == LayerHistory::LayerVoteType::NoVote) {
Ady Abraham89089c92020-04-15 18:24:41 -0700136 continue;
137 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800138
Ady Abraham89089c92020-04-15 18:24:41 -0700139 // Compute the layer's position on the screen
140 const Rect bounds = Rect(strong->getBounds());
141 const ui::Transform transform = strong->getTransform();
142 constexpr bool roundOutwards = true;
143 Rect transformed = transform.transform(bounds, roundOutwards);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800144
Ady Abraham89089c92020-04-15 18:24:41 -0700145 const float layerArea = transformed.getWidth() * transformed.getHeight();
146 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
Marin Shalamanov46084422020-10-13 12:33:42 +0200147 summary.push_back({strong->getName(), vote.type, vote.fps, vote.shouldBeSeamless, weight,
148 layerFocused});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149
Ady Abraham89089c92020-04-15 18:24:41 -0700150 if (CC_UNLIKELY(mTraceEnabled)) {
Marin Shalamanov46084422020-10-13 12:33:42 +0200151 trace(layer, *info, vote.type, static_cast<int>(std::round(vote.fps)));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152 }
153 }
154
155 return summary;
156}
157
158void LayerHistoryV2::partitionLayers(nsecs_t now) {
159 const nsecs_t threshold = getActiveLayerThreshold(now);
160
161 // Collect expired and inactive layers after active layers.
162 size_t i = 0;
163 while (i < mActiveLayersEnd) {
164 auto& [weak, info] = mLayerInfos[i];
165 if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) {
166 i++;
167 // Set layer vote if set
Ady Abraham60e42ea2020-03-09 19:17:31 -0700168 const auto frameRate = layer->getFrameRateForLayerTree();
Ady Abraham71c437d2020-01-31 15:56:57 -0800169 const auto voteType = [&]() {
170 switch (frameRate.type) {
171 case Layer::FrameRateCompatibility::Default:
172 return LayerVoteType::ExplicitDefault;
173 case Layer::FrameRateCompatibility::ExactOrMultiple:
174 return LayerVoteType::ExplicitExactOrMultiple;
175 case Layer::FrameRateCompatibility::NoVote:
176 return LayerVoteType::NoVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800177 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800178 }();
Ady Abraham5dde5972020-05-13 10:50:54 -0700179
180 if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) {
181 const auto type = layer->isVisible() ? voteType : LayerVoteType::NoVote;
Marin Shalamanov46084422020-10-13 12:33:42 +0200182 info->setLayerVote({type, frameRate.rate, frameRate.shouldBeSeamless});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800183 } else {
184 info->resetLayerVote();
185 }
186 continue;
187 }
188
189 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700190 trace(weak, *info, LayerHistory::LayerVoteType::NoVote, 0);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800191 }
192
Ady Abraham983e5682020-05-28 16:49:18 -0700193 info->onLayerInactive(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
195 }
196
197 // Collect expired layers after inactive layers.
198 size_t end = mLayerInfos.size();
199 while (i < end) {
200 if (mLayerInfos[i].first.promote()) {
201 i++;
202 } else {
203 std::swap(mLayerInfos[i], mLayerInfos[--end]);
204 }
205 }
206
207 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end());
208}
209
210void LayerHistoryV2::clear() {
211 std::lock_guard lock(mLock);
212
213 for (const auto& [layer, info] : activeLayers()) {
Ady Abraham983e5682020-05-28 16:49:18 -0700214 info->clearHistory(systemTime());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800215 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800216}
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700217
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700218std::string LayerHistoryV2::dump() const {
219 std::lock_guard lock(mLock);
220 return base::StringPrintf("LayerHistoryV2{size=%zu, active=%zu}", mLayerInfos.size(),
221 mActiveLayersEnd);
222}
223
Ady Abraham8a82ba62020-01-17 12:43:17 -0800224} // namespace android::scheduler::impl