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
21 #include <stdint.h>
22 #include <sys/types.h>
23
24 #include <android/native_window.h>
25 #include <gui/IGraphicBufferProducer.h>
26 #include <gui/ITransactionCompletedListener.h>
27 #include <math/mat4.h>
28
29 #ifndef NO_INPUT
30 #include <android/FocusRequest.h>
31 #include <input/InputWindow.h>
32 #endif
33
34 #include <gui/ISurfaceComposer.h>
35 #include <gui/LayerMetadata.h>
36 #include <gui/SurfaceControl.h>
37 #include <math/vec3.h>
38 #include <ui/BlurRegion.h>
39 #include <ui/GraphicTypes.h>
40 #include <ui/Rect.h>
41 #include <ui/Region.h>
42 #include <ui/Rotation.h>
43 #include <ui/StretchEffect.h>
44 #include <ui/Transform.h>
45 #include <utils/Errors.h>
46
47 namespace android {
48
49 class Parcel;
50 class ISurfaceComposerClient;
51
52 struct client_cache_t {
53 wp<IBinder> token = nullptr;
54 uint64_t id;
55
56 bool operator==(const client_cache_t& other) const { return id == other.id; }
57
isValidclient_cache_t58 bool isValid() const { return token != nullptr; }
59 };
60
61 /*
62 * Used to communicate layer information between SurfaceFlinger and its clients.
63 */
64 struct layer_state_t {
65 enum {
66 eLayerHidden = 0x01, // SURFACE_HIDDEN in SurfaceControl.java
67 eLayerOpaque = 0x02, // SURFACE_OPAQUE
68 eLayerSkipScreenshot = 0x40, // SKIP_SCREENSHOT
69 eLayerSecure = 0x80, // SECURE
70 // Queue up BufferStateLayer buffers instead of dropping the oldest buffer when this flag is
71 // set. This blocks the client until all the buffers have been presented. If the buffers
72 // have presentation timestamps, then we may drop buffers.
73 eEnableBackpressure = 0x100, // ENABLE_BACKPRESSURE
74 };
75
76 enum {
77 ePositionChanged = 0x00000001,
78 eLayerChanged = 0x00000002,
79 eSizeChanged = 0x00000004,
80 eAlphaChanged = 0x00000008,
81 eMatrixChanged = 0x00000010,
82 eTransparentRegionChanged = 0x00000020,
83 eFlagsChanged = 0x00000040,
84 eLayerStackChanged = 0x00000080,
85 eReleaseBufferListenerChanged = 0x00000400,
86 eShadowRadiusChanged = 0x00000800,
87 eLayerCreated = 0x00001000,
88 eBufferCropChanged = 0x00002000,
89 eRelativeLayerChanged = 0x00004000,
90 eReparent = 0x00008000,
91 eColorChanged = 0x00010000,
92 eDestroySurface = 0x00020000,
93 eTransformChanged = 0x00040000,
94 eTransformToDisplayInverseChanged = 0x00080000,
95 eCropChanged = 0x00100000,
96 eBufferChanged = 0x00200000,
97 eAcquireFenceChanged = 0x00400000,
98 eDataspaceChanged = 0x00800000,
99 eHdrMetadataChanged = 0x01000000,
100 eSurfaceDamageRegionChanged = 0x02000000,
101 eApiChanged = 0x04000000,
102 eSidebandStreamChanged = 0x08000000,
103 eColorTransformChanged = 0x10000000,
104 eHasListenerCallbacksChanged = 0x20000000,
105 eInputInfoChanged = 0x40000000,
106 eCornerRadiusChanged = 0x80000000,
107 eDestinationFrameChanged = 0x1'00000000,
108 eCachedBufferChanged = 0x2'00000000,
109 eBackgroundColorChanged = 0x4'00000000,
110 eMetadataChanged = 0x8'00000000,
111 eColorSpaceAgnosticChanged = 0x10'00000000,
112 eFrameRateSelectionPriority = 0x20'00000000,
113 eFrameRateChanged = 0x40'00000000,
114 eBackgroundBlurRadiusChanged = 0x80'00000000,
115 eProducerDisconnect = 0x100'00000000,
116 eFixedTransformHintChanged = 0x200'00000000,
117 eFrameNumberChanged = 0x400'00000000,
118 eBlurRegionsChanged = 0x800'00000000,
119 eAutoRefreshChanged = 0x1000'00000000,
120 eStretchChanged = 0x2000'00000000,
121 eTrustedOverlayChanged = 0x4000'00000000,
122 };
123
124 layer_state_t();
125
126 void merge(const layer_state_t& other);
127 status_t write(Parcel& output) const;
128 status_t read(const Parcel& input);
129 bool hasBufferChanges() const;
130 bool hasValidBuffer() const;
131
132 struct matrix22_t {
133 float dsdx{0};
134 float dtdx{0};
135 float dtdy{0};
136 float dsdy{0};
137 status_t write(Parcel& output) const;
138 status_t read(const Parcel& input);
139 };
140 sp<IBinder> surface;
141 int32_t layerId;
142 uint64_t what;
143 float x;
144 float y;
145 int32_t z;
146 uint32_t w;
147 uint32_t h;
148 uint32_t layerStack;
149 float alpha;
150 uint32_t flags;
151 uint32_t mask;
152 uint8_t reserved;
153 matrix22_t matrix;
154 float cornerRadius;
155 uint32_t backgroundBlurRadius;
156 sp<SurfaceControl> reparentSurfaceControl;
157
158 sp<SurfaceControl> relativeLayerSurfaceControl;
159
160 sp<SurfaceControl> parentSurfaceControlForChild;
161
162 half3 color;
163
164 // non POD must be last. see write/read
165 Region transparentRegion;
166
167 uint32_t transform;
168 bool transformToDisplayInverse;
169 Rect crop;
170 Rect orientedDisplaySpaceRect;
171 sp<GraphicBuffer> buffer;
172 sp<Fence> acquireFence;
173 ui::Dataspace dataspace;
174 HdrMetadata hdrMetadata;
175 Region surfaceDamageRegion;
176 int32_t api;
177 sp<NativeHandle> sidebandStream;
178 mat4 colorTransform;
179 std::vector<BlurRegion> blurRegions;
180
181 #ifndef NO_INPUT
182 sp<InputWindowHandle> inputHandle = new InputWindowHandle();
183 #endif
184
185 client_cache_t cachedBuffer;
186
187 LayerMetadata metadata;
188
189 // The following refer to the alpha, and dataspace, respectively of
190 // the background color layer
191 float bgColorAlpha;
192 ui::Dataspace bgColorDataspace;
193
194 // A color space agnostic layer means the color of this layer can be
195 // interpreted in any color space.
196 bool colorSpaceAgnostic;
197
198 std::vector<ListenerCallbacks> listeners;
199
200 // Draws a shadow around the surface.
201 float shadowRadius;
202
203 // Priority of the layer assigned by Window Manager.
204 int32_t frameRateSelectionPriority;
205
206 // Layer frame rate and compatibility. See ANativeWindow_setFrameRate().
207 float frameRate;
208 int8_t frameRateCompatibility;
209 int8_t changeFrameRateStrategy;
210
211 // Set by window manager indicating the layer and all its children are
212 // in a different orientation than the display. The hint suggests that
213 // the graphic producers should receive a transform hint as if the
214 // display was in this orientation. When the display changes to match
215 // the layer orientation, the graphic producer may not need to allocate
216 // a buffer of a different size. -1 means the transform hint is not set,
217 // otherwise the value will be a valid ui::Rotation.
218 ui::Transform::RotationFlags fixedTransformHint;
219
220 // Used by BlastBufferQueue to forward the framenumber generated by the
221 // graphics producer.
222 uint64_t frameNumber;
223
224 // Indicates that the consumer should acquire the next frame as soon as it
225 // can and not wait for a frame to become available. This is only relevant
226 // in shared buffer mode.
227 bool autoRefresh;
228
229 // An inherited state that indicates that this surface control and its children
230 // should be trusted for input occlusion detection purposes
231 bool isTrustedOverlay;
232
233 // Stretch effect to be applied to this layer
234 StretchEffect stretchEffect;
235
236 Rect bufferCrop;
237 Rect destinationFrame;
238
239 // Listens to when the buffer is safe to be released. This is used for blast
240 // layers only. The callback includes a release fence as well as the graphic
241 // buffer id to identify the buffer.
242 sp<ITransactionCompletedListener> releaseBufferListener;
243
244 // Keeps track of the release callback id associated with the listener. This
245 // is not sent to the server since the id can be reconstructed there. This
246 // is used to remove the old callback from the client process map if it is
247 // overwritten by another setBuffer call.
248 ReleaseCallbackId releaseCallbackId;
249 };
250
251 struct ComposerState {
252 layer_state_t state;
253 status_t write(Parcel& output) const;
254 status_t read(const Parcel& input);
255 };
256
257 struct DisplayState {
258 enum {
259 eSurfaceChanged = 0x01,
260 eLayerStackChanged = 0x02,
261 eDisplayProjectionChanged = 0x04,
262 eDisplaySizeChanged = 0x08
263 };
264
265 DisplayState();
266 void merge(const DisplayState& other);
267
268 uint32_t what;
269 sp<IBinder> token;
270 sp<IGraphicBufferProducer> surface;
271 uint32_t layerStack;
272
273 // These states define how layers are projected onto the physical display.
274 //
275 // Layers are first clipped to `layerStackSpaceRect'. They are then translated and
276 // scaled from `layerStackSpaceRect' to `orientedDisplaySpaceRect'. Finally, they are rotated
277 // according to `orientation', `width', and `height'.
278 //
279 // For example, assume layerStackSpaceRect is Rect(0, 0, 200, 100), orientedDisplaySpaceRect is
280 // Rect(20, 10, 420, 210), and the size of the display is WxH. When orientation is 0, layers
281 // will be scaled by a factor of 2 and translated by (20, 10). When orientation is 1, layers
282 // will be additionally rotated by 90 degrees around the origin clockwise and translated by (W,
283 // 0).
284 ui::Rotation orientation = ui::ROTATION_0;
285 Rect layerStackSpaceRect;
286 Rect orientedDisplaySpaceRect;
287
288 uint32_t width, height;
289
290 status_t write(Parcel& output) const;
291 status_t read(const Parcel& input);
292 };
293
294 struct InputWindowCommands {
295 #ifndef NO_INPUT
296 std::vector<FocusRequest> focusRequests;
297 #endif
298 bool syncInputWindows{false};
299
300 // Merges the passed in commands and returns true if there were any changes.
301 bool merge(const InputWindowCommands& other);
302 bool empty() const;
303 void clear();
304 status_t write(Parcel& output) const;
305 status_t read(const Parcel& input);
306 };
307
compare_type(const ComposerState & lhs,const ComposerState & rhs)308 static inline int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
309 if (lhs.state.surface < rhs.state.surface) return -1;
310 if (lhs.state.surface > rhs.state.surface) return 1;
311 return 0;
312 }
313
compare_type(const DisplayState & lhs,const DisplayState & rhs)314 static inline int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
315 return compare_type(lhs.token, rhs.token);
316 }
317
318 // Returns true if the frameRate is valid.
319 //
320 // @param frameRate the frame rate in Hz
321 // @param compatibility a ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_*
322 // @param changeFrameRateStrategy a ANATIVEWINDOW_CHANGE_FRAME_RATE_*
323 // @param functionName calling function or nullptr. Used for logging
324 // @param privileged whether caller has unscoped surfaceflinger access
325 bool ValidateFrameRate(float frameRate, int8_t compatibility, int8_t changeFrameRateStrategy,
326 const char* functionName, bool privileged = false);
327
328 struct CaptureArgs {
329 const static int32_t UNSET_UID = -1;
330 virtual ~CaptureArgs() = default;
331
332 ui::PixelFormat pixelFormat{ui::PixelFormat::RGBA_8888};
333 Rect sourceCrop;
334 float frameScaleX{1};
335 float frameScaleY{1};
336 bool captureSecureLayers{false};
337 int32_t uid{UNSET_UID};
338 // Force capture to be in a color space. If the value is ui::Dataspace::UNKNOWN, the captured
339 // result will be in the display's colorspace.
340 // The display may use non-RGB dataspace (ex. displayP3) that could cause pixel data could be
341 // different from SRGB (byte per color), and failed when checking colors in tests.
342 // NOTE: In normal cases, we want the screen to be captured in display's colorspace.
343 ui::Dataspace dataspace = ui::Dataspace::UNKNOWN;
344
345 // The receiver of the capture can handle protected buffer. A protected buffer has
346 // GRALLOC_USAGE_PROTECTED usage bit and must not be accessed unprotected behaviour.
347 // Any read/write access from unprotected context will result in undefined behaviour.
348 // Protected contents are typically DRM contents. This has no direct implication to the
349 // secure property of the surface, which is specified by the application explicitly to avoid
350 // the contents being accessed/captured by screenshot or unsecure display.
351 bool allowProtected = false;
352
353 bool grayscale = false;
354
355 virtual status_t write(Parcel& output) const;
356 virtual status_t read(const Parcel& input);
357 };
358
359 struct DisplayCaptureArgs : CaptureArgs {
360 sp<IBinder> displayToken;
361 uint32_t width{0};
362 uint32_t height{0};
363 bool useIdentityTransform{false};
364
365 status_t write(Parcel& output) const override;
366 status_t read(const Parcel& input) override;
367 };
368
369 struct LayerCaptureArgs : CaptureArgs {
370 sp<IBinder> layerHandle;
371 std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeHandles;
372 bool childrenOnly{false};
373
374 status_t write(Parcel& output) const override;
375 status_t read(const Parcel& input) override;
376 };
377
378 }; // namespace android
379
380 #endif // ANDROID_SF_LAYER_STATE_H
381