blob: 3f33ab875b3e94e0dbbff052920ef6beaa6bbd38 [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
Vishnu Nair8fc721b2022-12-22 20:06:32 +000019#include "FrontEnd/DisplayInfo.h"
20#include "FrontEnd/LayerLifecycleManager.h"
21#include "LayerHierarchy.h"
22#include "LayerSnapshot.h"
23#include "RequestedLayerState.h"
24
25namespace 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.
35class LayerSnapshotBuilder {
36public:
Vishnu Naird47bcee2023-02-24 18:08:51 +000037 enum class ForceUpdateFlags {
38 NONE,
39 ALL,
40 HIERARCHY,
41 };
Vishnu Nair8fc721b2022-12-22 20:06:32 +000042 struct Args {
Vishnu Nair3af0ec02023-02-10 04:13:48 +000043 LayerHierarchy root;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000044 const LayerLifecycleManager& layerLifecycleManager;
Vishnu Naird47bcee2023-02-24 18:08:51 +000045 ForceUpdateFlags forceUpdate = ForceUpdateFlags::NONE;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000046 bool includeMetadata = false;
Dominik Laskowski6b049ff2023-01-29 15:46:45 -050047 const DisplayInfos& displays;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000048 // Set to true if there were display changes since last update.
49 bool displayChanges = false;
50 const renderengine::ShadowSettings& globalShadowSettings;
Vishnu Naircfb2d252023-01-19 04:44:02 +000051 bool supportsBlur = true;
52 bool forceFullDamage = false;
Vishnu Nair3af0ec02023-02-10 04:13:48 +000053 std::optional<FloatRect> parentCrop = std::nullopt;
54 std::unordered_set<uint32_t> excludeLayerIds;
Vishnu Nairc765c6c2023-02-23 00:08:01 +000055 const std::unordered_map<std::string, bool>& supportedLayerGenericMetadata;
56 const std::unordered_map<std::string, uint32_t>& genericLayerMetadataKeyMap;
Vishnu Nair8fc721b2022-12-22 20:06:32 +000057 };
58 LayerSnapshotBuilder();
59
60 // Rebuild the snapshots from scratch.
61 LayerSnapshotBuilder(Args);
62
63 // Update an existing set of snapshot using change flags in RequestedLayerState
64 // and LayerLifecycleManager. This needs to be called before
65 // LayerLifecycleManager.commitChanges is called as that function will clear all
66 // change flags.
67 void update(const Args&);
68 std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
Vishnu Naircfb2d252023-01-19 04:44:02 +000069 LayerSnapshot* getSnapshot(uint32_t layerId) const;
Vishnu Naird0183602023-03-16 18:52:15 +000070 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
Vishnu Naircfb2d252023-01-19 04:44:02 +000071
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 static LayerSnapshot getRootSnapshot();
90
91 // return true if we were able to successfully update the snapshots via
92 // the fast path.
93 bool tryFastUpdate(const Args& args);
94
95 void updateSnapshots(const Args& args);
96
Vishnu Naircfb2d252023-01-19 04:44:02 +000097 const LayerSnapshot& updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
98 LayerHierarchy::TraversalPath& traversalPath,
99 const LayerSnapshot& parentSnapshot);
100 void updateSnapshot(LayerSnapshot&, const Args&, const RequestedLayerState&,
Vishnu Nair92990e22023-02-24 20:01:05 +0000101 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath&);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000102 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
103 bool parentIsRelative, const Args& args);
104 static void resetRelativeState(LayerSnapshot& snapshot);
105 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
106 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000107 void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
108 const LayerSnapshot& parentSnapshot, uint32_t displayRotationFlags);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000109 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
110 const renderengine::ShadowSettings& globalShadowSettings);
111 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
Vishnu Naircfb2d252023-01-19 04:44:02 +0000112 const LayerSnapshot& parentSnapshot, const LayerHierarchy::TraversalPath& path,
113 const Args& args);
Vishnu Nairfccd6362023-02-24 23:39:53 +0000114 // Return true if there are unreachable snapshots
115 bool sortSnapshotsByZ(const Args& args);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000116 LayerSnapshot* createSnapshot(const LayerHierarchy::TraversalPath& id,
Vishnu Nair92990e22023-02-24 20:01:05 +0000117 const RequestedLayerState& layer,
118 const LayerSnapshot& parentSnapshot);
Vishnu Naircfb2d252023-01-19 04:44:02 +0000119 void updateChildState(LayerSnapshot& snapshot, const LayerSnapshot& childSnapshot,
120 const Args& args);
Vishnu Nair29354ec2023-03-28 18:51:28 -0700121 void updateTouchableRegionCrop(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;
Vishnu Nair29354ec2023-03-28 18:51:28 -0700126 // Track snapshots that needs touchable region crop from other snapshots
127 std::unordered_set<LayerHierarchy::TraversalPath, LayerHierarchy::TraversalPathHash>
128 mNeedsTouchableRegionCrop;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000129 std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
130 LayerSnapshot mRootSnapshot;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000131 bool mResortSnapshots = false;
132 int mNumInterestingSnapshots = 0;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000133};
134
135} // namespace android::surfaceflinger::frontend