blob: a6d2c74c87f27207d422444fd3cc4c97a3e5ec22 [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
Ady Abraham8a82ba62020-01-17 12:43:17 -080023#include <cutils/properties.h>
24#include <utils/Log.h>
25#include <utils/Timers.h>
26#include <utils/Trace.h>
27
28#include <algorithm>
29#include <cmath>
30#include <string>
31#include <utility>
32
33#include "../Layer.h"
34#include "SchedulerUtils.h"
35
Ady Abraham8a82ba62020-01-17 12:43:17 -080036#include "LayerInfoV2.h"
37
38namespace android::scheduler::impl {
39
40namespace {
41
42bool isLayerActive(const Layer& layer, const LayerInfoV2& info, nsecs_t threshold) {
43 if (layer.getFrameRate().has_value()) {
44 return layer.isVisible();
45 }
46 return layer.isVisible() && info.getLastUpdatedTime() >= threshold;
47}
48
49bool traceEnabled() {
50 return property_get_bool("debug.sf.layer_history_trace", false);
51}
52
53bool useFrameRatePriority() {
54 char value[PROPERTY_VALUE_MAX];
55 property_get("debug.sf.use_frame_rate_priority", value, "1");
56 return atoi(value);
57}
58
59void trace(const wp<Layer>& weak, LayerHistory::LayerVoteType type, int fps) {
60 const auto layer = weak.promote();
61 if (!layer) return;
62
63 const auto& name = layer->getName();
64 const auto noVoteTag = "LFPS NoVote " + name;
65 const auto heuristicVoteTag = "LFPS Heuristic " + name;
66 const auto explicitVoteTag = "LFPS Explicit " + name;
67 const auto minVoteTag = "LFPS Min " + name;
68 const auto maxVoteTag = "LFPS Max " + name;
69
70 ATRACE_INT(noVoteTag.c_str(), type == LayerHistory::LayerVoteType::NoVote ? 1 : 0);
71 ATRACE_INT(heuristicVoteTag.c_str(), type == LayerHistory::LayerVoteType::Heuristic ? fps : 0);
72 ATRACE_INT(explicitVoteTag.c_str(), type == LayerHistory::LayerVoteType::Explicit ? fps : 0);
73 ATRACE_INT(minVoteTag.c_str(), type == LayerHistory::LayerVoteType::Min ? 1 : 0);
74 ATRACE_INT(maxVoteTag.c_str(), type == LayerHistory::LayerVoteType::Max ? 1 : 0);
75
76 ALOGD("%s: %s @ %d Hz", __FUNCTION__, name.c_str(), fps);
77}
78
79} // namespace
80
81LayerHistoryV2::LayerHistoryV2()
82 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {}
83LayerHistoryV2::~LayerHistoryV2() = default;
84
85void LayerHistoryV2::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate,
86 LayerVoteType type) {
87 const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate);
88 auto info = std::make_unique<LayerInfoV2>(highRefreshRatePeriod, type);
89 std::lock_guard lock(mLock);
90 mLayerInfos.emplace_back(layer, std::move(info));
91}
92
93void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now) {
94 std::lock_guard lock(mLock);
95
96 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
97 [layer](const auto& pair) { return pair.first == layer; });
98 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
99
100 const auto& info = it->second;
101 info->setLastPresentTime(presentTime, now);
102
103 // Activate layer if inactive.
104 if (const auto end = activeLayers().end(); it >= end) {
105 std::iter_swap(it, end);
106 mActiveLayersEnd++;
107 }
108}
109
110LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) {
111 LayerHistory::Summary summary;
112
113 std::lock_guard lock(mLock);
114
115 partitionLayers(now);
116
117 for (const auto& [layer, info] : activeLayers()) {
118 const auto strong = layer.promote();
119 if (!strong) {
120 continue;
121 }
122
123 const bool recent = info->isRecentlyActive(now);
124 if (recent) {
125 const auto [type, refreshRate] = info->getRefreshRate(now);
126 // Skip NoVote layer as those don't have any requirements
127 if (type == LayerHistory::LayerVoteType::NoVote) {
128 continue;
129 }
130
131 // Compute the layer's position on the screen
132 const Rect bounds = Rect(strong->getBounds());
133 const ui::Transform transform = strong->getTransform();
134 constexpr bool roundOutwards = true;
135 Rect transformed = transform.transform(bounds, roundOutwards);
136
137 const float layerArea = transformed.getWidth() * transformed.getHeight();
138 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
139 summary.push_back({strong->getName(), type, refreshRate, weight});
140
141 if (CC_UNLIKELY(mTraceEnabled)) {
142 trace(layer, type, static_cast<int>(std::round(refreshRate)));
143 }
144 } else if (CC_UNLIKELY(mTraceEnabled)) {
145 trace(layer, LayerHistory::LayerVoteType::NoVote, 0);
146 }
147 }
148
149 return summary;
150}
151
152void LayerHistoryV2::partitionLayers(nsecs_t now) {
153 const nsecs_t threshold = getActiveLayerThreshold(now);
154
155 // Collect expired and inactive layers after active layers.
156 size_t i = 0;
157 while (i < mActiveLayersEnd) {
158 auto& [weak, info] = mLayerInfos[i];
159 if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) {
160 i++;
161 // Set layer vote if set
162 const auto frameRate = layer->getFrameRate();
163 if (frameRate.has_value()) {
164 if (*frameRate == Layer::FRAME_RATE_NO_VOTE) {
165 info->setLayerVote(LayerVoteType::NoVote, 0.f);
166 } else {
167 info->setLayerVote(LayerVoteType::Explicit, *frameRate);
168 }
169 } else {
170 info->resetLayerVote();
171 }
172 continue;
173 }
174
175 if (CC_UNLIKELY(mTraceEnabled)) {
176 trace(weak, LayerHistory::LayerVoteType::NoVote, 0);
177 }
178
179 info->clearHistory();
180 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
181 }
182
183 // Collect expired layers after inactive layers.
184 size_t end = mLayerInfos.size();
185 while (i < end) {
186 if (mLayerInfos[i].first.promote()) {
187 i++;
188 } else {
189 std::swap(mLayerInfos[i], mLayerInfos[--end]);
190 }
191 }
192
193 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end());
194}
195
196void LayerHistoryV2::clear() {
197 std::lock_guard lock(mLock);
198
199 for (const auto& [layer, info] : activeLayers()) {
200 info->clearHistory();
201 }
202
203 mActiveLayersEnd = 0;
204}
205
206} // namespace android::scheduler::impl