blob: 79ffcbf65db638084c3bcbf58b14f883688ca4cc [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
20#define LOG_TAG "LayerLifecycleManager"
21
22#include "LayerLifecycleManager.h"
23#include "Layer.h" // temporarily needed for LayerHandle
24#include "LayerHandle.h"
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
32void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) {
33 if (newLayers.empty()) {
34 return;
35 }
36
37 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
38 for (auto& newLayer : newLayers) {
39 RequestedLayerState& layer = *newLayer.get();
Vishnu Nair80a5a702023-02-11 01:21:51 +000040 LLOGV(layer.id, "%s layer %s", __func__, layer.getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000041 auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer});
42 if (!inserted) {
43 LOG_ALWAYS_FATAL("Duplicate layer id %d found. Existing layer: %s", layer.id,
44 it->second.owner.getDebugString().c_str());
45 }
46
Vishnu Nair04f89692022-11-16 23:21:05 +000047 layer.parentId = linkLayer(layer.parentId, layer.id);
48 layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000049 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
50 // if this layer is mirroring a display, then walk though all the existing root layers
51 // for the layer stack and add them as children to be mirrored.
52 mDisplayMirroringLayers.emplace_back(layer.id);
53 for (auto& rootLayer : mLayers) {
54 if (rootLayer->isRoot() && rootLayer->layerStack == layer.layerStackToMirror) {
55 layer.mirrorIds.emplace_back(rootLayer->id);
56 linkLayer(rootLayer->id, layer.id);
57 }
58 }
59 } else {
60 // Check if we are mirroring a single layer, and if so add it to the list of children
61 // to be mirrored.
62 layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id);
63 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
64 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
65 }
66 }
Vishnu Nair04f89692022-11-16 23:21:05 +000067 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000068 if (layer.isRoot()) {
69 updateDisplayMirrorLayers(layer);
70 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000071 mLayers.emplace_back(std::move(newLayer));
72 }
73}
74
75void LayerLifecycleManager::onHandlesDestroyed(const std::vector<uint32_t>& destroyedHandles) {
76 std::vector<uint32_t> layersToBeDestroyed;
77 for (const auto& layerId : destroyedHandles) {
78 auto it = mIdToLayer.find(layerId);
79 if (it == mIdToLayer.end()) {
80 LOG_ALWAYS_FATAL("%s Layerid not found %d", __func__, layerId);
81 continue;
82 }
83 RequestedLayerState& layer = it->second.owner;
84 layer.handleAlive = false;
85 if (!layer.canBeDestroyed()) {
86 continue;
87 }
88 layer.changes |= RequestedLayerState::Changes::Destroyed;
89 layersToBeDestroyed.emplace_back(layerId);
90 }
91
92 if (layersToBeDestroyed.empty()) {
93 return;
94 }
95
96 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
97 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
98 uint32_t layerId = layersToBeDestroyed[i];
99 auto it = mIdToLayer.find(layerId);
100 if (it == mIdToLayer.end()) {
101 LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId);
102 continue;
103 }
104
105 RequestedLayerState& layer = it->second.owner;
106
Vishnu Nair04f89692022-11-16 23:21:05 +0000107 layer.parentId = unlinkLayer(layer.parentId, layer.id);
108 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000109 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
110 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
111 swapErase(mDisplayMirroringLayers, layer.id);
112 } else {
113 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
114 layer.mirrorIds.clear();
115 }
116
Vishnu Nair04f89692022-11-16 23:21:05 +0000117 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000118
119 auto& references = it->second.references;
120 for (uint32_t linkedLayerId : references) {
121 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
122 if (!linkedLayer) {
123 LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__,
124 linkedLayerId, layer.id);
125 continue;
126 };
127 if (linkedLayer->parentId == layer.id) {
128 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
129 if (linkedLayer->canBeDestroyed()) {
130 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
131 layersToBeDestroyed.emplace_back(linkedLayer->id);
132 }
133 }
134 if (linkedLayer->relativeParentId == layer.id) {
135 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
136 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000137 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
138 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000139 }
140 if (linkedLayer->touchCropId == layer.id) {
141 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
142 }
143 }
144 mIdToLayer.erase(it);
145 }
146
147 auto it = mLayers.begin();
148 while (it != mLayers.end()) {
149 RequestedLayerState* layer = it->get();
150 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000151 LLOGV(layer->id, "destroyed layer %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000152 std::iter_swap(it, mLayers.end() - 1);
153 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000154 if (it == mLayers.end() - 1) {
155 it = mLayers.erase(mLayers.end() - 1);
156 } else {
157 mLayers.erase(mLayers.end() - 1);
158 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000159 } else {
160 it++;
161 }
162 }
163}
164
165void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions) {
166 for (const auto& transaction : transactions) {
167 for (const auto& resolvedComposerState : transaction.states) {
168 const auto& clientState = resolvedComposerState.state;
169 uint32_t layerId = LayerHandle::getLayerId(clientState.surface);
170 if (layerId == UNASSIGNED_LAYER_ID) {
171 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
172 continue;
173 }
174
175 RequestedLayerState* layer = getLayerFromId(layerId);
176 if (layer == nullptr) {
177 LOG_ALWAYS_FATAL("%s Layer with handle %p (layerid=%d) not found", __func__,
178 clientState.surface.get(), layerId);
179 continue;
180 }
181
182 if (!layer->handleAlive) {
183 LOG_ALWAYS_FATAL("%s Layer's handle %p (layerid=%d) is not alive. Possible out of "
184 "order LayerLifecycleManager updates",
185 __func__, clientState.surface.get(), layerId);
186 continue;
187 }
188
189 uint32_t oldParentId = layer->parentId;
190 uint32_t oldRelativeParentId = layer->relativeParentId;
191 uint32_t oldTouchCropId = layer->touchCropId;
192 layer->merge(resolvedComposerState);
193
194 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000195 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000196 LayerCreationArgs backgroundLayerArgs{nullptr,
197 nullptr,
198 layer->name + "BackgroundColorLayer",
199 ISurfaceComposerClient::eFXSurfaceEffect,
Vishnu Naird47bcee2023-02-24 18:08:51 +0000200 {},
201 layer->id,
202 /*internalLayer=*/true};
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000203 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
204 newLayers.emplace_back(
205 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
206 RequestedLayerState* backgroundLayer = newLayers.back().get();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000207 backgroundLayer->bgColorLayer = true;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000208 backgroundLayer->handleAlive = false;
209 backgroundLayer->parentId = layer->id;
210 backgroundLayer->z = std::numeric_limits<int32_t>::min();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000211 backgroundLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000212 backgroundLayer->dataspace = layer->bgColorDataspace;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000213 layer->bgColorLayerId = backgroundLayer->id;
214 addLayers({std::move(newLayers)});
Vishnu Naird47bcee2023-02-24 18:08:51 +0000215 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000216 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000217 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
218 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
219 onHandlesDestroyed({bgColorLayer->id});
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000220 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
221 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000222 bgColorLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000223 bgColorLayer->dataspace = layer->bgColorDataspace;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000224 bgColorLayer->what |= layer_state_t::eColorChanged |
225 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
226 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000227 mGlobalChanges |= RequestedLayerState::Changes::Content;
228 }
229 }
230
231 if (oldParentId != layer->parentId) {
232 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000233 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000234 if (oldParentId == UNASSIGNED_LAYER_ID) {
235 updateDisplayMirrorLayers(*layer);
236 }
237 }
238 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
239 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000240 }
241 if (oldRelativeParentId != layer->relativeParentId) {
242 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000243 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000244 }
245 if (oldTouchCropId != layer->touchCropId) {
246 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000247 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 }
249
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000250 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 }
252 }
253}
254
255void LayerLifecycleManager::commitChanges() {
256 for (auto& layer : mLayers) {
257 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
258 for (auto listener : mListeners) {
259 listener->onLayerAdded(*layer);
260 }
261 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000262 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000263 }
264
265 for (auto& destroyedLayer : mDestroyedLayers) {
266 if (destroyedLayer->changes.test(RequestedLayerState::Changes::Created)) {
267 for (auto listener : mListeners) {
268 listener->onLayerAdded(*destroyedLayer);
269 }
270 }
271
272 for (auto listener : mListeners) {
273 listener->onLayerDestroyed(*destroyedLayer);
274 }
275 }
276 mDestroyedLayers.clear();
277 mGlobalChanges.clear();
278}
279
280void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
281 mListeners.emplace_back(std::move(listener));
282}
283
284void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
285 swapErase(mListeners, listener);
286}
287
288const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
289 return mLayers;
290}
291
292const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
293 const {
294 return mDestroyedLayers;
295}
296
297const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
298 return mGlobalChanges;
299}
300
301RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
302 if (id == UNASSIGNED_LAYER_ID) {
303 return nullptr;
304 }
305 auto it = mIdToLayer.find(id);
306 if (it == mIdToLayer.end()) {
307 return nullptr;
308 }
309 return &it->second.owner;
310}
311
312std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
313 if (id == UNASSIGNED_LAYER_ID) {
314 return nullptr;
315 }
316 auto it = mIdToLayer.find(id);
317 if (it == mIdToLayer.end()) {
318 return nullptr;
319 }
320 return &it->second.references;
321}
322
Vishnu Nair04f89692022-11-16 23:21:05 +0000323uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
324 if (layerId == UNASSIGNED_LAYER_ID) {
325 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000326 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000327
328 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
329 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000330 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
331 layerToLink);
332 return UNASSIGNED_LAYER_ID;
333 }
334 linkedLayers->emplace_back(layerToLink);
335 return layerId;
336}
337
338uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
339 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
340 if (!linkedLayers) {
341 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000342 }
343 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000344 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000345}
346
Vishnu Naira9c43762023-01-27 19:10:25 +0000347std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
348 uint32_t linkedLayer) {
349 for (uint32_t layerId : layerIds) {
350 unlinkLayer(layerId, linkedLayer);
351 }
352 return {};
353}
354
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000355std::string LayerLifecycleManager::References::getDebugString() const {
356 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
357 std::for_each(references.begin(), references.end(),
358 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
359 debugInfo += std::to_string(reference) + ",";
360 });
361 return debugInfo;
362}
363
Vishnu Nair04f89692022-11-16 23:21:05 +0000364void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
365 auto it = mIdToLayer.find(relativeRootId);
366 if (it == mIdToLayer.end()) {
367 return;
368 }
369 RequestedLayerState& layer = it->second.owner;
370 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
371 layer.changes |=
372 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
373 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
374}
375
Vishnu Naira9c43762023-01-27 19:10:25 +0000376// Some layers mirror the entire display stack. Since we don't have a single root layer per display
377// we have to track all these layers and update what they mirror when the list of root layers
378// on a display changes. This function walks through the list of display mirroring layers
379// and updates its list of layers that its mirroring. This function should be called when a new
380// root layer is added, removed or moved to another display.
381void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
382 for (uint32_t mirrorLayerId : mDisplayMirroringLayers) {
383 RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId);
384 bool canBeMirrored =
385 rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror;
386 bool currentlyMirrored =
387 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
388 rootLayer.id) != mirrorLayer->mirrorIds.end();
389
390 if (canBeMirrored && !currentlyMirrored) {
391 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
392 linkLayer(rootLayer.id, mirrorLayer->id);
393 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
394 } else if (!canBeMirrored && currentlyMirrored) {
395 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
396 unlinkLayer(rootLayer.id, mirrorLayer->id);
397 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
398 }
399 }
400}
401
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000402} // namespace android::surfaceflinger::frontend