blob: 678d36b5cf23b353dfa6eabf6af9779f19335aa5 [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"
22#include "SwapErase.h"
23
24namespace android::surfaceflinger::frontend {
25
26namespace {
27auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
28 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
29 auto lhsLayer = lhs.first->getLayer();
30 auto rhsLayer = rhs.first->getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +000031 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
32 return lhsLayer->layerStack.id > rhsLayer->layerStack.id;
Vishnu Nair04f89692022-11-16 23:21:05 +000033 }
34 if (lhsLayer->z != rhsLayer->z) {
35 return lhsLayer->z < rhsLayer->z;
36 }
37 return lhsLayer->id < rhsLayer->id;
38};
39
40void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec,
41 std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) {
42 auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare);
43 vec.insert(it, std::move(value));
44}
45} // namespace
46
47LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
48
49LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
50 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
51 mChildren = hierarchy.mChildren;
52}
53
54void LayerHierarchy::traverse(const Visitor& visitor,
55 LayerHierarchy::TraversalPath& traversalPath) const {
56 if (mLayer) {
57 bool breakTraversal = !visitor(*this, traversalPath);
58 if (breakTraversal) {
59 return;
60 }
61 }
62 if (traversalPath.hasRelZLoop()) {
63 LOG_ALWAYS_FATAL("Found relative z loop layerId:%d", traversalPath.invalidRelativeRootId);
64 }
65 for (auto& [child, childVariant] : mChildren) {
66 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
67 childVariant);
68 child->traverse(visitor, traversalPath);
69 }
70}
71
72void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
73 LayerHierarchy::TraversalPath& traversalPath) const {
74 bool traverseThisLayer = (mLayer != nullptr);
75 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
76 auto& [child, childVariant] = *it;
77 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000078 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000079 bool breakTraversal = !visitor(*this, traversalPath);
80 if (breakTraversal) {
81 return;
82 }
Vishnu Nair04f89692022-11-16 23:21:05 +000083 }
84 if (childVariant == LayerHierarchy::Variant::Detached) {
85 continue;
86 }
87 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
88 childVariant);
89 child->traverseInZOrder(visitor, traversalPath);
90 }
91
92 if (traverseThisLayer) {
93 visitor(*this, traversalPath);
94 }
95}
96
97void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
98 insertSorted(mChildren, {child, variant});
99}
100
101void LayerHierarchy::removeChild(LayerHierarchy* child) {
102 auto it = std::find_if(mChildren.begin(), mChildren.end(),
103 [child](const std::pair<LayerHierarchy*, Variant>& x) {
104 return x.first == child;
105 });
106 if (it == mChildren.end()) {
107 LOG_ALWAYS_FATAL("Could not find child!");
108 }
109 mChildren.erase(it);
110}
111
112void LayerHierarchy::sortChildrenByZOrder() {
113 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
114}
115
116void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
117 auto it = std::find_if(mChildren.begin(), mChildren.end(),
118 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
119 return child.first == hierarchy;
120 });
121 if (it == mChildren.end()) {
122 LOG_ALWAYS_FATAL("Could not find child!");
123 } else {
124 it->second = variant;
125 }
126}
127
128const RequestedLayerState* LayerHierarchy::getLayer() const {
129 return mLayer;
130}
131
132std::string LayerHierarchy::getDebugStringShort() const {
133 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000134 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000135 if (mChildren.empty()) {
136 debug += "no children";
137 } else {
138 debug += std::to_string(mChildren.size()) + " children";
139 }
140 return debug + "}";
141}
142
143std::string LayerHierarchy::getDebugString(const char* prefix) const {
144 std::string debug = prefix + getDebugStringShort();
145 for (auto& [child, childVariant] : mChildren) {
146 std::string childPrefix = " " + std::string(prefix) + " " + std::to_string(childVariant);
147 debug += "\n" + child->getDebugString(childPrefix.c_str());
148 }
149 return debug;
150}
151
152bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
153 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
154 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
155 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
156 if (traversalPath.hasRelZLoop()) {
157 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
158 return false;
159 }
160 return true;
161 });
162 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
163}
164
165LayerHierarchyBuilder::LayerHierarchyBuilder(
166 const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
167 mHierarchies.reserve(layers.size());
168 mLayerIdToHierarchy.reserve(layers.size());
169 for (auto& layer : layers) {
170 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
171 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
172 }
173 for (const auto& layer : layers) {
174 onLayerAdded(layer.get());
175 }
176 detachHierarchyFromRelativeParent(&mOffscreenRoot);
177}
178
179void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
180 auto layer = hierarchy->mLayer;
181 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
182 ? LayerHierarchy::Variant::Detached
183 : LayerHierarchy::Variant::Attached;
184
185 LayerHierarchy* parent;
186
187 if (layer->parentId != UNASSIGNED_LAYER_ID) {
188 parent = getHierarchyFromId(layer->parentId);
189 } else if (layer->canBeRoot) {
190 parent = &mRoot;
191 } else {
192 parent = &mOffscreenRoot;
193 }
194 parent->addChild(hierarchy, type);
195 hierarchy->mParent = parent;
196}
197
198void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
199 hierarchy->mParent->removeChild(hierarchy);
200 hierarchy->mParent = nullptr;
201}
202
203void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
204 auto layer = hierarchy->mLayer;
205 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
206 return;
207 }
208
209 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
210 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
211 } else {
212 hierarchy->mRelativeParent = &mOffscreenRoot;
213 }
214 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
215 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
216}
217
218void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
219 if (hierarchy->mRelativeParent) {
220 hierarchy->mRelativeParent->removeChild(hierarchy);
221 }
222 hierarchy->mRelativeParent = nullptr;
223 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
224}
225
226void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
227 if (root->mLayer) {
228 attachToRelativeParent(root);
229 }
230 for (auto& [child, childVariant] : root->mChildren) {
231 if (childVariant == LayerHierarchy::Variant::Detached ||
232 childVariant == LayerHierarchy::Variant::Attached) {
233 attachHierarchyToRelativeParent(child);
234 }
235 }
236}
237
238void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
239 if (root->mLayer) {
240 detachFromRelativeParent(root);
241 }
242 for (auto& [child, childVariant] : root->mChildren) {
243 if (childVariant == LayerHierarchy::Variant::Detached ||
244 childVariant == LayerHierarchy::Variant::Attached) {
245 detachHierarchyFromRelativeParent(child);
246 }
247 }
248}
249
250void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
251 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
252 attachToParent(hierarchy);
253 attachToRelativeParent(hierarchy);
254
255 if (layer->mirrorId != UNASSIGNED_LAYER_ID) {
256 LayerHierarchy* mirror = getHierarchyFromId(layer->mirrorId);
257 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
258 }
259}
260
261void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
262 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
263 if (!hierarchy) {
264 // Layer was never part of the hierarchy if it was created and destroyed in the same
265 // transaction.
266 return;
267 }
268 // detach from parent
269 detachFromRelativeParent(hierarchy);
270 detachFromParent(hierarchy);
271
272 // detach children
273 for (auto& [child, variant] : hierarchy->mChildren) {
274 if (variant == LayerHierarchy::Variant::Attached ||
275 variant == LayerHierarchy::Variant::Detached) {
276 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
277 child->mParent = &mOffscreenRoot;
278 } else if (variant == LayerHierarchy::Variant::Relative) {
279 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
280 child->mRelativeParent = &mOffscreenRoot;
281 }
282 }
283
284 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
285 return layerHierarchy.get() == hierarchy;
286 });
287 mLayerIdToHierarchy.erase(layer->id);
288}
289
290void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
291 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
292 auto it = hierarchy->mChildren.begin();
293 while (it != hierarchy->mChildren.end()) {
294 if (it->second == LayerHierarchy::Variant::Mirror) {
295 hierarchy->mChildren.erase(it);
296 break;
297 }
298 it++;
299 }
300
301 if (layer->mirrorId != UNASSIGNED_LAYER_ID) {
302 hierarchy->addChild(getHierarchyFromId(layer->mirrorId), LayerHierarchy::Variant::Mirror);
303 }
304}
305
306void LayerHierarchyBuilder::update(
307 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
308 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
309 // rebuild map
310 for (auto& layer : layers) {
311 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
312 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
313 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
314 }
315 }
316
317 for (auto& layer : layers) {
318 if (layer->changes.get() == 0) {
319 continue;
320 }
321 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
322 onLayerAdded(layer.get());
323 continue;
324 }
325 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
326 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
327 detachFromParent(hierarchy);
328 attachToParent(hierarchy);
329 }
330 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
331 detachFromRelativeParent(hierarchy);
332 attachToRelativeParent(hierarchy);
333 }
334 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
335 hierarchy->mParent->sortChildrenByZOrder();
336 if (hierarchy->mRelativeParent) {
337 hierarchy->mRelativeParent->sortChildrenByZOrder();
338 }
339 }
340 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
341 updateMirrorLayer(layer.get());
342 }
343 }
344
345 for (auto& layer : destroyedLayers) {
346 onLayerDestroyed(layer.get());
347 }
348 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
349 // from our relative parents. This walks down both trees to do so. We can optimize this
350 // further by tracking onscreen, offscreen state in LayerHierarchy.
351 detachHierarchyFromRelativeParent(&mOffscreenRoot);
352 attachHierarchyToRelativeParent(&mRoot);
353}
354
355const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
356 return mRoot;
357}
358
359const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
360 return mOffscreenRoot;
361}
362
363std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
364 if (depth > 10) return "too deep, loop?";
365 if (layerId == UNASSIGNED_LAYER_ID) return "";
366 auto it = mLayerIdToHierarchy.find(layerId);
367 if (it == mLayerIdToHierarchy.end()) return "not found";
368
369 LayerHierarchy* hierarchy = it->second;
370 if (!hierarchy->mLayer) return "none";
371
372 std::string debug =
373 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
374 if (hierarchy->mRelativeParent) {
375 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
376 }
377 if (hierarchy->mParent) {
378 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
379 }
380 return debug;
381}
382
383LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
384 bool childrenOnly) const {
385 auto it = mLayerIdToHierarchy.find(layerId);
386 if (it == mLayerIdToHierarchy.end()) return {nullptr};
387
388 LayerHierarchy hierarchy(*it->second, childrenOnly);
389 return hierarchy;
390}
391
392LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
393 auto it = mLayerIdToHierarchy.find(layerId);
394 if (it == mLayerIdToHierarchy.end()) {
395 if (crashOnFailure) {
396 LOG_ALWAYS_FATAL("Could not find hierarchy for layer id %d", layerId);
397 }
398 return nullptr;
399 };
400
401 return it->second;
402}
403
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000404const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000405 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
406
407std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000408 if (id == UNASSIGNED_LAYER_ID) {
409 return "TraversalPath{ROOT}";
410 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000411 std::string debugString = "TraversalPath{.id = " + std::to_string(id);
412
413 if (!mirrorRootIds.empty()) {
414 debugString += ", .mirrorRootIds=";
415 for (auto rootId : mirrorRootIds) {
416 debugString += std::to_string(rootId) + ",";
417 }
418 }
419
420 if (!relativeRootIds.empty()) {
421 debugString += ", .relativeRootIds=";
422 for (auto rootId : relativeRootIds) {
423 debugString += std::to_string(rootId) + ",";
424 }
425 }
426
427 if (hasRelZLoop()) {
428 debugString += ", hasRelZLoop=true invalidRelativeRootId=";
429 debugString += std::to_string(invalidRelativeRootId) + ",";
430 }
431
432 debugString += "}";
433 return debugString;
434}
435
436// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
437// of scope the TraversalPath is reset to its original state.
438LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
439 uint32_t layerId,
440 LayerHierarchy::Variant variant)
441 : mTraversalPath(traversalPath),
442 mParentId(traversalPath.id),
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000443 mParentVariant(traversalPath.variant),
444 mParentDetached(traversalPath.detached) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000445 // Update the traversal id with the child layer id and variant. Parent id and variant are
446 // stored to reset the id upon destruction.
447 traversalPath.id = layerId;
448 traversalPath.variant = variant;
449 if (variant == LayerHierarchy::Variant::Mirror) {
450 traversalPath.mirrorRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000451 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000452 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
453 layerId) != traversalPath.relativeRootIds.end()) {
454 traversalPath.invalidRelativeRootId = layerId;
455 }
456 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000457 } else if (variant == LayerHierarchy::Variant::Detached) {
458 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000459 }
460}
461LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
462 // Reset the traversal id to its original parent state using the state that was saved in
463 // the constructor.
464 if (mTraversalPath.variant == LayerHierarchy::Variant::Mirror) {
465 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000466 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000467 mTraversalPath.relativeRootIds.pop_back();
468 }
469 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
470 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
471 }
472 mTraversalPath.id = mParentId;
473 mTraversalPath.variant = mParentVariant;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000474 mTraversalPath.detached = mParentDetached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000475}
476
477} // namespace android::surfaceflinger::frontend