1 /*
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 LOG_NDEBUG 0
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
20 #undef LOG_TAG
21 #define LOG_TAG "SurfaceFlinger"
22
23 #include "LayerLifecycleManager.h"
24 #include "Client.h" // temporarily needed for LayerCreationArgs
25 #include "LayerLog.h"
26 #include "SwapErase.h"
27
28 namespace android::surfaceflinger::frontend {
29
30 using namespace ftl::flag_operators;
31
32 namespace {
33 // Returns true if the layer is root of a display and can be mirrored by mirroringLayer
canMirrorRootLayer(RequestedLayerState & mirroringLayer,RequestedLayerState & rootLayer)34 bool canMirrorRootLayer(RequestedLayerState& mirroringLayer, RequestedLayerState& rootLayer) {
35 return rootLayer.isRoot() && rootLayer.layerStack == mirroringLayer.layerStackToMirror &&
36 rootLayer.id != mirroringLayer.id;
37 }
38 } // namespace
39
addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers)40 void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) {
41 if (newLayers.empty()) {
42 return;
43 }
44
45 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
46 for (auto& newLayer : newLayers) {
47 RequestedLayerState& layer = *newLayer.get();
48 auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer});
49 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!inserted,
50 "Duplicate layer id found. New layer: %s Existing layer: "
51 "%s",
52 layer.getDebugString().c_str(),
53 it->second.owner.getDebugString().c_str());
54 mAddedLayers.push_back(newLayer.get());
55 mChangedLayers.push_back(newLayer.get());
56 layer.parentId = linkLayer(layer.parentId, layer.id);
57 layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id);
58 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
59 // Set mirror layer's default layer stack to -1 so it doesn't end up rendered on a
60 // display accidentally.
61 layer.layerStack = ui::INVALID_LAYER_STACK;
62
63 // if this layer is mirroring a display, then walk though all the existing root layers
64 // for the layer stack and add them as children to be mirrored.
65 mDisplayMirroringLayers.emplace_back(layer.id);
66 for (auto& rootLayer : mLayers) {
67 if (canMirrorRootLayer(layer, *rootLayer)) {
68 layer.mirrorIds.emplace_back(rootLayer->id);
69 linkLayer(rootLayer->id, layer.id);
70 }
71 }
72 } else {
73 // Check if we are mirroring a single layer, and if so add it to the list of children
74 // to be mirrored.
75 layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id);
76 if (!FlagManager::getInstance().detached_mirror()) {
77 if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) {
78 layer.mirrorIds.emplace_back(layer.layerIdToMirror);
79 }
80 }
81 }
82 layer.touchCropId = linkLayer(layer.touchCropId, layer.id);
83 if (layer.isRoot()) {
84 updateDisplayMirrorLayers(layer);
85 }
86 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
87 mLayers.emplace_back(std::move(newLayer));
88 }
89 }
90
onHandlesDestroyed(const std::vector<std::pair<uint32_t,std::string>> & destroyedHandles,bool ignoreUnknownHandles)91 void LayerLifecycleManager::onHandlesDestroyed(
92 const std::vector<std::pair<uint32_t, std::string /* debugName */>>& destroyedHandles,
93 bool ignoreUnknownHandles) {
94 std::vector<uint32_t> layersToBeDestroyed;
95 for (const auto& [layerId, name] : destroyedHandles) {
96 auto it = mIdToLayer.find(layerId);
97 if (it == mIdToLayer.end()) {
98 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownHandles, "%s Layerid not found %s[%d]",
99 __func__, name.c_str(), layerId);
100 continue;
101 }
102 RequestedLayerState& layer = it->second.owner;
103 LLOGV(layer.id, "%s", layer.getDebugString().c_str());
104 layer.handleAlive = false;
105 if (!layer.canBeDestroyed()) {
106 continue;
107 }
108 layer.changes |= RequestedLayerState::Changes::Destroyed;
109 layersToBeDestroyed.emplace_back(layerId);
110 }
111
112 if (layersToBeDestroyed.empty()) {
113 return;
114 }
115
116 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
117 for (size_t i = 0; i < layersToBeDestroyed.size(); i++) {
118 uint32_t layerId = layersToBeDestroyed[i];
119 auto it = mIdToLayer.find(layerId);
120 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mIdToLayer.end(), "%s Layer with id %d not found",
121 __func__, layerId);
122
123 RequestedLayerState& layer = it->second.owner;
124
125 layer.parentId = unlinkLayer(layer.parentId, layer.id);
126 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
127 if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) {
128 layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id);
129 swapErase(mDisplayMirroringLayers, layer.id);
130 } else {
131 layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id);
132 layer.mirrorIds.clear();
133 }
134
135 layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id);
136
137 auto& references = it->second.references;
138 for (uint32_t linkedLayerId : references) {
139 RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId);
140 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!linkedLayer,
141 "%s Layerid reference %d not found for %d", __func__,
142 linkedLayerId, layer.id);
143 if (linkedLayer->parentId == layer.id) {
144 linkedLayer->parentId = UNASSIGNED_LAYER_ID;
145 if (linkedLayer->canBeDestroyed()) {
146 linkedLayer->changes |= RequestedLayerState::Changes::Destroyed;
147 layersToBeDestroyed.emplace_back(linkedLayer->id);
148 }
149 }
150 if (linkedLayer->relativeParentId == layer.id) {
151 linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID;
152 }
153 if (swapErase(linkedLayer->mirrorIds, layer.id)) {
154 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
155 }
156 if (linkedLayer->layerIdToMirror == layer.id) {
157 linkedLayer->layerIdToMirror = UNASSIGNED_LAYER_ID;
158 linkedLayer->changes |= RequestedLayerState::Changes::Mirror;
159 }
160 if (linkedLayer->touchCropId == layer.id) {
161 linkedLayer->touchCropId = UNASSIGNED_LAYER_ID;
162 }
163 }
164 mIdToLayer.erase(it);
165 }
166
167 auto it = mLayers.begin();
168 while (it != mLayers.end()) {
169 RequestedLayerState* layer = it->get();
170 if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) {
171 LLOGV(layer->id, "destroyed %s", layer->getDebugStringShort().c_str());
172 std::iter_swap(it, mLayers.end() - 1);
173 mDestroyedLayers.emplace_back(std::move(mLayers.back()));
174 if (it == mLayers.end() - 1) {
175 it = mLayers.erase(mLayers.end() - 1);
176 } else {
177 mLayers.erase(mLayers.end() - 1);
178 }
179 } else {
180 it++;
181 }
182 }
183 }
184
applyTransactions(const std::vector<TransactionState> & transactions,bool ignoreUnknownLayers)185 void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions,
186 bool ignoreUnknownLayers) {
187 for (const auto& transaction : transactions) {
188 for (const auto& resolvedComposerState : transaction.states) {
189 const auto& clientState = resolvedComposerState.state;
190 uint32_t layerId = resolvedComposerState.layerId;
191 if (layerId == UNASSIGNED_LAYER_ID) {
192 ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get());
193 continue;
194 }
195
196 RequestedLayerState* layer = getLayerFromId(layerId);
197 if (layer == nullptr) {
198 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!ignoreUnknownLayers,
199 "%s Layer with layerid=%d not found", __func__,
200 layerId);
201 continue;
202 }
203
204 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(!layer->handleAlive,
205 "%s Layer's with layerid=%d) is not alive. Possible "
206 "out of "
207 "order LayerLifecycleManager updates",
208 __func__, layerId);
209
210 if (layer->changes.get() == 0) {
211 mChangedLayers.push_back(layer);
212 }
213
214 if (transaction.flags & ISurfaceComposer::eAnimation) {
215 layer->changes |= RequestedLayerState::Changes::Animation;
216 }
217
218 uint32_t oldParentId = layer->parentId;
219 uint32_t oldRelativeParentId = layer->relativeParentId;
220 uint32_t oldTouchCropId = layer->touchCropId;
221 layer->merge(resolvedComposerState);
222
223 if (layer->what & layer_state_t::eBackgroundColorChanged) {
224 if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) {
225 LayerCreationArgs
226 backgroundLayerArgs(LayerCreationArgs::getInternalLayerId(
227 LayerCreationArgs::sInternalSequence++),
228 /*internalLayer=*/true);
229 backgroundLayerArgs.parentId = layer->id;
230 backgroundLayerArgs.name = layer->name + "BackgroundColorLayer";
231 backgroundLayerArgs.flags = ISurfaceComposerClient::eFXSurfaceEffect;
232 std::vector<std::unique_ptr<RequestedLayerState>> newLayers;
233 newLayers.emplace_back(
234 std::make_unique<RequestedLayerState>(backgroundLayerArgs));
235 RequestedLayerState* backgroundLayer = newLayers.back().get();
236 backgroundLayer->bgColorLayer = true;
237 backgroundLayer->handleAlive = false;
238 backgroundLayer->parentId = layer->id;
239 backgroundLayer->z = std::numeric_limits<int32_t>::min();
240 backgroundLayer->color = layer->bgColor;
241 backgroundLayer->dataspace = layer->bgColorDataspace;
242 layer->bgColorLayerId = backgroundLayer->id;
243 addLayers({std::move(newLayers)});
244 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) {
245 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
246 layer->bgColorLayerId = UNASSIGNED_LAYER_ID;
247 bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id);
248 onHandlesDestroyed({{bgColorLayer->id, bgColorLayer->debugName}});
249 } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) {
250 RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId);
251 bgColorLayer->color = layer->bgColor;
252 bgColorLayer->dataspace = layer->bgColorDataspace;
253 bgColorLayer->what |= layer_state_t::eColorChanged |
254 layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged;
255 bgColorLayer->changes |= RequestedLayerState::Changes::Content;
256 mChangedLayers.push_back(bgColorLayer);
257 mGlobalChanges |= RequestedLayerState::Changes::Content;
258 }
259 }
260
261 if (oldParentId != layer->parentId) {
262 unlinkLayer(oldParentId, layer->id);
263 layer->parentId = linkLayer(layer->parentId, layer->id);
264 if (oldParentId == UNASSIGNED_LAYER_ID) {
265 updateDisplayMirrorLayers(*layer);
266 }
267 }
268 if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) {
269 updateDisplayMirrorLayers(*layer);
270 }
271 if (oldRelativeParentId != layer->relativeParentId) {
272 unlinkLayer(oldRelativeParentId, layer->id);
273 layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id);
274 }
275 if (oldTouchCropId != layer->touchCropId) {
276 unlinkLayer(oldTouchCropId, layer->id);
277 layer->touchCropId = linkLayer(layer->touchCropId, layer->id);
278 }
279
280 mGlobalChanges |= layer->changes;
281 }
282 }
283 }
284
commitChanges()285 void LayerLifecycleManager::commitChanges() {
286 for (auto layer : mAddedLayers) {
287 for (auto& listener : mListeners) {
288 listener->onLayerAdded(*layer);
289 }
290 }
291 mAddedLayers.clear();
292
293 for (auto& layer : mLayers) {
294 layer->clearChanges();
295 }
296
297 for (auto& destroyedLayer : mDestroyedLayers) {
298 for (auto& listener : mListeners) {
299 listener->onLayerDestroyed(*destroyedLayer);
300 }
301 }
302 mDestroyedLayers.clear();
303 mChangedLayers.clear();
304 mGlobalChanges.clear();
305 }
306
addLifecycleListener(std::shared_ptr<ILifecycleListener> listener)307 void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
308 mListeners.emplace_back(std::move(listener));
309 }
310
removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener)311 void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) {
312 swapErase(mListeners, listener);
313 }
314
getLayers() const315 const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const {
316 return mLayers;
317 }
318
getDestroyedLayers() const319 const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers()
320 const {
321 return mDestroyedLayers;
322 }
323
getChangedLayers() const324 const std::vector<RequestedLayerState*>& LayerLifecycleManager::getChangedLayers() const {
325 return mChangedLayers;
326 }
327
getGlobalChanges() const328 const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const {
329 return mGlobalChanges;
330 }
331
getLayerFromId(uint32_t id) const332 const RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) const {
333 if (id == UNASSIGNED_LAYER_ID) {
334 return nullptr;
335 }
336 auto it = mIdToLayer.find(id);
337 if (it == mIdToLayer.end()) {
338 return nullptr;
339 }
340 return &it->second.owner;
341 }
342
getLayerFromId(uint32_t id)343 RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) {
344 if (id == UNASSIGNED_LAYER_ID) {
345 return nullptr;
346 }
347 auto it = mIdToLayer.find(id);
348 if (it == mIdToLayer.end()) {
349 return nullptr;
350 }
351 return &it->second.owner;
352 }
353
getLinkedLayersFromId(uint32_t id)354 std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) {
355 if (id == UNASSIGNED_LAYER_ID) {
356 return nullptr;
357 }
358 auto it = mIdToLayer.find(id);
359 if (it == mIdToLayer.end()) {
360 return nullptr;
361 }
362 return &it->second.references;
363 }
364
linkLayer(uint32_t layerId,uint32_t layerToLink)365 uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) {
366 if (layerId == UNASSIGNED_LAYER_ID) {
367 return UNASSIGNED_LAYER_ID;
368 }
369
370 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
371 if (!linkedLayers) {
372 ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId,
373 layerToLink);
374 return UNASSIGNED_LAYER_ID;
375 }
376 linkedLayers->emplace_back(layerToLink);
377 return layerId;
378 }
379
unlinkLayer(uint32_t layerId,uint32_t linkedLayer)380 uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) {
381 std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId);
382 if (!linkedLayers) {
383 return UNASSIGNED_LAYER_ID;
384 }
385 swapErase(*linkedLayers, linkedLayer);
386 return UNASSIGNED_LAYER_ID;
387 }
388
unlinkLayers(const std::vector<uint32_t> & layerIds,uint32_t linkedLayer)389 std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds,
390 uint32_t linkedLayer) {
391 for (uint32_t layerId : layerIds) {
392 unlinkLayer(layerId, linkedLayer);
393 }
394 return {};
395 }
396
getDebugString() const397 std::string LayerLifecycleManager::References::getDebugString() const {
398 std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:";
399 std::for_each(references.begin(), references.end(),
400 [&debugInfo = debugInfo](const uint32_t& reference) mutable {
401 debugInfo += std::to_string(reference) + ",";
402 });
403 return debugInfo;
404 }
405
fixRelativeZLoop(uint32_t relativeRootId)406 void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) {
407 auto it = mIdToLayer.find(relativeRootId);
408 if (it == mIdToLayer.end()) {
409 return;
410 }
411 RequestedLayerState& layer = it->second.owner;
412 layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id);
413 layer.changes |=
414 RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent;
415 mGlobalChanges |= RequestedLayerState::Changes::Hierarchy;
416 }
417
418 // Some layers mirror the entire display stack. Since we don't have a single root layer per display
419 // we have to track all these layers and update what they mirror when the list of root layers
420 // on a display changes. This function walks through the list of display mirroring layers
421 // and updates its list of layers that its mirroring. This function should be called when a new
422 // root layer is added, removed or moved to another display.
updateDisplayMirrorLayers(RequestedLayerState & rootLayer)423 void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) {
424 for (uint32_t mirroringLayerId : mDisplayMirroringLayers) {
425 RequestedLayerState* mirrorLayer = getLayerFromId(mirroringLayerId);
426 bool canBeMirrored = canMirrorRootLayer(*mirrorLayer, rootLayer);
427 bool currentlyMirrored =
428 std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(),
429 rootLayer.id) != mirrorLayer->mirrorIds.end();
430
431 if (canBeMirrored && !currentlyMirrored) {
432 mirrorLayer->mirrorIds.emplace_back(rootLayer.id);
433 linkLayer(rootLayer.id, mirrorLayer->id);
434 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
435 } else if (!canBeMirrored && currentlyMirrored) {
436 swapErase(mirrorLayer->mirrorIds, rootLayer.id);
437 unlinkLayer(rootLayer.id, mirrorLayer->id);
438 mirrorLayer->changes |= RequestedLayerState::Changes::Mirror;
439 }
440 }
441 }
442
isLayerSecure(uint32_t layerId) const443 bool LayerLifecycleManager::isLayerSecure(uint32_t layerId) const {
444 if (layerId == UNASSIGNED_LAYER_ID) {
445 return false;
446 }
447
448 if (getLayerFromId(layerId)->flags & layer_state_t::eLayerSecure) {
449 return true;
450 }
451 return isLayerSecure(getLayerFromId(layerId)->parentId);
452 }
453
454 } // namespace android::surfaceflinger::frontend
455