| Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 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 | #pragma once | 
 | 18 |  | 
 | 19 | #include "RequestedLayerState.h" | 
 | 20 | #include "TransactionState.h" | 
 | 21 |  | 
 | 22 | namespace 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. | 
 | 38 | class LayerLifecycleManager { | 
 | 39 | public: | 
 | 40 |     // External state changes should be updated in the following order: | 
 | 41 |     void addLayers(std::vector<std::unique_ptr<RequestedLayerState>>); | 
 | 42 |     void applyTransactions(const std::vector<TransactionState>&); | 
 | 43 |     void onHandlesDestroyed(const std::vector<uint32_t>&); | 
 | 44 |  | 
| Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 45 |     // Detaches the layer from its relative parent to prevent a loop in the | 
 | 46 |     // layer hierarchy. This overrides the RequestedLayerState and leaves | 
 | 47 |     // the system in an invalid state. This is always a client error that | 
 | 48 |     // needs to be fixed but overriding the state allows us to fail gracefully. | 
 | 49 |     void fixRelativeZLoop(uint32_t relativeRootId); | 
 | 50 |  | 
| Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 51 |     // Destroys RequestedLayerStates that are marked to be destroyed. Invokes all | 
 | 52 |     // ILifecycleListener callbacks and clears any change flags from previous state | 
 | 53 |     // updates. This function should be called outside the hot path since it's not | 
 | 54 |     // critical to composition. | 
 | 55 |     void commitChanges(); | 
 | 56 |  | 
 | 57 |     class ILifecycleListener { | 
 | 58 |     public: | 
 | 59 |         virtual ~ILifecycleListener() = default; | 
 | 60 |         // Called on commitChanges when a layer is added. The callback includes | 
 | 61 |         // the layer state the client was created with as well as any state updates | 
 | 62 |         // until changes were committed. | 
 | 63 |         virtual void onLayerAdded(const RequestedLayerState&) = 0; | 
 | 64 |         // Called on commitChanges when a layer has been destroyed. The callback | 
 | 65 |         // includes the final state before the layer was destroyed. | 
 | 66 |         virtual void onLayerDestroyed(const RequestedLayerState&) = 0; | 
 | 67 |     }; | 
 | 68 |     void addLifecycleListener(std::shared_ptr<ILifecycleListener>); | 
 | 69 |     void removeLifecycleListener(std::shared_ptr<ILifecycleListener>); | 
 | 70 |     const std::vector<std::unique_ptr<RequestedLayerState>>& getLayers() const; | 
 | 71 |     const std::vector<std::unique_ptr<RequestedLayerState>>& getDestroyedLayers() const; | 
 | 72 |     const ftl::Flags<RequestedLayerState::Changes> getGlobalChanges() const; | 
 | 73 |  | 
 | 74 | private: | 
 | 75 |     friend class LayerLifecycleManagerTest; | 
 | 76 |     friend class HierarchyBuilderTest; | 
 | 77 |     friend class android::SurfaceFlinger; | 
 | 78 |  | 
 | 79 |     RequestedLayerState* getLayerFromId(uint32_t); | 
 | 80 |     std::vector<uint32_t>* getLinkedLayersFromId(uint32_t); | 
| Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 81 |     uint32_t linkLayer(uint32_t layerId, uint32_t layerToLink); | 
 | 82 |     uint32_t unlinkLayer(uint32_t layerId, uint32_t linkedLayer); | 
| Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 83 |  | 
 | 84 |     struct References { | 
 | 85 |         // Lifetime tied to mLayers | 
 | 86 |         RequestedLayerState& owner; | 
 | 87 |         std::vector<uint32_t> references; | 
 | 88 |         std::string getDebugString() const; | 
 | 89 |     }; | 
 | 90 |     std::unordered_map<uint32_t, References> mIdToLayer; | 
 | 91 |     // Listeners are invoked once changes are committed. | 
 | 92 |     std::vector<std::shared_ptr<ILifecycleListener>> mListeners; | 
 | 93 |  | 
 | 94 |     // Aggregation of changes since last commit. | 
 | 95 |     ftl::Flags<RequestedLayerState::Changes> mGlobalChanges; | 
 | 96 |     std::vector<std::unique_ptr<RequestedLayerState>> mLayers; | 
 | 97 |     // Layers pending destruction. Layers will be destroyed once changes are committed. | 
 | 98 |     std::vector<std::unique_ptr<RequestedLayerState>> mDestroyedLayers; | 
 | 99 | }; | 
 | 100 |  | 
 | 101 | } // namespace android::surfaceflinger::frontend |