blob: abb7e668c346239c2c27cefef74578623483e28e [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 {
39 const LayerHierarchy& root;
40 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 Nair8fc721b2022-12-22 20:06:32 +000049 };
50 LayerSnapshotBuilder();
51
52 // Rebuild the snapshots from scratch.
53 LayerSnapshotBuilder(Args);
54
55 // Update an existing set of snapshot using change flags in RequestedLayerState
56 // and LayerLifecycleManager. This needs to be called before
57 // LayerLifecycleManager.commitChanges is called as that function will clear all
58 // change flags.
59 void update(const Args&);
60 std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
Vishnu Naircfb2d252023-01-19 04:44:02 +000061 LayerSnapshot* getSnapshot(uint32_t layerId) const;
62
63 typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor;
64
65 // Visit each visible snapshot in z-order
66 void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
67
68 typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
69 // Visit each visible snapshot in z-order and move the snapshot if needed
70 void forEachVisibleSnapshot(const Visitor& visitor);
71
72 // Visit each snapshot interesting to input reverse z-order
73 void forEachInputSnapshot(const ConstVisitor& visitor) const;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000074
75private:
76 friend class LayerSnapshotTest;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000077 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
78 static LayerSnapshot getRootSnapshot();
79
80 // return true if we were able to successfully update the snapshots via
81 // the fast path.
82 bool tryFastUpdate(const Args& args);
83
84 void updateSnapshots(const Args& args);
85
Vishnu Naircfb2d252023-01-19 04:44:02 +000086 const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
87 LayerHierarchy::TraversalPath& traversalPath,
88 const LayerSnapshot& parentSnapshot);
89 void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&,
90 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&,
91 bool newSnapshot);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000092 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
93 bool parentIsRelative, const Args& args);
94 static void resetRelativeState(LayerSnapshot& snapshot);
95 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
96 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +000097 void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
98 const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +000099 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
100 const renderengine::ShadowSettings& globalShadowSettings);
101 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000102 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
103 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000104 void sortSnapshotsByZ(const Args& args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000105 LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
106 const RequestedLayerState& layer);
107 void updateChildState(LayerSnapshot& snapshot, const LayerSnapshot& childSnapshot,
108 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000109
110 struct TraversalPathHash {
111 std::size_t operator()(const LayerHierarchy::TraversalPath& key) const {
112 uint32_t hashCode = key.id * 31;
113 for (auto mirrorRoot : key.mirrorRootIds) {
114 hashCode += mirrorRoot * 31;
115 }
116 return std::hash<size_t>{}(hashCode);
117 }
118 };
119 std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*, TraversalPathHash>
120 mIdToSnapshot;
121 std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
122 LayerSnapshot mRootSnapshot;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000123 bool mResortSnapshots = false;
124 int mNumInterestingSnapshots = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000125};
126
127} // namespace android::surfaceflinger::frontend