blob: 547a852b9e3781a38f0e6c4862332808f4992a28 [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"
25#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 }
44
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 Nairdc4d31b2022-11-17 03:20:58 +000069 mLayers.emplace_back(std::move(newLayer));
70 }
71}
72
73void LayerLifecycleManager::onHandlesDestroyed(const std::vector<uint32_t>& destroyedHandles) {
74 std::vector<uint32_t> layersToBeDestroyed;
75 for (const auto& layerId : destroyedHandles) {
76 auto it = mIdToLayer.find(layerId);
77 if (it == mIdToLayer.end()) {
78 LOG_ALWAYS_FATAL("%s Layerid not found %d", __func__, layerId);
79 continue;
80 }
81 RequestedLayerState& layer = it->second.owner;
82 layer.handleAlive = false;
83 if (!layer.canBeDestroyed()) {
84 continue;
85 }
86 layer.changes |= RequestedLayerState::Changes::Destroyed;
87 layersToBeDestroyed.emplace_back(layerId);
88 }
89
90 if (layersToBeDestroyed.empty()) {
91 return;
92 }
93
94 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
95 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
96 uint32_t layerId = layersToBeDestroyed[i];
97 auto it = mIdToLayer.find(layerId);
98 if (it == mIdToLayer.end()) {
99 LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId);
100 continue;
101 }
102
103 RequestedLayerState& layer = it->second.owner;
104
Vishnu Nair04f89692022-11-16 23:21:05 +0000105 layer.parentId = unlinkLayer(layer.parentId, layer.id);
106 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000107 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
108 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
109 swapErase(mDisplayMirroringLayers, layer.id);
110 } else {
111 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
112 layer.mirrorIds.clear();
113 }
114
Vishnu Nair04f89692022-11-16 23:21:05 +0000115 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000116
117 auto& references = it->second.references;
118 for (uint32_t linkedLayerId : references) {
119 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
120 if (!linkedLayer) {
121 LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__,
122 linkedLayerId, layer.id);
123 continue;
124 };
125 if (linkedLayer->parentId == layer.id) {
126 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
127 if (linkedLayer->canBeDestroyed()) {
128 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
129 layersToBeDestroyed.emplace_back(linkedLayer->id);
130 }
131 }
132 if (linkedLayer->relativeParentId == layer.id) {
133 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
134 }
Vishnu Naira9c43762023-01-27 19:10:25 +0000135 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
136 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000137 }
138 if (linkedLayer->touchCropId == layer.id) {
139 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
140 }
141 }
142 mIdToLayer.erase(it);
143 }
144
145 auto it = mLayers.begin();
146 while (it != mLayers.end()) {
147 RequestedLayerState* layer = it->get();
148 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
149 ALOGV("%s destroyed layer %s", __func__, layer->getDebugStringShort().c_str());
150 std::iter_swap(it, mLayers.end() - 1);
151 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
Vishnu Nairaa548fd2022-11-23 18:50:09 +0000152 if (it == mLayers.end() - 1) {
153 it = mLayers.erase(mLayers.end() - 1);
154 } else {
155 mLayers.erase(mLayers.end() - 1);
156 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000157 } else {
158 it++;
159 }
160 }
161}
162
163void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions) {
164 for (const auto& transaction : transactions) {
165 for (const auto& resolvedComposerState : transaction.states) {
166 const auto& clientState = resolvedComposerState.state;
167 uint32_t layerId = LayerHandle::getLayerId(clientState.surface);
168 if (layerId == UNASSIGNED_LAYER_ID) {
169 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
170 continue;
171 }
172
173 RequestedLayerState* layer = getLayerFromId(layerId);
174 if (layer == nullptr) {
175 LOG_ALWAYS_FATAL("%s Layer with handle %p (layerid=%d) not found", __func__,
176 clientState.surface.get(), layerId);
177 continue;
178 }
179
180 if (!layer->handleAlive) {
181 LOG_ALWAYS_FATAL("%s Layer's handle %p (layerid=%d) is not alive. Possible out of "
182 "order LayerLifecycleManager updates",
183 __func__, clientState.surface.get(), layerId);
184 continue;
185 }
186
187 uint32_t oldParentId = layer->parentId;
188 uint32_t oldRelativeParentId = layer->relativeParentId;
189 uint32_t oldTouchCropId = layer->touchCropId;
190 layer->merge(resolvedComposerState);
191
192 if (layer->what & layer_state_t::eBackgroundColorChanged) {
193 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColorAlpha != 0) {
194 LayerCreationArgs backgroundLayerArgs{nullptr,
195 nullptr,
196 layer->name + "BackgroundColorLayer",
197 ISurfaceComposerClient::eFXSurfaceEffect,
198 {}};
199 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
200 newLayers.emplace_back(
201 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
202 RequestedLayerState* backgroundLayer = newLayers.back().get();
203 backgroundLayer->handleAlive = false;
204 backgroundLayer->parentId = layer->id;
205 backgroundLayer->z = std::numeric_limits<int32_t>::min();
206 backgroundLayer->color.rgb = layer->color.rgb;
207 backgroundLayer->color.a = layer->bgColorAlpha;
208 backgroundLayer->dataspace = layer->bgColorDataspace;
209
210 layer->bgColorLayerId = backgroundLayer->id;
211 addLayers({std::move(newLayers)});
212 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID &&
213 layer->bgColorAlpha == 0) {
214 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
215 bgColorLayer->parentId = UNASSIGNED_LAYER_ID;
216 onHandlesDestroyed({layer->bgColorLayerId});
217 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
218 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
219 bgColorLayer->color.rgb = layer->color.rgb;
220 bgColorLayer->color.a = layer->bgColorAlpha;
221 bgColorLayer->dataspace = layer->bgColorDataspace;
222 mGlobalChanges |= RequestedLayerState::Changes::Content;
223 }
224 }
225
226 if (oldParentId != layer->parentId) {
227 unlinkLayer(oldParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000228 layer->parentId = linkLayer(layer->parentId, layer->id);
Vishnu Naira9c43762023-01-27 19:10:25 +0000229 if (oldParentId == UNASSIGNED_LAYER_ID) {
230 updateDisplayMirrorLayers(*layer);
231 }
232 }
233 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
234 updateDisplayMirrorLayers(*layer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000235 }
236 if (oldRelativeParentId != layer->relativeParentId) {
237 unlinkLayer(oldRelativeParentId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000238 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000239 }
240 if (oldTouchCropId != layer->touchCropId) {
241 unlinkLayer(oldTouchCropId, layer->id);
Vishnu Nair04f89692022-11-16 23:21:05 +0000242 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000243 }
244
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000245 mGlobalChanges |= layer->changes;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000246 }
247 }
248}
249
250void LayerLifecycleManager::commitChanges() {
251 for (auto& layer : mLayers) {
252 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
253 for (auto listener : mListeners) {
254 listener->onLayerAdded(*layer);
255 }
256 }
257 layer->what = 0;
258 layer->changes.clear();
259 }
260
261 for (auto& destroyedLayer : mDestroyedLayers) {
262 if (destroyedLayer->changes.test(RequestedLayerState::Changes::Created)) {
263 for (auto listener : mListeners) {
264 listener->onLayerAdded(*destroyedLayer);
265 }
266 }
267
268 for (auto listener : mListeners) {
269 listener->onLayerDestroyed(*destroyedLayer);
270 }
271 }
272 mDestroyedLayers.clear();
273 mGlobalChanges.clear();
274}
275
276void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
277 mListeners.emplace_back(std::move(listener));
278}
279
280void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
281 swapErase(mListeners, listener);
282}
283
284const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
285 return mLayers;
286}
287
288const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
289 const {
290 return mDestroyedLayers;
291}
292
293const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
294 return mGlobalChanges;
295}
296
297RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
298 if (id == UNASSIGNED_LAYER_ID) {
299 return nullptr;
300 }
301 auto it = mIdToLayer.find(id);
302 if (it == mIdToLayer.end()) {
303 return nullptr;
304 }
305 return &it->second.owner;
306}
307
308std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
309 if (id == UNASSIGNED_LAYER_ID) {
310 return nullptr;
311 }
312 auto it = mIdToLayer.find(id);
313 if (it == mIdToLayer.end()) {
314 return nullptr;
315 }
316 return &it->second.references;
317}
318
Vishnu Nair04f89692022-11-16 23:21:05 +0000319uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
320 if (layerId == UNASSIGNED_LAYER_ID) {
321 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000322 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000323
324 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
325 if (!linkedLayers) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000326 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
327 layerToLink);
328 return UNASSIGNED_LAYER_ID;
329 }
330 linkedLayers->emplace_back(layerToLink);
331 return layerId;
332}
333
334uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
335 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
336 if (!linkedLayers) {
337 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000338 }
339 swapErase(*linkedLayers, linkedLayer);
Vishnu Nair04f89692022-11-16 23:21:05 +0000340 return UNASSIGNED_LAYER_ID;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000341}
342
Vishnu Naira9c43762023-01-27 19:10:25 +0000343std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
344 uint32_t linkedLayer) {
345 for (uint32_t layerId : layerIds) {
346 unlinkLayer(layerId, linkedLayer);
347 }
348 return {};
349}
350
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000351std::string LayerLifecycleManager::References::getDebugString() const {
352 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
353 std::for_each(references.begin(), references.end(),
354 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
355 debugInfo += std::to_string(reference) + ",";
356 });
357 return debugInfo;
358}
359
Vishnu Nair04f89692022-11-16 23:21:05 +0000360void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
361 auto it = mIdToLayer.find(relativeRootId);
362 if (it == mIdToLayer.end()) {
363 return;
364 }
365 RequestedLayerState& layer = it->second.owner;
366 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
367 layer.changes |=
368 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
369 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
370}
371
Vishnu Naira9c43762023-01-27 19:10:25 +0000372// Some layers mirror the entire display stack. Since we don't have a single root layer per display
373// we have to track all these layers and update what they mirror when the list of root layers
374// on a display changes. This function walks through the list of display mirroring layers
375// and updates its list of layers that its mirroring. This function should be called when a new
376// root layer is added, removed or moved to another display.
377void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
378 for (uint32_t mirrorLayerId : mDisplayMirroringLayers) {
379 RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId);
380 bool canBeMirrored =
381 rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror;
382 bool currentlyMirrored =
383 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
384 rootLayer.id) != mirrorLayer->mirrorIds.end();
385
386 if (canBeMirrored && !currentlyMirrored) {
387 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
388 linkLayer(rootLayer.id, mirrorLayer->id);
389 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
390 } else if (!canBeMirrored && currentlyMirrored) {
391 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
392 unlinkLayer(rootLayer.id, mirrorLayer->id);
393 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
394 }
395 }
396}
397
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000398} // namespace android::surfaceflinger::frontend