blob: 7b1ff2710bae60abe086064604f11493bef2c6bc [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;
Vishnu Naird0183602023-03-16 18:52:15 +000071 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
Vishnu Naircfb2d252023-01-19 04:44:02 +000072
73 typedef std::function<void(const LayerSnapshot& snapshot)> ConstVisitor;
74
75 // Visit each visible snapshot in z-order
76 void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
77
Vishnu Nair3af0ec02023-02-10 04:13:48 +000078 // Visit each visible snapshot in z-order
79 void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const;
80
Vishnu Naircfb2d252023-01-19 04:44:02 +000081 typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
82 // Visit each visible snapshot in z-order and move the snapshot if needed
83 void forEachVisibleSnapshot(const Visitor& visitor);
84
85 // Visit each snapshot interesting to input reverse z-order
86 void forEachInputSnapshot(const ConstVisitor& visitor) const;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000087
88private:
89 friend class LayerSnapshotTest;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000090 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&,
Vishnu Nair92990e22023-02-24 20:01:05 +0000102 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000103 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
104 bool parentIsRelative, const Args& args);
105 static void resetRelativeState(LayerSnapshot& snapshot);
106 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
107 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000108 void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
109 const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000110 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
111 const renderengine::ShadowSettings& globalShadowSettings);
112 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000113 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
114 const Args& args);
Vishnu Nairfccd6362023-02-24 23:39:53 +0000115 // Return true if there are unreachable snapshots
116 bool sortSnapshotsByZ(const Args& args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000117 LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
Vishnu Nair92990e22023-02-24 20:01:05 +0000118 const RequestedLayerState& layer,
119 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000120 void updateChildState(LayerSnapshot& snapshot, const LayerSnapshot& childSnapshot,
121 const Args& args);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000122
Vishnu Naird0183602023-03-16 18:52:15 +0000123 std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*,
124 LayerHierarchy::TraversalPathHash>
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000125 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