blob: 548cbc0f8035e3bf19f432f27b61de9682be0e33 [file] [log] [blame]
Vishnu Nair8fc721b2022-12-22 20:06:32 +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 "Display/DisplayMap.h"
20#include "FrontEnd/DisplayInfo.h"
21#include "FrontEnd/LayerLifecycleManager.h"
22#include "LayerHierarchy.h"
23#include "LayerSnapshot.h"
24#include "RequestedLayerState.h"
25
26namespace android::surfaceflinger::frontend {
27
28// Walks through the layer hierarchy to build an ordered list
29// of LayerSnapshots that can be passed on to CompositionEngine.
30// This builder does a minimum amount of work to update
31// an existing set of snapshots based on hierarchy changes
32// and RequestedLayerState changes.
33
34// The builder also uses a fast path to update
35// snapshots when there are only buffer updates.
36class LayerSnapshotBuilder {
37public:
Vishnu Naird47bcee2023-02-24 18:08:51 +000038 enum class ForceUpdateFlags {
39 NONE,
40 ALL,
41 HIERARCHY,
42 };
Vishnu Nair8fc721b2022-12-22 20:06:32 +000043 struct Args {
Vishnu Nair3af0ec02023-02-10 04:13:48 +000044 LayerHierarchy root;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000045 const LayerLifecycleManager& layerLifecycleManager;
Vishnu Naird47bcee2023-02-24 18:08:51 +000046 ForceUpdateFlags forceUpdate = ForceUpdateFlags::NONE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000047 bool includeMetadata = false;
48 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays;
49 // Set to true if there were display changes since last update.
50 bool displayChanges = false;
51 const renderengine::ShadowSettings& globalShadowSettings;
Vishnu Naircfb2d252023-01-19 04:44:02 +000052 bool supportsBlur = true;
53 bool forceFullDamage = false;
Vishnu Nair3af0ec02023-02-10 04:13:48 +000054 std::optional<FloatRect> parentCrop = std::nullopt;
55 std::unordered_set<uint32_t> excludeLayerIds;
Vishnu Nairc765c6c2023-02-23 00:08:01 +000056 const std::unordered_map<std::string, bool>& supportedLayerGenericMetadata;
57 const std::unordered_map<std::string, uint32_t>& genericLayerMetadataKeyMap;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000058 };
59 LayerSnapshotBuilder();
60
61 // Rebuild the snapshots from scratch.
62 LayerSnapshotBuilder(Args);
63
64 // Update an existing set of snapshot using change flags in RequestedLayerState
65 // and LayerLifecycleManager. This needs to be called before
66 // LayerLifecycleManager.commitChanges is called as that function will clear all
67 // change flags.
68 void update(const Args&);
69 std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
Vishnu Naircfb2d252023-01-19 04:44:02 +000070 LayerSnapshot* getSnapshot(uint32_t layerId) const;
71
72 typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor;
73
74 // Visit each visible snapshot in z-order
75 void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
76
Vishnu Nair3af0ec02023-02-10 04:13:48 +000077 // Visit each visible snapshot in z-order
78 void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const;
79
Vishnu Naircfb2d252023-01-19 04:44:02 +000080 typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
81 // Visit each visible snapshot in z-order and move the snapshot if needed
82 void forEachVisibleSnapshot(const Visitor& visitor);
83
84 // Visit each snapshot interesting to input reverse z-order
85 void forEachInputSnapshot(const ConstVisitor& visitor) const;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000086
87private:
88 friend class LayerSnapshotTest;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000089 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
90 static LayerSnapshot getRootSnapshot();
91
92 // return true if we were able to successfully update the snapshots via
93 // the fast path.
94 bool tryFastUpdate(const Args& args);
95
96 void updateSnapshots(const Args& args);
97
Vishnu Naircfb2d252023-01-19 04:44:02 +000098 const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
99 LayerHierarchy::TraversalPath& traversalPath,
100 const LayerSnapshot& parentSnapshot);
101 void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&,
102 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&,
103 bool newSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000104 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
105 bool parentIsRelative, const Args& args);
106 static void resetRelativeState(LayerSnapshot& snapshot);
107 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
108 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000109 void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
110 const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000111 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
112 const renderengine::ShadowSettings& globalShadowSettings);
113 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000114 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
115 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000116 void sortSnapshotsByZ(const Args& args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000117 LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
118 const RequestedLayerState& layer);
119 void updateChildState(LayerSnapshot& snapshot, const LayerSnapshot& childSnapshot,
120 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000121
122 struct TraversalPathHash {
123 std::size_t operator()(const LayerHierarchy::TraversalPath& key) const {
124 uint32_t hashCode = key.id * 31;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000125 if (key.mirrorRootId != UNASSIGNED_LAYER_ID) {
126 hashCode += key.mirrorRootId * 31;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000127 }
128 return std::hash<size_t>{}(hashCode);
129 }
130 };
131 std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*, TraversalPathHash>
132 mIdToSnapshot;
133 std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
134 LayerSnapshot mRootSnapshot;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000135 bool mResortSnapshots = false;
136 int mNumInterestingSnapshots = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000137};
138
139} // namespace android::surfaceflinger::frontend