blob: e864ff60f77c82f076ddb9709f0301c1d22a01a2 [file] [log] [blame]
Vishnu Nairdc4d31b2022-11-17 03:20:58 +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
fengqiancd3f6d82024-06-27 16:14:09 +080017// #define LOG_NDEBUG 0
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070021#define LOG_TAG "SurfaceFlinger"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000022
23#include "LayerLifecycleManager.h"
Vishnu Nair1391de22023-03-05 19:56:14 -080024#include "Client.h" // temporarily needed for LayerCreationArgs
Vishnu Nair80a5a702023-02-11 01:21:51 +000025#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000026#include "SwapErase.h"
27
28namespace android::surfaceflinger::frontend {
29
30using namespace ftl::flag_operators;
31
Vishnu Nair52c4f252023-06-14 15:25:12 -070032namespace {
33// Returns true if the layer is root of a display and can be mirrored by mirroringLayer
34bool canMirrorRootLayer(RequestedLayerState& mirroringLayer, RequestedLayerState& rootLayer) {
35 return rootLayer.isRoot() && rootLayer.layerStack == mirroringLayer.layerStackToMirror &&
36 rootLayer.id != mirroringLayer.id;
37}
38} // namespace
39
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000040void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) {
41 if (newLayers.empty()) {
42 return;
43 }
44
45 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
46 for (auto& newLayer : newLayers) {
47 RequestedLayerState& layer = *newLayer.get();
48 auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer});
Vishnu Nair606d9d02023-08-19 14:20:18 -070049 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!inserted,
50 "Duplicate layer id found. New layer: %s Existing layer: "
51 "%s",
52 layer.getDebugString().c_str(),
53 it->second.owner.getDebugString().c_str());
Vishnu Nair150065b2023-04-17 19:14:11 -070054 mAddedLayers.push_back(newLayer.get());
Vishnu Naira02943f2023-06-03 13:44:46 -070055 mChangedLayers.push_back(newLayer.get());
Vishnu Nair04f89692022-11-16 23:21:05 +000056 layer.parentId = linkLayer(layer.parentId, layer.id);
57 layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000058 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
Vishnu Nair52c4f252023-06-14 15:25:12 -070059 // Set mirror layer's default layer stack to -1 so it doesn't end up rendered on a
60 // display accidentally.
61 layer.layerStack = ui::INVALID_LAYER_STACK;
62
Vishnu Naira9c43762023-01-27 19:10:25 +000063 // if this layer is mirroring a display, then walk though all the existing root layers
64 // for the layer stack and add them as children to be mirrored.
65 mDisplayMirroringLayers.emplace_back(layer.id);
66 for (auto& rootLayer : mLayers) {
Vishnu Nair52c4f252023-06-14 15:25:12 -070067 if (canMirrorRootLayer(layer, *rootLayer)) {
Vishnu Naira9c43762023-01-27 19:10:25 +000068 layer.mirrorIds.emplace_back(rootLayer->id);
69 linkLayer(rootLayer->id, layer.id);
70 }
71 }
72 } else {
73 // Check if we are mirroring a single layer, and if so add it to the list of children
74 // to be mirrored.
75 layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id);
76 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
77 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
78 }
79 }
Vishnu Nair04f89692022-11-16 23:21:05 +000080 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000081 if (layer.isRoot()) {
82 updateDisplayMirrorLayers(layer);
83 }
Vishnu Nair92990e22023-02-24 20:01:05 +000084 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000085 mLayers.emplace_back(std::move(newLayer));
86 }
87}
88
Vishnu Nair606d9d02023-08-19 14:20:18 -070089void LayerLifecycleManager::onHandlesDestroyed(
90 const std::vector<std::pair<uint32_t, std::string /* debugName */>>& destroyedHandles,
91 bool ignoreUnknownHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000092 std::vector<uint32_t> layersToBeDestroyed;
Vishnu Nair606d9d02023-08-19 14:20:18 -070093 for (const auto& [layerId, name] : destroyedHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000094 auto it = mIdToLayer.find(layerId);
95 if (it == mIdToLayer.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -070096 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownHandles, "%s Layerid not found %s[%d]",
97 __func__, name.c_str(), layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000098 continue;
99 }
100 RequestedLayerState& layer = it->second.owner;
Vishnu Nair92990e22023-02-24 20:01:05 +0000101 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000102 layer.handleAlive = false;
103 if (!layer.canBeDestroyed()) {
104 continue;
105 }
106 layer.changes |= RequestedLayerState::Changes::Destroyed;
107 layersToBeDestroyed.emplace_back(layerId);
108 }
109
110 if (layersToBeDestroyed.empty()) {
111 return;
112 }
113
114 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
115 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
116 uint32_t layerId = layersToBeDestroyed[i];
117 auto it = mIdToLayer.find(layerId);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700118 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mIdToLayer.end(), "%s Layer with id %d not found",
119 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000120
121 RequestedLayerState& layer = it->second.owner;
122
Vishnu Nair04f89692022-11-16 23:21:05 +0000123 layer.parentId = unlinkLayer(layer.parentId, layer.id);
124 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000125 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
126 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
127 swapErase(mDisplayMirroringLayers, layer.id);
128 } else {
129 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
130 layer.mirrorIds.clear();
131 }
132
Vishnu Nair04f89692022-11-16 23:21:05 +0000133 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000134
135 auto& references = it->second.references;
136 for (uint32_t linkedLayerId : references) {
137 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700138 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!linkedLayer,
139 "%s Layerid reference %d not found for %d", __func__,
140 linkedLayerId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000141 if (linkedLayer->parentId == layer.id) {
142 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
143 if (linkedLayer->canBeDestroyed()) {
144 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
145 layersToBeDestroyed.emplace_back(linkedLayer->id);
146 }
147 }
148 if (linkedLayer->relativeParentId == layer.id) {
149 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
150 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000151 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
152 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000153 }
154 if (linkedLayer->touchCropId == layer.id) {
155 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
156 }
157 }
158 mIdToLayer.erase(it);
159 }
160
161 auto it = mLayers.begin();
162 while (it != mLayers.end()) {
163 RequestedLayerState* layer = it->get();
164 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000165 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000166 std::iter_swap(it, mLayers.end() - 1);
167 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000168 if (it == mLayers.end() - 1) {
169 it = mLayers.erase(mLayers.end() - 1);
170 } else {
171 mLayers.erase(mLayers.end() - 1);
172 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000173 } else {
174 it++;
175 }
176 }
177}
178
Vishnu Nair20e1f962023-03-29 15:58:34 -0700179void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions,
180 bool ignoreUnknownLayers) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000181 for (const auto& transaction : transactions) {
182 for (const auto& resolvedComposerState : transaction.states) {
183 const auto& clientState = resolvedComposerState.state;
Vishnu Nair1391de22023-03-05 19:56:14 -0800184 uint32_t layerId = resolvedComposerState.layerId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000185 if (layerId == UNASSIGNED_LAYER_ID) {
186 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
187 continue;
188 }
189
190 RequestedLayerState* layer = getLayerFromId(layerId);
191 if (layer == nullptr) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700192 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownLayers,
193 "%s Layer with layerid=%d not found", __func__,
194 layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000195 continue;
196 }
197
Vishnu Nair606d9d02023-08-19 14:20:18 -0700198 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!layer->handleAlive,
199 "%s Layer's with layerid=%d) is not alive. Possible "
200 "out of "
201 "order LayerLifecycleManager updates",
202 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000203
Vishnu Naira02943f2023-06-03 13:44:46 -0700204 if (layer->changes.get() == 0) {
205 mChangedLayers.push_back(layer);
206 }
207
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000208 if (transaction.flags & ISurfaceComposer::eAnimation) {
209 layer->changes |= RequestedLayerState::Changes::Animation;
210 }
211
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000212 uint32_t oldParentId = layer->parentId;
213 uint32_t oldRelativeParentId = layer->relativeParentId;
214 uint32_t oldTouchCropId = layer->touchCropId;
215 layer->merge(resolvedComposerState);
216
217 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000218 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Naire4af0952023-05-01 09:11:33 -0700219 LayerCreationArgs
220 backgroundLayerArgs(LayerCreationArgs::getInternalLayerId(
221 LayerCreationArgs::sInternalSequence++),
222 /*internalLayer=*/true);
Vishnu Nair1391de22023-03-05 19:56:14 -0800223 backgroundLayerArgs.parentId = layer->id;
224 backgroundLayerArgs.name = layer->name + "BackgroundColorLayer";
225 backgroundLayerArgs.flags = ISurfaceComposerClient::eFXSurfaceEffect;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000226 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
227 newLayers.emplace_back(
228 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
229 RequestedLayerState* backgroundLayer = newLayers.back().get();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000230 backgroundLayer->bgColorLayer = true;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000231 backgroundLayer->handleAlive = false;
232 backgroundLayer->parentId = layer->id;
233 backgroundLayer->z = std::numeric_limits<int32_t>::min();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000234 backgroundLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000235 backgroundLayer->dataspace = layer->bgColorDataspace;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000236 layer->bgColorLayerId = backgroundLayer->id;
237 addLayers({std::move(newLayers)});
Vishnu Naird47bcee2023-02-24 18:08:51 +0000238 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000239 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000240 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
241 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700242 onHandlesDestroyed({{bgColorLayer->id, bgColorLayer->debugName}});
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000243 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
244 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000245 bgColorLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000246 bgColorLayer->dataspace = layer->bgColorDataspace;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000247 bgColorLayer->what |= layer_state_t::eColorChanged |
248 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
249 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
Vishnu Naira02943f2023-06-03 13:44:46 -0700250 mChangedLayers.push_back(bgColorLayer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 mGlobalChanges |= RequestedLayerState::Changes::Content;
252 }
253 }
254
255 if (oldParentId != layer->parentId) {
256 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000257 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000258 if (oldParentId == UNASSIGNED_LAYER_ID) {
259 updateDisplayMirrorLayers(*layer);
260 }
261 }
262 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
263 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000264 }
265 if (oldRelativeParentId != layer->relativeParentId) {
266 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000267 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000268 }
269 if (oldTouchCropId != layer->touchCropId) {
270 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000271 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000272 }
273
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000274 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000275 }
276 }
277}
278
279void LayerLifecycleManager::commitChanges() {
Vishnu Nair150065b2023-04-17 19:14:11 -0700280 for (auto layer : mAddedLayers) {
281 for (auto& listener : mListeners) {
282 listener->onLayerAdded(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000283 }
Vishnu Nair150065b2023-04-17 19:14:11 -0700284 }
285 mAddedLayers.clear();
286
287 for (auto& layer : mLayers) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000288 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000289 }
290
291 for (auto& destroyedLayer : mDestroyedLayers) {
Vishnu Nair150065b2023-04-17 19:14:11 -0700292 for (auto& listener : mListeners) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000293 listener->onLayerDestroyed(*destroyedLayer);
294 }
295 }
296 mDestroyedLayers.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700297 mChangedLayers.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000298 mGlobalChanges.clear();
299}
300
301void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
302 mListeners.emplace_back(std::move(listener));
303}
304
305void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
306 swapErase(mListeners, listener);
307}
308
309const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
310 return mLayers;
311}
312
313const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
314 const {
315 return mDestroyedLayers;
316}
317
Vishnu Naira02943f2023-06-03 13:44:46 -0700318const std::vector<RequestedLayerState*>& LayerLifecycleManager::getChangedLayers() const {
319 return mChangedLayers;
320}
321
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000322const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
323 return mGlobalChanges;
324}
325
Vishnu Naira02943f2023-06-03 13:44:46 -0700326const RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) const {
327 if (id == UNASSIGNED_LAYER_ID) {
328 return nullptr;
329 }
330 auto it = mIdToLayer.find(id);
331 if (it == mIdToLayer.end()) {
332 return nullptr;
333 }
334 return &it->second.owner;
335}
336
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000337RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
338 if (id == UNASSIGNED_LAYER_ID) {
339 return nullptr;
340 }
341 auto it = mIdToLayer.find(id);
342 if (it == mIdToLayer.end()) {
343 return nullptr;
344 }
345 return &it->second.owner;
346}
347
348std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
349 if (id == UNASSIGNED_LAYER_ID) {
350 return nullptr;
351 }
352 auto it = mIdToLayer.find(id);
353 if (it == mIdToLayer.end()) {
354 return nullptr;
355 }
356 return &it->second.references;
357}
358
Vishnu Nair04f89692022-11-16 23:21:05 +0000359uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
360 if (layerId == UNASSIGNED_LAYER_ID) {
361 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000362 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000363
364 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
365 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000366 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
367 layerToLink);
368 return UNASSIGNED_LAYER_ID;
369 }
370 linkedLayers->emplace_back(layerToLink);
371 return layerId;
372}
373
374uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
375 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
376 if (!linkedLayers) {
377 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000378 }
379 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000380 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000381}
382
Vishnu Naira9c43762023-01-27 19:10:25 +0000383std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
384 uint32_t linkedLayer) {
385 for (uint32_t layerId : layerIds) {
386 unlinkLayer(layerId, linkedLayer);
387 }
388 return {};
389}
390
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000391std::string LayerLifecycleManager::References::getDebugString() const {
392 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
393 std::for_each(references.begin(), references.end(),
394 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
395 debugInfo += std::to_string(reference) + ",";
396 });
397 return debugInfo;
398}
399
Vishnu Nair04f89692022-11-16 23:21:05 +0000400void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
401 auto it = mIdToLayer.find(relativeRootId);
402 if (it == mIdToLayer.end()) {
403 return;
404 }
405 RequestedLayerState& layer = it->second.owner;
406 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
407 layer.changes |=
408 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
409 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
410}
411
Vishnu Naira9c43762023-01-27 19:10:25 +0000412// Some layers mirror the entire display stack. Since we don't have a single root layer per display
413// we have to track all these layers and update what they mirror when the list of root layers
414// on a display changes. This function walks through the list of display mirroring layers
415// and updates its list of layers that its mirroring. This function should be called when a new
416// root layer is added, removed or moved to another display.
417void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700418 for (uint32_t mirroringLayerId : mDisplayMirroringLayers) {
419 RequestedLayerState* mirrorLayer = getLayerFromId(mirroringLayerId);
420 bool canBeMirrored = canMirrorRootLayer(*mirrorLayer, rootLayer);
Vishnu Naira9c43762023-01-27 19:10:25 +0000421 bool currentlyMirrored =
422 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
423 rootLayer.id) != mirrorLayer->mirrorIds.end();
424
425 if (canBeMirrored && !currentlyMirrored) {
426 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
427 linkLayer(rootLayer.id, mirrorLayer->id);
428 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
429 } else if (!canBeMirrored && currentlyMirrored) {
430 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
431 unlinkLayer(rootLayer.id, mirrorLayer->id);
432 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
433 }
434 }
435}
436
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000437bool LayerLifecycleManager::isLayerSecure(uint32_t layerId) const {
438 if (layerId == UNASSIGNED_LAYER_ID) {
439 return false;
440 }
441
442 if (getLayerFromId(layerId)->flags & layer_state_t::eLayerSecure) {
443 return true;
444 }
445 return isLayerSecure(getLayerFromId(layerId)->parentId);
446}
447
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000448} // namespace android::surfaceflinger::frontend