1 /*
2  * Copyright 2019 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 <cstdint>
20 
21 #include <android/gui/CachingHint.h>
22 #include <gui/DisplayLuts.h>
23 #include <gui/HdrMetadata.h>
24 #include <math/mat4.h>
25 #include <ui/BlurRegion.h>
26 #include <ui/FloatRect.h>
27 #include <ui/LayerStack.h>
28 #include <ui/PictureProfileHandle.h>
29 #include <ui/Rect.h>
30 #include <ui/Region.h>
31 #include <ui/ShadowSettings.h>
32 #include <ui/Transform.h>
33 
34 // TODO(b/129481165): remove the #pragma below and fix conversion issues
35 #pragma clang diagnostic push
36 #pragma clang diagnostic ignored "-Wconversion"
37 #pragma clang diagnostic ignored "-Wextra"
38 
39 #include <gui/BufferQueue.h>
40 #include <ui/EdgeExtensionEffect.h>
41 #include <ui/GraphicBuffer.h>
42 #include <ui/GraphicTypes.h>
43 #include <ui/StretchEffect.h>
44 
45 #include "DisplayHardware/Hal.h"
46 
47 #include <aidl/android/hardware/graphics/composer3/Composition.h>
48 
49 // TODO(b/129481165): remove the #pragma below and fix conversion issues
50 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
51 
52 namespace android::compositionengine {
53 
54 namespace hal = android::hardware::graphics::composer::hal;
55 
56 // More complex metadata for this layer
57 struct GenericLayerMetadataEntry {
58     // True if the metadata may affect the composed result.
59     // See setLayerGenericMetadata in IComposerClient.hal
60     bool mandatory;
61 
62     // Byte blob or parcel
63     std::vector<uint8_t> value;
64 
65     std::string dumpAsString() const;
66 
67     struct Hasher {
operatorGenericLayerMetadataEntry::Hasher68         size_t operator()(const GenericLayerMetadataEntry& entry) const {
69             size_t hash = 0;
70             for (const auto value : entry.value) {
71                 hashCombineSingleHashed(hash, value);
72             }
73             return hash;
74         }
75     };
76 };
77 
78 inline bool operator==(const GenericLayerMetadataEntry& lhs, const GenericLayerMetadataEntry& rhs) {
79     return lhs.mandatory == rhs.mandatory && lhs.value == rhs.value;
80 }
81 
82 // Defining PrintTo helps with Google Tests.
PrintTo(const GenericLayerMetadataEntry & v,::std::ostream * os)83 inline void PrintTo(const GenericLayerMetadataEntry& v, ::std::ostream* os) {
84     *os << v.dumpAsString();
85 }
86 
87 using GenericLayerMetadataMap = std::unordered_map<std::string, GenericLayerMetadataEntry>;
88 
89 /*
90  * Used by LayerFE::getCompositionState
91  * Note that fields that affect HW composer state may need to be mirrored into
92  * android::compositionengine::impl::planner::LayerState
93  */
94 struct LayerFECompositionState {
95     // If set to true, forces client composition on all output layers until
96     // the next geometry change.
97     bool forceClientComposition{false};
98 
99     // TODO(b/121291683): Reorganize and rename the contents of this structure
100 
101     /*
102      * Visibility state
103      */
104 
105     // The filter that determines which outputs include this layer
106     ui::LayerFilter outputFilter;
107 
108     // If false, this layer should not be considered visible
109     bool isVisible{true};
110 
111     // True if the layer is completely opaque
112     bool isOpaque{true};
113 
114     // If true, invalidates the entire visible region
115     bool contentDirty{false};
116 
117     // The alpha value for this layer
118     float alpha{1.f};
119 
120     // Background blur in pixels
121     int backgroundBlurRadius{0};
122 
123     // The transform from layer local coordinates to composition coordinates
124     ui::Transform geomLayerTransform;
125 
126     // The inverse of the layer transform
127     ui::Transform geomInverseLayerTransform;
128 
129     // The hint from the layer producer as to what portion of the layer is
130     // transparent.
131     Region transparentRegionHint;
132 
133     // The blend mode for this layer
134     hal::BlendMode blendMode{hal::BlendMode::INVALID};
135 
136     // The bounds of the layer in layer local coordinates
137     FloatRect geomLayerBounds;
138 
139     // The crop to apply to the layer in layer local coordinates
140     FloatRect geomLayerCrop;
141 
142     ShadowSettings shadowSettings;
143 
144     // List of regions that require blur
145     std::vector<BlurRegion> blurRegions;
146 
147     StretchEffect stretchEffect;
148     EdgeExtensionEffect edgeExtensionEffect;
149 
150     /*
151      * Geometry state
152      */
153 
154     bool isSecure{false};
155     bool geomUsesSourceCrop{false};
156     bool geomBufferUsesDisplayInverseTransform{false};
157     uint32_t geomBufferTransform{0};
158     Rect geomBufferSize;
159     Rect geomContentCrop;
160     FloatRect geomCrop;
161 
162     GenericLayerMetadataMap metadata;
163 
164     /*
165      * Per-frame content
166      */
167 
168     // The type of composition for this layer
169     aidl::android::hardware::graphics::composer3::Composition compositionType{
170             aidl::android::hardware::graphics::composer3::Composition::INVALID};
171 
172     // The buffer and related state
173     sp<GraphicBuffer> buffer;
174     sp<Fence> acquireFence = Fence::NO_FENCE;
175     Region surfaceDamage;
176     uint64_t frameNumber = 0;
177 
178     // The handle to use for a sideband stream for this layer
179     sp<NativeHandle> sidebandStream;
180     // If true, this sideband layer has a frame update
181     bool sidebandStreamHasFrame{false};
182 
183     // The color for this layer
184     half4 color;
185 
186     /*
187      * Per-frame presentation state
188      */
189 
190     // If true, this layer will use the dataspace chosen for the output and
191     // ignore the dataspace value just below
192     bool isColorspaceAgnostic{false};
193 
194     // The dataspace for this layer
195     ui::Dataspace dataspace{ui::Dataspace::UNKNOWN};
196 
197     // The metadata for this layer
198     HdrMetadata hdrMetadata;
199 
200     // The color transform
201     mat4 colorTransform;
202     bool colorTransformIsIdentity{true};
203 
204     // True if the layer has protected content
205     bool hasProtectedContent{false};
206 
207     /*
208      * Cursor state
209      */
210 
211     // The output-independent frame for the cursor
212     Rect cursorFrame;
213 
214     // framerate of the layer as measured by LayerHistory
215     float fps;
216 
217     // The dimming flag
218     bool dimmingEnabled{true};
219 
220     float currentHdrSdrRatio = 1.f;
221     float desiredHdrSdrRatio = 1.f;
222 
223     // A picture profile handle refers to a PictureProfile configured on the display, which is a
224     // set of parameters that configures the picture processing hardware that is used to enhance
225     // the quality of buffer contents.
226     PictureProfileHandle pictureProfileHandle{PictureProfileHandle::NONE};
227 
228     // A layer's priority in terms of limited picture processing pipeline utilization.
229     int64_t pictureProfilePriority;
230 
231     gui::CachingHint cachingHint = gui::CachingHint::Enabled;
232 
233     std::shared_ptr<gui::DisplayLuts> luts;
234 
235     virtual ~LayerFECompositionState();
236 
237     // Debugging
238     virtual void dump(std::string& out) const;
239 };
240 
241 } // namespace android::compositionengine
242