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     Rect 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     ui::Transform localTransformInverse;
81     gui::WindowInfo inputInfo;
82     ui::Transform localTransform;
83     // set to true if this snapshot will ignore local transforms. Used when the snapshot
84     // is a mirror root
85     bool ignoreLocalTransform;
86     gui::DropInputMode dropInputMode;
87     gui::TrustedOverlay trustedOverlay;
88     gui::GameMode gameMode;
89     scheduler::LayerInfo::FrameRate frameRate;
90     scheduler::LayerInfo::FrameRate inheritedFrameRate;
91     scheduler::LayerInfo::FrameRateSelectionStrategy frameRateSelectionStrategy;
92     scheduler::FrameRateCompatibility defaultFrameRateCompatibility =
93             scheduler::FrameRateCompatibility::Default;
94     ui::Transform::RotationFlags fixedTransformHint;
95     std::optional<ui::Transform::RotationFlags> transformHint;
96     bool handleSkipScreenshotFlag = false;
97     int32_t frameRateSelectionPriority = -1;
98     LayerHierarchy::TraversalPath mirrorRootPath;
99     uint32_t touchCropId;
100     gui::Uid uid = gui::Uid::INVALID;
101     gui::Pid pid = gui::Pid::INVALID;
102     enum class Reachablilty : uint32_t {
103         // Can traverse the hierarchy from a root node and reach this snapshot
104         Reachable,
105         // Cannot traverse the hierarchy from a root node and reach this snapshot
106         Unreachable,
107         // Can only reach this node from a relative parent. This means the nodes parents are
108         // not reachable.
109         // See example scenario:
110         // ROOT
111         // ├── 1
112         // │   ├── 11
113         // │   │   └── 111
114         // │   ├── 12
115         // │   │   └ - 111 (relative)
116         // │   ├── 13
117         // │   └── 14
118         // │       └ * 12 (mirroring)
119         // └── 2
120         // 111 will create two snapshots, first when visited from 1 -> 12 or 1 -> 11 and the
121         // second when visited from 1 -> 14 -> 12. Because its parent 11 doesn't exist in the
122         // mirrored hierarchy, the second snapshot will be marked as ReachableByRelativeParent.
123         // This snapshot doesn't have any valid properties because it cannot inherit from its
124         // parent. Therefore, snapshots that are not reachable will be ignored for composition
125         // and input.
126         ReachableByRelativeParent
127     };
128     Reachablilty reachablilty;
129     // True when the surfaceDamage is recognized as a small area update.
130     bool isSmallDirty = false;
131 
132     static bool isOpaqueFormat(PixelFormat format);
133     static bool isTransformValid(const ui::Transform& t);
134 
135     bool canReceiveInput() const;
136     bool drawShadows() const;
137     bool fillsColor() const;
138     bool getIsVisible() const;
139     bool hasBlur() const;
140     bool hasBufferOrSidebandStream() const;
141     bool hasEffect() const;
142     bool hasSomethingToDraw() const;
143     bool isContentOpaque() const;
144     bool isHiddenByPolicy() const;
145     std::string getDebugString() const;
146     std::string getIsVisibleReason() const;
147     bool hasInputInfo() const;
148     FloatRect sourceBounds() const;
149     bool isFrontBuffered() const;
150     Hwc2::IComposerClient::BlendMode getBlendMode(const RequestedLayerState& requested) const;
151     friend std::ostream& operator<<(std::ostream& os, const LayerSnapshot& obj);
152     void merge(const RequestedLayerState& requested, bool forceUpdate, bool displayChanges,
153                bool forceFullDamage, uint32_t displayRotationFlags);
154 };
155 
156 } // namespace android::surfaceflinger::frontend
157