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