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