1 /*
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 "LayerHistory"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21 #include "LayerHistory.h"
22
23 #include <android-base/stringprintf.h>
24 #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"
35 #include "LayerInfo.h"
36
37 namespace android::scheduler {
38
39 namespace {
40
isLayerActive(const LayerInfo & info,nsecs_t threshold)41 bool isLayerActive(const LayerInfo& info, nsecs_t threshold) {
42 // Layers with an explicit vote are always kept active
43 if (info.getSetFrameRateVote().rate.isValid()) {
44 return true;
45 }
46
47 return info.isVisible() && info.getLastUpdatedTime() >= threshold;
48 }
49
traceEnabled()50 bool traceEnabled() {
51 return property_get_bool("debug.sf.layer_history_trace", false);
52 }
53
useFrameRatePriority()54 bool useFrameRatePriority() {
55 char value[PROPERTY_VALUE_MAX];
56 property_get("debug.sf.use_frame_rate_priority", value, "1");
57 return atoi(value);
58 }
59
trace(const LayerInfo & info,LayerHistory::LayerVoteType type,int fps)60 void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) {
61 const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) {
62 ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0);
63 };
64
65 traceType(LayerHistory::LayerVoteType::NoVote, 1);
66 traceType(LayerHistory::LayerVoteType::Heuristic, fps);
67 traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps);
68 traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps);
69 traceType(LayerHistory::LayerVoteType::ExplicitExact, fps);
70 traceType(LayerHistory::LayerVoteType::Min, 1);
71 traceType(LayerHistory::LayerVoteType::Max, 1);
72
73 ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps);
74 }
75 } // namespace
76
LayerHistory()77 LayerHistory::LayerHistory()
78 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {
79 LayerInfo::setTraceEnabled(mTraceEnabled);
80 }
81
82 LayerHistory::~LayerHistory() = default;
83
registerLayer(Layer * layer,LayerVoteType type)84 void LayerHistory::registerLayer(Layer* layer, LayerVoteType type) {
85 std::lock_guard lock(mLock);
86 LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first != LayerStatus::NotFound,
87 "%s already registered", layer->getName().c_str());
88 auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type);
89
90 // The layer can be placed on either map, it is assumed that partitionLayers() will be called
91 // to correct them.
92 mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))});
93 }
94
deregisterLayer(Layer * layer)95 void LayerHistory::deregisterLayer(Layer* layer) {
96 std::lock_guard lock(mLock);
97 if (!mActiveLayerInfos.erase(layer->getSequence())) {
98 if (!mInactiveLayerInfos.erase(layer->getSequence())) {
99 LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer);
100 }
101 }
102 }
103
record(Layer * layer,nsecs_t presentTime,nsecs_t now,LayerUpdateType updateType)104 void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now,
105 LayerUpdateType updateType) {
106 std::lock_guard lock(mLock);
107 auto id = layer->getSequence();
108
109 auto [found, layerPair] = findLayer(id);
110 if (found == LayerStatus::NotFound) {
111 // Offscreen layer
112 ALOGV("%s: %s not registered", __func__, layer->getName().c_str());
113 return;
114 }
115
116 const auto& info = layerPair->second;
117 const auto layerProps = LayerInfo::LayerProps{
118 .visible = layer->isVisible(),
119 .bounds = layer->getBounds(),
120 .transform = layer->getTransform(),
121 .setFrameRateVote = layer->getFrameRateForLayerTree(),
122 .frameRateSelectionPriority = layer->getFrameRateSelectionPriority(),
123 };
124
125 info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps);
126
127 // Activate layer if inactive.
128 if (found == LayerStatus::LayerInInactiveMap) {
129 mActiveLayerInfos.insert(
130 {id, std::make_pair(layerPair->first, std::move(layerPair->second))});
131 mInactiveLayerInfos.erase(id);
132 }
133 }
134
summarize(const RefreshRateConfigs & configs,nsecs_t now)135 auto LayerHistory::summarize(const RefreshRateConfigs& configs, nsecs_t now) -> Summary {
136 Summary summary;
137
138 std::lock_guard lock(mLock);
139
140 partitionLayers(now);
141
142 for (const auto& [key, value] : mActiveLayerInfos) {
143 auto& info = value.second;
144 const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority();
145 const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority);
146 ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority,
147 layerFocused ? "" : "not");
148
149 const auto vote = info->getRefreshRateVote(configs, now);
150 // Skip NoVote layer as those don't have any requirements
151 if (vote.type == LayerVoteType::NoVote) {
152 continue;
153 }
154
155 // Compute the layer's position on the screen
156 const Rect bounds = Rect(info->getBounds());
157 const ui::Transform transform = info->getTransform();
158 constexpr bool roundOutwards = true;
159 Rect transformed = transform.transform(bounds, roundOutwards);
160
161 const float layerArea = transformed.getWidth() * transformed.getHeight();
162 float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f;
163 summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps,
164 vote.seamlessness, weight, layerFocused});
165
166 if (CC_UNLIKELY(mTraceEnabled)) {
167 trace(*info, vote.type, vote.fps.getIntValue());
168 }
169 }
170
171 return summary;
172 }
173
partitionLayers(nsecs_t now)174 void LayerHistory::partitionLayers(nsecs_t now) {
175 const nsecs_t threshold = getActiveLayerThreshold(now);
176
177 // iterate over inactive map
178 LayerInfos::iterator it = mInactiveLayerInfos.begin();
179 while (it != mInactiveLayerInfos.end()) {
180 auto& [layerUnsafe, info] = it->second;
181 if (isLayerActive(*info, threshold)) {
182 // move this to the active map
183
184 mActiveLayerInfos.insert({it->first, std::move(it->second)});
185 it = mInactiveLayerInfos.erase(it);
186 } else {
187 if (CC_UNLIKELY(mTraceEnabled)) {
188 trace(*info, LayerVoteType::NoVote, 0);
189 }
190 info->onLayerInactive(now);
191 it++;
192 }
193 }
194
195 // iterate over active map
196 it = mActiveLayerInfos.begin();
197 while (it != mActiveLayerInfos.end()) {
198 auto& [layerUnsafe, info] = it->second;
199 if (isLayerActive(*info, threshold)) {
200 // Set layer vote if set
201 const auto frameRate = info->getSetFrameRateVote();
202 const auto voteType = [&]() {
203 switch (frameRate.type) {
204 case Layer::FrameRateCompatibility::Default:
205 return LayerVoteType::ExplicitDefault;
206 case Layer::FrameRateCompatibility::ExactOrMultiple:
207 return LayerVoteType::ExplicitExactOrMultiple;
208 case Layer::FrameRateCompatibility::NoVote:
209 return LayerVoteType::NoVote;
210 case Layer::FrameRateCompatibility::Exact:
211 return LayerVoteType::ExplicitExact;
212 }
213 }();
214
215 if (frameRate.rate.isValid() || voteType == LayerVoteType::NoVote) {
216 const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote;
217 info->setLayerVote({type, frameRate.rate, frameRate.seamlessness});
218 } else {
219 info->resetLayerVote();
220 }
221
222 it++;
223 } else {
224 if (CC_UNLIKELY(mTraceEnabled)) {
225 trace(*info, LayerVoteType::NoVote, 0);
226 }
227 info->onLayerInactive(now);
228 // move this to the inactive map
229 mInactiveLayerInfos.insert({it->first, std::move(it->second)});
230 it = mActiveLayerInfos.erase(it);
231 }
232 }
233 }
234
clear()235 void LayerHistory::clear() {
236 std::lock_guard lock(mLock);
237 for (const auto& [key, value] : mActiveLayerInfos) {
238 value.second->clearHistory(systemTime());
239 }
240 }
241
dump() const242 std::string LayerHistory::dump() const {
243 std::lock_guard lock(mLock);
244 return base::StringPrintf("LayerHistory{size=%zu, active=%zu}",
245 mActiveLayerInfos.size() + mInactiveLayerInfos.size(),
246 mActiveLayerInfos.size());
247 }
248
getLayerFramerate(nsecs_t now,int32_t id) const249 float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const {
250 std::lock_guard lock(mLock);
251 auto [found, layerPair] = findLayer(id);
252 if (found != LayerStatus::NotFound) {
253 return layerPair->second->getFps(now).getValue();
254 }
255 return 0.f;
256 }
257
findLayer(int32_t id)258 auto LayerHistory::findLayer(int32_t id) -> std::pair<LayerStatus, LayerPair*> {
259 // the layer could be in either the active or inactive map, try both
260 auto it = mActiveLayerInfos.find(id);
261 if (it != mActiveLayerInfos.end()) {
262 return {LayerStatus::LayerInActiveMap, &(it->second)};
263 }
264 it = mInactiveLayerInfos.find(id);
265 if (it != mInactiveLayerInfos.end()) {
266 return {LayerStatus::LayerInInactiveMap, &(it->second)};
267 }
268 return {LayerStatus::NotFound, nullptr};
269 }
270
271 } // namespace android::scheduler
272