blob: 52f8bea4ba64bc848772003954968280f4f73e75 [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
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000021
22#include "LayerLifecycleManager.h"
Vishnu Nair1391de22023-03-05 19:56:14 -080023#include "Client.h" // temporarily needed for LayerCreationArgs
Vishnu Nair80a5a702023-02-11 01:21:51 +000024#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000025#include "SwapErase.h"
26
27namespace android::surfaceflinger::frontend {
28
29using namespace ftl::flag_operators;
30
Vishnu Nair52c4f252023-06-14 15:25:12 -070031namespace {
32// Returns true if the layer is root of a display and can be mirrored by mirroringLayer
33bool canMirrorRootLayer(RequestedLayerState& mirroringLayer, RequestedLayerState& rootLayer) {
34 return rootLayer.isRoot() && rootLayer.layerStack == mirroringLayer.layerStackToMirror &&
35 rootLayer.id != mirroringLayer.id;
36}
37} // namespace
38
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000039void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) {
40 if (newLayers.empty()) {
41 return;
42 }
43
Vishnu Nairdf59f472024-05-17 16:51:33 +000044 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy |
45 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000046 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);
Vishnu Nair491827d2024-04-29 23:43:26 +000076 if (!FlagManager::getInstance().detached_mirror()) {
77 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
78 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
79 }
Vishnu Naira9c43762023-01-27 19:10:25 +000080 }
81 }
Vishnu Nair04f89692022-11-16 23:21:05 +000082 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000083 if (layer.isRoot()) {
84 updateDisplayMirrorLayers(layer);
85 }
Vishnu Nair92990e22023-02-24 20:01:05 +000086 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000087 mLayers.emplace_back(std::move(newLayer));
88 }
89}
90
Vishnu Nair606d9d02023-08-19 14:20:18 -070091void LayerLifecycleManager::onHandlesDestroyed(
92 const std::vector<std::pair<uint32_t, std::string /* debugName */>>& destroyedHandles,
93 bool ignoreUnknownHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000094 std::vector<uint32_t> layersToBeDestroyed;
Vishnu Nair606d9d02023-08-19 14:20:18 -070095 for (const auto& [layerId, name] : destroyedHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000096 auto it = mIdToLayer.find(layerId);
97 if (it == mIdToLayer.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -070098 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownHandles, "%s Layerid not found %s[%d]",
99 __func__, name.c_str(), layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000100 continue;
101 }
102 RequestedLayerState& layer = it->second.owner;
Vishnu Nair92990e22023-02-24 20:01:05 +0000103 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000104 layer.handleAlive = false;
105 if (!layer.canBeDestroyed()) {
106 continue;
107 }
Vishnu Nairdf59f472024-05-17 16:51:33 +0000108 layer.changes |= RequestedLayerState::Changes::Destroyed |
109 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000110 layersToBeDestroyed.emplace_back(layerId);
111 }
112
113 if (layersToBeDestroyed.empty()) {
114 return;
115 }
116
Vishnu Nairdf59f472024-05-17 16:51:33 +0000117 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy |
118 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000119 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
120 uint32_t layerId = layersToBeDestroyed[i];
121 auto it = mIdToLayer.find(layerId);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700122 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mIdToLayer.end(), "%s Layer with id %d not found",
123 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000124
125 RequestedLayerState& layer = it->second.owner;
126
Vishnu Nair04f89692022-11-16 23:21:05 +0000127 layer.parentId = unlinkLayer(layer.parentId, layer.id);
128 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000129 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
130 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
131 swapErase(mDisplayMirroringLayers, layer.id);
132 } else {
133 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
134 layer.mirrorIds.clear();
135 }
136
Vishnu Nair04f89692022-11-16 23:21:05 +0000137 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000138
139 auto& references = it->second.references;
140 for (uint32_t linkedLayerId : references) {
141 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700142 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!linkedLayer,
143 "%s Layerid reference %d not found for %d", __func__,
144 linkedLayerId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000145 if (linkedLayer->parentId == layer.id) {
146 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
147 if (linkedLayer->canBeDestroyed()) {
Vishnu Nairdf59f472024-05-17 16:51:33 +0000148 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed |
149 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000150 layersToBeDestroyed.emplace_back(linkedLayer->id);
151 }
152 }
153 if (linkedLayer->relativeParentId == layer.id) {
154 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
155 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000156 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
157 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000158 }
159 if (linkedLayer->touchCropId == layer.id) {
160 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
161 }
162 }
163 mIdToLayer.erase(it);
164 }
165
166 auto it = mLayers.begin();
167 while (it != mLayers.end()) {
168 RequestedLayerState* layer = it->get();
169 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000170 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000171 std::iter_swap(it, mLayers.end() - 1);
172 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000173 if (it == mLayers.end() - 1) {
174 it = mLayers.erase(mLayers.end() - 1);
175 } else {
176 mLayers.erase(mLayers.end() - 1);
177 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000178 } else {
179 it++;
180 }
181 }
182}
183
Vishnu Nair20e1f962023-03-29 15:58:34 -0700184void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions,
185 bool ignoreUnknownLayers) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000186 for (const auto& transaction : transactions) {
187 for (const auto& resolvedComposerState : transaction.states) {
188 const auto& clientState = resolvedComposerState.state;
Vishnu Nair1391de22023-03-05 19:56:14 -0800189 uint32_t layerId = resolvedComposerState.layerId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000190 if (layerId == UNASSIGNED_LAYER_ID) {
191 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
192 continue;
193 }
194
195 RequestedLayerState* layer = getLayerFromId(layerId);
196 if (layer == nullptr) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700197 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownLayers,
198 "%s Layer with layerid=%d not found", __func__,
199 layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000200 continue;
201 }
202
Vishnu Nair606d9d02023-08-19 14:20:18 -0700203 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!layer->handleAlive,
204 "%s Layer's with layerid=%d) is not alive. Possible "
205 "out of "
206 "order LayerLifecycleManager updates",
207 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000208
Vishnu Naira02943f2023-06-03 13:44:46 -0700209 if (layer->changes.get() == 0) {
210 mChangedLayers.push_back(layer);
211 }
212
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000213 if (transaction.flags & ISurfaceComposer::eAnimation) {
214 layer->changes |= RequestedLayerState::Changes::Animation;
215 }
216
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000217 uint32_t oldParentId = layer->parentId;
218 uint32_t oldRelativeParentId = layer->relativeParentId;
219 uint32_t oldTouchCropId = layer->touchCropId;
220 layer->merge(resolvedComposerState);
221
222 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000223 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Naire4af0952023-05-01 09:11:33 -0700224 LayerCreationArgs
225 backgroundLayerArgs(LayerCreationArgs::getInternalLayerId(
226 LayerCreationArgs::sInternalSequence++),
227 /*internalLayer=*/true);
Vishnu Nair1391de22023-03-05 19:56:14 -0800228 backgroundLayerArgs.parentId = layer->id;
229 backgroundLayerArgs.name = layer->name + "BackgroundColorLayer";
230 backgroundLayerArgs.flags = ISurfaceComposerClient::eFXSurfaceEffect;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000231 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
232 newLayers.emplace_back(
233 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
234 RequestedLayerState* backgroundLayer = newLayers.back().get();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000235 backgroundLayer->bgColorLayer = true;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000236 backgroundLayer->handleAlive = false;
237 backgroundLayer->parentId = layer->id;
238 backgroundLayer->z = std::numeric_limits<int32_t>::min();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000239 backgroundLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000240 backgroundLayer->dataspace = layer->bgColorDataspace;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000241 layer->bgColorLayerId = backgroundLayer->id;
242 addLayers({std::move(newLayers)});
Vishnu Naird47bcee2023-02-24 18:08:51 +0000243 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000244 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000245 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
246 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
Vishnu Nair606d9d02023-08-19 14:20:18 -0700247 onHandlesDestroyed({{bgColorLayer->id, bgColorLayer->debugName}});
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
249 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000250 bgColorLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 bgColorLayer->dataspace = layer->bgColorDataspace;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000252 bgColorLayer->what |= layer_state_t::eColorChanged |
253 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
254 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
Vishnu Naira02943f2023-06-03 13:44:46 -0700255 mChangedLayers.push_back(bgColorLayer);
Vishnu Nairdf59f472024-05-17 16:51:33 +0000256 mGlobalChanges |= RequestedLayerState::Changes::Content |
257 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000258 }
259 }
260
261 if (oldParentId != layer->parentId) {
262 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000263 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000264 if (oldParentId == UNASSIGNED_LAYER_ID) {
265 updateDisplayMirrorLayers(*layer);
266 }
267 }
268 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
269 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000270 }
271 if (oldRelativeParentId != layer->relativeParentId) {
272 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000273 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 }
275 if (oldTouchCropId != layer->touchCropId) {
276 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000277 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000278 }
279
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000280 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000281 }
282 }
283}
284
285void LayerLifecycleManager::commitChanges() {
Vishnu Nair150065b2023-04-17 19:14:11 -0700286 for (auto layer : mAddedLayers) {
287 for (auto& listener : mListeners) {
288 listener->onLayerAdded(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000289 }
Vishnu Nair150065b2023-04-17 19:14:11 -0700290 }
291 mAddedLayers.clear();
292
293 for (auto& layer : mLayers) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000294 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000295 }
296
297 for (auto& destroyedLayer : mDestroyedLayers) {
Vishnu Nair150065b2023-04-17 19:14:11 -0700298 for (auto& listener : mListeners) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000299 listener->onLayerDestroyed(*destroyedLayer);
300 }
301 }
302 mDestroyedLayers.clear();
Vishnu Naira02943f2023-06-03 13:44:46 -0700303 mChangedLayers.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000304 mGlobalChanges.clear();
305}
306
307void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
308 mListeners.emplace_back(std::move(listener));
309}
310
311void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
312 swapErase(mListeners, listener);
313}
314
315const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
316 return mLayers;
317}
318
319const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
320 const {
321 return mDestroyedLayers;
322}
323
Vishnu Naira02943f2023-06-03 13:44:46 -0700324const std::vector<RequestedLayerState*>& LayerLifecycleManager::getChangedLayers() const {
325 return mChangedLayers;
326}
327
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000328const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
329 return mGlobalChanges;
330}
331
Vishnu Naira02943f2023-06-03 13:44:46 -0700332const RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) const {
333 if (id == UNASSIGNED_LAYER_ID) {
334 return nullptr;
335 }
336 auto it = mIdToLayer.find(id);
337 if (it == mIdToLayer.end()) {
338 return nullptr;
339 }
340 return &it->second.owner;
341}
342
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000343RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
344 if (id == UNASSIGNED_LAYER_ID) {
345 return nullptr;
346 }
347 auto it = mIdToLayer.find(id);
348 if (it == mIdToLayer.end()) {
349 return nullptr;
350 }
351 return &it->second.owner;
352}
353
354std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
355 if (id == UNASSIGNED_LAYER_ID) {
356 return nullptr;
357 }
358 auto it = mIdToLayer.find(id);
359 if (it == mIdToLayer.end()) {
360 return nullptr;
361 }
362 return &it->second.references;
363}
364
Vishnu Nair04f89692022-11-16 23:21:05 +0000365uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
366 if (layerId == UNASSIGNED_LAYER_ID) {
367 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000368 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000369
370 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
371 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000372 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
373 layerToLink);
374 return UNASSIGNED_LAYER_ID;
375 }
376 linkedLayers->emplace_back(layerToLink);
377 return layerId;
378}
379
380uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
381 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
382 if (!linkedLayers) {
383 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000384 }
385 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000386 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000387}
388
Vishnu Naira9c43762023-01-27 19:10:25 +0000389std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
390 uint32_t linkedLayer) {
391 for (uint32_t layerId : layerIds) {
392 unlinkLayer(layerId, linkedLayer);
393 }
394 return {};
395}
396
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000397std::string LayerLifecycleManager::References::getDebugString() const {
398 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
399 std::for_each(references.begin(), references.end(),
400 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
401 debugInfo += std::to_string(reference) + ",";
402 });
403 return debugInfo;
404}
405
Vishnu Nair04f89692022-11-16 23:21:05 +0000406void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
407 auto it = mIdToLayer.find(relativeRootId);
408 if (it == mIdToLayer.end()) {
409 return;
410 }
411 RequestedLayerState& layer = it->second.owner;
412 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
413 layer.changes |=
414 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
Vishnu Nairdf59f472024-05-17 16:51:33 +0000415 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy |
416 RequestedLayerState::Changes::RequiresComposition;
Vishnu Nair04f89692022-11-16 23:21:05 +0000417}
418
Vishnu Naira9c43762023-01-27 19:10:25 +0000419// Some layers mirror the entire display stack. Since we don't have a single root layer per display
420// we have to track all these layers and update what they mirror when the list of root layers
421// on a display changes. This function walks through the list of display mirroring layers
422// and updates its list of layers that its mirroring. This function should be called when a new
423// root layer is added, removed or moved to another display.
424void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
Vishnu Nair52c4f252023-06-14 15:25:12 -0700425 for (uint32_t mirroringLayerId : mDisplayMirroringLayers) {
426 RequestedLayerState* mirrorLayer = getLayerFromId(mirroringLayerId);
427 bool canBeMirrored = canMirrorRootLayer(*mirrorLayer, rootLayer);
Vishnu Naira9c43762023-01-27 19:10:25 +0000428 bool currentlyMirrored =
429 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
430 rootLayer.id) != mirrorLayer->mirrorIds.end();
431
432 if (canBeMirrored && !currentlyMirrored) {
433 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
434 linkLayer(rootLayer.id, mirrorLayer->id);
435 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
436 } else if (!canBeMirrored && currentlyMirrored) {
437 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
438 unlinkLayer(rootLayer.id, mirrorLayer->id);
439 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
440 }
441 }
442}
443
Chavi Weingarten4aa22af2023-11-17 19:37:07 +0000444bool LayerLifecycleManager::isLayerSecure(uint32_t layerId) const {
445 if (layerId == UNASSIGNED_LAYER_ID) {
446 return false;
447 }
448
449 if (getLayerFromId(layerId)->flags & layer_state_t::eLayerSecure) {
450 return true;
451 }
452 return isLayerSecure(getLayerFromId(layerId)->parentId);
453}
454
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000455} // namespace android::surfaceflinger::frontend