blob: 33cc42951fd3a6884430337e1f912dfcada1600b [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();
40 auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer});
41 if (!inserted) {
42 LOG_ALWAYS_FATAL("Duplicate layer id %d found. Existing layer: %s", layer.id,
43 it->second.owner.getDebugString().c_str());
44 }
45
Vishnu Nair04f89692022-11-16 23:21:05 +000046 layer.parentId = linkLayer(layer.parentId, layer.id);
47 layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000048 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
49 // if this layer is mirroring a display, then walk though all the existing root layers
50 // for the layer stack and add them as children to be mirrored.
51 mDisplayMirroringLayers.emplace_back(layer.id);
52 for (auto& rootLayer : mLayers) {
53 if (rootLayer->isRoot() && rootLayer->layerStack == layer.layerStackToMirror) {
54 layer.mirrorIds.emplace_back(rootLayer->id);
55 linkLayer(rootLayer->id, layer.id);
56 }
57 }
58 } else {
59 // Check if we are mirroring a single layer, and if so add it to the list of children
60 // to be mirrored.
61 layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id);
62 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
63 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
64 }
65 }
Vishnu Nair04f89692022-11-16 23:21:05 +000066 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000067 if (layer.isRoot()) {
68 updateDisplayMirrorLayers(layer);
69 }
Vishnu Nair92990e22023-02-24 20:01:05 +000070 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
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;
Vishnu Nair92990e22023-02-24 20:01:05 +000084 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000085 layer.handleAlive = false;
86 if (!layer.canBeDestroyed()) {
87 continue;
88 }
89 layer.changes |= RequestedLayerState::Changes::Destroyed;
90 layersToBeDestroyed.emplace_back(layerId);
91 }
92
93 if (layersToBeDestroyed.empty()) {
94 return;
95 }
96
97 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
98 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
99 uint32_t layerId = layersToBeDestroyed[i];
100 auto it = mIdToLayer.find(layerId);
101 if (it == mIdToLayer.end()) {
102 LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId);
103 continue;
104 }
105
106 RequestedLayerState& layer = it->second.owner;
107
Vishnu Nair04f89692022-11-16 23:21:05 +0000108 layer.parentId = unlinkLayer(layer.parentId, layer.id);
109 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000110 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
111 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
112 swapErase(mDisplayMirroringLayers, layer.id);
113 } else {
114 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
115 layer.mirrorIds.clear();
116 }
117
Vishnu Nair04f89692022-11-16 23:21:05 +0000118 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000119
120 auto& references = it->second.references;
121 for (uint32_t linkedLayerId : references) {
122 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
123 if (!linkedLayer) {
124 LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__,
125 linkedLayerId, layer.id);
126 continue;
127 };
128 if (linkedLayer->parentId == layer.id) {
129 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
130 if (linkedLayer->canBeDestroyed()) {
131 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
132 layersToBeDestroyed.emplace_back(linkedLayer->id);
133 }
134 }
135 if (linkedLayer->relativeParentId == layer.id) {
136 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
137 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000138 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
139 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000140 }
141 if (linkedLayer->touchCropId == layer.id) {
142 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
143 }
144 }
145 mIdToLayer.erase(it);
146 }
147
148 auto it = mLayers.begin();
149 while (it != mLayers.end()) {
150 RequestedLayerState* layer = it->get();
151 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000152 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000153 std::iter_swap(it, mLayers.end() - 1);
154 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000155 if (it == mLayers.end() - 1) {
156 it = mLayers.erase(mLayers.end() - 1);
157 } else {
158 mLayers.erase(mLayers.end() - 1);
159 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000160 } else {
161 it++;
162 }
163 }
164}
165
166void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions) {
167 for (const auto& transaction : transactions) {
168 for (const auto& resolvedComposerState : transaction.states) {
169 const auto& clientState = resolvedComposerState.state;
170 uint32_t layerId = LayerHandle::getLayerId(clientState.surface);
171 if (layerId == UNASSIGNED_LAYER_ID) {
172 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
173 continue;
174 }
175
176 RequestedLayerState* layer = getLayerFromId(layerId);
177 if (layer == nullptr) {
178 LOG_ALWAYS_FATAL("%s Layer with handle %p (layerid=%d) not found", __func__,
179 clientState.surface.get(), layerId);
180 continue;
181 }
182
183 if (!layer->handleAlive) {
184 LOG_ALWAYS_FATAL("%s Layer's handle %p (layerid=%d) is not alive. Possible out of "
185 "order LayerLifecycleManager updates",
186 __func__, clientState.surface.get(), layerId);
187 continue;
188 }
189
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000190 if (transaction.flags & ISurfaceComposer::eAnimation) {
191 layer->changes |= RequestedLayerState::Changes::Animation;
192 }
193
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000194 uint32_t oldParentId = layer->parentId;
195 uint32_t oldRelativeParentId = layer->relativeParentId;
196 uint32_t oldTouchCropId = layer->touchCropId;
197 layer->merge(resolvedComposerState);
198
199 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000200 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000201 LayerCreationArgs backgroundLayerArgs{nullptr,
202 nullptr,
203 layer->name + "BackgroundColorLayer",
204 ISurfaceComposerClient::eFXSurfaceEffect,
Vishnu Naird47bcee2023-02-24 18:08:51 +0000205 {},
206 layer->id,
207 /*internalLayer=*/true};
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000208 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
209 newLayers.emplace_back(
210 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
211 RequestedLayerState* backgroundLayer = newLayers.back().get();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000212 backgroundLayer->bgColorLayer = true;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000213 backgroundLayer->handleAlive = false;
214 backgroundLayer->parentId = layer->id;
215 backgroundLayer->z = std::numeric_limits<int32_t>::min();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000216 backgroundLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000217 backgroundLayer->dataspace = layer->bgColorDataspace;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000218 layer->bgColorLayerId = backgroundLayer->id;
219 addLayers({std::move(newLayers)});
Vishnu Naird47bcee2023-02-24 18:08:51 +0000220 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000221 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000222 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
223 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
224 onHandlesDestroyed({bgColorLayer->id});
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000225 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
226 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000227 bgColorLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000228 bgColorLayer->dataspace = layer->bgColorDataspace;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000229 bgColorLayer->what |= layer_state_t::eColorChanged |
230 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
231 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000232 mGlobalChanges |= RequestedLayerState::Changes::Content;
233 }
234 }
235
236 if (oldParentId != layer->parentId) {
237 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000238 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000239 if (oldParentId == UNASSIGNED_LAYER_ID) {
240 updateDisplayMirrorLayers(*layer);
241 }
242 }
243 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
244 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000245 }
246 if (oldRelativeParentId != layer->relativeParentId) {
247 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000248 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000249 }
250 if (oldTouchCropId != layer->touchCropId) {
251 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000252 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000253 }
254
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000255 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000256 }
257 }
258}
259
260void LayerLifecycleManager::commitChanges() {
261 for (auto& layer : mLayers) {
262 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
263 for (auto listener : mListeners) {
264 listener->onLayerAdded(*layer);
265 }
266 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000267 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000268 }
269
270 for (auto& destroyedLayer : mDestroyedLayers) {
271 if (destroyedLayer->changes.test(RequestedLayerState::Changes::Created)) {
272 for (auto listener : mListeners) {
273 listener->onLayerAdded(*destroyedLayer);
274 }
275 }
276
277 for (auto listener : mListeners) {
278 listener->onLayerDestroyed(*destroyedLayer);
279 }
280 }
281 mDestroyedLayers.clear();
282 mGlobalChanges.clear();
283}
284
285void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
286 mListeners.emplace_back(std::move(listener));
287}
288
289void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
290 swapErase(mListeners, listener);
291}
292
293const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
294 return mLayers;
295}
296
297const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
298 const {
299 return mDestroyedLayers;
300}
301
302const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
303 return mGlobalChanges;
304}
305
306RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
307 if (id == UNASSIGNED_LAYER_ID) {
308 return nullptr;
309 }
310 auto it = mIdToLayer.find(id);
311 if (it == mIdToLayer.end()) {
312 return nullptr;
313 }
314 return &it->second.owner;
315}
316
317std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
318 if (id == UNASSIGNED_LAYER_ID) {
319 return nullptr;
320 }
321 auto it = mIdToLayer.find(id);
322 if (it == mIdToLayer.end()) {
323 return nullptr;
324 }
325 return &it->second.references;
326}
327
Vishnu Nair04f89692022-11-16 23:21:05 +0000328uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
329 if (layerId == UNASSIGNED_LAYER_ID) {
330 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000331 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000332
333 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
334 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000335 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
336 layerToLink);
337 return UNASSIGNED_LAYER_ID;
338 }
339 linkedLayers->emplace_back(layerToLink);
340 return layerId;
341}
342
343uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
344 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
345 if (!linkedLayers) {
346 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000347 }
348 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000349 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000350}
351
Vishnu Naira9c43762023-01-27 19:10:25 +0000352std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
353 uint32_t linkedLayer) {
354 for (uint32_t layerId : layerIds) {
355 unlinkLayer(layerId, linkedLayer);
356 }
357 return {};
358}
359
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000360std::string LayerLifecycleManager::References::getDebugString() const {
361 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
362 std::for_each(references.begin(), references.end(),
363 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
364 debugInfo += std::to_string(reference) + ",";
365 });
366 return debugInfo;
367}
368
Vishnu Nair04f89692022-11-16 23:21:05 +0000369void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
370 auto it = mIdToLayer.find(relativeRootId);
371 if (it == mIdToLayer.end()) {
372 return;
373 }
374 RequestedLayerState& layer = it->second.owner;
375 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
376 layer.changes |=
377 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
378 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
379}
380
Vishnu Naira9c43762023-01-27 19:10:25 +0000381// Some layers mirror the entire display stack. Since we don't have a single root layer per display
382// we have to track all these layers and update what they mirror when the list of root layers
383// on a display changes. This function walks through the list of display mirroring layers
384// and updates its list of layers that its mirroring. This function should be called when a new
385// root layer is added, removed or moved to another display.
386void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
387 for (uint32_t mirrorLayerId : mDisplayMirroringLayers) {
388 RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId);
389 bool canBeMirrored =
390 rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror;
391 bool currentlyMirrored =
392 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
393 rootLayer.id) != mirrorLayer->mirrorIds.end();
394
395 if (canBeMirrored && !currentlyMirrored) {
396 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
397 linkLayer(rootLayer.id, mirrorLayer->id);
398 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
399 } else if (!canBeMirrored && currentlyMirrored) {
400 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
401 unlinkLayer(rootLayer.id, mirrorLayer->id);
402 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
403 }
404 }
405}
406
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000407} // namespace android::surfaceflinger::frontend