blob: ce085f4a1d23361f91172cd76d52334c781dcf37 [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 Abraham71c437d2020-01-31 15:56:57 -080043 if (layer.getFrameRate().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}
82
83} // namespace
84
85LayerHistoryV2::LayerHistoryV2()
86 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {}
87LayerHistoryV2::~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);
92 auto info = std::make_unique<LayerInfoV2>(highRefreshRatePeriod, type);
93 std::lock_guard lock(mLock);
94 mLayerInfos.emplace_back(layer, std::move(info));
95}
96
97void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now) {
98 std::lock_guard lock(mLock);
99
100 const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(),
101 [layer](const auto& pair) { return pair.first == layer; });
102 LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer);
103
104 const auto& info = it->second;
105 info->setLastPresentTime(presentTime, now);
106
107 // Activate layer if inactive.
108 if (const auto end = activeLayers().end(); it >= end) {
109 std::iter_swap(it, end);
110 mActiveLayersEnd++;
111 }
112}
113
114LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) {
115 LayerHistory::Summary summary;
116
117 std::lock_guard lock(mLock);
118
119 partitionLayers(now);
120
121 for (const auto& [layer, info] : activeLayers()) {
122 const auto strong = layer.promote();
123 if (!strong) {
124 continue;
125 }
126
127 const bool recent = info->isRecentlyActive(now);
128 if (recent) {
129 const auto [type, refreshRate] = info->getRefreshRate(now);
130 // Skip NoVote layer as those don't have any requirements
131 if (type == LayerHistory::LayerVoteType::NoVote) {
132 continue;
133 }
134
135 // Compute the layer's position on the screen
136 const Rect bounds = Rect(strong->getBounds());
137 const ui::Transform transform = strong->getTransform();
138 constexpr bool roundOutwards = true;
139 Rect transformed = transform.transform(bounds, roundOutwards);
140
141 const float layerArea = transformed.getWidth() * transformed.getHeight();
142 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
143 summary.push_back({strong->getName(), type, refreshRate, weight});
144
145 if (CC_UNLIKELY(mTraceEnabled)) {
146 trace(layer, type, static_cast<int>(std::round(refreshRate)));
147 }
148 } else if (CC_UNLIKELY(mTraceEnabled)) {
149 trace(layer, LayerHistory::LayerVoteType::NoVote, 0);
150 }
151 }
152
153 return summary;
154}
155
156void LayerHistoryV2::partitionLayers(nsecs_t now) {
157 const nsecs_t threshold = getActiveLayerThreshold(now);
158
159 // Collect expired and inactive layers after active layers.
160 size_t i = 0;
161 while (i < mActiveLayersEnd) {
162 auto& [weak, info] = mLayerInfos[i];
163 if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) {
164 i++;
165 // Set layer vote if set
166 const auto frameRate = layer->getFrameRate();
Ady Abraham71c437d2020-01-31 15:56:57 -0800167 const auto voteType = [&]() {
168 switch (frameRate.type) {
169 case Layer::FrameRateCompatibility::Default:
170 return LayerVoteType::ExplicitDefault;
171 case Layer::FrameRateCompatibility::ExactOrMultiple:
172 return LayerVoteType::ExplicitExactOrMultiple;
173 case Layer::FrameRateCompatibility::NoVote:
174 return LayerVoteType::NoVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800175 }
Ady Abraham71c437d2020-01-31 15:56:57 -0800176 }();
177 if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) {
178 info->setLayerVote(voteType, frameRate.rate);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179 } else {
180 info->resetLayerVote();
181 }
182 continue;
183 }
184
185 if (CC_UNLIKELY(mTraceEnabled)) {
186 trace(weak, LayerHistory::LayerVoteType::NoVote, 0);
187 }
188
189 info->clearHistory();
190 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
191 }
192
193 // Collect expired layers after inactive layers.
194 size_t end = mLayerInfos.size();
195 while (i < end) {
196 if (mLayerInfos[i].first.promote()) {
197 i++;
198 } else {
199 std::swap(mLayerInfos[i], mLayerInfos[--end]);
200 }
201 }
202
203 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end());
204}
205
206void LayerHistoryV2::clear() {
207 std::lock_guard lock(mLock);
208
209 for (const auto& [layer, info] : activeLayers()) {
210 info->clearHistory();
211 }
212
213 mActiveLayersEnd = 0;
214}
215
216} // namespace android::scheduler::impl