Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +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 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 19 | #include "FrontEnd/DisplayInfo.h" |
| 20 | #include "FrontEnd/LayerLifecycleManager.h" |
| 21 | #include "LayerHierarchy.h" |
| 22 | #include "LayerSnapshot.h" |
| 23 | #include "RequestedLayerState.h" |
| 24 | |
| 25 | namespace android::surfaceflinger::frontend { |
| 26 | |
| 27 | // Walks through the layer hierarchy to build an ordered list |
| 28 | // of LayerSnapshots that can be passed on to CompositionEngine. |
| 29 | // This builder does a minimum amount of work to update |
| 30 | // an existing set of snapshots based on hierarchy changes |
| 31 | // and RequestedLayerState changes. |
| 32 | |
| 33 | // The builder also uses a fast path to update |
| 34 | // snapshots when there are only buffer updates. |
| 35 | class LayerSnapshotBuilder { |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 36 | private: |
| 37 | static LayerSnapshot getRootSnapshot(); |
| 38 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 39 | public: |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 40 | enum class ForceUpdateFlags { |
| 41 | NONE, |
| 42 | ALL, |
| 43 | HIERARCHY, |
| 44 | }; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 45 | struct Args { |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 46 | LayerHierarchy root; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 47 | const LayerLifecycleManager& layerLifecycleManager; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 48 | ForceUpdateFlags forceUpdate = ForceUpdateFlags::NONE; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 49 | bool includeMetadata = false; |
Dominik Laskowski | 6b049ff | 2023-01-29 15:46:45 -0500 | [diff] [blame] | 50 | const DisplayInfos& displays; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 51 | // Set to true if there were display changes since last update. |
| 52 | bool displayChanges = false; |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 53 | const ShadowSettings& globalShadowSettings; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 54 | bool supportsBlur = true; |
| 55 | bool forceFullDamage = false; |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 56 | std::optional<FloatRect> parentCrop = std::nullopt; |
| 57 | std::unordered_set<uint32_t> excludeLayerIds; |
Vishnu Nair | c765c6c | 2023-02-23 00:08:01 +0000 | [diff] [blame] | 58 | const std::unordered_map<std::string, bool>& supportedLayerGenericMetadata; |
| 59 | const std::unordered_map<std::string, uint32_t>& genericLayerMetadataKeyMap; |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 60 | bool skipRoundCornersWhenProtected = false; |
Chavi Weingarten | 4aa22af | 2023-11-17 19:37:07 +0000 | [diff] [blame] | 61 | LayerSnapshot rootSnapshot = getRootSnapshot(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 62 | }; |
| 63 | LayerSnapshotBuilder(); |
| 64 | |
| 65 | // Rebuild the snapshots from scratch. |
| 66 | LayerSnapshotBuilder(Args); |
| 67 | |
| 68 | // Update an existing set of snapshot using change flags in RequestedLayerState |
| 69 | // and LayerLifecycleManager. This needs to be called before |
| 70 | // LayerLifecycleManager.commitChanges is called as that function will clear all |
| 71 | // change flags. |
| 72 | void update(const Args&); |
| 73 | std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots(); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 74 | LayerSnapshot* getSnapshot(uint32_t layerId) const; |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 75 | LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 76 | |
| 77 | typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor; |
| 78 | |
| 79 | // Visit each visible snapshot in z-order |
| 80 | void forEachVisibleSnapshot(const ConstVisitor& visitor) const; |
| 81 | |
Vishnu Nair | 3af0ec0 | 2023-02-10 04:13:48 +0000 | [diff] [blame] | 82 | // Visit each visible snapshot in z-order |
| 83 | void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const; |
| 84 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 85 | typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor; |
| 86 | // Visit each visible snapshot in z-order and move the snapshot if needed |
| 87 | void forEachVisibleSnapshot(const Visitor& visitor); |
| 88 | |
| 89 | // Visit each snapshot interesting to input reverse z-order |
| 90 | void forEachInputSnapshot(const ConstVisitor& visitor) const; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | friend class LayerSnapshotTest; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 94 | |
| 95 | // return true if we were able to successfully update the snapshots via |
| 96 | // the fast path. |
| 97 | bool tryFastUpdate(const Args& args); |
| 98 | |
| 99 | void updateSnapshots(const Args& args); |
| 100 | |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 101 | const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy, |
| 102 | LayerHierarchy::TraversalPath& traversalPath, |
Vishnu Nair | d1f7498 | 2023-06-15 20:16:51 -0700 | [diff] [blame] | 103 | const LayerSnapshot& parentSnapshot, int depth); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 104 | void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 105 | const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 106 | static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot, |
| 107 | bool parentIsRelative, const Args& args); |
| 108 | static void resetRelativeState(LayerSnapshot& snapshot); |
| 109 | static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState, |
Vishnu Nair | 0808ae6 | 2023-08-07 21:42:42 -0700 | [diff] [blame] | 110 | const LayerSnapshot& parentSnapshot, const Args& args); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 111 | void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState, |
| 112 | const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 113 | static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested, |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 114 | const ShadowSettings& globalShadowSettings); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 115 | void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested, |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 116 | const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path, |
| 117 | const Args& args); |
Vishnu Nair | fccd636 | 2023-02-24 23:39:53 +0000 | [diff] [blame] | 118 | // Return true if there are unreachable snapshots |
| 119 | bool sortSnapshotsByZ(const Args& args); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 120 | LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id, |
Vishnu Nair | 92990e2 | 2023-02-24 20:01:05 +0000 | [diff] [blame] | 121 | const RequestedLayerState& layer, |
| 122 | const LayerSnapshot& parentSnapshot); |
Vishnu Nair | 42b918e | 2023-07-18 20:05:29 +0000 | [diff] [blame] | 123 | void updateFrameRateFromChildSnapshot(LayerSnapshot& snapshot, |
| 124 | const LayerSnapshot& childSnapshot, const Args& args); |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 125 | void updateTouchableRegionCrop(const Args& args); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 126 | |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 127 | std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*, |
| 128 | LayerHierarchy::TraversalPathHash> |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 129 | mPathToSnapshot; |
| 130 | std::multimap<uint32_t, LayerSnapshot*> mIdToSnapshots; |
| 131 | |
Vishnu Nair | 29354ec | 2023-03-28 18:51:28 -0700 | [diff] [blame] | 132 | // Track snapshots that needs touchable region crop from other snapshots |
| 133 | std::unordered_set<LayerHierarchy::TraversalPath, LayerHierarchy::TraversalPathHash> |
| 134 | mNeedsTouchableRegionCrop; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 135 | std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots; |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 136 | bool mResortSnapshots = false; |
| 137 | int mNumInterestingSnapshots = 0; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | } // namespace android::surfaceflinger::frontend |