blob: cd9515cd02cfb8c2511a13f0fdd530def00f31d4 [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) {
Vishnu Naire4af0952023-05-01 09:11:33 -070041 LOG_ALWAYS_FATAL("Duplicate layer id found. New layer: %s Existing layer: %s",
42 layer.getDebugString().c_str(),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000043 it->second.owner.getDebugString().c_str());
44 }
Vishnu Nair150065b2023-04-17 19:14:11 -070045 mAddedLayers.push_back(newLayer.get());
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
Vishnu Nair191eeb82023-03-11 10:16:07 -080075void LayerLifecycleManager::onHandlesDestroyed(const std::vector<uint32_t>& destroyedHandles,
76 bool ignoreUnknownHandles) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000077 std::vector<uint32_t> layersToBeDestroyed;
78 for (const auto& layerId : destroyedHandles) {
79 auto it = mIdToLayer.find(layerId);
80 if (it == mIdToLayer.end()) {
Vishnu Nair191eeb82023-03-11 10:16:07 -080081 LOG_ALWAYS_FATAL_IF(!ignoreUnknownHandles, "%s Layerid not found %d", __func__,
82 layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000083 continue;
84 }
85 RequestedLayerState& layer = it->second.owner;
Vishnu Nair92990e22023-02-24 20:01:05 +000086 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000087 layer.handleAlive = false;
88 if (!layer.canBeDestroyed()) {
89 continue;
90 }
91 layer.changes |= RequestedLayerState::Changes::Destroyed;
92 layersToBeDestroyed.emplace_back(layerId);
93 }
94
95 if (layersToBeDestroyed.empty()) {
96 return;
97 }
98
99 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
100 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
101 uint32_t layerId = layersToBeDestroyed[i];
102 auto it = mIdToLayer.find(layerId);
103 if (it == mIdToLayer.end()) {
104 LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId);
105 continue;
106 }
107
108 RequestedLayerState& layer = it->second.owner;
109
Vishnu Nair04f89692022-11-16 23:21:05 +0000110 layer.parentId = unlinkLayer(layer.parentId, layer.id);
111 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000112 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
113 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
114 swapErase(mDisplayMirroringLayers, layer.id);
115 } else {
116 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
117 layer.mirrorIds.clear();
118 }
119
Vishnu Nair04f89692022-11-16 23:21:05 +0000120 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000121
122 auto& references = it->second.references;
123 for (uint32_t linkedLayerId : references) {
124 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
125 if (!linkedLayer) {
126 LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__,
127 linkedLayerId, layer.id);
128 continue;
129 };
130 if (linkedLayer->parentId == layer.id) {
131 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
132 if (linkedLayer->canBeDestroyed()) {
133 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
134 layersToBeDestroyed.emplace_back(linkedLayer->id);
135 }
136 }
137 if (linkedLayer->relativeParentId == layer.id) {
138 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
139 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000140 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
141 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000142 }
143 if (linkedLayer->touchCropId == layer.id) {
144 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
145 }
146 }
147 mIdToLayer.erase(it);
148 }
149
150 auto it = mLayers.begin();
151 while (it != mLayers.end()) {
152 RequestedLayerState* layer = it->get();
153 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
Vishnu Nair92990e22023-02-24 20:01:05 +0000154 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000155 std::iter_swap(it, mLayers.end() - 1);
156 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000157 if (it == mLayers.end() - 1) {
158 it = mLayers.erase(mLayers.end() - 1);
159 } else {
160 mLayers.erase(mLayers.end() - 1);
161 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000162 } else {
163 it++;
164 }
165 }
166}
167
Vishnu Nair20e1f962023-03-29 15:58:34 -0700168void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions,
169 bool ignoreUnknownLayers) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000170 for (const auto& transaction : transactions) {
171 for (const auto& resolvedComposerState : transaction.states) {
172 const auto& clientState = resolvedComposerState.state;
Vishnu Nair1391de22023-03-05 19:56:14 -0800173 uint32_t layerId = resolvedComposerState.layerId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000174 if (layerId == UNASSIGNED_LAYER_ID) {
175 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
176 continue;
177 }
178
179 RequestedLayerState* layer = getLayerFromId(layerId);
180 if (layer == nullptr) {
Vishnu Nair20e1f962023-03-29 15:58:34 -0700181 LOG_ALWAYS_FATAL_IF(!ignoreUnknownLayers, "%s Layer with layerid=%d not found",
182 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000183 continue;
184 }
185
186 if (!layer->handleAlive) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800187 LOG_ALWAYS_FATAL("%s Layer's with layerid=%d) is not alive. Possible out of "
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000188 "order LayerLifecycleManager updates",
Vishnu Nair1391de22023-03-05 19:56:14 -0800189 __func__, layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000190 continue;
191 }
192
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000193 if (transaction.flags & ISurfaceComposer::eAnimation) {
194 layer->changes |= RequestedLayerState::Changes::Animation;
195 }
196
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000197 uint32_t oldParentId = layer->parentId;
198 uint32_t oldRelativeParentId = layer->relativeParentId;
199 uint32_t oldTouchCropId = layer->touchCropId;
200 layer->merge(resolvedComposerState);
201
202 if (layer->what & layer_state_t::eBackgroundColorChanged) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000203 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
Vishnu Naire4af0952023-05-01 09:11:33 -0700204 LayerCreationArgs
205 backgroundLayerArgs(LayerCreationArgs::getInternalLayerId(
206 LayerCreationArgs::sInternalSequence++),
207 /*internalLayer=*/true);
Vishnu Nair1391de22023-03-05 19:56:14 -0800208 backgroundLayerArgs.parentId = layer->id;
209 backgroundLayerArgs.name = layer->name + "BackgroundColorLayer";
210 backgroundLayerArgs.flags = ISurfaceComposerClient::eFXSurfaceEffect;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000211 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
212 newLayers.emplace_back(
213 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
214 RequestedLayerState* backgroundLayer = newLayers.back().get();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000215 backgroundLayer->bgColorLayer = true;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000216 backgroundLayer->handleAlive = false;
217 backgroundLayer->parentId = layer->id;
218 backgroundLayer->z = std::numeric_limits<int32_t>::min();
Vishnu Naird47bcee2023-02-24 18:08:51 +0000219 backgroundLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000220 backgroundLayer->dataspace = layer->bgColorDataspace;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000221 layer->bgColorLayerId = backgroundLayer->id;
222 addLayers({std::move(newLayers)});
Vishnu Naird47bcee2023-02-24 18:08:51 +0000223 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000224 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000225 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
226 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
227 onHandlesDestroyed({bgColorLayer->id});
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000228 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
229 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
Vishnu Naird47bcee2023-02-24 18:08:51 +0000230 bgColorLayer->color = layer->bgColor;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000231 bgColorLayer->dataspace = layer->bgColorDataspace;
Vishnu Naird47bcee2023-02-24 18:08:51 +0000232 bgColorLayer->what |= layer_state_t::eColorChanged |
233 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
234 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000235 mGlobalChanges |= RequestedLayerState::Changes::Content;
236 }
237 }
238
239 if (oldParentId != layer->parentId) {
240 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000241 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000242 if (oldParentId == UNASSIGNED_LAYER_ID) {
243 updateDisplayMirrorLayers(*layer);
244 }
245 }
246 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
247 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 }
249 if (oldRelativeParentId != layer->relativeParentId) {
250 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000251 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000252 }
253 if (oldTouchCropId != layer->touchCropId) {
254 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000255 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000256 }
257
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000258 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000259 }
260 }
261}
262
263void LayerLifecycleManager::commitChanges() {
Vishnu Nair150065b2023-04-17 19:14:11 -0700264 for (auto layer : mAddedLayers) {
265 for (auto& listener : mListeners) {
266 listener->onLayerAdded(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000267 }
Vishnu Nair150065b2023-04-17 19:14:11 -0700268 }
269 mAddedLayers.clear();
270
271 for (auto& layer : mLayers) {
Vishnu Naird47bcee2023-02-24 18:08:51 +0000272 layer->clearChanges();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000273 }
274
275 for (auto& destroyedLayer : mDestroyedLayers) {
Vishnu Nair150065b2023-04-17 19:14:11 -0700276 for (auto& listener : mListeners) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000277 listener->onLayerDestroyed(*destroyedLayer);
278 }
279 }
280 mDestroyedLayers.clear();
281 mGlobalChanges.clear();
282}
283
284void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
285 mListeners.emplace_back(std::move(listener));
286}
287
288void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
289 swapErase(mListeners, listener);
290}
291
292const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
293 return mLayers;
294}
295
296const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
297 const {
298 return mDestroyedLayers;
299}
300
301const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
302 return mGlobalChanges;
303}
304
305RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
306 if (id == UNASSIGNED_LAYER_ID) {
307 return nullptr;
308 }
309 auto it = mIdToLayer.find(id);
310 if (it == mIdToLayer.end()) {
311 return nullptr;
312 }
313 return &it->second.owner;
314}
315
316std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
317 if (id == UNASSIGNED_LAYER_ID) {
318 return nullptr;
319 }
320 auto it = mIdToLayer.find(id);
321 if (it == mIdToLayer.end()) {
322 return nullptr;
323 }
324 return &it->second.references;
325}
326
Vishnu Nair04f89692022-11-16 23:21:05 +0000327uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
328 if (layerId == UNASSIGNED_LAYER_ID) {
329 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000330 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000331
332 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
333 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000334 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
335 layerToLink);
336 return UNASSIGNED_LAYER_ID;
337 }
338 linkedLayers->emplace_back(layerToLink);
339 return layerId;
340}
341
342uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
343 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
344 if (!linkedLayers) {
345 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000346 }
347 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000348 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000349}
350
Vishnu Naira9c43762023-01-27 19:10:25 +0000351std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
352 uint32_t linkedLayer) {
353 for (uint32_t layerId : layerIds) {
354 unlinkLayer(layerId, linkedLayer);
355 }
356 return {};
357}
358
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000359std::string LayerLifecycleManager::References::getDebugString() const {
360 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
361 std::for_each(references.begin(), references.end(),
362 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
363 debugInfo += std::to_string(reference) + ",";
364 });
365 return debugInfo;
366}
367
Vishnu Nair04f89692022-11-16 23:21:05 +0000368void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
369 auto it = mIdToLayer.find(relativeRootId);
370 if (it == mIdToLayer.end()) {
371 return;
372 }
373 RequestedLayerState& layer = it->second.owner;
374 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
375 layer.changes |=
376 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
377 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
378}
379
Vishnu Naira9c43762023-01-27 19:10:25 +0000380// Some layers mirror the entire display stack. Since we don't have a single root layer per display
381// we have to track all these layers and update what they mirror when the list of root layers
382// on a display changes. This function walks through the list of display mirroring layers
383// and updates its list of layers that its mirroring. This function should be called when a new
384// root layer is added, removed or moved to another display.
385void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
386 for (uint32_t mirrorLayerId : mDisplayMirroringLayers) {
387 RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId);
388 bool canBeMirrored =
389 rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror;
390 bool currentlyMirrored =
391 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
392 rootLayer.id) != mirrorLayer->mirrorIds.end();
393
394 if (canBeMirrored && !currentlyMirrored) {
395 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
396 linkLayer(rootLayer.id, mirrorLayer->id);
397 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
398 } else if (!canBeMirrored && currentlyMirrored) {
399 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
400 unlinkLayer(rootLayer.id, mirrorLayer->id);
401 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
402 }
403 }
404}
405
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000406} // namespace android::surfaceflinger::frontend