blob: 39a6b777bbf4b700476f2c39f424b63a3cff2cdb [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
21#include "LayerHierarchy.h"
Vishnu Nair80a5a702023-02-11 01:21:51 +000022#include "LayerLog.h"
Vishnu Nair04f89692022-11-16 23:21:05 +000023#include "SwapErase.h"
24
25namespace android::surfaceflinger::frontend {
26
27namespace {
28auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
29 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
30 auto lhsLayer = lhs.first->getLayer();
31 auto rhsLayer = rhs.first->getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +000032 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
Vishnu Nair444f3952023-04-11 13:01:02 -070033 return lhsLayer->layerStack.id < rhsLayer->layerStack.id;
Vishnu Nair04f89692022-11-16 23:21:05 +000034 }
35 if (lhsLayer->z != rhsLayer->z) {
36 return lhsLayer->z < rhsLayer->z;
37 }
38 return lhsLayer->id < rhsLayer->id;
39};
40
41void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec,
42 std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) {
43 auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare);
44 vec.insert(it, std::move(value));
45}
46} // namespace
47
48LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
49
50LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
51 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
52 mChildren = hierarchy.mChildren;
53}
54
Melody Hsubb2ec6a2024-05-27 22:18:05 +000055void LayerHierarchy::traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& traversalPath,
56 uint32_t depth) const {
57 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
58 "Cycle detected in LayerHierarchy::traverse. See "
59 "traverse_stack_overflow_transactions.winscope");
60
Vishnu Nair04f89692022-11-16 23:21:05 +000061 if (mLayer) {
62 bool breakTraversal = !visitor(*this, traversalPath);
63 if (breakTraversal) {
64 return;
65 }
66 }
Vishnu Nair606d9d02023-08-19 14:20:18 -070067
68 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
69 traversalPath.invalidRelativeRootId);
Vishnu Nair04f89692022-11-16 23:21:05 +000070 for (auto& [child, childVariant] : mChildren) {
71 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
72 childVariant);
Melody Hsubb2ec6a2024-05-27 22:18:05 +000073 child->traverse(visitor, traversalPath, depth + 1);
Vishnu Nair04f89692022-11-16 23:21:05 +000074 }
75}
76
77void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
78 LayerHierarchy::TraversalPath& traversalPath) const {
79 bool traverseThisLayer = (mLayer != nullptr);
80 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
81 auto& [child, childVariant] = *it;
82 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000083 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000084 bool breakTraversal = !visitor(*this, traversalPath);
85 if (breakTraversal) {
86 return;
87 }
Vishnu Nair04f89692022-11-16 23:21:05 +000088 }
89 if (childVariant == LayerHierarchy::Variant::Detached) {
90 continue;
91 }
92 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
93 childVariant);
94 child->traverseInZOrder(visitor, traversalPath);
95 }
96
97 if (traverseThisLayer) {
98 visitor(*this, traversalPath);
99 }
100}
101
102void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
103 insertSorted(mChildren, {child, variant});
104}
105
106void LayerHierarchy::removeChild(LayerHierarchy* child) {
107 auto it = std::find_if(mChildren.begin(), mChildren.end(),
108 [child](const std::pair<LayerHierarchy*, Variant>& x) {
109 return x.first == child;
110 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700111 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
Vishnu Nair04f89692022-11-16 23:21:05 +0000112 mChildren.erase(it);
113}
114
115void LayerHierarchy::sortChildrenByZOrder() {
116 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
117}
118
119void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
120 auto it = std::find_if(mChildren.begin(), mChildren.end(),
121 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
122 return child.first == hierarchy;
123 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700124 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
125 it->second = variant;
Vishnu Nair04f89692022-11-16 23:21:05 +0000126}
127
128const RequestedLayerState* LayerHierarchy::getLayer() const {
129 return mLayer;
130}
131
Vishnu Nairea6ff812023-02-27 17:41:39 +0000132const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
133 return mRelativeParent;
134}
135
136const LayerHierarchy* LayerHierarchy::getParent() const {
137 return mParent;
138}
139
Vishnu Nair04f89692022-11-16 23:21:05 +0000140std::string LayerHierarchy::getDebugStringShort() const {
141 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000142 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000143 if (mChildren.empty()) {
144 debug += "no children";
145 } else {
146 debug += std::to_string(mChildren.size()) + " children";
147 }
148 return debug + "}";
149}
150
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000151void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
Vishnu Nair6f878312023-09-08 11:05:01 -0700152 LayerHierarchy::Variant variant, bool isLastChild,
153 bool includeMirroredHierarchy) const {
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000154 if (!mLayer) {
155 out << " ROOT";
156 } else {
157 out << prefix + (isLastChild ? "└─ " : "├─ ");
158 if (variant == LayerHierarchy::Variant::Relative) {
159 out << "(Relative) ";
Vishnu Nair491827d2024-04-29 23:43:26 +0000160 } else if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700161 if (!includeMirroredHierarchy) {
162 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
163 return;
164 }
165 out << "(Mirroring) ";
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000166 }
167 out << *mLayer;
Vishnu Nair04f89692022-11-16 23:21:05 +0000168 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000169
170 for (size_t i = 0; i < mChildren.size(); i++) {
171 auto& [child, childVariant] = mChildren[i];
172 if (childVariant == LayerHierarchy::Variant::Detached) continue;
173 const bool lastChild = i == (mChildren.size() - 1);
174 std::string childPrefix = prefix;
175 if (mLayer) {
176 childPrefix += (isLastChild ? " " : "│ ");
177 }
178 out << "\n";
Vishnu Nair6f878312023-09-08 11:05:01 -0700179 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000180 }
181 return;
Vishnu Nair04f89692022-11-16 23:21:05 +0000182}
183
184bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
185 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
186 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
187 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
188 if (traversalPath.hasRelZLoop()) {
189 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
190 return false;
191 }
192 return true;
193 });
194 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
195}
196
Vishnu Naira0292282023-12-16 14:32:00 -0800197void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
198 mLayerIdToHierarchy.clear();
199 mHierarchies.clear();
200 mRoot = nullptr;
201 mOffscreenRoot = nullptr;
202
Vishnu Nair04f89692022-11-16 23:21:05 +0000203 mHierarchies.reserve(layers.size());
204 mLayerIdToHierarchy.reserve(layers.size());
205 for (auto& layer : layers) {
206 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
207 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
208 }
209 for (const auto& layer : layers) {
210 onLayerAdded(layer.get());
211 }
212 detachHierarchyFromRelativeParent(&mOffscreenRoot);
Vishnu Naira0292282023-12-16 14:32:00 -0800213 mInitialized = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000214}
215
216void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
217 auto layer = hierarchy->mLayer;
218 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
219 ? LayerHierarchy::Variant::Detached
220 : LayerHierarchy::Variant::Attached;
221
222 LayerHierarchy* parent;
223
224 if (layer->parentId != UNASSIGNED_LAYER_ID) {
225 parent = getHierarchyFromId(layer->parentId);
226 } else if (layer->canBeRoot) {
227 parent = &mRoot;
228 } else {
229 parent = &mOffscreenRoot;
230 }
231 parent->addChild(hierarchy, type);
232 hierarchy->mParent = parent;
233}
234
235void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
236 hierarchy->mParent->removeChild(hierarchy);
237 hierarchy->mParent = nullptr;
238}
239
240void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
241 auto layer = hierarchy->mLayer;
242 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
243 return;
244 }
245
246 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
247 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
248 } else {
249 hierarchy->mRelativeParent = &mOffscreenRoot;
250 }
251 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
252 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
253}
254
255void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
256 if (hierarchy->mRelativeParent) {
257 hierarchy->mRelativeParent->removeChild(hierarchy);
258 }
259 hierarchy->mRelativeParent = nullptr;
260 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
261}
262
Vishnu Nairf12a6782024-06-03 23:08:15 +0000263std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) {
264 std::vector<LayerHierarchy*> hierarchies;
265 hierarchies.push_back(root);
266 std::vector<LayerHierarchy*> descendants;
267 for (size_t i = 0; i < hierarchies.size(); i++) {
268 LayerHierarchy* hierarchy = hierarchies[i];
269 if (hierarchy->mLayer) {
270 descendants.push_back(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000271 }
Vishnu Nairf12a6782024-06-03 23:08:15 +0000272 for (auto& [child, childVariant] : hierarchy->mChildren) {
273 if (childVariant == LayerHierarchy::Variant::Detached ||
274 childVariant == LayerHierarchy::Variant::Attached) {
275 hierarchies.push_back(child);
276 }
277 }
278 }
279 return descendants;
280}
281
282void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
283 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
284 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
285 attachToRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000286 }
287}
288
289void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
Vishnu Nairf12a6782024-06-03 23:08:15 +0000290 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
291 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
292 detachFromRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000293 }
294}
295
296void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
297 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
298 attachToParent(hierarchy);
299 attachToRelativeParent(hierarchy);
300
Vishnu Naira9c43762023-01-27 19:10:25 +0000301 for (uint32_t mirrorId : layer->mirrorIds) {
302 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000303 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
304 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000305 if (FlagManager::getInstance().detached_mirror()) {
306 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
307 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
308 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
309 }
310 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000311}
312
313void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000314 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000315 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
316 if (!hierarchy) {
317 // Layer was never part of the hierarchy if it was created and destroyed in the same
318 // transaction.
319 return;
320 }
321 // detach from parent
322 detachFromRelativeParent(hierarchy);
323 detachFromParent(hierarchy);
324
325 // detach children
326 for (auto& [child, variant] : hierarchy->mChildren) {
327 if (variant == LayerHierarchy::Variant::Attached ||
328 variant == LayerHierarchy::Variant::Detached) {
329 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
330 child->mParent = &mOffscreenRoot;
331 } else if (variant == LayerHierarchy::Variant::Relative) {
332 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
333 child->mRelativeParent = &mOffscreenRoot;
334 }
335 }
336
337 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
338 return layerHierarchy.get() == hierarchy;
339 });
340 mLayerIdToHierarchy.erase(layer->id);
341}
342
343void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
344 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
345 auto it = hierarchy->mChildren.begin();
346 while (it != hierarchy->mChildren.end()) {
Vishnu Nair491827d2024-04-29 23:43:26 +0000347 if (LayerHierarchy::isMirror(it->second)) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000348 it = hierarchy->mChildren.erase(it);
349 } else {
350 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000351 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000352 }
353
Vishnu Naira9c43762023-01-27 19:10:25 +0000354 for (uint32_t mirrorId : layer->mirrorIds) {
355 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000356 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000357 if (FlagManager::getInstance().detached_mirror()) {
358 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
359 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
360 LayerHierarchy::Variant::Detached_Mirror);
361 }
362 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000363}
364
Vishnu Naira0292282023-12-16 14:32:00 -0800365void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000366 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
367 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
368 // rebuild map
369 for (auto& layer : layers) {
370 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
371 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
372 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
373 }
374 }
375
376 for (auto& layer : layers) {
377 if (layer->changes.get() == 0) {
378 continue;
379 }
380 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
381 onLayerAdded(layer.get());
382 continue;
383 }
384 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
385 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
386 detachFromParent(hierarchy);
387 attachToParent(hierarchy);
388 }
389 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
390 detachFromRelativeParent(hierarchy);
391 attachToRelativeParent(hierarchy);
392 }
393 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
394 hierarchy->mParent->sortChildrenByZOrder();
395 if (hierarchy->mRelativeParent) {
396 hierarchy->mRelativeParent->sortChildrenByZOrder();
397 }
398 }
399 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
400 updateMirrorLayer(layer.get());
401 }
402 }
403
404 for (auto& layer : destroyedLayers) {
405 onLayerDestroyed(layer.get());
406 }
407 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
408 // from our relative parents. This walks down both trees to do so. We can optimize this
409 // further by tracking onscreen, offscreen state in LayerHierarchy.
410 detachHierarchyFromRelativeParent(&mOffscreenRoot);
411 attachHierarchyToRelativeParent(&mRoot);
412}
413
Vishnu Naira0292282023-12-16 14:32:00 -0800414void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
415 if (!mInitialized) {
416 ATRACE_NAME("LayerHierarchyBuilder:init");
417 init(layerLifecycleManager.getLayers());
418 } else if (layerLifecycleManager.getGlobalChanges().test(
419 RequestedLayerState::Changes::Hierarchy)) {
420 ATRACE_NAME("LayerHierarchyBuilder:update");
421 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
422 } else {
423 return; // nothing to do
424 }
425
426 uint32_t invalidRelativeRoot;
427 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
428 while (hasRelZLoop) {
429 ATRACE_NAME("FixRelZLoop");
430 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
431 /*overwrite=*/false);
432 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
433 // reinitialize the hierarchy with the updated layer data
434 init(layerLifecycleManager.getLayers());
435 // check if we have any remaining loops
436 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
437 }
438}
439
Vishnu Nair04f89692022-11-16 23:21:05 +0000440const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
441 return mRoot;
442}
443
444const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
445 return mOffscreenRoot;
446}
447
448std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
449 if (depth > 10) return "too deep, loop?";
450 if (layerId == UNASSIGNED_LAYER_ID) return "";
451 auto it = mLayerIdToHierarchy.find(layerId);
452 if (it == mLayerIdToHierarchy.end()) return "not found";
453
454 LayerHierarchy* hierarchy = it->second;
455 if (!hierarchy->mLayer) return "none";
456
457 std::string debug =
458 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
459 if (hierarchy->mRelativeParent) {
460 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
461 }
462 if (hierarchy->mParent) {
463 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
464 }
465 return debug;
466}
467
468LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
469 bool childrenOnly) const {
470 auto it = mLayerIdToHierarchy.find(layerId);
471 if (it == mLayerIdToHierarchy.end()) return {nullptr};
472
473 LayerHierarchy hierarchy(*it->second, childrenOnly);
474 return hierarchy;
475}
476
477LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
478 auto it = mLayerIdToHierarchy.find(layerId);
479 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700480 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
481 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000482 return nullptr;
483 };
484
485 return it->second;
486}
487
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000488const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000489 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
490
491std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000492 if (id == UNASSIGNED_LAYER_ID) {
493 return "TraversalPath{ROOT}";
494 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000495 std::stringstream ss;
496 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000497
Vishnu Nair6f878312023-09-08 11:05:01 -0700498 if (!mirrorRootIds.empty()) {
499 ss << ", .mirrorRootIds=";
500 for (auto rootId : mirrorRootIds) {
501 ss << rootId << ",";
502 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000503 }
504
505 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000506 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000507 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000508 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000509 }
510 }
511
512 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000513 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000514 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000515 ss << "}";
516 return ss.str();
517}
Vishnu Nair04f89692022-11-16 23:21:05 +0000518
Vishnu Nair04f89692022-11-16 23:21:05 +0000519// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
520// of scope the TraversalPath is reset to its original state.
521LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
522 uint32_t layerId,
523 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000524 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000525 // Update the traversal id with the child layer id and variant. Parent id and variant are
526 // stored to reset the id upon destruction.
527 traversalPath.id = layerId;
528 traversalPath.variant = variant;
Vishnu Nair491827d2024-04-29 23:43:26 +0000529 if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700530 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000531 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000532 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
533 layerId) != traversalPath.relativeRootIds.end()) {
534 traversalPath.invalidRelativeRootId = layerId;
535 }
536 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000537 } else if (variant == LayerHierarchy::Variant::Detached) {
538 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000539 }
540}
541LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
542 // Reset the traversal id to its original parent state using the state that was saved in
543 // the constructor.
Vishnu Nair491827d2024-04-29 23:43:26 +0000544 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700545 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000546 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000547 mTraversalPath.relativeRootIds.pop_back();
548 }
549 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
550 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
551 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000552 mTraversalPath.id = mParentPath.id;
553 mTraversalPath.variant = mParentPath.variant;
554 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000555}
556
557} // namespace android::surfaceflinger::frontend