1 /*
2  * Copyright (C) 2008 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 #ifndef ANDROID_SF_LAYER_STATE_H
18 #define ANDROID_SF_LAYER_STATE_H
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <utils/Errors.h>
24 
25 #include <gui/IGraphicBufferProducer.h>
26 #include <math/mat4.h>
27 
28 #ifndef NO_INPUT
29 #include <input/InputWindow.h>
30 #endif
31 
32 #include <gui/LayerMetadata.h>
33 #include <math/vec3.h>
34 #include <ui/GraphicTypes.h>
35 #include <ui/Rect.h>
36 #include <ui/Region.h>
37 
38 namespace android {
39 
40 class Parcel;
41 class ISurfaceComposerClient;
42 
43 struct client_cache_t {
44     wp<IBinder> token = nullptr;
45     uint64_t id;
46 
47     bool operator==(const client_cache_t& other) const { return id == other.id; }
48 
isValidclient_cache_t49     bool isValid() const { return token != nullptr; }
50 };
51 
52 /*
53  * Used to communicate layer information between SurfaceFlinger and its clients.
54  */
55 struct layer_state_t {
56     enum {
57         eLayerHidden = 0x01, // SURFACE_HIDDEN in SurfaceControl.java
58         eLayerOpaque = 0x02, // SURFACE_OPAQUE
59         eLayerSecure = 0x80, // SECURE
60     };
61 
62     enum {
63         ePositionChanged = 0x00000001,
64         eLayerChanged = 0x00000002,
65         eSizeChanged = 0x00000004,
66         eAlphaChanged = 0x00000008,
67         eMatrixChanged = 0x00000010,
68         eTransparentRegionChanged = 0x00000020,
69         eFlagsChanged = 0x00000040,
70         eLayerStackChanged = 0x00000080,
71         eCropChanged_legacy = 0x00000100,
72         eDeferTransaction_legacy = 0x00000200,
73         eOverrideScalingModeChanged = 0x00000400,
74         eGeometryAppliesWithResize = 0x00000800,
75         eReparentChildren = 0x00001000,
76         eDetachChildren = 0x00002000,
77         eRelativeLayerChanged = 0x00004000,
78         eReparent = 0x00008000,
79         eColorChanged = 0x00010000,
80         eDestroySurface = 0x00020000,
81         eTransformChanged = 0x00040000,
82         eTransformToDisplayInverseChanged = 0x00080000,
83         eCropChanged = 0x00100000,
84         eBufferChanged = 0x00200000,
85         eAcquireFenceChanged = 0x00400000,
86         eDataspaceChanged = 0x00800000,
87         eHdrMetadataChanged = 0x01000000,
88         eSurfaceDamageRegionChanged = 0x02000000,
89         eApiChanged = 0x04000000,
90         eSidebandStreamChanged = 0x08000000,
91         eColorTransformChanged = 0x10000000,
92         eHasListenerCallbacksChanged = 0x20000000,
93         eInputInfoChanged = 0x40000000,
94         eCornerRadiusChanged = 0x80000000,
95         eFrameChanged = 0x1'00000000,
96         eCachedBufferChanged = 0x2'00000000,
97         eBackgroundColorChanged = 0x4'00000000,
98         eMetadataChanged = 0x8'00000000,
99         eColorSpaceAgnosticChanged = 0x10'00000000,
100     };
101 
layer_state_tlayer_state_t102     layer_state_t()
103           : what(0),
104             x(0),
105             y(0),
106             z(0),
107             w(0),
108             h(0),
109             layerStack(0),
110             alpha(0),
111             flags(0),
112             mask(0),
113             reserved(0),
114             crop_legacy(Rect::INVALID_RECT),
115             cornerRadius(0.0f),
116             frameNumber_legacy(0),
117             overrideScalingMode(-1),
118             transform(0),
119             transformToDisplayInverse(false),
120             crop(Rect::INVALID_RECT),
121             frame(Rect::INVALID_RECT),
122             dataspace(ui::Dataspace::UNKNOWN),
123             surfaceDamageRegion(),
124             api(-1),
125             colorTransform(mat4()),
126             hasListenerCallbacks(false),
127             bgColorAlpha(0),
128             bgColorDataspace(ui::Dataspace::UNKNOWN),
129             colorSpaceAgnostic(false) {
130         matrix.dsdx = matrix.dtdy = 1.0f;
131         matrix.dsdy = matrix.dtdx = 0.0f;
132         hdrMetadata.validTypes = 0;
133     }
134 
135     void merge(const layer_state_t& other);
136     status_t write(Parcel& output) const;
137     status_t read(const Parcel& input);
138 
139     struct matrix22_t {
140         float dsdx{0};
141         float dtdx{0};
142         float dtdy{0};
143         float dsdy{0};
144     };
145     sp<IBinder> surface;
146     uint64_t what;
147     float x;
148     float y;
149     int32_t z;
150     uint32_t w;
151     uint32_t h;
152     uint32_t layerStack;
153     float alpha;
154     uint8_t flags;
155     uint8_t mask;
156     uint8_t reserved;
157     matrix22_t matrix;
158     Rect crop_legacy;
159     float cornerRadius;
160     sp<IBinder> barrierHandle_legacy;
161     sp<IBinder> reparentHandle;
162     uint64_t frameNumber_legacy;
163     int32_t overrideScalingMode;
164 
165     sp<IGraphicBufferProducer> barrierGbp_legacy;
166 
167     sp<IBinder> relativeLayerHandle;
168 
169     sp<IBinder> parentHandleForChild;
170 
171     half3 color;
172 
173     // non POD must be last. see write/read
174     Region transparentRegion;
175 
176     uint32_t transform;
177     bool transformToDisplayInverse;
178     Rect crop;
179     Rect frame;
180     sp<GraphicBuffer> buffer;
181     sp<Fence> acquireFence;
182     ui::Dataspace dataspace;
183     HdrMetadata hdrMetadata;
184     Region surfaceDamageRegion;
185     int32_t api;
186     sp<NativeHandle> sidebandStream;
187     mat4 colorTransform;
188 
189     bool hasListenerCallbacks;
190 #ifndef NO_INPUT
191     InputWindowInfo inputInfo;
192 #endif
193 
194     client_cache_t cachedBuffer;
195 
196     LayerMetadata metadata;
197 
198     // The following refer to the alpha, and dataspace, respectively of
199     // the background color layer
200     float bgColorAlpha;
201     ui::Dataspace bgColorDataspace;
202 
203     // A color space agnostic layer means the color of this layer can be
204     // interpreted in any color space.
205     bool colorSpaceAgnostic;
206 };
207 
208 struct ComposerState {
209     sp<ISurfaceComposerClient> client;
210     layer_state_t state;
211     status_t write(Parcel& output) const;
212     status_t read(const Parcel& input);
213 };
214 
215 struct DisplayState {
216     enum {
217         eOrientationDefault = 0,
218         eOrientation90 = 1,
219         eOrientation180 = 2,
220         eOrientation270 = 3,
221         eOrientationUnchanged = 4,
222         eOrientationSwapMask = 0x01
223     };
224 
225     enum {
226         eSurfaceChanged = 0x01,
227         eLayerStackChanged = 0x02,
228         eDisplayProjectionChanged = 0x04,
229         eDisplaySizeChanged = 0x08
230     };
231 
232     DisplayState();
233     void merge(const DisplayState& other);
234 
235     uint32_t what;
236     sp<IBinder> token;
237     sp<IGraphicBufferProducer> surface;
238     uint32_t layerStack;
239 
240     // These states define how layers are projected onto the physical display.
241     //
242     // Layers are first clipped to `viewport'.  They are then translated and
243     // scaled from `viewport' to `frame'.  Finally, they are rotated according
244     // to `orientation', `width', and `height'.
245     //
246     // For example, assume viewport is Rect(0, 0, 200, 100), frame is Rect(20,
247     // 10, 420, 210), and the size of the display is WxH.  When orientation is
248     // 0, layers will be scaled by a factor of 2 and translated by (20, 10).
249     // When orientation is 1, layers will be additionally rotated by 90
250     // degrees around the origin clockwise and translated by (W, 0).
251     uint32_t orientation;
252     Rect viewport;
253     Rect frame;
254 
255     uint32_t width, height;
256 
257     status_t write(Parcel& output) const;
258     status_t read(const Parcel& input);
259 };
260 
261 struct InputWindowCommands {
262     struct TransferTouchFocusCommand {
263         sp<IBinder> fromToken;
264         sp<IBinder> toToken;
265     };
266 
267     std::vector<TransferTouchFocusCommand> transferTouchFocusCommands;
268     bool syncInputWindows{false};
269 
270     void merge(const InputWindowCommands& other);
271     void clear();
272     void write(Parcel& output) const;
273     void read(const Parcel& input);
274 };
275 
compare_type(const ComposerState & lhs,const ComposerState & rhs)276 static inline int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
277     if (lhs.client < rhs.client) return -1;
278     if (lhs.client > rhs.client) return 1;
279     if (lhs.state.surface < rhs.state.surface) return -1;
280     if (lhs.state.surface > rhs.state.surface) return 1;
281     return 0;
282 }
283 
compare_type(const DisplayState & lhs,const DisplayState & rhs)284 static inline int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
285     return compare_type(lhs.token, rhs.token);
286 }
287 
288 }; // namespace android
289 
290 #endif // ANDROID_SF_LAYER_STATE_H
291