xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/FrontEnd/LayerSnapshot.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 <compositionengine/LayerFECompositionState.h>
20 #include <renderengine/LayerSettings.h>
21 #include "DisplayHardware/ComposerHal.h"
22 #include "LayerHierarchy.h"
23 #include "RequestedLayerState.h"
24 #include "Scheduler/LayerInfo.h"
25 #include "android-base/stringprintf.h"
26 
27 namespace android::surfaceflinger::frontend {
28 
29 struct RoundedCornerState {
30     RoundedCornerState() = default;
RoundedCornerStateRoundedCornerState31     RoundedCornerState(const FloatRect& cropRect, const vec2& radius)
32           : cropRect(cropRect), radius(radius) {}
33 
34     // Rounded rectangle in local layer coordinate space.
35     FloatRect cropRect = FloatRect();
36     // Radius of the rounded rectangle.
37     vec2 radius;
hasRoundedCornersRoundedCornerState38     bool hasRoundedCorners() const { return radius.x > 0.0f && radius.y > 0.0f; }
39     bool operator==(RoundedCornerState const& rhs) const {
40         return cropRect == rhs.cropRect && radius == rhs.radius;
41     }
42 };
43 
44 // LayerSnapshot stores Layer state used by CompositionEngine and RenderEngine. Composition
45 // Engine uses a pointer to LayerSnapshot (as LayerFECompositionState*) and the LayerSettings
46 // passed to Render Engine are created using properties stored on this struct.
47 struct LayerSnapshot : public compositionengine::LayerFECompositionState {
48     LayerSnapshot() = default;
49     LayerSnapshot(const RequestedLayerState&, const LayerHierarchy::TraversalPath&);
50 
51     LayerHierarchy::TraversalPath path;
52     size_t globalZ = std::numeric_limits<ssize_t>::max();
53     bool invalidTransform = false;
54     bool isHiddenByPolicyFromParent = false;
55     bool isHiddenByPolicyFromRelativeParent = false;
56     ftl::Flags<RequestedLayerState::Changes> changes;
57     uint64_t clientChanges = 0;
58     // Some consumers of this snapshot (input, layer traces) rely on each snapshot to be unique.
59     // For mirrored layers, snapshots will have the same sequence so this unique id provides
60     // an alternative identifier when needed.
61     uint32_t uniqueSequence;
62     // Layer id used to create this snapshot. Multiple snapshots will have the same sequence if they
63     // generated from the same layer, for example when mirroring.
64     int32_t sequence;
65     std::string name;
66     std::string debugName;
67     bool contentOpaque;
68     bool layerOpaqueFlagSet;
69     RoundedCornerState roundedCorner;
70     FloatRect transformedBounds;
71     Rect transformedBoundsWithoutTransparentRegion;
72     bool premultipliedAlpha;
73     ui::Transform parentTransform;
74     Rect bufferSize;
75     FloatRect croppedBufferSize;
76     std::shared_ptr<renderengine::ExternalTexture> externalTexture;
77     gui::LayerMetadata layerMetadata;
78     gui::LayerMetadata relativeLayerMetadata;
79     bool hasReadyFrame; // used in post composition to check if there is another frame ready
80     bool autoRefresh;
81     ui::Transform localTransformInverse;
82     gui::WindowInfo inputInfo;
83     ui::Transform localTransform;
84     // set to true if this snapshot will ignore local transforms. Used when the snapshot
85     // is a mirror root
86     bool ignoreLocalTransform;
87     gui::DropInputMode dropInputMode;
88     gui::TrustedOverlay trustedOverlay;
89     gui::GameMode gameMode;
90     scheduler::LayerInfo::FrameRate frameRate;
91     scheduler::LayerInfo::FrameRate inheritedFrameRate;
92     scheduler::LayerInfo::FrameRateSelectionStrategy frameRateSelectionStrategy;
93     scheduler::FrameRateCompatibility defaultFrameRateCompatibility =
94             scheduler::FrameRateCompatibility::Default;
95     ui::Transform::RotationFlags fixedTransformHint;
96     std::optional<ui::Transform::RotationFlags> transformHint;
97     bool handleSkipScreenshotFlag = false;
98     int32_t frameRateSelectionPriority = -1;
99     LayerHierarchy::TraversalPath mirrorRootPath;
100     uint32_t touchCropId;
101     gui::Uid uid = gui::Uid::INVALID;
102     gui::Pid pid = gui::Pid::INVALID;
103     enum class Reachablilty : uint32_t {
104         // Can traverse the hierarchy from a root node and reach this snapshot
105         Reachable,
106         // Cannot traverse the hierarchy from a root node and reach this snapshot
107         Unreachable,
108         // Can only reach this node from a relative parent. This means the nodes parents are
109         // not reachable.
110         // See example scenario:
111         // ROOT
112         // ├── 1
113         // │   ├── 11
114         // │   │   └── 111
115         // │   ├── 12
116         // │   │   └ - 111 (relative)
117         // │   ├── 13
118         // │   └── 14
119         // │       └ * 12 (mirroring)
120         // └── 2
121         // 111 will create two snapshots, first when visited from 1 -> 12 or 1 -> 11 and the
122         // second when visited from 1 -> 14 -> 12. Because its parent 11 doesn't exist in the
123         // mirrored hierarchy, the second snapshot will be marked as ReachableByRelativeParent.
124         // This snapshot doesn't have any valid properties because it cannot inherit from its
125         // parent. Therefore, snapshots that are not reachable will be ignored for composition
126         // and input.
127         ReachableByRelativeParent
128     };
129     Reachablilty reachablilty;
130     // True when the surfaceDamage is recognized as a small area update.
131     bool isSmallDirty = false;
132 
133     static bool isOpaqueFormat(PixelFormat format);
134     static bool isTransformValid(const ui::Transform& t);
135 
136     bool canReceiveInput() const;
137     bool drawShadows() const;
138     bool fillsColor() const;
139     bool getIsVisible() const;
140     bool hasBlur() const;
141     bool hasBufferOrSidebandStream() const;
142     bool hasEffect() const;
143     bool hasSomethingToDraw() const;
144     bool isContentOpaque() const;
145     bool isHiddenByPolicy() const;
146     std::string getDebugString() const;
147     std::string getIsVisibleReason() const;
148     bool hasInputInfo() const;
149     FloatRect sourceBounds() const;
150     bool isFrontBuffered() const;
151     Hwc2::IComposerClient::BlendMode getBlendMode(const RequestedLayerState& requested) const;
152     friend std::ostream& operator<<(std::ostream& os, const LayerSnapshot& obj);
153     void merge(const RequestedLayerState& requested, bool forceUpdate, bool displayChanges,
154                bool forceFullDamage, uint32_t displayRotationFlags);
155 };
156 
157 } // namespace android::surfaceflinger::frontend
158