blob: 14d67c63a54a5cc4c4aa5b6c4fa4bb78a13561eb [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
19#define LOG_TAG "LayerHierarchy"
20
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) {
33 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
55void LayerHierarchy::traverse(const Visitor& visitor,
56 LayerHierarchy::TraversalPath& traversalPath) const {
57 if (mLayer) {
58 bool breakTraversal = !visitor(*this, traversalPath);
59 if (breakTraversal) {
60 return;
61 }
62 }
63 if (traversalPath.hasRelZLoop()) {
64 LOG_ALWAYS_FATAL("Found relative z loop layerId:%d", traversalPath.invalidRelativeRootId);
65 }
66 for (auto& [child, childVariant] : mChildren) {
67 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
68 childVariant);
69 child->traverse(visitor, traversalPath);
70 }
71}
72
73void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
74 LayerHierarchy::TraversalPath& traversalPath) const {
75 bool traverseThisLayer = (mLayer != nullptr);
76 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
77 auto& [child, childVariant] = *it;
78 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000079 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000080 bool breakTraversal = !visitor(*this, traversalPath);
81 if (breakTraversal) {
82 return;
83 }
Vishnu Nair04f89692022-11-16 23:21:05 +000084 }
85 if (childVariant == LayerHierarchy::Variant::Detached) {
86 continue;
87 }
88 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
89 childVariant);
90 child->traverseInZOrder(visitor, traversalPath);
91 }
92
93 if (traverseThisLayer) {
94 visitor(*this, traversalPath);
95 }
96}
97
98void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
99 insertSorted(mChildren, {child, variant});
100}
101
102void LayerHierarchy::removeChild(LayerHierarchy* child) {
103 auto it = std::find_if(mChildren.begin(), mChildren.end(),
104 [child](const std::pair<LayerHierarchy*, Variant>& x) {
105 return x.first == child;
106 });
107 if (it == mChildren.end()) {
108 LOG_ALWAYS_FATAL("Could not find child!");
109 }
110 mChildren.erase(it);
111}
112
113void LayerHierarchy::sortChildrenByZOrder() {
114 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
115}
116
117void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
118 auto it = std::find_if(mChildren.begin(), mChildren.end(),
119 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
120 return child.first == hierarchy;
121 });
122 if (it == mChildren.end()) {
123 LOG_ALWAYS_FATAL("Could not find child!");
124 } else {
125 it->second = variant;
126 }
127}
128
129const RequestedLayerState* LayerHierarchy::getLayer() const {
130 return mLayer;
131}
132
133std::string LayerHierarchy::getDebugStringShort() const {
134 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000135 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000136 if (mChildren.empty()) {
137 debug += "no children";
138 } else {
139 debug += std::to_string(mChildren.size()) + " children";
140 }
141 return debug + "}";
142}
143
144std::string LayerHierarchy::getDebugString(const char* prefix) const {
145 std::string debug = prefix + getDebugStringShort();
146 for (auto& [child, childVariant] : mChildren) {
147 std::string childPrefix = " " + std::string(prefix) + " " + std::to_string(childVariant);
148 debug += "\n" + child->getDebugString(childPrefix.c_str());
149 }
150 return debug;
151}
152
153bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
154 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
155 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
156 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
157 if (traversalPath.hasRelZLoop()) {
158 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
159 return false;
160 }
161 return true;
162 });
163 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
164}
165
166LayerHierarchyBuilder::LayerHierarchyBuilder(
167 const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
168 mHierarchies.reserve(layers.size());
169 mLayerIdToHierarchy.reserve(layers.size());
170 for (auto& layer : layers) {
171 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
172 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
173 }
174 for (const auto& layer : layers) {
175 onLayerAdded(layer.get());
176 }
177 detachHierarchyFromRelativeParent(&mOffscreenRoot);
178}
179
180void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
181 auto layer = hierarchy->mLayer;
182 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
183 ? LayerHierarchy::Variant::Detached
184 : LayerHierarchy::Variant::Attached;
185
186 LayerHierarchy* parent;
187
188 if (layer->parentId != UNASSIGNED_LAYER_ID) {
189 parent = getHierarchyFromId(layer->parentId);
190 } else if (layer->canBeRoot) {
191 parent = &mRoot;
192 } else {
193 parent = &mOffscreenRoot;
194 }
195 parent->addChild(hierarchy, type);
196 hierarchy->mParent = parent;
197}
198
199void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
200 hierarchy->mParent->removeChild(hierarchy);
201 hierarchy->mParent = nullptr;
202}
203
204void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
205 auto layer = hierarchy->mLayer;
206 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
207 return;
208 }
209
210 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
211 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
212 } else {
213 hierarchy->mRelativeParent = &mOffscreenRoot;
214 }
215 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
216 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
217}
218
219void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
220 if (hierarchy->mRelativeParent) {
221 hierarchy->mRelativeParent->removeChild(hierarchy);
222 }
223 hierarchy->mRelativeParent = nullptr;
224 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
225}
226
227void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
228 if (root->mLayer) {
229 attachToRelativeParent(root);
230 }
231 for (auto& [child, childVariant] : root->mChildren) {
232 if (childVariant == LayerHierarchy::Variant::Detached ||
233 childVariant == LayerHierarchy::Variant::Attached) {
234 attachHierarchyToRelativeParent(child);
235 }
236 }
237}
238
239void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
240 if (root->mLayer) {
241 detachFromRelativeParent(root);
242 }
243 for (auto& [child, childVariant] : root->mChildren) {
244 if (childVariant == LayerHierarchy::Variant::Detached ||
245 childVariant == LayerHierarchy::Variant::Attached) {
246 detachHierarchyFromRelativeParent(child);
247 }
248 }
249}
250
251void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
252 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
253 attachToParent(hierarchy);
254 attachToRelativeParent(hierarchy);
255
Vishnu Naira9c43762023-01-27 19:10:25 +0000256 for (uint32_t mirrorId : layer->mirrorIds) {
257 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000258 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
259 }
260}
261
262void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000263 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000264 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
265 if (!hierarchy) {
266 // Layer was never part of the hierarchy if it was created and destroyed in the same
267 // transaction.
268 return;
269 }
270 // detach from parent
271 detachFromRelativeParent(hierarchy);
272 detachFromParent(hierarchy);
273
274 // detach children
275 for (auto& [child, variant] : hierarchy->mChildren) {
276 if (variant == LayerHierarchy::Variant::Attached ||
277 variant == LayerHierarchy::Variant::Detached) {
278 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
279 child->mParent = &mOffscreenRoot;
280 } else if (variant == LayerHierarchy::Variant::Relative) {
281 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
282 child->mRelativeParent = &mOffscreenRoot;
283 }
284 }
285
286 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
287 return layerHierarchy.get() == hierarchy;
288 });
289 mLayerIdToHierarchy.erase(layer->id);
290}
291
292void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
293 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
294 auto it = hierarchy->mChildren.begin();
295 while (it != hierarchy->mChildren.end()) {
296 if (it->second == LayerHierarchy::Variant::Mirror) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000297 it = hierarchy->mChildren.erase(it);
298 } else {
299 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000300 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000301 }
302
Vishnu Naira9c43762023-01-27 19:10:25 +0000303 for (uint32_t mirrorId : layer->mirrorIds) {
304 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000305 }
306}
307
308void LayerHierarchyBuilder::update(
309 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
310 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
311 // rebuild map
312 for (auto& layer : layers) {
313 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
314 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
315 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
316 }
317 }
318
319 for (auto& layer : layers) {
320 if (layer->changes.get() == 0) {
321 continue;
322 }
323 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
324 onLayerAdded(layer.get());
325 continue;
326 }
327 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
328 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
329 detachFromParent(hierarchy);
330 attachToParent(hierarchy);
331 }
332 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
333 detachFromRelativeParent(hierarchy);
334 attachToRelativeParent(hierarchy);
335 }
336 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
337 hierarchy->mParent->sortChildrenByZOrder();
338 if (hierarchy->mRelativeParent) {
339 hierarchy->mRelativeParent->sortChildrenByZOrder();
340 }
341 }
342 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
343 updateMirrorLayer(layer.get());
344 }
345 }
346
347 for (auto& layer : destroyedLayers) {
348 onLayerDestroyed(layer.get());
349 }
350 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
351 // from our relative parents. This walks down both trees to do so. We can optimize this
352 // further by tracking onscreen, offscreen state in LayerHierarchy.
353 detachHierarchyFromRelativeParent(&mOffscreenRoot);
354 attachHierarchyToRelativeParent(&mRoot);
355}
356
357const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
358 return mRoot;
359}
360
361const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
362 return mOffscreenRoot;
363}
364
365std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
366 if (depth > 10) return "too deep, loop?";
367 if (layerId == UNASSIGNED_LAYER_ID) return "";
368 auto it = mLayerIdToHierarchy.find(layerId);
369 if (it == mLayerIdToHierarchy.end()) return "not found";
370
371 LayerHierarchy* hierarchy = it->second;
372 if (!hierarchy->mLayer) return "none";
373
374 std::string debug =
375 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
376 if (hierarchy->mRelativeParent) {
377 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
378 }
379 if (hierarchy->mParent) {
380 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
381 }
382 return debug;
383}
384
385LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
386 bool childrenOnly) const {
387 auto it = mLayerIdToHierarchy.find(layerId);
388 if (it == mLayerIdToHierarchy.end()) return {nullptr};
389
390 LayerHierarchy hierarchy(*it->second, childrenOnly);
391 return hierarchy;
392}
393
394LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
395 auto it = mLayerIdToHierarchy.find(layerId);
396 if (it == mLayerIdToHierarchy.end()) {
397 if (crashOnFailure) {
398 LOG_ALWAYS_FATAL("Could not find hierarchy for layer id %d", layerId);
399 }
400 return nullptr;
401 };
402
403 return it->second;
404}
405
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000406const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000407 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
408
409std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000410 if (id == UNASSIGNED_LAYER_ID) {
411 return "TraversalPath{ROOT}";
412 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000413 std::stringstream ss;
414 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000415
Vishnu Nair80a5a702023-02-11 01:21:51 +0000416 if (mirrorRootId != UNASSIGNED_LAYER_ID) {
417 ss << ", .mirrorRootId=" << mirrorRootId;
Vishnu Nair04f89692022-11-16 23:21:05 +0000418 }
419
420 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000421 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000422 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000423 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000424 }
425 }
426
427 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000428 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000429 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000430 ss << "}";
431 return ss.str();
432}
Vishnu Nair04f89692022-11-16 23:21:05 +0000433
Vishnu Nair80a5a702023-02-11 01:21:51 +0000434LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::getMirrorRoot() const {
435 LOG_ALWAYS_FATAL_IF(!isClone(), "Cannot get mirror root of a non cloned node");
436 TraversalPath mirrorRootPath = *this;
437 mirrorRootPath.id = mirrorRootId;
438 return mirrorRootPath;
Vishnu Nair04f89692022-11-16 23:21:05 +0000439}
440
441// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
442// of scope the TraversalPath is reset to its original state.
443LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
444 uint32_t layerId,
445 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000446 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000447 // Update the traversal id with the child layer id and variant. Parent id and variant are
448 // stored to reset the id upon destruction.
449 traversalPath.id = layerId;
450 traversalPath.variant = variant;
451 if (variant == LayerHierarchy::Variant::Mirror) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000452 traversalPath.mirrorRootId = mParentPath.id;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000453 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000454 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
455 layerId) != traversalPath.relativeRootIds.end()) {
456 traversalPath.invalidRelativeRootId = layerId;
457 }
458 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000459 } else if (variant == LayerHierarchy::Variant::Detached) {
460 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000461 }
462}
463LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
464 // Reset the traversal id to its original parent state using the state that was saved in
465 // the constructor.
466 if (mTraversalPath.variant == LayerHierarchy::Variant::Mirror) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000467 mTraversalPath.mirrorRootId = mParentPath.mirrorRootId;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000468 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000469 mTraversalPath.relativeRootIds.pop_back();
470 }
471 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
472 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
473 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000474 mTraversalPath.id = mParentPath.id;
475 mTraversalPath.variant = mParentPath.variant;
476 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000477}
478
479} // namespace android::surfaceflinger::frontend