blob: 101060c1acea6c2c0c78f3a5660dad914003953a [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) {
Ady Abraham60e42ea2020-03-09 19:17:31 -070043 if (layer.getFrameRateForLayerTree().rate > 0) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080044 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;
Ady Abraham71c437d2020-01-31 15:56:57 -080066 const auto explicitDefaultVoteTag = "LFPS ExplicitDefault" + name;
67 const auto explicitExactOrMultipleVoteTag = "LFPS ExplicitExactOrMultiple" + name;
Ady Abraham8a82ba62020-01-17 12:43:17 -080068 const auto minVoteTag = "LFPS Min " + name;
69 const auto maxVoteTag = "LFPS Max " + name;
70
71 ATRACE_INT(noVoteTag.c_str(), type == LayerHistory::LayerVoteType::NoVote ? 1 : 0);
72 ATRACE_INT(heuristicVoteTag.c_str(), type == LayerHistory::LayerVoteType::Heuristic ? fps : 0);
Ady Abraham71c437d2020-01-31 15:56:57 -080073 ATRACE_INT(explicitDefaultVoteTag.c_str(),
74 type == LayerHistory::LayerVoteType::ExplicitDefault ? fps : 0);
75 ATRACE_INT(explicitExactOrMultipleVoteTag.c_str(),
76 type == LayerHistory::LayerVoteType::ExplicitExactOrMultiple ? fps : 0);
Ady Abraham8a82ba62020-01-17 12:43:17 -080077 ATRACE_INT(minVoteTag.c_str(), type == LayerHistory::LayerVoteType::Min ? 1 : 0);
78 ATRACE_INT(maxVoteTag.c_str(), type == LayerHistory::LayerVoteType::Max ? 1 : 0);
79
80 ALOGD("%s: %s @ %d Hz", __FUNCTION__, name.c_str(), fps);
81}
Ady Abraham8a82ba62020-01-17 12:43:17 -080082} // namespace
83
84LayerHistoryV2::LayerHistoryV2()
85 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {}
86LayerHistoryV2::~LayerHistoryV2() = default;
87
88void LayerHistoryV2::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate,
89 LayerVoteType type) {
90 const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate);
91 auto info = std::make_unique<LayerInfoV2>(highRefreshRatePeriod, type);
92 std::lock_guard lock(mLock);
93 mLayerInfos.emplace_back(layer, std::move(info));
94}
95
96void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now) {
97 std::lock_guard lock(mLock);
98
99 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
100 [layer](const auto& pair) { return pair.first == layer; });
101 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
102
103 const auto& info = it->second;
104 info->setLastPresentTime(presentTime, now);
105
106 // Activate layer if inactive.
107 if (const auto end = activeLayers().end(); it >= end) {
108 std::iter_swap(it, end);
109 mActiveLayersEnd++;
110 }
111}
112
113LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) {
114 LayerHistory::Summary summary;
115
116 std::lock_guard lock(mLock);
117
118 partitionLayers(now);
119
120 for (const auto& [layer, info] : activeLayers()) {
121 const auto strong = layer.promote();
122 if (!strong) {
123 continue;
124 }
125
Ana Krulec3803b8d2020-02-03 16:35:46 -0800126 // TODO(b/144307188): This needs to be plugged into layer summary as
127 // an additional parameter.
128 ALOGV("Layer has priority: %d", strong->getFrameRateSelectionPriority());
129
Ady Abraham8a82ba62020-01-17 12:43:17 -0800130 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
Ady Abraham60e42ea2020-03-09 19:17:31 -0700169 const auto frameRate = layer->getFrameRateForLayerTree();
Ady Abraham71c437d2020-01-31 15:56:57 -0800170 const auto voteType = [&]() {
171 switch (frameRate.type) {
172 case Layer::FrameRateCompatibility::Default:
173 return LayerVoteType::ExplicitDefault;
174 case Layer::FrameRateCompatibility::ExactOrMultiple:
175 return LayerVoteType::ExplicitExactOrMultiple;
176 case Layer::FrameRateCompatibility::NoVote:
177 return LayerVoteType::NoVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800178 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800179 }();
180 if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) {
181 info->setLayerVote(voteType, frameRate.rate);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800182 } else {
183 info->resetLayerVote();
184 }
185 continue;
186 }
187
188 if (CC_UNLIKELY(mTraceEnabled)) {
189 trace(weak, LayerHistory::LayerVoteType::NoVote, 0);
190 }
191
192 info->clearHistory();
193 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
194 }
195
196 // Collect expired layers after inactive layers.
197 size_t end = mLayerInfos.size();
198 while (i < end) {
199 if (mLayerInfos[i].first.promote()) {
200 i++;
201 } else {
202 std::swap(mLayerInfos[i], mLayerInfos[--end]);
203 }
204 }
205
206 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end());
207}
208
209void LayerHistoryV2::clear() {
210 std::lock_guard lock(mLock);
211
212 for (const auto& [layer, info] : activeLayers()) {
213 info->clearHistory();
214 }
215
216 mActiveLayersEnd = 0;
217}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800218} // namespace android::scheduler::impl