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,
__anonbe4e23780202(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) const55 void 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
traverseInZOrder(const Visitor & visitor,LayerHierarchy::TraversalPath & traversalPath) const73 void 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) {
79 traverseThisLayer = false;
80 bool breakTraversal = !visitor(*this, traversalPath);
81 if (breakTraversal) {
82 return;
83 }
84 }
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
addChild(LayerHierarchy * child,LayerHierarchy::Variant variant)98 void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
99 insertSorted(mChildren, {child, variant});
100 }
101
removeChild(LayerHierarchy * child)102 void 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
sortChildrenByZOrder()113 void LayerHierarchy::sortChildrenByZOrder() {
114 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
115 }
116
updateChild(LayerHierarchy * hierarchy,LayerHierarchy::Variant variant)117 void 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
getLayer() const129 const RequestedLayerState* LayerHierarchy::getLayer() const {
130 return mLayer;
131 }
132
getRelativeParent() const133 const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
134 return mRelativeParent;
135 }
136
getParent() const137 const LayerHierarchy* LayerHierarchy::getParent() const {
138 return mParent;
139 }
140
getDebugStringShort() const141 std::string LayerHierarchy::getDebugStringShort() const {
142 std::string debug = "LayerHierarchy{";
143 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
144 if (mChildren.empty()) {
145 debug += "no children";
146 } else {
147 debug += std::to_string(mChildren.size()) + " children";
148 }
149 return debug + "}";
150 }
151
getDebugString(const char * prefix) const152 std::string LayerHierarchy::getDebugString(const char* prefix) const {
153 std::string debug = prefix + getDebugStringShort();
154 for (auto& [child, childVariant] : mChildren) {
155 std::string childPrefix = " " + std::string(prefix) + " " + std::to_string(childVariant);
156 debug += "\n" + child->getDebugString(childPrefix.c_str());
157 }
158 return debug;
159 }
160
hasRelZLoop(uint32_t & outInvalidRelativeRoot) const161 bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
162 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
163 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
164 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
165 if (traversalPath.hasRelZLoop()) {
166 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
167 return false;
168 }
169 return true;
170 });
171 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
172 }
173
LayerHierarchyBuilder(const std::vector<std::unique_ptr<RequestedLayerState>> & layers)174 LayerHierarchyBuilder::LayerHierarchyBuilder(
175 const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
176 mHierarchies.reserve(layers.size());
177 mLayerIdToHierarchy.reserve(layers.size());
178 for (auto& layer : layers) {
179 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
180 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
181 }
182 for (const auto& layer : layers) {
183 onLayerAdded(layer.get());
184 }
185 detachHierarchyFromRelativeParent(&mOffscreenRoot);
186 }
187
attachToParent(LayerHierarchy * hierarchy)188 void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
189 auto layer = hierarchy->mLayer;
190 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
191 ? LayerHierarchy::Variant::Detached
192 : LayerHierarchy::Variant::Attached;
193
194 LayerHierarchy* parent;
195
196 if (layer->parentId != UNASSIGNED_LAYER_ID) {
197 parent = getHierarchyFromId(layer->parentId);
198 } else if (layer->canBeRoot) {
199 parent = &mRoot;
200 } else {
201 parent = &mOffscreenRoot;
202 }
203 parent->addChild(hierarchy, type);
204 hierarchy->mParent = parent;
205 }
206
detachFromParent(LayerHierarchy * hierarchy)207 void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
208 hierarchy->mParent->removeChild(hierarchy);
209 hierarchy->mParent = nullptr;
210 }
211
attachToRelativeParent(LayerHierarchy * hierarchy)212 void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
213 auto layer = hierarchy->mLayer;
214 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
215 return;
216 }
217
218 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
219 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
220 } else {
221 hierarchy->mRelativeParent = &mOffscreenRoot;
222 }
223 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
224 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
225 }
226
detachFromRelativeParent(LayerHierarchy * hierarchy)227 void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
228 if (hierarchy->mRelativeParent) {
229 hierarchy->mRelativeParent->removeChild(hierarchy);
230 }
231 hierarchy->mRelativeParent = nullptr;
232 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
233 }
234
attachHierarchyToRelativeParent(LayerHierarchy * root)235 void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
236 if (root->mLayer) {
237 attachToRelativeParent(root);
238 }
239 for (auto& [child, childVariant] : root->mChildren) {
240 if (childVariant == LayerHierarchy::Variant::Detached ||
241 childVariant == LayerHierarchy::Variant::Attached) {
242 attachHierarchyToRelativeParent(child);
243 }
244 }
245 }
246
detachHierarchyFromRelativeParent(LayerHierarchy * root)247 void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
248 if (root->mLayer) {
249 detachFromRelativeParent(root);
250 }
251 for (auto& [child, childVariant] : root->mChildren) {
252 if (childVariant == LayerHierarchy::Variant::Detached ||
253 childVariant == LayerHierarchy::Variant::Attached) {
254 detachHierarchyFromRelativeParent(child);
255 }
256 }
257 }
258
onLayerAdded(RequestedLayerState * layer)259 void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
260 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
261 attachToParent(hierarchy);
262 attachToRelativeParent(hierarchy);
263
264 for (uint32_t mirrorId : layer->mirrorIds) {
265 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
266 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
267 }
268 }
269
onLayerDestroyed(RequestedLayerState * layer)270 void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
271 LLOGV(layer->id, "");
272 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
273 if (!hierarchy) {
274 // Layer was never part of the hierarchy if it was created and destroyed in the same
275 // transaction.
276 return;
277 }
278 // detach from parent
279 detachFromRelativeParent(hierarchy);
280 detachFromParent(hierarchy);
281
282 // detach children
283 for (auto& [child, variant] : hierarchy->mChildren) {
284 if (variant == LayerHierarchy::Variant::Attached ||
285 variant == LayerHierarchy::Variant::Detached) {
286 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
287 child->mParent = &mOffscreenRoot;
288 } else if (variant == LayerHierarchy::Variant::Relative) {
289 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
290 child->mRelativeParent = &mOffscreenRoot;
291 }
292 }
293
294 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
295 return layerHierarchy.get() == hierarchy;
296 });
297 mLayerIdToHierarchy.erase(layer->id);
298 }
299
updateMirrorLayer(RequestedLayerState * layer)300 void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
301 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
302 auto it = hierarchy->mChildren.begin();
303 while (it != hierarchy->mChildren.end()) {
304 if (it->second == LayerHierarchy::Variant::Mirror) {
305 it = hierarchy->mChildren.erase(it);
306 } else {
307 it++;
308 }
309 }
310
311 for (uint32_t mirrorId : layer->mirrorIds) {
312 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
313 }
314 }
315
update(const std::vector<std::unique_ptr<RequestedLayerState>> & layers,const std::vector<std::unique_ptr<RequestedLayerState>> & destroyedLayers)316 void LayerHierarchyBuilder::update(
317 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
318 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
319 // rebuild map
320 for (auto& layer : layers) {
321 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
322 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
323 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
324 }
325 }
326
327 for (auto& layer : layers) {
328 if (layer->changes.get() == 0) {
329 continue;
330 }
331 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
332 onLayerAdded(layer.get());
333 continue;
334 }
335 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
336 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
337 detachFromParent(hierarchy);
338 attachToParent(hierarchy);
339 }
340 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
341 detachFromRelativeParent(hierarchy);
342 attachToRelativeParent(hierarchy);
343 }
344 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
345 hierarchy->mParent->sortChildrenByZOrder();
346 if (hierarchy->mRelativeParent) {
347 hierarchy->mRelativeParent->sortChildrenByZOrder();
348 }
349 }
350 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
351 updateMirrorLayer(layer.get());
352 }
353 }
354
355 for (auto& layer : destroyedLayers) {
356 onLayerDestroyed(layer.get());
357 }
358 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
359 // from our relative parents. This walks down both trees to do so. We can optimize this
360 // further by tracking onscreen, offscreen state in LayerHierarchy.
361 detachHierarchyFromRelativeParent(&mOffscreenRoot);
362 attachHierarchyToRelativeParent(&mRoot);
363 }
364
getHierarchy() const365 const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
366 return mRoot;
367 }
368
getOffscreenHierarchy() const369 const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
370 return mOffscreenRoot;
371 }
372
getDebugString(uint32_t layerId,uint32_t depth) const373 std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
374 if (depth > 10) return "too deep, loop?";
375 if (layerId == UNASSIGNED_LAYER_ID) return "";
376 auto it = mLayerIdToHierarchy.find(layerId);
377 if (it == mLayerIdToHierarchy.end()) return "not found";
378
379 LayerHierarchy* hierarchy = it->second;
380 if (!hierarchy->mLayer) return "none";
381
382 std::string debug =
383 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
384 if (hierarchy->mRelativeParent) {
385 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
386 }
387 if (hierarchy->mParent) {
388 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
389 }
390 return debug;
391 }
392
getPartialHierarchy(uint32_t layerId,bool childrenOnly) const393 LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
394 bool childrenOnly) const {
395 auto it = mLayerIdToHierarchy.find(layerId);
396 if (it == mLayerIdToHierarchy.end()) return {nullptr};
397
398 LayerHierarchy hierarchy(*it->second, childrenOnly);
399 return hierarchy;
400 }
401
getHierarchyFromId(uint32_t layerId,bool crashOnFailure)402 LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
403 auto it = mLayerIdToHierarchy.find(layerId);
404 if (it == mLayerIdToHierarchy.end()) {
405 if (crashOnFailure) {
406 LOG_ALWAYS_FATAL("Could not find hierarchy for layer id %d", layerId);
407 }
408 return nullptr;
409 };
410
411 return it->second;
412 }
413
414 const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
415 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
416
toString() const417 std::string LayerHierarchy::TraversalPath::toString() const {
418 if (id == UNASSIGNED_LAYER_ID) {
419 return "TraversalPath{ROOT}";
420 }
421 std::stringstream ss;
422 ss << "TraversalPath{.id = " << id;
423
424 if (mirrorRootId != UNASSIGNED_LAYER_ID) {
425 ss << ", .mirrorRootId=" << mirrorRootId;
426 }
427
428 if (!relativeRootIds.empty()) {
429 ss << ", .relativeRootIds=";
430 for (auto rootId : relativeRootIds) {
431 ss << rootId << ",";
432 }
433 }
434
435 if (hasRelZLoop()) {
436 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
437 }
438 ss << "}";
439 return ss.str();
440 }
441
getMirrorRoot() const442 LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::getMirrorRoot() const {
443 LOG_ALWAYS_FATAL_IF(!isClone(), "Cannot get mirror root of a non cloned node");
444 TraversalPath mirrorRootPath = *this;
445 mirrorRootPath.id = mirrorRootId;
446 return mirrorRootPath;
447 }
448
449 // Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
450 // of scope the TraversalPath is reset to its original state.
ScopedAddToTraversalPath(TraversalPath & traversalPath,uint32_t layerId,LayerHierarchy::Variant variant)451 LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
452 uint32_t layerId,
453 LayerHierarchy::Variant variant)
454 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
455 // Update the traversal id with the child layer id and variant. Parent id and variant are
456 // stored to reset the id upon destruction.
457 traversalPath.id = layerId;
458 traversalPath.variant = variant;
459 if (variant == LayerHierarchy::Variant::Mirror) {
460 traversalPath.mirrorRootId = mParentPath.id;
461 } else if (variant == LayerHierarchy::Variant::Relative) {
462 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
463 layerId) != traversalPath.relativeRootIds.end()) {
464 traversalPath.invalidRelativeRootId = layerId;
465 }
466 traversalPath.relativeRootIds.emplace_back(layerId);
467 } else if (variant == LayerHierarchy::Variant::Detached) {
468 traversalPath.detached = true;
469 }
470 }
~ScopedAddToTraversalPath()471 LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
472 // Reset the traversal id to its original parent state using the state that was saved in
473 // the constructor.
474 if (mTraversalPath.variant == LayerHierarchy::Variant::Mirror) {
475 mTraversalPath.mirrorRootId = mParentPath.mirrorRootId;
476 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
477 mTraversalPath.relativeRootIds.pop_back();
478 }
479 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
480 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
481 }
482 mTraversalPath.id = mParentPath.id;
483 mTraversalPath.variant = mParentPath.variant;
484 mTraversalPath.detached = mParentPath.detached;
485 }
486
487 } // namespace android::surfaceflinger::frontend
488