1 /*
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
19 #define LOG_TAG "SurfaceFlinger"
20
21 #include "LayerHierarchy.h"
22 #include "LayerLog.h"
23 #include "SwapErase.h"
24
25 namespace android::surfaceflinger::frontend {
26
27 namespace {
28 auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
__anoncf78cdd30202(const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs, const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) 29 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
30 auto lhsLayer = lhs.first->getLayer();
31 auto rhsLayer = rhs.first->getLayer();
32 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
33 return lhsLayer->layerStack.id < rhsLayer->layerStack.id;
34 }
35 if (lhsLayer->z != rhsLayer->z) {
36 return lhsLayer->z < rhsLayer->z;
37 }
38 return lhsLayer->id < rhsLayer->id;
39 };
40
insertSorted(std::vector<std::pair<LayerHierarchy *,LayerHierarchy::Variant>> & vec,std::pair<LayerHierarchy *,LayerHierarchy::Variant> value)41 void 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
LayerHierarchy(RequestedLayerState * layer)48 LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
49
LayerHierarchy(const LayerHierarchy & hierarchy,bool childrenOnly)50 LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
51 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
52 mChildren = hierarchy.mChildren;
53 }
54
traverse(const Visitor & visitor,LayerHierarchy::TraversalPath & traversalPath,uint32_t depth) const55 void 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
61 if (mLayer) {
62 bool breakTraversal = !visitor(*this, traversalPath);
63 if (breakTraversal) {
64 return;
65 }
66 }
67
68 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
69 traversalPath.invalidRelativeRootId);
70 for (auto& [child, childVariant] : mChildren) {
71 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
72 childVariant);
73 child->traverse(visitor, traversalPath, depth + 1);
74 }
75 }
76
traverseInZOrder(const Visitor & visitor,LayerHierarchy::TraversalPath & traversalPath) const77 void 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) {
83 traverseThisLayer = false;
84 bool breakTraversal = !visitor(*this, traversalPath);
85 if (breakTraversal) {
86 return;
87 }
88 }
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
addChild(LayerHierarchy * child,LayerHierarchy::Variant variant)102 void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
103 insertSorted(mChildren, {child, variant});
104 }
105
removeChild(LayerHierarchy * child)106 void 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 });
111 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
112 mChildren.erase(it);
113 }
114
sortChildrenByZOrder()115 void LayerHierarchy::sortChildrenByZOrder() {
116 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
117 }
118
updateChild(LayerHierarchy * hierarchy,LayerHierarchy::Variant variant)119 void 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 });
124 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
125 it->second = variant;
126 }
127
getLayer() const128 const RequestedLayerState* LayerHierarchy::getLayer() const {
129 return mLayer;
130 }
131
getRelativeParent() const132 const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
133 return mRelativeParent;
134 }
135
getParent() const136 const LayerHierarchy* LayerHierarchy::getParent() const {
137 return mParent;
138 }
139
getDebugStringShort() const140 std::string LayerHierarchy::getDebugStringShort() const {
141 std::string debug = "LayerHierarchy{";
142 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
143 if (mChildren.empty()) {
144 debug += "no children";
145 } else {
146 debug += std::to_string(mChildren.size()) + " children";
147 }
148 return debug + "}";
149 }
150
dump(std::ostream & out,const std::string & prefix,LayerHierarchy::Variant variant,bool isLastChild,bool includeMirroredHierarchy) const151 void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
152 LayerHierarchy::Variant variant, bool isLastChild,
153 bool includeMirroredHierarchy) const {
154 if (!mLayer) {
155 out << " ROOT";
156 } else {
157 out << prefix + (isLastChild ? "└─ " : "├─ ");
158 if (variant == LayerHierarchy::Variant::Relative) {
159 out << "(Relative) ";
160 } else if (LayerHierarchy::isMirror(variant)) {
161 if (!includeMirroredHierarchy) {
162 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
163 return;
164 }
165 out << "(Mirroring) ";
166 }
167 out << *mLayer;
168 }
169
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";
179 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
180 }
181 return;
182 }
183
hasRelZLoop(uint32_t & outInvalidRelativeRoot) const184 bool 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
init(const std::vector<std::unique_ptr<RequestedLayerState>> & layers)197 void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
198 mLayerIdToHierarchy.clear();
199 mHierarchies.clear();
200 mRoot = nullptr;
201 mOffscreenRoot = nullptr;
202
203 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);
213 mInitialized = true;
214 }
215
attachToParent(LayerHierarchy * hierarchy)216 void 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
detachFromParent(LayerHierarchy * hierarchy)235 void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
236 hierarchy->mParent->removeChild(hierarchy);
237 hierarchy->mParent = nullptr;
238 }
239
attachToRelativeParent(LayerHierarchy * hierarchy)240 void 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
detachFromRelativeParent(LayerHierarchy * hierarchy)255 void 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
getDescendants(LayerHierarchy * root)263 std::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);
271 }
272 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
attachHierarchyToRelativeParent(LayerHierarchy * root)282 void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
283 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
284 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
285 attachToRelativeParent(hierarchy);
286 }
287 }
288
detachHierarchyFromRelativeParent(LayerHierarchy * root)289 void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
290 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
291 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
292 detachFromRelativeParent(hierarchy);
293 }
294 }
295
onLayerAdded(RequestedLayerState * layer)296 void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
297 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
298 attachToParent(hierarchy);
299 attachToRelativeParent(hierarchy);
300
301 for (uint32_t mirrorId : layer->mirrorIds) {
302 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
303 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
304 }
305 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 }
311 }
312
onLayerDestroyed(RequestedLayerState * layer)313 void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
314 LLOGV(layer->id, "");
315 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
updateMirrorLayer(RequestedLayerState * layer)343 void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
344 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
345 auto it = hierarchy->mChildren.begin();
346 while (it != hierarchy->mChildren.end()) {
347 if (LayerHierarchy::isMirror(it->second)) {
348 it = hierarchy->mChildren.erase(it);
349 } else {
350 it++;
351 }
352 }
353
354 for (uint32_t mirrorId : layer->mirrorIds) {
355 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
356 }
357 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 }
363 }
364
doUpdate(const std::vector<std::unique_ptr<RequestedLayerState>> & layers,const std::vector<std::unique_ptr<RequestedLayerState>> & destroyedLayers)365 void LayerHierarchyBuilder::doUpdate(
366 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
update(LayerLifecycleManager & layerLifecycleManager)414 void 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
getHierarchy() const440 const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
441 return mRoot;
442 }
443
getOffscreenHierarchy() const444 const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
445 return mOffscreenRoot;
446 }
447
getDebugString(uint32_t layerId,uint32_t depth) const448 std::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
getPartialHierarchy(uint32_t layerId,bool childrenOnly) const468 LayerHierarchy 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
getHierarchyFromId(uint32_t layerId,bool crashOnFailure)477 LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
478 auto it = mLayerIdToHierarchy.find(layerId);
479 if (it == mLayerIdToHierarchy.end()) {
480 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
481 layerId);
482 return nullptr;
483 };
484
485 return it->second;
486 }
487
488 const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
489 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
490
toString() const491 std::string LayerHierarchy::TraversalPath::toString() const {
492 if (id == UNASSIGNED_LAYER_ID) {
493 return "TraversalPath{ROOT}";
494 }
495 std::stringstream ss;
496 ss << "TraversalPath{.id = " << id;
497
498 if (!mirrorRootIds.empty()) {
499 ss << ", .mirrorRootIds=";
500 for (auto rootId : mirrorRootIds) {
501 ss << rootId << ",";
502 }
503 }
504
505 if (!relativeRootIds.empty()) {
506 ss << ", .relativeRootIds=";
507 for (auto rootId : relativeRootIds) {
508 ss << rootId << ",";
509 }
510 }
511
512 if (hasRelZLoop()) {
513 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
514 }
515 ss << "}";
516 return ss.str();
517 }
518
519 // 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.
ScopedAddToTraversalPath(TraversalPath & traversalPath,uint32_t layerId,LayerHierarchy::Variant variant)521 LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
522 uint32_t layerId,
523 LayerHierarchy::Variant variant)
524 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
525 // 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;
529 if (LayerHierarchy::isMirror(variant)) {
530 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
531 } else if (variant == LayerHierarchy::Variant::Relative) {
532 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);
537 } else if (variant == LayerHierarchy::Variant::Detached) {
538 traversalPath.detached = true;
539 }
540 }
~ScopedAddToTraversalPath()541 LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
542 // Reset the traversal id to its original parent state using the state that was saved in
543 // the constructor.
544 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
545 mTraversalPath.mirrorRootIds.pop_back();
546 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
547 mTraversalPath.relativeRootIds.pop_back();
548 }
549 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
550 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
551 }
552 mTraversalPath.id = mParentPath.id;
553 mTraversalPath.variant = mParentPath.variant;
554 mTraversalPath.detached = mParentPath.detached;
555 }
556
557 } // namespace android::surfaceflinger::frontend
558