blob: d709530990208aa5f45f69dd2e934d08dabe1c40 [file] [log] [blame]
Vishnu Nair04f89692022-11-16 23:21:05 +00001/*
2 * Copyright 2022 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#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070019#define LOG_TAG "SurfaceFlinger"
Vishnu Nair04f89692022-11-16 23:21:05 +000020
qinyige10e011d02024-05-23 15:59:08 +080021#include <android-base/logging.h>
22
Vishnu Nair04f89692022-11-16 23:21:05 +000023#include "LayerHierarchy.h"
Vishnu Nair80a5a702023-02-11 01:21:51 +000024#include "LayerLog.h"
Vishnu Nair04f89692022-11-16 23:21:05 +000025#include "SwapErase.h"
26
27namespace android::surfaceflinger::frontend {
28
29namespace {
30auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
31 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
32 auto lhsLayer = lhs.first->getLayer();
33 auto rhsLayer = rhs.first->getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +000034 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
Vishnu Nair444f3952023-04-11 13:01:02 -070035 return lhsLayer->layerStack.id < rhsLayer->layerStack.id;
Vishnu Nair04f89692022-11-16 23:21:05 +000036 }
37 if (lhsLayer->z != rhsLayer->z) {
38 return lhsLayer->z < rhsLayer->z;
39 }
40 return lhsLayer->id < rhsLayer->id;
41};
42
43void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec,
44 std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) {
45 auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare);
46 vec.insert(it, std::move(value));
47}
48} // namespace
49
50LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
51
52LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
53 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
54 mChildren = hierarchy.mChildren;
55}
56
Melody Hsubb2ec6a2024-05-27 22:18:05 +000057void LayerHierarchy::traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& traversalPath,
58 uint32_t depth) const {
59 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
60 "Cycle detected in LayerHierarchy::traverse. See "
61 "traverse_stack_overflow_transactions.winscope");
62
Vishnu Nair04f89692022-11-16 23:21:05 +000063 if (mLayer) {
64 bool breakTraversal = !visitor(*this, traversalPath);
65 if (breakTraversal) {
66 return;
67 }
68 }
Vishnu Nair606d9d02023-08-19 14:20:18 -070069
70 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
71 traversalPath.invalidRelativeRootId);
Vishnu Nair04f89692022-11-16 23:21:05 +000072 for (auto& [child, childVariant] : mChildren) {
73 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
74 childVariant);
Melody Hsubb2ec6a2024-05-27 22:18:05 +000075 child->traverse(visitor, traversalPath, depth + 1);
Vishnu Nair04f89692022-11-16 23:21:05 +000076 }
77}
78
79void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
80 LayerHierarchy::TraversalPath& traversalPath) const {
81 bool traverseThisLayer = (mLayer != nullptr);
82 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
83 auto& [child, childVariant] = *it;
84 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000085 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000086 bool breakTraversal = !visitor(*this, traversalPath);
87 if (breakTraversal) {
88 return;
89 }
Vishnu Nair04f89692022-11-16 23:21:05 +000090 }
91 if (childVariant == LayerHierarchy::Variant::Detached) {
92 continue;
93 }
94 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
95 childVariant);
96 child->traverseInZOrder(visitor, traversalPath);
97 }
98
99 if (traverseThisLayer) {
100 visitor(*this, traversalPath);
101 }
102}
103
104void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
105 insertSorted(mChildren, {child, variant});
106}
107
108void LayerHierarchy::removeChild(LayerHierarchy* child) {
109 auto it = std::find_if(mChildren.begin(), mChildren.end(),
110 [child](const std::pair<LayerHierarchy*, Variant>& x) {
111 return x.first == child;
112 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700113 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
Vishnu Nair04f89692022-11-16 23:21:05 +0000114 mChildren.erase(it);
115}
116
117void LayerHierarchy::sortChildrenByZOrder() {
118 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
119}
120
121void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
122 auto it = std::find_if(mChildren.begin(), mChildren.end(),
123 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
124 return child.first == hierarchy;
125 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700126 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
127 it->second = variant;
Vishnu Nair04f89692022-11-16 23:21:05 +0000128}
129
130const RequestedLayerState* LayerHierarchy::getLayer() const {
131 return mLayer;
132}
133
Vishnu Nairea6ff812023-02-27 17:41:39 +0000134const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
135 return mRelativeParent;
136}
137
138const LayerHierarchy* LayerHierarchy::getParent() const {
139 return mParent;
140}
141
Vishnu Nair04f89692022-11-16 23:21:05 +0000142std::string LayerHierarchy::getDebugStringShort() const {
143 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000144 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000145 if (mChildren.empty()) {
146 debug += "no children";
147 } else {
148 debug += std::to_string(mChildren.size()) + " children";
149 }
150 return debug + "}";
151}
152
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000153void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
Vishnu Nair6f878312023-09-08 11:05:01 -0700154 LayerHierarchy::Variant variant, bool isLastChild,
155 bool includeMirroredHierarchy) const {
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000156 if (!mLayer) {
157 out << " ROOT";
158 } else {
159 out << prefix + (isLastChild ? "└─ " : "├─ ");
160 if (variant == LayerHierarchy::Variant::Relative) {
161 out << "(Relative) ";
Vishnu Nair491827d2024-04-29 23:43:26 +0000162 } else if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700163 if (!includeMirroredHierarchy) {
164 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
165 return;
166 }
167 out << "(Mirroring) ";
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000168 }
169 out << *mLayer;
Vishnu Nair04f89692022-11-16 23:21:05 +0000170 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000171
172 for (size_t i = 0; i < mChildren.size(); i++) {
173 auto& [child, childVariant] = mChildren[i];
174 if (childVariant == LayerHierarchy::Variant::Detached) continue;
175 const bool lastChild = i == (mChildren.size() - 1);
176 std::string childPrefix = prefix;
177 if (mLayer) {
178 childPrefix += (isLastChild ? " " : "│ ");
179 }
180 out << "\n";
Vishnu Nair6f878312023-09-08 11:05:01 -0700181 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000182 }
183 return;
Vishnu Nair04f89692022-11-16 23:21:05 +0000184}
185
186bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
187 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
188 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
189 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
190 if (traversalPath.hasRelZLoop()) {
191 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
192 return false;
193 }
194 return true;
195 });
196 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
197}
198
Vishnu Naira0292282023-12-16 14:32:00 -0800199void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
200 mLayerIdToHierarchy.clear();
201 mHierarchies.clear();
202 mRoot = nullptr;
203 mOffscreenRoot = nullptr;
204
Vishnu Nair04f89692022-11-16 23:21:05 +0000205 mHierarchies.reserve(layers.size());
206 mLayerIdToHierarchy.reserve(layers.size());
207 for (auto& layer : layers) {
208 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
209 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
210 }
211 for (const auto& layer : layers) {
212 onLayerAdded(layer.get());
213 }
214 detachHierarchyFromRelativeParent(&mOffscreenRoot);
Vishnu Naira0292282023-12-16 14:32:00 -0800215 mInitialized = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000216}
217
218void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
219 auto layer = hierarchy->mLayer;
220 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
221 ? LayerHierarchy::Variant::Detached
222 : LayerHierarchy::Variant::Attached;
223
224 LayerHierarchy* parent;
225
226 if (layer->parentId != UNASSIGNED_LAYER_ID) {
227 parent = getHierarchyFromId(layer->parentId);
228 } else if (layer->canBeRoot) {
229 parent = &mRoot;
230 } else {
231 parent = &mOffscreenRoot;
232 }
233 parent->addChild(hierarchy, type);
234 hierarchy->mParent = parent;
235}
236
237void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
238 hierarchy->mParent->removeChild(hierarchy);
239 hierarchy->mParent = nullptr;
240}
241
242void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
243 auto layer = hierarchy->mLayer;
244 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
245 return;
246 }
247
248 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
249 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
250 } else {
251 hierarchy->mRelativeParent = &mOffscreenRoot;
252 }
253 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
254 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
255}
256
257void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
258 if (hierarchy->mRelativeParent) {
259 hierarchy->mRelativeParent->removeChild(hierarchy);
260 }
261 hierarchy->mRelativeParent = nullptr;
262 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
263}
264
Vishnu Nairf12a6782024-06-03 23:08:15 +0000265std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) {
266 std::vector<LayerHierarchy*> hierarchies;
267 hierarchies.push_back(root);
268 std::vector<LayerHierarchy*> descendants;
269 for (size_t i = 0; i < hierarchies.size(); i++) {
270 LayerHierarchy* hierarchy = hierarchies[i];
271 if (hierarchy->mLayer) {
272 descendants.push_back(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000273 }
Vishnu Nairf12a6782024-06-03 23:08:15 +0000274 for (auto& [child, childVariant] : hierarchy->mChildren) {
275 if (childVariant == LayerHierarchy::Variant::Detached ||
276 childVariant == LayerHierarchy::Variant::Attached) {
277 hierarchies.push_back(child);
278 }
279 }
280 }
281 return descendants;
282}
283
284void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
285 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
286 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
287 attachToRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000288 }
289}
290
291void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
Vishnu Nairf12a6782024-06-03 23:08:15 +0000292 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
293 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
294 detachFromRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000295 }
296}
297
298void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
299 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
300 attachToParent(hierarchy);
301 attachToRelativeParent(hierarchy);
302
Vishnu Naira9c43762023-01-27 19:10:25 +0000303 for (uint32_t mirrorId : layer->mirrorIds) {
304 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000305 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
306 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000307 if (FlagManager::getInstance().detached_mirror()) {
308 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
309 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
310 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
311 }
312 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000313}
314
315void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000316 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000317 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
318 if (!hierarchy) {
319 // Layer was never part of the hierarchy if it was created and destroyed in the same
320 // transaction.
321 return;
322 }
323 // detach from parent
324 detachFromRelativeParent(hierarchy);
325 detachFromParent(hierarchy);
326
327 // detach children
328 for (auto& [child, variant] : hierarchy->mChildren) {
329 if (variant == LayerHierarchy::Variant::Attached ||
330 variant == LayerHierarchy::Variant::Detached) {
331 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
332 child->mParent = &mOffscreenRoot;
333 } else if (variant == LayerHierarchy::Variant::Relative) {
334 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
335 child->mRelativeParent = &mOffscreenRoot;
336 }
337 }
338
339 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
340 return layerHierarchy.get() == hierarchy;
341 });
342 mLayerIdToHierarchy.erase(layer->id);
343}
344
345void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
346 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
347 auto it = hierarchy->mChildren.begin();
348 while (it != hierarchy->mChildren.end()) {
Vishnu Nair491827d2024-04-29 23:43:26 +0000349 if (LayerHierarchy::isMirror(it->second)) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000350 it = hierarchy->mChildren.erase(it);
351 } else {
352 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000353 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000354 }
355
Vishnu Naira9c43762023-01-27 19:10:25 +0000356 for (uint32_t mirrorId : layer->mirrorIds) {
357 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000358 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000359 if (FlagManager::getInstance().detached_mirror()) {
360 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
361 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
362 LayerHierarchy::Variant::Detached_Mirror);
363 }
364 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000365}
366
Vishnu Naira0292282023-12-16 14:32:00 -0800367void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000368 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
369 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
370 // rebuild map
371 for (auto& layer : layers) {
372 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
373 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
374 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
375 }
376 }
377
378 for (auto& layer : layers) {
379 if (layer->changes.get() == 0) {
380 continue;
381 }
382 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
383 onLayerAdded(layer.get());
384 continue;
385 }
386 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
387 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
388 detachFromParent(hierarchy);
389 attachToParent(hierarchy);
390 }
391 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
392 detachFromRelativeParent(hierarchy);
393 attachToRelativeParent(hierarchy);
394 }
395 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
396 hierarchy->mParent->sortChildrenByZOrder();
397 if (hierarchy->mRelativeParent) {
398 hierarchy->mRelativeParent->sortChildrenByZOrder();
399 }
400 }
401 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
402 updateMirrorLayer(layer.get());
403 }
404 }
405
406 for (auto& layer : destroyedLayers) {
407 onLayerDestroyed(layer.get());
408 }
409 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
410 // from our relative parents. This walks down both trees to do so. We can optimize this
411 // further by tracking onscreen, offscreen state in LayerHierarchy.
412 detachHierarchyFromRelativeParent(&mOffscreenRoot);
413 attachHierarchyToRelativeParent(&mRoot);
414}
415
Vishnu Naira0292282023-12-16 14:32:00 -0800416void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
417 if (!mInitialized) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000418 SFTRACE_NAME("LayerHierarchyBuilder:init");
Vishnu Naira0292282023-12-16 14:32:00 -0800419 init(layerLifecycleManager.getLayers());
420 } else if (layerLifecycleManager.getGlobalChanges().test(
421 RequestedLayerState::Changes::Hierarchy)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000422 SFTRACE_NAME("LayerHierarchyBuilder:update");
Vishnu Naira0292282023-12-16 14:32:00 -0800423 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
424 } else {
425 return; // nothing to do
426 }
427
428 uint32_t invalidRelativeRoot;
429 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
430 while (hasRelZLoop) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000431 SFTRACE_NAME("FixRelZLoop");
Vishnu Naira0292282023-12-16 14:32:00 -0800432 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
433 /*overwrite=*/false);
434 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
435 // reinitialize the hierarchy with the updated layer data
436 init(layerLifecycleManager.getLayers());
437 // check if we have any remaining loops
438 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
439 }
440}
441
Vishnu Nair04f89692022-11-16 23:21:05 +0000442const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
443 return mRoot;
444}
445
446const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
447 return mOffscreenRoot;
448}
449
450std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
451 if (depth > 10) return "too deep, loop?";
452 if (layerId == UNASSIGNED_LAYER_ID) return "";
453 auto it = mLayerIdToHierarchy.find(layerId);
454 if (it == mLayerIdToHierarchy.end()) return "not found";
455
456 LayerHierarchy* hierarchy = it->second;
457 if (!hierarchy->mLayer) return "none";
458
459 std::string debug =
460 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
461 if (hierarchy->mRelativeParent) {
462 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
463 }
464 if (hierarchy->mParent) {
465 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
466 }
467 return debug;
468}
469
470LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
471 bool childrenOnly) const {
472 auto it = mLayerIdToHierarchy.find(layerId);
473 if (it == mLayerIdToHierarchy.end()) return {nullptr};
474
475 LayerHierarchy hierarchy(*it->second, childrenOnly);
476 return hierarchy;
477}
478
479LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
480 auto it = mLayerIdToHierarchy.find(layerId);
481 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700482 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
483 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000484 return nullptr;
485 };
486
487 return it->second;
488}
489
qinyige10e011d02024-05-23 15:59:08 +0800490void LayerHierarchyBuilder::logSampledChildren(const LayerHierarchy& hierarchy) const {
491 LOG(ERROR) << "Dumping random sampling of child layers.";
492 int sampleSize = static_cast<int>(hierarchy.mChildren.size() / 100 + 1);
493 for (const auto& [child, variant] : hierarchy.mChildren) {
494 if (rand() % sampleSize == 0) {
495 LOG(ERROR) << "Child Layer: " << *(child->mLayer);
496 }
497 }
498}
499
500void LayerHierarchyBuilder::dumpLayerSample(const LayerHierarchy& root) const {
501 LOG(ERROR) << "Dumping layer keeping > 20 children alive:";
502 // If mLayer is nullptr, it will be skipped while traversing.
503 if (!root.mLayer && root.mChildren.size() > 20) {
504 LOG(ERROR) << "ROOT has " << root.mChildren.size() << " children";
505 logSampledChildren(root);
506 }
507 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
508 if (hierarchy.mChildren.size() <= 20) {
509 return true;
510 }
511 // mLayer is ensured to be non-null. See LayerHierarchy::traverse.
512 const auto* layer = hierarchy.mLayer;
513 const auto childrenCount = hierarchy.mChildren.size();
514 LOG(ERROR) << "Layer " << *layer << " has " << childrenCount << " children";
515
516 const auto* parent = hierarchy.mParent;
517 while (parent != nullptr) {
518 if (!parent->mLayer) break;
519 LOG(ERROR) << "Parent Layer: " << *(parent->mLayer);
520 parent = parent->mParent;
521 }
522
523 logSampledChildren(hierarchy);
524 // Stop traversing.
525 return false;
526 });
527 LOG(ERROR) << "Dumping random sampled layers.";
528 size_t numLayers = 0;
529 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
530 if (hierarchy.mLayer) numLayers++;
531 if ((rand() % 20 == 13) && hierarchy.mLayer) {
532 LOG(ERROR) << "Layer: " << *(hierarchy.mLayer);
533 }
534 return true;
535 });
536 LOG(ERROR) << "Total layer count: " << numLayers;
537}
538
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000539const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000540 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
541
542std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000543 if (id == UNASSIGNED_LAYER_ID) {
544 return "TraversalPath{ROOT}";
545 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000546 std::stringstream ss;
547 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000548
Vishnu Nair6f878312023-09-08 11:05:01 -0700549 if (!mirrorRootIds.empty()) {
550 ss << ", .mirrorRootIds=";
551 for (auto rootId : mirrorRootIds) {
552 ss << rootId << ",";
553 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000554 }
555
556 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000557 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000558 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000559 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000560 }
561 }
562
563 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000564 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000565 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000566 ss << "}";
567 return ss.str();
568}
Vishnu Nair04f89692022-11-16 23:21:05 +0000569
Vishnu Nair04f89692022-11-16 23:21:05 +0000570// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
571// of scope the TraversalPath is reset to its original state.
572LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
573 uint32_t layerId,
574 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000575 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000576 // Update the traversal id with the child layer id and variant. Parent id and variant are
577 // stored to reset the id upon destruction.
578 traversalPath.id = layerId;
579 traversalPath.variant = variant;
Vishnu Nair491827d2024-04-29 23:43:26 +0000580 if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700581 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000582 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000583 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
584 layerId) != traversalPath.relativeRootIds.end()) {
585 traversalPath.invalidRelativeRootId = layerId;
586 }
587 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000588 } else if (variant == LayerHierarchy::Variant::Detached) {
589 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000590 }
591}
592LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
593 // Reset the traversal id to its original parent state using the state that was saved in
594 // the constructor.
Vishnu Nair491827d2024-04-29 23:43:26 +0000595 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700596 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000597 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000598 mTraversalPath.relativeRootIds.pop_back();
599 }
600 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
601 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
602 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000603 mTraversalPath.id = mParentPath.id;
604 mTraversalPath.variant = mParentPath.variant;
605 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000606}
607
608} // namespace android::surfaceflinger::frontend