blob: 330da9a3d3e6f8c5eaa677a802fae3493a8bbd86 [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#pragma once
18
19#include "RequestedLayerState.h"
20#include "TransactionState.h"
21
22namespace android::surfaceflinger::frontend {
23
24// Owns a collection of RequestedLayerStates and manages their lifecycle
25// and state changes.
26//
27// RequestedLayerStates are tracked and destroyed if they have no parent and
28// no handle left to keep them alive. The handle does not keep a reference to
29// the RequestedLayerState but a layer id associated with the RequestedLayerState.
30// If the handle is destroyed and the RequestedLayerState does not have a parent,
31// the LayerLifecycleManager destroys the RequestedLayerState.
32//
33// Threading: This class is not thread safe, it requires external synchronization.
34//
35// Typical usage: Input states (new layers, transactions, destroyed layer handles)
36// are collected in the background passed into the LayerLifecycleManager to update
37// layer lifecycle and layer state at start of composition.
38class LayerLifecycleManager {
39public:
40 // External state changes should be updated in the following order:
41 void addLayers(std::vector<std::unique_ptr<RequestedLayerState>>);
Vishnu Nair20e1f962023-03-29 15:58:34 -070042 // Ignore unknown layers when interoping with legacy front end. In legacy we destroy
43 // the layers it is unreachable. When using the LayerLifecycleManager for layer trace
44 // generation we may encounter layers which are known because we don't have an explicit
45 // lifecycle. Ignore these errors while we have to interop with legacy.
46 void applyTransactions(const std::vector<TransactionState>&, bool ignoreUnknownLayers = false);
Vishnu Nair191eeb82023-03-11 10:16:07 -080047 // Ignore unknown handles when iteroping with legacy front end. In the old world, we
48 // would create child layers which are not necessary with the new front end. This means
49 // we will get notified for handle changes that don't exist in the new front end.
Vishnu Nair606d9d02023-08-19 14:20:18 -070050 void onHandlesDestroyed(const std::vector<std::pair<uint32_t, std::string /* debugName */>>&,
51 bool ignoreUnknownHandles = false);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000052
Vishnu Nair04f89692022-11-16 23:21:05 +000053 // Detaches the layer from its relative parent to prevent a loop in the
54 // layer hierarchy. This overrides the RequestedLayerState and leaves
55 // the system in an invalid state. This is always a client error that
56 // needs to be fixed but overriding the state allows us to fail gracefully.
57 void fixRelativeZLoop(uint32_t relativeRootId);
58
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000059 // Destroys RequestedLayerStates that are marked to be destroyed. Invokes all
60 // ILifecycleListener callbacks and clears any change flags from previous state
61 // updates. This function should be called outside the hot path since it's not
62 // critical to composition.
63 void commitChanges();
64
65 class ILifecycleListener {
66 public:
67 virtual ~ILifecycleListener() = default;
68 // Called on commitChanges when a layer is added. The callback includes
69 // the layer state the client was created with as well as any state updates
70 // until changes were committed.
71 virtual void onLayerAdded(const RequestedLayerState&) = 0;
72 // Called on commitChanges when a layer has been destroyed. The callback
73 // includes the final state before the layer was destroyed.
74 virtual void onLayerDestroyed(const RequestedLayerState&) = 0;
75 };
76 void addLifecycleListener(std::shared_ptr<ILifecycleListener>);
77 void removeLifecycleListener(std::shared_ptr<ILifecycleListener>);
78 const std::vector<std::unique_ptr<RequestedLayerState>>& getLayers() const;
79 const std::vector<std::unique_ptr<RequestedLayerState>>& getDestroyedLayers() const;
Vishnu Naira02943f2023-06-03 13:44:46 -070080 const std::vector<RequestedLayerState*>& getChangedLayers() const;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000081 const ftl::Flags<RequestedLayerState::Changes> getGlobalChanges() const;
Vishnu Naira02943f2023-06-03 13:44:46 -070082 const RequestedLayerState* getLayerFromId(uint32_t) const;
Chavi Weingarten4aa22af2023-11-17 19:37:07 +000083 bool isLayerSecure(uint32_t) const;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000084
85private:
86 friend class LayerLifecycleManagerTest;
87 friend class HierarchyBuilderTest;
88 friend class android::SurfaceFlinger;
89
90 RequestedLayerState* getLayerFromId(uint32_t);
91 std::vector<uint32_t>* getLinkedLayersFromId(uint32_t);
Vishnu Nair04f89692022-11-16 23:21:05 +000092 uint32_t linkLayer(uint32_t layerId, uint32_t layerToLink);
93 uint32_t unlinkLayer(uint32_t layerId, uint32_t linkedLayer);
Vishnu Naira9c43762023-01-27 19:10:25 +000094 std::vector<uint32_t> unlinkLayers(const std::vector<uint32_t>& layerIds, uint32_t linkedLayer);
95
96 void updateDisplayMirrorLayers(RequestedLayerState& rootLayer);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000097
98 struct References {
99 // Lifetime tied to mLayers
100 RequestedLayerState& owner;
101 std::vector<uint32_t> references;
102 std::string getDebugString() const;
103 };
104 std::unordered_map<uint32_t, References> mIdToLayer;
105 // Listeners are invoked once changes are committed.
106 std::vector<std::shared_ptr<ILifecycleListener>> mListeners;
Vishnu Naira9c43762023-01-27 19:10:25 +0000107 // Layers that mirror a display stack (see updateDisplayMirrorLayers)
108 std::vector<uint32_t> mDisplayMirroringLayers;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000109
110 // Aggregation of changes since last commit.
111 ftl::Flags<RequestedLayerState::Changes> mGlobalChanges;
112 std::vector<std::unique_ptr<RequestedLayerState>> mLayers;
113 // Layers pending destruction. Layers will be destroyed once changes are committed.
114 std::vector<std::unique_ptr<RequestedLayerState>> mDestroyedLayers;
Vishnu Nair150065b2023-04-17 19:14:11 -0700115 // Keeps track of all the layers that were added in order. Changes will be cleared once
116 // committed.
117 std::vector<RequestedLayerState*> mAddedLayers;
Vishnu Naira02943f2023-06-03 13:44:46 -0700118 // Keeps track of new and layers with states changes since last commit.
119 std::vector<RequestedLayerState*> mChangedLayers;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000120};
121
122} // namespace android::surfaceflinger::frontend