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 <android/native_window.h>
24 #include <gui/IGraphicBufferProducer.h>
25 #include <gui/ITransactionCompletedListener.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 #include <ui/Rotation.h>
38 #include <ui/Transform.h>
39 #include <utils/Errors.h>
40 
41 namespace android {
42 
43 class Parcel;
44 class ISurfaceComposerClient;
45 
46 struct client_cache_t {
47     wp<IBinder> token = nullptr;
48     uint64_t id;
49 
50     bool operator==(const client_cache_t& other) const { return id == other.id; }
51 
isValidclient_cache_t52     bool isValid() const { return token != nullptr; }
53 };
54 
55 /*
56  * Used to communicate layer information between SurfaceFlinger and its clients.
57  */
58 struct layer_state_t {
59     enum {
60         eLayerHidden = 0x01, // SURFACE_HIDDEN in SurfaceControl.java
61         eLayerOpaque = 0x02, // SURFACE_OPAQUE
62         eLayerSecure = 0x80, // SECURE
63     };
64 
65     enum {
66         ePositionChanged = 0x00000001,
67         eLayerChanged = 0x00000002,
68         eSizeChanged = 0x00000004,
69         eAlphaChanged = 0x00000008,
70         eMatrixChanged = 0x00000010,
71         eTransparentRegionChanged = 0x00000020,
72         eFlagsChanged = 0x00000040,
73         eLayerStackChanged = 0x00000080,
74         eCropChanged_legacy = 0x00000100,
75         eDeferTransaction_legacy = 0x00000200,
76         eOverrideScalingModeChanged = 0x00000400,
77         eShadowRadiusChanged = 0x00000800,
78         eReparentChildren = 0x00001000,
79         eDetachChildren = 0x00002000,
80         eRelativeLayerChanged = 0x00004000,
81         eReparent = 0x00008000,
82         eColorChanged = 0x00010000,
83         eDestroySurface = 0x00020000,
84         eTransformChanged = 0x00040000,
85         eTransformToDisplayInverseChanged = 0x00080000,
86         eCropChanged = 0x00100000,
87         eBufferChanged = 0x00200000,
88         eAcquireFenceChanged = 0x00400000,
89         eDataspaceChanged = 0x00800000,
90         eHdrMetadataChanged = 0x01000000,
91         eSurfaceDamageRegionChanged = 0x02000000,
92         eApiChanged = 0x04000000,
93         eSidebandStreamChanged = 0x08000000,
94         eColorTransformChanged = 0x10000000,
95         eHasListenerCallbacksChanged = 0x20000000,
96         eInputInfoChanged = 0x40000000,
97         eCornerRadiusChanged = 0x80000000,
98         eFrameChanged = 0x1'00000000,
99         eCachedBufferChanged = 0x2'00000000,
100         eBackgroundColorChanged = 0x4'00000000,
101         eMetadataChanged = 0x8'00000000,
102         eColorSpaceAgnosticChanged = 0x10'00000000,
103         eFrameRateSelectionPriority = 0x20'00000000,
104         eFrameRateChanged = 0x40'00000000,
105         eBackgroundBlurRadiusChanged = 0x80'00000000,
106         eProducerDisconnect = 0x100'00000000,
107         eFixedTransformHintChanged = 0x200'00000000,
108     };
109 
layer_state_tlayer_state_t110     layer_state_t()
111           : what(0),
112             x(0),
113             y(0),
114             z(0),
115             w(0),
116             h(0),
117             layerStack(0),
118             alpha(0),
119             flags(0),
120             mask(0),
121             reserved(0),
122             crop_legacy(Rect::INVALID_RECT),
123             cornerRadius(0.0f),
124             backgroundBlurRadius(0),
125             frameNumber_legacy(0),
126             overrideScalingMode(-1),
127             transform(0),
128             transformToDisplayInverse(false),
129             crop(Rect::INVALID_RECT),
130             frame(Rect::INVALID_RECT),
131             dataspace(ui::Dataspace::UNKNOWN),
132             surfaceDamageRegion(),
133             api(-1),
134             colorTransform(mat4()),
135             bgColorAlpha(0),
136             bgColorDataspace(ui::Dataspace::UNKNOWN),
137             colorSpaceAgnostic(false),
138             shadowRadius(0.0f),
139             frameRateSelectionPriority(-1),
140             frameRate(0.0f),
141             frameRateCompatibility(ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT),
142             fixedTransformHint(ui::Transform::ROT_INVALID) {
143         matrix.dsdx = matrix.dtdy = 1.0f;
144         matrix.dsdy = matrix.dtdx = 0.0f;
145         hdrMetadata.validTypes = 0;
146     }
147 
148     void merge(const layer_state_t& other);
149     status_t write(Parcel& output) const;
150     status_t read(const Parcel& input);
151 
152     struct matrix22_t {
153         float dsdx{0};
154         float dtdx{0};
155         float dtdy{0};
156         float dsdy{0};
157     };
158     sp<IBinder> surface;
159     uint64_t what;
160     float x;
161     float y;
162     int32_t z;
163     uint32_t w;
164     uint32_t h;
165     uint32_t layerStack;
166     float alpha;
167     uint8_t flags;
168     uint8_t mask;
169     uint8_t reserved;
170     matrix22_t matrix;
171     Rect crop_legacy;
172     float cornerRadius;
173     uint32_t backgroundBlurRadius;
174     sp<IBinder> barrierHandle_legacy;
175     sp<IBinder> reparentHandle;
176     uint64_t frameNumber_legacy;
177     int32_t overrideScalingMode;
178 
179     sp<IGraphicBufferProducer> barrierGbp_legacy;
180 
181     sp<IBinder> relativeLayerHandle;
182 
183     sp<IBinder> parentHandleForChild;
184 
185     half3 color;
186 
187     // non POD must be last. see write/read
188     Region transparentRegion;
189 
190     uint32_t transform;
191     bool transformToDisplayInverse;
192     Rect crop;
193     Rect frame;
194     sp<GraphicBuffer> buffer;
195     sp<Fence> acquireFence;
196     ui::Dataspace dataspace;
197     HdrMetadata hdrMetadata;
198     Region surfaceDamageRegion;
199     int32_t api;
200     sp<NativeHandle> sidebandStream;
201     mat4 colorTransform;
202 
203 #ifndef NO_INPUT
204     InputWindowInfo inputInfo;
205 #endif
206 
207     client_cache_t cachedBuffer;
208 
209     LayerMetadata metadata;
210 
211     // The following refer to the alpha, and dataspace, respectively of
212     // the background color layer
213     float bgColorAlpha;
214     ui::Dataspace bgColorDataspace;
215 
216     // A color space agnostic layer means the color of this layer can be
217     // interpreted in any color space.
218     bool colorSpaceAgnostic;
219 
220     std::vector<ListenerCallbacks> listeners;
221 
222     // Draws a shadow around the surface.
223     float shadowRadius;
224 
225     // Priority of the layer assigned by Window Manager.
226     int32_t frameRateSelectionPriority;
227 
228     // Layer frame rate and compatibility. See ANativeWindow_setFrameRate().
229     float frameRate;
230     int8_t frameRateCompatibility;
231 
232     // Set by window manager indicating the layer and all its children are
233     // in a different orientation than the display. The hint suggests that
234     // the graphic producers should receive a transform hint as if the
235     // display was in this orientation. When the display changes to match
236     // the layer orientation, the graphic producer may not need to allocate
237     // a buffer of a different size. -1 means the transform hint is not set,
238     // otherwise the value will be a valid ui::Rotation.
239     ui::Transform::RotationFlags fixedTransformHint;
240 };
241 
242 struct ComposerState {
243     layer_state_t state;
244     status_t write(Parcel& output) const;
245     status_t read(const Parcel& input);
246 };
247 
248 struct DisplayState {
249     enum {
250         eSurfaceChanged = 0x01,
251         eLayerStackChanged = 0x02,
252         eDisplayProjectionChanged = 0x04,
253         eDisplaySizeChanged = 0x08
254     };
255 
256     DisplayState();
257     void merge(const DisplayState& other);
258 
259     uint32_t what;
260     sp<IBinder> token;
261     sp<IGraphicBufferProducer> surface;
262     uint32_t layerStack;
263 
264     // These states define how layers are projected onto the physical display.
265     //
266     // Layers are first clipped to `viewport'.  They are then translated and
267     // scaled from `viewport' to `frame'.  Finally, they are rotated according
268     // to `orientation', `width', and `height'.
269     //
270     // For example, assume viewport is Rect(0, 0, 200, 100), frame is Rect(20,
271     // 10, 420, 210), and the size of the display is WxH.  When orientation is
272     // 0, layers will be scaled by a factor of 2 and translated by (20, 10).
273     // When orientation is 1, layers will be additionally rotated by 90
274     // degrees around the origin clockwise and translated by (W, 0).
275     ui::Rotation orientation = ui::ROTATION_0;
276     Rect viewport;
277     Rect frame;
278 
279     uint32_t width, height;
280 
281     status_t write(Parcel& output) const;
282     status_t read(const Parcel& input);
283 };
284 
285 struct InputWindowCommands {
286     bool syncInputWindows{false};
287 
288     void merge(const InputWindowCommands& other);
289     void clear();
290     void write(Parcel& output) const;
291     void read(const Parcel& input);
292 };
293 
compare_type(const ComposerState & lhs,const ComposerState & rhs)294 static inline int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
295     if (lhs.state.surface < rhs.state.surface) return -1;
296     if (lhs.state.surface > rhs.state.surface) return 1;
297     return 0;
298 }
299 
compare_type(const DisplayState & lhs,const DisplayState & rhs)300 static inline int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
301     return compare_type(lhs.token, rhs.token);
302 }
303 
304 // Returns true if the frameRate and compatibility are valid values, false
305 // othwerise. If either of the params are invalid, an error log is printed, and
306 // functionName is added to the log to indicate which function call failed.
307 // functionName can be null.
308 bool ValidateFrameRate(float frameRate, int8_t compatibility, const char* functionName);
309 
310 }; // namespace android
311 
312 #endif // ANDROID_SF_LAYER_STATE_H
313