blob: 6cacfb5946a8c571ce9561e07f536a5e6965123f [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"
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
31void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) {
32 if (newLayers.empty()) {
33 return;
34 }
35
36 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
37 for (auto& newLayer : newLayers) {
38 RequestedLayerState& layer = *newLayer.get();
39 auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer});
40 if (!inserted) {
41 LOG_ALWAYS_FATAL("Duplicate layer id %d found. Existing layer: %s", layer.id,
42 it->second.owner.getDebugString().c_str());
43 }
Vishnu Nair150065b2023-04-17 19:14:11 -070044 mAddedLayers.push_back(newLayer.get());
Vishnu Nair04f89692022-11-16 23:21:05 +000045 layer.parentId = linkLayer(layer.parentId, layer.id);
46 layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000047 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
48 // if this layer is mirroring a display, then walk though all the existing root layers
49 // for the layer stack and add them as children to be mirrored.
50 mDisplayMirroringLayers.emplace_back(layer.id);
51 for (auto& rootLayer : mLayers) {
52 if (rootLayer->isRoot() && rootLayer->layerStack == layer.layerStackToMirror) {
53 layer.mirrorIds.emplace_back(rootLayer->id);
54 linkLayer(rootLayer->id, layer.id);
55 }
56 }
57 } else {
58 // Check if we are mirroring a single layer, and if so add it to the list of children
59 // to be mirrored.
60 layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id);
61 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
62 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
63 }
64 }
Vishnu Nair04f89692022-11-16 23:21:05 +000065 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +000066 if (layer.isRoot()) {
67 updateDisplayMirrorLayers(layer);
68 }
Vishnu Nair92990e22023-02-24 20:01:05 +000069 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000070 mLayers.emplace_back(std::move(newLayer));
71 }
72}
73
Vishnu Nair191eeb82023-03-11 10:16:07 -080074void LayerLifecycleManager::onHandlesDestroyed(const std::vector<uint32_t>& destroyedHandles,
75 bool ignoreUnknownHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000076 std::vector<uint32_t> layersToBeDestroyed;
77 for (const auto& layerId : destroyedHandles) {
78 auto it = mIdToLayer.find(layerId);
79 if (it == mIdToLayer.end()) {
Vishnu Nair191eeb82023-03-11 10:16:07 -080080 LOG_ALWAYS_FATAL_IF(!ignoreUnknownHandles, "%s Layerid not found %d", __func__,
81 layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000082 continue;
83 }
84 RequestedLayerState& layer = it->second.owner;
Vishnu Nair92990e22023-02-24 20:01:05 +000085 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000086 layer.handleAlive = false;
87 if (!layer.canBeDestroyed()) {
88 continue;
89 }
90 layer.changes |= RequestedLayerState::Changes::Destroyed;
91 layersToBeDestroyed.emplace_back(layerId);
92 }
93
94 if (layersToBeDestroyed.empty()) {
95 return;
96 }
97
98 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
99 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
100 uint32_t layerId = layersToBeDestroyed[i];
101 auto it = mIdToLayer.find(layerId);
102 if (it == mIdToLayer.end()) {
103 LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId);
104 continue;
105 }
106
107 RequestedLayerState& layer = it->second.owner;
108
Vishnu Nair04f89692022-11-16 23:21:05 +0000109 layer.parentId = unlinkLayer(layer.parentId, layer.id);
110 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000111 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
112 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
113 swapErase(mDisplayMirroringLayers, layer.id);
114 } else {
115 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
116 layer.mirrorIds.clear();
117 }
118
Vishnu Nair04f89692022-11-16 23:21:05 +0000119 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000120
121 auto& references = it->second.references;
122 for (uint32_t linkedLayerId : references) {
123 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
124 if (!linkedLayer) {
125 LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__,
126 linkedLayerId, layer.id);
127 continue;
128 };
129 if (linkedLayer->parentId == layer.id) {
130 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
131 if (linkedLayer->canBeDestroyed()) {
132 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
133 layersToBeDestroyed.emplace_back(linkedLayer->id);
134 }
135 }
136 if (linkedLayer->relativeParentId == layer.id) {
137 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
138 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000139 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
140 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000141 }
142 if (linkedLayer->touchCropId == layer.id) {
143 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
144 }
145 }
146 mIdToLayer.erase(it);
147 }
148
149 auto it = mLayers.begin();
150 while (it != mLayers.end()) {
151 RequestedLayerState* layer = it->get();
152 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000153 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000154 std::iter_swap(it, mLayers.end() - 1);
155 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000156 if (it == mLayers.end() - 1) {
157 it = mLayers.erase(mLayers.end() - 1);
158 } else {
159 mLayers.erase(mLayers.end() - 1);
160 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161 } else {
162 it++;
163 }
164 }
165}
166
Vishnu Nair20e1f962023-03-29 15:58:34 -0700167void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions,
168 bool ignoreUnknownLayers) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000169 for (const auto& transaction : transactions) {
170 for (const auto& resolvedComposerState : transaction.states) {
171 const auto& clientState = resolvedComposerState.state;
Vishnu Nair1391de22023-03-05 19:56:14 -0800172 uint32_t layerId = resolvedComposerState.layerId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000173 if (layerId == UNASSIGNED_LAYER_ID) {
174 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
175 continue;
176 }
177
178 RequestedLayerState* layer = getLayerFromId(layerId);
179 if (layer == nullptr) {
Vishnu Nair20e1f962023-03-29 15:58:34 -0700180 LOG_ALWAYS_FATAL_IF(!ignoreUnknownLayers, "%s Layer with layerid=%d not found",
181 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000182 continue;
183 }
184
185 if (!layer->handleAlive) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800186 LOG_ALWAYS_FATAL("%s Layer's with layerid=%d) is not alive. Possible out of "
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000187 "order LayerLifecycleManager updates",
Vishnu Nair1391de22023-03-05 19:56:14 -0800188 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000189 continue;
190 }
191
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000192 if (transaction.flags & ISurfaceComposer::eAnimation) {
193 layer->changes |= RequestedLayerState::Changes::Animation;
194 }
195
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000196 uint32_t oldParentId = layer->parentId;
197 uint32_t oldRelativeParentId = layer->relativeParentId;
198 uint32_t oldTouchCropId = layer->touchCropId;
199 layer->merge(resolvedComposerState);
200
201 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000202 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800203 LayerCreationArgs backgroundLayerArgs(layer->id,
204 /*internalLayer=*/true);
205 backgroundLayerArgs.parentId = layer->id;
206 backgroundLayerArgs.name = layer->name + "BackgroundColorLayer";
207 backgroundLayerArgs.flags = ISurfaceComposerClient::eFXSurfaceEffect;
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() {
Vishnu Nair150065b2023-04-17 19:14:11 -0700261 for (auto layer : mAddedLayers) {
262 for (auto& listener : mListeners) {
263 listener->onLayerAdded(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000264 }
Vishnu Nair150065b2023-04-17 19:14:11 -0700265 }
266 mAddedLayers.clear();
267
268 for (auto& layer : mLayers) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000269 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000270 }
271
272 for (auto& destroyedLayer : mDestroyedLayers) {
Vishnu Nair150065b2023-04-17 19:14:11 -0700273 for (auto& listener : mListeners) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 listener->onLayerDestroyed(*destroyedLayer);
275 }
276 }
277 mDestroyedLayers.clear();
278 mGlobalChanges.clear();
279}
280
281void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
282 mListeners.emplace_back(std::move(listener));
283}
284
285void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
286 swapErase(mListeners, listener);
287}
288
289const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
290 return mLayers;
291}
292
293const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
294 const {
295 return mDestroyedLayers;
296}
297
298const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
299 return mGlobalChanges;
300}
301
302RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
303 if (id == UNASSIGNED_LAYER_ID) {
304 return nullptr;
305 }
306 auto it = mIdToLayer.find(id);
307 if (it == mIdToLayer.end()) {
308 return nullptr;
309 }
310 return &it->second.owner;
311}
312
313std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
314 if (id == UNASSIGNED_LAYER_ID) {
315 return nullptr;
316 }
317 auto it = mIdToLayer.find(id);
318 if (it == mIdToLayer.end()) {
319 return nullptr;
320 }
321 return &it->second.references;
322}
323
Vishnu Nair04f89692022-11-16 23:21:05 +0000324uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
325 if (layerId == UNASSIGNED_LAYER_ID) {
326 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000327 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000328
329 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
330 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000331 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
332 layerToLink);
333 return UNASSIGNED_LAYER_ID;
334 }
335 linkedLayers->emplace_back(layerToLink);
336 return layerId;
337}
338
339uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
340 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
341 if (!linkedLayers) {
342 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000343 }
344 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000345 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000346}
347
Vishnu Naira9c43762023-01-27 19:10:25 +0000348std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
349 uint32_t linkedLayer) {
350 for (uint32_t layerId : layerIds) {
351 unlinkLayer(layerId, linkedLayer);
352 }
353 return {};
354}
355
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000356std::string LayerLifecycleManager::References::getDebugString() const {
357 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
358 std::for_each(references.begin(), references.end(),
359 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
360 debugInfo += std::to_string(reference) + ",";
361 });
362 return debugInfo;
363}
364
Vishnu Nair04f89692022-11-16 23:21:05 +0000365void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
366 auto it = mIdToLayer.find(relativeRootId);
367 if (it == mIdToLayer.end()) {
368 return;
369 }
370 RequestedLayerState& layer = it->second.owner;
371 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
372 layer.changes |=
373 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
374 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
375}
376
Vishnu Naira9c43762023-01-27 19:10:25 +0000377// Some layers mirror the entire display stack. Since we don't have a single root layer per display
378// we have to track all these layers and update what they mirror when the list of root layers
379// on a display changes. This function walks through the list of display mirroring layers
380// and updates its list of layers that its mirroring. This function should be called when a new
381// root layer is added, removed or moved to another display.
382void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
383 for (uint32_t mirrorLayerId : mDisplayMirroringLayers) {
384 RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId);
385 bool canBeMirrored =
386 rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror;
387 bool currentlyMirrored =
388 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
389 rootLayer.id) != mirrorLayer->mirrorIds.end();
390
391 if (canBeMirrored && !currentlyMirrored) {
392 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
393 linkLayer(rootLayer.id, mirrorLayer->id);
394 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
395 } else if (!canBeMirrored && currentlyMirrored) {
396 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
397 unlinkLayer(rootLayer.id, mirrorLayer->id);
398 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
399 }
400 }
401}
402
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000403} // namespace android::surfaceflinger::frontend