blob: da536b6660558f125c679eb577e0b0a1416800fa [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 }
Wenhui Yang30623812024-10-28 17:57:29 +0000169
170 out << *mLayer << " pid=" << mLayer->ownerPid.val() << " uid=" << mLayer->ownerUid.val();
Vishnu Nair04f89692022-11-16 23:21:05 +0000171 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000172
173 for (size_t i = 0; i < mChildren.size(); i++) {
174 auto& [child, childVariant] = mChildren[i];
175 if (childVariant == LayerHierarchy::Variant::Detached) continue;
176 const bool lastChild = i == (mChildren.size() - 1);
177 std::string childPrefix = prefix;
178 if (mLayer) {
179 childPrefix += (isLastChild ? " " : "│ ");
180 }
181 out << "\n";
Vishnu Nair6f878312023-09-08 11:05:01 -0700182 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000183 }
184 return;
Vishnu Nair04f89692022-11-16 23:21:05 +0000185}
186
187bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
188 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
189 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
190 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
191 if (traversalPath.hasRelZLoop()) {
192 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
193 return false;
194 }
195 return true;
196 });
197 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
198}
199
Vishnu Naira0292282023-12-16 14:32:00 -0800200void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
201 mLayerIdToHierarchy.clear();
202 mHierarchies.clear();
203 mRoot = nullptr;
204 mOffscreenRoot = nullptr;
205
Vishnu Nair04f89692022-11-16 23:21:05 +0000206 mHierarchies.reserve(layers.size());
207 mLayerIdToHierarchy.reserve(layers.size());
208 for (auto& layer : layers) {
209 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
210 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
211 }
212 for (const auto& layer : layers) {
213 onLayerAdded(layer.get());
214 }
215 detachHierarchyFromRelativeParent(&mOffscreenRoot);
Vishnu Naira0292282023-12-16 14:32:00 -0800216 mInitialized = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000217}
218
219void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
220 auto layer = hierarchy->mLayer;
221 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
222 ? LayerHierarchy::Variant::Detached
223 : LayerHierarchy::Variant::Attached;
224
225 LayerHierarchy* parent;
226
227 if (layer->parentId != UNASSIGNED_LAYER_ID) {
228 parent = getHierarchyFromId(layer->parentId);
229 } else if (layer->canBeRoot) {
230 parent = &mRoot;
231 } else {
232 parent = &mOffscreenRoot;
233 }
234 parent->addChild(hierarchy, type);
235 hierarchy->mParent = parent;
236}
237
238void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
239 hierarchy->mParent->removeChild(hierarchy);
240 hierarchy->mParent = nullptr;
241}
242
243void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
244 auto layer = hierarchy->mLayer;
245 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
246 return;
247 }
248
249 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
250 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
251 } else {
252 hierarchy->mRelativeParent = &mOffscreenRoot;
253 }
254 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
255 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
256}
257
258void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
259 if (hierarchy->mRelativeParent) {
260 hierarchy->mRelativeParent->removeChild(hierarchy);
261 }
262 hierarchy->mRelativeParent = nullptr;
263 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
264}
265
Vishnu Nairf12a6782024-06-03 23:08:15 +0000266std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) {
267 std::vector<LayerHierarchy*> hierarchies;
268 hierarchies.push_back(root);
269 std::vector<LayerHierarchy*> descendants;
270 for (size_t i = 0; i < hierarchies.size(); i++) {
271 LayerHierarchy* hierarchy = hierarchies[i];
272 if (hierarchy->mLayer) {
273 descendants.push_back(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000274 }
Vishnu Nairf12a6782024-06-03 23:08:15 +0000275 for (auto& [child, childVariant] : hierarchy->mChildren) {
276 if (childVariant == LayerHierarchy::Variant::Detached ||
277 childVariant == LayerHierarchy::Variant::Attached) {
278 hierarchies.push_back(child);
279 }
280 }
281 }
282 return descendants;
283}
284
285void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
286 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
287 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
288 attachToRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000289 }
290}
291
292void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
Vishnu Nairf12a6782024-06-03 23:08:15 +0000293 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
294 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
295 detachFromRelativeParent(hierarchy);
Vishnu Nair04f89692022-11-16 23:21:05 +0000296 }
297}
298
299void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
300 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
301 attachToParent(hierarchy);
302 attachToRelativeParent(hierarchy);
303
Vishnu Naira9c43762023-01-27 19:10:25 +0000304 for (uint32_t mirrorId : layer->mirrorIds) {
305 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000306 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
307 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000308 if (FlagManager::getInstance().detached_mirror()) {
309 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
310 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
311 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
312 }
313 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000314}
315
316void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000317 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000318 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
319 if (!hierarchy) {
320 // Layer was never part of the hierarchy if it was created and destroyed in the same
321 // transaction.
322 return;
323 }
324 // detach from parent
325 detachFromRelativeParent(hierarchy);
326 detachFromParent(hierarchy);
327
328 // detach children
329 for (auto& [child, variant] : hierarchy->mChildren) {
330 if (variant == LayerHierarchy::Variant::Attached ||
331 variant == LayerHierarchy::Variant::Detached) {
332 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
333 child->mParent = &mOffscreenRoot;
334 } else if (variant == LayerHierarchy::Variant::Relative) {
335 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
336 child->mRelativeParent = &mOffscreenRoot;
337 }
338 }
339
340 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
341 return layerHierarchy.get() == hierarchy;
342 });
343 mLayerIdToHierarchy.erase(layer->id);
344}
345
346void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
347 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
348 auto it = hierarchy->mChildren.begin();
349 while (it != hierarchy->mChildren.end()) {
Vishnu Nair491827d2024-04-29 23:43:26 +0000350 if (LayerHierarchy::isMirror(it->second)) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000351 it = hierarchy->mChildren.erase(it);
352 } else {
353 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000354 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000355 }
356
Vishnu Naira9c43762023-01-27 19:10:25 +0000357 for (uint32_t mirrorId : layer->mirrorIds) {
358 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000359 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000360 if (FlagManager::getInstance().detached_mirror()) {
361 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
362 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
363 LayerHierarchy::Variant::Detached_Mirror);
364 }
365 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000366}
367
Vishnu Naira0292282023-12-16 14:32:00 -0800368void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000369 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
370 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
371 // rebuild map
372 for (auto& layer : layers) {
373 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
374 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
375 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
376 }
377 }
378
379 for (auto& layer : layers) {
380 if (layer->changes.get() == 0) {
381 continue;
382 }
383 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
384 onLayerAdded(layer.get());
385 continue;
386 }
387 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
388 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
389 detachFromParent(hierarchy);
390 attachToParent(hierarchy);
391 }
392 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
393 detachFromRelativeParent(hierarchy);
394 attachToRelativeParent(hierarchy);
395 }
396 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
397 hierarchy->mParent->sortChildrenByZOrder();
398 if (hierarchy->mRelativeParent) {
399 hierarchy->mRelativeParent->sortChildrenByZOrder();
400 }
401 }
402 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
403 updateMirrorLayer(layer.get());
404 }
405 }
406
407 for (auto& layer : destroyedLayers) {
408 onLayerDestroyed(layer.get());
409 }
410 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
411 // from our relative parents. This walks down both trees to do so. We can optimize this
412 // further by tracking onscreen, offscreen state in LayerHierarchy.
413 detachHierarchyFromRelativeParent(&mOffscreenRoot);
414 attachHierarchyToRelativeParent(&mRoot);
415}
416
Vishnu Naira0292282023-12-16 14:32:00 -0800417void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
418 if (!mInitialized) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000419 SFTRACE_NAME("LayerHierarchyBuilder:init");
Vishnu Naira0292282023-12-16 14:32:00 -0800420 init(layerLifecycleManager.getLayers());
421 } else if (layerLifecycleManager.getGlobalChanges().test(
422 RequestedLayerState::Changes::Hierarchy)) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000423 SFTRACE_NAME("LayerHierarchyBuilder:update");
Vishnu Naira0292282023-12-16 14:32:00 -0800424 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
425 } else {
426 return; // nothing to do
427 }
428
429 uint32_t invalidRelativeRoot;
430 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
431 while (hasRelZLoop) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +0000432 SFTRACE_NAME("FixRelZLoop");
Vishnu Naira0292282023-12-16 14:32:00 -0800433 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
434 /*overwrite=*/false);
435 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
436 // reinitialize the hierarchy with the updated layer data
437 init(layerLifecycleManager.getLayers());
438 // check if we have any remaining loops
439 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
440 }
441}
442
Vishnu Nair04f89692022-11-16 23:21:05 +0000443const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
444 return mRoot;
445}
446
447const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
448 return mOffscreenRoot;
449}
450
451std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
452 if (depth > 10) return "too deep, loop?";
453 if (layerId == UNASSIGNED_LAYER_ID) return "";
454 auto it = mLayerIdToHierarchy.find(layerId);
455 if (it == mLayerIdToHierarchy.end()) return "not found";
456
457 LayerHierarchy* hierarchy = it->second;
458 if (!hierarchy->mLayer) return "none";
459
460 std::string debug =
461 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
462 if (hierarchy->mRelativeParent) {
463 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
464 }
465 if (hierarchy->mParent) {
466 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
467 }
468 return debug;
469}
470
471LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
472 bool childrenOnly) const {
473 auto it = mLayerIdToHierarchy.find(layerId);
474 if (it == mLayerIdToHierarchy.end()) return {nullptr};
475
476 LayerHierarchy hierarchy(*it->second, childrenOnly);
477 return hierarchy;
478}
479
480LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
481 auto it = mLayerIdToHierarchy.find(layerId);
482 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700483 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
484 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000485 return nullptr;
486 };
487
488 return it->second;
489}
490
qinyige10e011d02024-05-23 15:59:08 +0800491void LayerHierarchyBuilder::logSampledChildren(const LayerHierarchy& hierarchy) const {
492 LOG(ERROR) << "Dumping random sampling of child layers.";
493 int sampleSize = static_cast<int>(hierarchy.mChildren.size() / 100 + 1);
494 for (const auto& [child, variant] : hierarchy.mChildren) {
495 if (rand() % sampleSize == 0) {
496 LOG(ERROR) << "Child Layer: " << *(child->mLayer);
497 }
498 }
499}
500
501void LayerHierarchyBuilder::dumpLayerSample(const LayerHierarchy& root) const {
502 LOG(ERROR) << "Dumping layer keeping > 20 children alive:";
503 // If mLayer is nullptr, it will be skipped while traversing.
504 if (!root.mLayer && root.mChildren.size() > 20) {
505 LOG(ERROR) << "ROOT has " << root.mChildren.size() << " children";
506 logSampledChildren(root);
507 }
508 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
509 if (hierarchy.mChildren.size() <= 20) {
510 return true;
511 }
512 // mLayer is ensured to be non-null. See LayerHierarchy::traverse.
513 const auto* layer = hierarchy.mLayer;
514 const auto childrenCount = hierarchy.mChildren.size();
515 LOG(ERROR) << "Layer " << *layer << " has " << childrenCount << " children";
516
517 const auto* parent = hierarchy.mParent;
518 while (parent != nullptr) {
519 if (!parent->mLayer) break;
520 LOG(ERROR) << "Parent Layer: " << *(parent->mLayer);
521 parent = parent->mParent;
522 }
523
524 logSampledChildren(hierarchy);
525 // Stop traversing.
526 return false;
527 });
528 LOG(ERROR) << "Dumping random sampled layers.";
529 size_t numLayers = 0;
530 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
531 if (hierarchy.mLayer) numLayers++;
532 if ((rand() % 20 == 13) && hierarchy.mLayer) {
533 LOG(ERROR) << "Layer: " << *(hierarchy.mLayer);
534 }
535 return true;
536 });
537 LOG(ERROR) << "Total layer count: " << numLayers;
538}
539
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000540const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000541 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
542
543std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000544 if (id == UNASSIGNED_LAYER_ID) {
545 return "TraversalPath{ROOT}";
546 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000547 std::stringstream ss;
548 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000549
Vishnu Nair6f878312023-09-08 11:05:01 -0700550 if (!mirrorRootIds.empty()) {
551 ss << ", .mirrorRootIds=";
552 for (auto rootId : mirrorRootIds) {
553 ss << rootId << ",";
554 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000555 }
556
557 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000558 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000559 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000560 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000561 }
562 }
563
564 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000565 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000566 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000567 ss << "}";
568 return ss.str();
569}
Vishnu Nair04f89692022-11-16 23:21:05 +0000570
Vishnu Nair04f89692022-11-16 23:21:05 +0000571// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
572// of scope the TraversalPath is reset to its original state.
573LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
574 uint32_t layerId,
575 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000576 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000577 // Update the traversal id with the child layer id and variant. Parent id and variant are
578 // stored to reset the id upon destruction.
579 traversalPath.id = layerId;
580 traversalPath.variant = variant;
Vishnu Nair491827d2024-04-29 23:43:26 +0000581 if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700582 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000583 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000584 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
585 layerId) != traversalPath.relativeRootIds.end()) {
586 traversalPath.invalidRelativeRootId = layerId;
587 }
588 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000589 } else if (variant == LayerHierarchy::Variant::Detached) {
590 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000591 }
592}
593LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
594 // Reset the traversal id to its original parent state using the state that was saved in
595 // the constructor.
Vishnu Nair491827d2024-04-29 23:43:26 +0000596 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700597 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000598 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000599 mTraversalPath.relativeRootIds.pop_back();
600 }
601 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
602 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
603 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000604 mTraversalPath.id = mParentPath.id;
605 mTraversalPath.variant = mParentPath.variant;
606 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000607}
608
609} // namespace android::surfaceflinger::frontend