blob: 0902ab80679e1502c3d9203313c8c8606ab73968 [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:
38 struct Args {
Vishnu Nair3af0ec02023-02-10 04:13:48 +000039 LayerHierarchy root;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000040 const LayerLifecycleManager& layerLifecycleManager;
41 bool forceUpdate = false;
42 bool includeMetadata = false;
43 const display::DisplayMap<ui::LayerStack, frontend::DisplayInfo>& displays;
44 // Set to true if there were display changes since last update.
45 bool displayChanges = false;
46 const renderengine::ShadowSettings& globalShadowSettings;
Vishnu Naircfb2d252023-01-19 04:44:02 +000047 bool supportsBlur = true;
48 bool forceFullDamage = false;
Vishnu Nair3af0ec02023-02-10 04:13:48 +000049 std::optional<FloatRect> parentCrop = std::nullopt;
50 std::unordered_set<uint32_t> excludeLayerIds;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000051 };
52 LayerSnapshotBuilder();
53
54 // Rebuild the snapshots from scratch.
55 LayerSnapshotBuilder(Args);
56
57 // Update an existing set of snapshot using change flags in RequestedLayerState
58 // and LayerLifecycleManager. This needs to be called before
59 // LayerLifecycleManager.commitChanges is called as that function will clear all
60 // change flags.
61 void update(const Args&);
62 std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
Vishnu Naircfb2d252023-01-19 04:44:02 +000063 LayerSnapshot* getSnapshot(uint32_t layerId) const;
64
65 typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor;
66
67 // Visit each visible snapshot in z-order
68 void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
69
Vishnu Nair3af0ec02023-02-10 04:13:48 +000070 // Visit each visible snapshot in z-order
71 void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const;
72
Vishnu Naircfb2d252023-01-19 04:44:02 +000073 typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
74 // Visit each visible snapshot in z-order and move the snapshot if needed
75 void forEachVisibleSnapshot(const Visitor& visitor);
76
77 // Visit each snapshot interesting to input reverse z-order
78 void forEachInputSnapshot(const ConstVisitor& visitor) const;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000079
80private:
81 friend class LayerSnapshotTest;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000082 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
83 static LayerSnapshot getRootSnapshot();
84
85 // return true if we were able to successfully update the snapshots via
86 // the fast path.
87 bool tryFastUpdate(const Args& args);
88
89 void updateSnapshots(const Args& args);
90
Vishnu Naircfb2d252023-01-19 04:44:02 +000091 const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
92 LayerHierarchy::TraversalPath& traversalPath,
93 const LayerSnapshot& parentSnapshot);
94 void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&,
95 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&,
96 bool newSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000097 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
98 bool parentIsRelative, const Args& args);
99 static void resetRelativeState(LayerSnapshot& snapshot);
100 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
101 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000102 void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
103 const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000104 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
105 const renderengine::ShadowSettings& globalShadowSettings);
106 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000107 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
108 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000109 void sortSnapshotsByZ(const Args& args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000110 LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
111 const RequestedLayerState& layer);
112 void updateChildState(LayerSnapshot& snapshot, const LayerSnapshot& childSnapshot,
113 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000114
115 struct TraversalPathHash {
116 std::size_t operator()(const LayerHierarchy::TraversalPath& key) const {
117 uint32_t hashCode = key.id * 31;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000118 if (key.mirrorRootId != UNASSIGNED_LAYER_ID) {
119 hashCode += key.mirrorRootId * 31;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000120 }
121 return std::hash<size_t>{}(hashCode);
122 }
123 };
124 std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*, TraversalPathHash>
125 mIdToSnapshot;
126 std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
127 LayerSnapshot mRootSnapshot;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000128 bool mResortSnapshots = false;
129 int mNumInterestingSnapshots = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000130};
131
132} // namespace android::surfaceflinger::frontend