blob: 33b250c79372085e24318ef73de876a3d2f2c359 [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;
47 };
48 LayerSnapshotBuilder();
49
50 // Rebuild the snapshots from scratch.
51 LayerSnapshotBuilder(Args);
52
53 // Update an existing set of snapshot using change flags in RequestedLayerState
54 // and LayerLifecycleManager. This needs to be called before
55 // LayerLifecycleManager.commitChanges is called as that function will clear all
56 // change flags.
57 void update(const Args&);
58 std::vector<std::unique_ptr<LayerSnapshot>>& getSnapshots();
59
60private:
61 friend class LayerSnapshotTest;
62 LayerSnapshot* getSnapshot(uint32_t layerId) const;
63 LayerSnapshot* getSnapshot(const LayerHierarchy::TraversalPath& id) const;
64 static LayerSnapshot getRootSnapshot();
65
66 // return true if we were able to successfully update the snapshots via
67 // the fast path.
68 bool tryFastUpdate(const Args& args);
69
70 void updateSnapshots(const Args& args);
71
72 void updateSnapshotsInHierarchy(const Args&, const LayerHierarchy& hierarchy,
73 LayerHierarchy::TraversalPath& traversalPath,
74 const LayerSnapshot& parentSnapshot);
75 void updateSnapshot(LayerSnapshot& snapshot, const Args& args, const RequestedLayerState&,
76 const LayerSnapshot& parentSnapshot,
77 const LayerHierarchy::TraversalPath& path);
78 static void updateRelativeState(LayerSnapshot& snapshot, const LayerSnapshot& parentSnapshot,
79 bool parentIsRelative, const Args& args);
80 static void resetRelativeState(LayerSnapshot& snapshot);
81 static void updateRoundedCorner(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
82 const LayerSnapshot& parentSnapshot);
83 static void updateLayerBounds(LayerSnapshot& snapshot, const RequestedLayerState& layerState,
84 const LayerSnapshot& parentSnapshot,
85 uint32_t displayRotationFlags);
86 static void updateShadows(LayerSnapshot& snapshot, const RequestedLayerState& requested,
87 const renderengine::ShadowSettings& globalShadowSettings);
88 void updateInput(LayerSnapshot& snapshot, const RequestedLayerState& requested,
89 const LayerSnapshot& parentSnapshot, const frontend::DisplayInfo& displayInfo,
90 bool noValidDisplay, const LayerHierarchy::TraversalPath& path);
91 void sortSnapshotsByZ(const Args& args);
92 LayerSnapshot* getOrCreateSnapshot(const LayerHierarchy::TraversalPath& id,
93 const RequestedLayerState& layer);
94
95 struct TraversalPathHash {
96 std::size_t operator()(const LayerHierarchy::TraversalPath& key) const {
97 uint32_t hashCode = key.id * 31;
98 for (auto mirrorRoot : key.mirrorRootIds) {
99 hashCode += mirrorRoot * 31;
100 }
101 return std::hash<size_t>{}(hashCode);
102 }
103 };
104 std::unordered_map<LayerHierarchy::TraversalPath, LayerSnapshot*, TraversalPathHash>
105 mIdToSnapshot;
106 std::vector<std::unique_ptr<LayerSnapshot>> mSnapshots;
107 LayerSnapshot mRootSnapshot;
108};
109
110} // namespace android::surfaceflinger::frontend