1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright 2022 The Android Open Source Project 3*38e8c45fSAndroid Build Coastguard Worker * 4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*38e8c45fSAndroid Build Coastguard Worker * 8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*38e8c45fSAndroid Build Coastguard Worker * 10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License. 15*38e8c45fSAndroid Build Coastguard Worker */ 16*38e8c45fSAndroid Build Coastguard Worker 17*38e8c45fSAndroid Build Coastguard Worker #pragma once 18*38e8c45fSAndroid Build Coastguard Worker 19*38e8c45fSAndroid Build Coastguard Worker #include "RequestedLayerState.h" 20*38e8c45fSAndroid Build Coastguard Worker #include "TransactionState.h" 21*38e8c45fSAndroid Build Coastguard Worker 22*38e8c45fSAndroid Build Coastguard Worker namespace android::surfaceflinger::frontend { 23*38e8c45fSAndroid Build Coastguard Worker 24*38e8c45fSAndroid Build Coastguard Worker // Owns a collection of RequestedLayerStates and manages their lifecycle 25*38e8c45fSAndroid Build Coastguard Worker // and state changes. 26*38e8c45fSAndroid Build Coastguard Worker // 27*38e8c45fSAndroid Build Coastguard Worker // RequestedLayerStates are tracked and destroyed if they have no parent and 28*38e8c45fSAndroid Build Coastguard Worker // no handle left to keep them alive. The handle does not keep a reference to 29*38e8c45fSAndroid Build Coastguard Worker // the RequestedLayerState but a layer id associated with the RequestedLayerState. 30*38e8c45fSAndroid Build Coastguard Worker // If the handle is destroyed and the RequestedLayerState does not have a parent, 31*38e8c45fSAndroid Build Coastguard Worker // the LayerLifecycleManager destroys the RequestedLayerState. 32*38e8c45fSAndroid Build Coastguard Worker // 33*38e8c45fSAndroid Build Coastguard Worker // Threading: This class is not thread safe, it requires external synchronization. 34*38e8c45fSAndroid Build Coastguard Worker // 35*38e8c45fSAndroid Build Coastguard Worker // Typical usage: Input states (new layers, transactions, destroyed layer handles) 36*38e8c45fSAndroid Build Coastguard Worker // are collected in the background passed into the LayerLifecycleManager to update 37*38e8c45fSAndroid Build Coastguard Worker // layer lifecycle and layer state at start of composition. 38*38e8c45fSAndroid Build Coastguard Worker class LayerLifecycleManager { 39*38e8c45fSAndroid Build Coastguard Worker public: 40*38e8c45fSAndroid Build Coastguard Worker // External state changes should be updated in the following order: 41*38e8c45fSAndroid Build Coastguard Worker void addLayers(std::vector<std::unique_ptr<RequestedLayerState>>); 42*38e8c45fSAndroid Build Coastguard Worker // Ignore unknown layers when interoping with legacy front end. In legacy we destroy 43*38e8c45fSAndroid Build Coastguard Worker // the layers it is unreachable. When using the LayerLifecycleManager for layer trace 44*38e8c45fSAndroid Build Coastguard Worker // generation we may encounter layers which are known because we don't have an explicit 45*38e8c45fSAndroid Build Coastguard Worker // lifecycle. Ignore these errors while we have to interop with legacy. 46*38e8c45fSAndroid Build Coastguard Worker void applyTransactions(const std::vector<TransactionState>&, bool ignoreUnknownLayers = false); 47*38e8c45fSAndroid Build Coastguard Worker // Ignore unknown handles when iteroping with legacy front end. In the old world, we 48*38e8c45fSAndroid Build Coastguard Worker // would create child layers which are not necessary with the new front end. This means 49*38e8c45fSAndroid Build Coastguard Worker // we will get notified for handle changes that don't exist in the new front end. 50*38e8c45fSAndroid Build Coastguard Worker void onHandlesDestroyed(const std::vector<std::pair<uint32_t, std::string /* debugName */>>&, 51*38e8c45fSAndroid Build Coastguard Worker bool ignoreUnknownHandles = false); 52*38e8c45fSAndroid Build Coastguard Worker 53*38e8c45fSAndroid Build Coastguard Worker // Detaches the layer from its relative parent to prevent a loop in the 54*38e8c45fSAndroid Build Coastguard Worker // layer hierarchy. This overrides the RequestedLayerState and leaves 55*38e8c45fSAndroid Build Coastguard Worker // the system in an invalid state. This is always a client error that 56*38e8c45fSAndroid Build Coastguard Worker // needs to be fixed but overriding the state allows us to fail gracefully. 57*38e8c45fSAndroid Build Coastguard Worker void fixRelativeZLoop(uint32_t relativeRootId); 58*38e8c45fSAndroid Build Coastguard Worker 59*38e8c45fSAndroid Build Coastguard Worker // Destroys RequestedLayerStates that are marked to be destroyed. Invokes all 60*38e8c45fSAndroid Build Coastguard Worker // ILifecycleListener callbacks and clears any change flags from previous state 61*38e8c45fSAndroid Build Coastguard Worker // updates. This function should be called outside the hot path since it's not 62*38e8c45fSAndroid Build Coastguard Worker // critical to composition. 63*38e8c45fSAndroid Build Coastguard Worker void commitChanges(); 64*38e8c45fSAndroid Build Coastguard Worker 65*38e8c45fSAndroid Build Coastguard Worker class ILifecycleListener { 66*38e8c45fSAndroid Build Coastguard Worker public: 67*38e8c45fSAndroid Build Coastguard Worker virtual ~ILifecycleListener() = default; 68*38e8c45fSAndroid Build Coastguard Worker // Called on commitChanges when a layer is added. The callback includes 69*38e8c45fSAndroid Build Coastguard Worker // the layer state the client was created with as well as any state updates 70*38e8c45fSAndroid Build Coastguard Worker // until changes were committed. 71*38e8c45fSAndroid Build Coastguard Worker virtual void onLayerAdded(const RequestedLayerState&) = 0; 72*38e8c45fSAndroid Build Coastguard Worker // Called on commitChanges when a layer has been destroyed. The callback 73*38e8c45fSAndroid Build Coastguard Worker // includes the final state before the layer was destroyed. 74*38e8c45fSAndroid Build Coastguard Worker virtual void onLayerDestroyed(const RequestedLayerState&) = 0; 75*38e8c45fSAndroid Build Coastguard Worker }; 76*38e8c45fSAndroid Build Coastguard Worker void addLifecycleListener(std::shared_ptr<ILifecycleListener>); 77*38e8c45fSAndroid Build Coastguard Worker void removeLifecycleListener(std::shared_ptr<ILifecycleListener>); 78*38e8c45fSAndroid Build Coastguard Worker const std::vector<std::unique_ptr<RequestedLayerState>>& getLayers() const; 79*38e8c45fSAndroid Build Coastguard Worker const std::vector<std::unique_ptr<RequestedLayerState>>& getDestroyedLayers() const; 80*38e8c45fSAndroid Build Coastguard Worker const std::vector<RequestedLayerState*>& getChangedLayers() const; 81*38e8c45fSAndroid Build Coastguard Worker const ftl::Flags<RequestedLayerState::Changes> getGlobalChanges() const; 82*38e8c45fSAndroid Build Coastguard Worker const RequestedLayerState* getLayerFromId(uint32_t) const; 83*38e8c45fSAndroid Build Coastguard Worker bool isLayerSecure(uint32_t) const; 84*38e8c45fSAndroid Build Coastguard Worker 85*38e8c45fSAndroid Build Coastguard Worker private: 86*38e8c45fSAndroid Build Coastguard Worker friend class LayerLifecycleManagerTest; 87*38e8c45fSAndroid Build Coastguard Worker friend class HierarchyBuilderTest; 88*38e8c45fSAndroid Build Coastguard Worker friend class android::SurfaceFlinger; 89*38e8c45fSAndroid Build Coastguard Worker 90*38e8c45fSAndroid Build Coastguard Worker RequestedLayerState* getLayerFromId(uint32_t); 91*38e8c45fSAndroid Build Coastguard Worker std::vector<uint32_t>* getLinkedLayersFromId(uint32_t); 92*38e8c45fSAndroid Build Coastguard Worker uint32_t linkLayer(uint32_t layerId, uint32_t layerToLink); 93*38e8c45fSAndroid Build Coastguard Worker uint32_t unlinkLayer(uint32_t layerId, uint32_t linkedLayer); 94*38e8c45fSAndroid Build Coastguard Worker std::vector<uint32_t> unlinkLayers(const std::vector<uint32_t>& layerIds, uint32_t linkedLayer); 95*38e8c45fSAndroid Build Coastguard Worker 96*38e8c45fSAndroid Build Coastguard Worker void updateDisplayMirrorLayers(RequestedLayerState& rootLayer); 97*38e8c45fSAndroid Build Coastguard Worker 98*38e8c45fSAndroid Build Coastguard Worker struct References { 99*38e8c45fSAndroid Build Coastguard Worker // Lifetime tied to mLayers 100*38e8c45fSAndroid Build Coastguard Worker RequestedLayerState& owner; 101*38e8c45fSAndroid Build Coastguard Worker std::vector<uint32_t> references; 102*38e8c45fSAndroid Build Coastguard Worker std::string getDebugString() const; 103*38e8c45fSAndroid Build Coastguard Worker }; 104*38e8c45fSAndroid Build Coastguard Worker std::unordered_map<uint32_t, References> mIdToLayer; 105*38e8c45fSAndroid Build Coastguard Worker // Listeners are invoked once changes are committed. 106*38e8c45fSAndroid Build Coastguard Worker std::vector<std::shared_ptr<ILifecycleListener>> mListeners; 107*38e8c45fSAndroid Build Coastguard Worker // Layers that mirror a display stack (see updateDisplayMirrorLayers) 108*38e8c45fSAndroid Build Coastguard Worker std::vector<uint32_t> mDisplayMirroringLayers; 109*38e8c45fSAndroid Build Coastguard Worker 110*38e8c45fSAndroid Build Coastguard Worker // Aggregation of changes since last commit. 111*38e8c45fSAndroid Build Coastguard Worker ftl::Flags<RequestedLayerState::Changes> mGlobalChanges; 112*38e8c45fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<RequestedLayerState>> mLayers; 113*38e8c45fSAndroid Build Coastguard Worker // Layers pending destruction. Layers will be destroyed once changes are committed. 114*38e8c45fSAndroid Build Coastguard Worker std::vector<std::unique_ptr<RequestedLayerState>> mDestroyedLayers; 115*38e8c45fSAndroid Build Coastguard Worker // Keeps track of all the layers that were added in order. Changes will be cleared once 116*38e8c45fSAndroid Build Coastguard Worker // committed. 117*38e8c45fSAndroid Build Coastguard Worker std::vector<RequestedLayerState*> mAddedLayers; 118*38e8c45fSAndroid Build Coastguard Worker // Keeps track of new and layers with states changes since last commit. 119*38e8c45fSAndroid Build Coastguard Worker std::vector<RequestedLayerState*> mChangedLayers; 120*38e8c45fSAndroid Build Coastguard Worker }; 121*38e8c45fSAndroid Build Coastguard Worker 122*38e8c45fSAndroid Build Coastguard Worker } // namespace android::surfaceflinger::frontend 123