blob: 4a291aeb0248c9510c815d3aeaf99892a3710b2c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopian9cce3252010-02-09 17:46:37 -080017#ifndef ANDROID_SF_LAYER_STATE_H
18#define ANDROID_SF_LAYER_STATE_H
chaviw308ddba2020-08-11 16:23:51 -070019#define SAFE_PARCEL(FUNC, ...) \
20 { \
21 status_t error = FUNC(__VA_ARGS__); \
22 if (error) { \
23 ALOGE("ERROR(%d). Failed to call parcel %s(%s)", error, #FUNC, #__VA_ARGS__); \
24 return error; \
25 } \
26 }
27
28#define SAFE_PARCEL_READ_SIZE(FUNC, COUNT, SIZE) \
29 { \
30 SAFE_PARCEL(FUNC, COUNT); \
31 if (static_cast<unsigned int>(*COUNT) > SIZE) { \
32 ALOGE("ERROR(BAD_VALUE). %s was greater than dataSize", #COUNT); \
33 return BAD_VALUE; \
34 } \
35 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include <stdint.h>
38#include <sys/types.h>
39
Steven Thomas62a4cf82020-01-31 12:04:03 -080040#include <android/native_window.h>
Robert Carr0d480722017-01-10 16:42:54 -080041#include <gui/IGraphicBufferProducer.h>
Valerie Hau9dab9732019-08-20 09:29:25 -070042#include <gui/ITransactionCompletedListener.h>
Peiyong Lind3788632018-09-18 16:01:31 -070043#include <math/mat4.h>
Robert Carr2c358bf2018-08-08 15:58:15 -070044
45#ifndef NO_INPUT
Vishnu Naire798b472020-07-23 13:52:21 -070046#include <android/FocusRequest.h>
Robert Carr2c358bf2018-08-08 15:58:15 -070047#include <input/InputWindow.h>
48#endif
49
chaviw618c42d2020-07-24 15:25:08 -070050#include <gui/ISurfaceComposer.h>
Evan Rosky1f6d6d52018-12-06 10:47:26 -080051#include <gui/LayerMetadata.h>
Pablo Gamito11dcc222020-09-12 15:49:39 +000052#include <gui/SurfaceControl.h>
chaviw13fdc492017-06-27 12:40:18 -070053#include <math/vec3.h>
Lucas Dupinc3800b82020-10-02 16:24:48 -070054#include <ui/BlurRegion.h>
Marissa Wall61c58622018-07-18 10:12:20 -070055#include <ui/GraphicTypes.h>
Marissa Wall11303242018-07-26 13:36:24 -070056#include <ui/Rect.h>
57#include <ui/Region.h>
Dominik Laskowski718f9602019-11-09 20:01:35 -080058#include <ui/Rotation.h>
Vishnu Nair6213bd92020-05-08 17:42:25 -070059#include <ui/Transform.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080060#include <utils/Errors.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062namespace android {
63
64class Parcel;
Mathias Agopian698c0872011-06-28 19:09:31 -070065class ISurfaceComposerClient;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Marissa Wall947d34e2019-03-29 14:03:53 -070067struct client_cache_t {
68 wp<IBinder> token = nullptr;
69 uint64_t id;
70
71 bool operator==(const client_cache_t& other) const { return id == other.id; }
72
73 bool isValid() const { return token != nullptr; }
Marissa Wall78b72202019-03-15 14:58:34 -070074};
75
Andy McFadden4125a4f2014-01-29 17:17:11 -080076/*
77 * Used to communicate layer information between SurfaceFlinger and its clients.
78 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079struct layer_state_t {
Mathias Agopian3165cc22012-08-08 19:42:09 -070080 enum {
chaviwc5676c62020-09-18 15:01:04 -070081 eLayerHidden = 0x01, // SURFACE_HIDDEN in SurfaceControl.java
82 eLayerOpaque = 0x02, // SURFACE_OPAQUE
83 eLayerSkipScreenshot = 0x40, // SKIP_SCREENSHOT
84 eLayerSecure = 0x80, // SECURE
Mathias Agopian3165cc22012-08-08 19:42:09 -070085 };
86
87 enum {
Marissa Wall11303242018-07-26 13:36:24 -070088 ePositionChanged = 0x00000001,
89 eLayerChanged = 0x00000002,
90 eSizeChanged = 0x00000004,
91 eAlphaChanged = 0x00000008,
92 eMatrixChanged = 0x00000010,
93 eTransparentRegionChanged = 0x00000020,
94 eFlagsChanged = 0x00000040,
95 eLayerStackChanged = 0x00000080,
Marissa Wallf58c14b2018-07-24 10:50:43 -070096 eCropChanged_legacy = 0x00000100,
97 eDeferTransaction_legacy = 0x00000200,
Robert Carr916b0362020-10-06 13:53:03 -070098 /* was ScalingModeChanged, now available 0x00000400, */
Vishnu Nairc97b8db2019-10-29 18:19:35 -070099 eShadowRadiusChanged = 0x00000800,
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700100 eReparentChildren = 0x00001000,
101 eDetachChildren = 0x00002000,
102 eRelativeLayerChanged = 0x00004000,
103 eReparent = 0x00008000,
104 eColorChanged = 0x00010000,
105 eDestroySurface = 0x00020000,
106 eTransformChanged = 0x00040000,
107 eTransformToDisplayInverseChanged = 0x00080000,
108 eCropChanged = 0x00100000,
109 eBufferChanged = 0x00200000,
110 eAcquireFenceChanged = 0x00400000,
111 eDataspaceChanged = 0x00800000,
112 eHdrMetadataChanged = 0x01000000,
113 eSurfaceDamageRegionChanged = 0x02000000,
114 eApiChanged = 0x04000000,
115 eSidebandStreamChanged = 0x08000000,
Peiyong Lind3788632018-09-18 16:01:31 -0700116 eColorTransformChanged = 0x10000000,
Marissa Wall3dad52d2019-03-22 14:03:19 -0700117 eHasListenerCallbacksChanged = 0x20000000,
Robert Carr2c358bf2018-08-08 15:58:15 -0700118 eInputInfoChanged = 0x40000000,
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700119 eCornerRadiusChanged = 0x80000000,
Marissa Wall861616d2018-10-22 12:52:23 -0700120 eFrameChanged = 0x1'00000000,
Marissa Wallebc2c052019-01-16 19:16:55 -0800121 eCachedBufferChanged = 0x2'00000000,
Valerie Haudd0b7572019-01-29 14:59:27 -0800122 eBackgroundColorChanged = 0x4'00000000,
123 eMetadataChanged = 0x8'00000000,
Peiyong Linc502cb72019-03-01 15:00:23 -0800124 eColorSpaceAgnosticChanged = 0x10'00000000,
Ana Krulecc84d09b2019-11-02 23:10:29 +0100125 eFrameRateSelectionPriority = 0x20'00000000,
Steven Thomas3172e202020-01-06 19:25:30 -0800126 eFrameRateChanged = 0x40'00000000,
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800127 eBackgroundBlurRadiusChanged = 0x80'00000000,
Valerie Hau871d6352020-01-29 08:44:02 -0800128 eProducerDisconnect = 0x100'00000000,
Vishnu Nair6213bd92020-05-08 17:42:25 -0700129 eFixedTransformHintChanged = 0x200'00000000,
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700130 eFrameNumberChanged = 0x400'00000000,
Robert Carr9b611b72020-10-19 12:00:23 -0700131 eFrameTimelineVsyncChanged = 0x800'00000000,
Lucas Dupinc3800b82020-10-02 16:24:48 -0700132 eBlurRegionsChanged = 0x1000'00000000,
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800133 eAutoRefreshChanged = 0x2000'00000000,
Mathias Agopian3165cc22012-08-08 19:42:09 -0700134 };
135
Pablo Gamito11dcc222020-09-12 15:49:39 +0000136 layer_state_t();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
Robert Carr2c5f6d22017-09-26 12:30:35 -0700138 void merge(const layer_state_t& other);
Marissa Wall11303242018-07-26 13:36:24 -0700139 status_t write(Parcel& output) const;
140 status_t read(const Parcel& input);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141
Marissa Wall11303242018-07-26 13:36:24 -0700142 struct matrix22_t {
143 float dsdx{0};
144 float dtdx{0};
145 float dtdy{0};
146 float dsdy{0};
chaviw308ddba2020-08-11 16:23:51 -0700147 status_t write(Parcel& output) const;
148 status_t read(const Parcel& input);
Marissa Wall11303242018-07-26 13:36:24 -0700149 };
150 sp<IBinder> surface;
Pablo Gamitodbc31672020-09-01 18:28:58 +0000151 int32_t layerId;
Garfield Tan8a3083e2018-12-03 13:21:07 -0800152 uint64_t what;
Marissa Wall11303242018-07-26 13:36:24 -0700153 float x;
154 float y;
155 int32_t z;
156 uint32_t w;
157 uint32_t h;
158 uint32_t layerStack;
159 float alpha;
160 uint8_t flags;
161 uint8_t mask;
162 uint8_t reserved;
163 matrix22_t matrix;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700164 Rect crop_legacy;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700165 float cornerRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800166 uint32_t backgroundBlurRadius;
Pablo Gamito11dcc222020-09-12 15:49:39 +0000167 sp<SurfaceControl> barrierSurfaceControl_legacy;
168 sp<SurfaceControl> reparentSurfaceControl;
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700169 uint64_t barrierFrameNumber;
Robert Carr0d480722017-01-10 16:42:54 -0800170
Pablo Gamito11dcc222020-09-12 15:49:39 +0000171 sp<SurfaceControl> relativeLayerSurfaceControl;
Robert Carrdb66e622017-04-10 16:55:57 -0700172
Pablo Gamito11dcc222020-09-12 15:49:39 +0000173 sp<SurfaceControl> parentSurfaceControlForChild;
chaviw06178942017-07-27 10:25:59 -0700174
Marissa Wall11303242018-07-26 13:36:24 -0700175 half3 color;
chaviw13fdc492017-06-27 12:40:18 -0700176
Marissa Wall11303242018-07-26 13:36:24 -0700177 // non POD must be last. see write/read
178 Region transparentRegion;
Marissa Wall61c58622018-07-18 10:12:20 -0700179
180 uint32_t transform;
181 bool transformToDisplayInverse;
182 Rect crop;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200183 Rect orientedDisplaySpaceRect;
Marissa Wall61c58622018-07-18 10:12:20 -0700184 sp<GraphicBuffer> buffer;
185 sp<Fence> acquireFence;
186 ui::Dataspace dataspace;
187 HdrMetadata hdrMetadata;
188 Region surfaceDamageRegion;
189 int32_t api;
190 sp<NativeHandle> sidebandStream;
Peiyong Lind3788632018-09-18 16:01:31 -0700191 mat4 colorTransform;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700192 std::vector<BlurRegion> blurRegions;
Marissa Wallc837b5e2018-10-12 10:04:44 -0700193
Robert Carr2c358bf2018-08-08 15:58:15 -0700194#ifndef NO_INPUT
Chris Ye0783e992020-06-02 21:34:49 -0700195 sp<InputWindowHandle> inputHandle = new InputWindowHandle();
Robert Carr2c358bf2018-08-08 15:58:15 -0700196#endif
Marissa Wallebc2c052019-01-16 19:16:55 -0800197
Marissa Wall947d34e2019-03-29 14:03:53 -0700198 client_cache_t cachedBuffer;
Valerie Haued54efa2019-01-11 20:03:14 -0800199
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800200 LayerMetadata metadata;
Valerie Haudd0b7572019-01-29 14:59:27 -0800201
202 // The following refer to the alpha, and dataspace, respectively of
203 // the background color layer
204 float bgColorAlpha;
205 ui::Dataspace bgColorDataspace;
Peiyong Linc502cb72019-03-01 15:00:23 -0800206
207 // A color space agnostic layer means the color of this layer can be
208 // interpreted in any color space.
209 bool colorSpaceAgnostic;
Valerie Hau9dab9732019-08-20 09:29:25 -0700210
211 std::vector<ListenerCallbacks> listeners;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700212
213 // Draws a shadow around the surface.
214 float shadowRadius;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100215
216 // Priority of the layer assigned by Window Manager.
217 int32_t frameRateSelectionPriority;
Steven Thomas3172e202020-01-06 19:25:30 -0800218
Steven Thomas62a4cf82020-01-31 12:04:03 -0800219 // Layer frame rate and compatibility. See ANativeWindow_setFrameRate().
Steven Thomas3172e202020-01-06 19:25:30 -0800220 float frameRate;
Steven Thomas62a4cf82020-01-31 12:04:03 -0800221 int8_t frameRateCompatibility;
Marin Shalamanov46084422020-10-13 12:33:42 +0200222 bool shouldBeSeamless;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700223
224 // Set by window manager indicating the layer and all its children are
225 // in a different orientation than the display. The hint suggests that
226 // the graphic producers should receive a transform hint as if the
227 // display was in this orientation. When the display changes to match
228 // the layer orientation, the graphic producer may not need to allocate
229 // a buffer of a different size. -1 means the transform hint is not set,
230 // otherwise the value will be a valid ui::Rotation.
231 ui::Transform::RotationFlags fixedTransformHint;
Vishnu Nair6b7c5c92020-09-29 17:27:05 -0700232
233 // Used by BlastBufferQueue to forward the framenumber generated by the
234 // graphics producer.
235 uint64_t frameNumber;
Robert Carr9b611b72020-10-19 12:00:23 -0700236
237 int64_t frameTimelineVsyncId;
Vishnu Naircf26a0a2020-11-13 12:56:20 -0800238
239 // Indicates that the consumer should acquire the next frame as soon as it
240 // can and not wait for a frame to become available. This is only relevant
241 // in shared buffer mode.
242 bool autoRefresh;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243};
244
Mathias Agopian698c0872011-06-28 19:09:31 -0700245struct ComposerState {
Mathias Agopian698c0872011-06-28 19:09:31 -0700246 layer_state_t state;
Marissa Wall11303242018-07-26 13:36:24 -0700247 status_t write(Parcel& output) const;
248 status_t read(const Parcel& input);
Mathias Agopian698c0872011-06-28 19:09:31 -0700249};
250
Mathias Agopian8b33f032012-07-24 20:43:54 -0700251struct DisplayState {
Mathias Agopian3165cc22012-08-08 19:42:09 -0700252 enum {
Marissa Wall11303242018-07-26 13:36:24 -0700253 eSurfaceChanged = 0x01,
254 eLayerStackChanged = 0x02,
255 eDisplayProjectionChanged = 0x04,
256 eDisplaySizeChanged = 0x08
Mathias Agopiane57f2922012-08-09 16:29:12 -0700257 };
258
Pablo Ceballos60d69222015-08-07 14:47:20 -0700259 DisplayState();
Robert Carr2c5f6d22017-09-26 12:30:35 -0700260 void merge(const DisplayState& other);
Pablo Ceballos60d69222015-08-07 14:47:20 -0700261
Mathias Agopiane57f2922012-08-09 16:29:12 -0700262 uint32_t what;
263 sp<IBinder> token;
Andy McFadden2adaf042012-12-18 09:49:45 -0800264 sp<IGraphicBufferProducer> surface;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700265 uint32_t layerStack;
Chia-I Wuff79ef82018-08-22 15:27:32 -0700266
267 // These states define how layers are projected onto the physical display.
268 //
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200269 // Layers are first clipped to `layerStackSpaceRect'. They are then translated and
270 // scaled from `layerStackSpaceRect' to `orientedDisplaySpaceRect'. Finally, they are rotated
271 // according to `orientation', `width', and `height'.
Chia-I Wuff79ef82018-08-22 15:27:32 -0700272 //
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200273 // For example, assume layerStackSpaceRect is Rect(0, 0, 200, 100), orientedDisplaySpaceRect is
274 // Rect(20, 10, 420, 210), and the size of the display is WxH. When orientation is 0, layers
275 // will be scaled by a factor of 2 and translated by (20, 10). When orientation is 1, layers
276 // will be additionally rotated by 90 degrees around the origin clockwise and translated by (W,
277 // 0).
Dominik Laskowski718f9602019-11-09 20:01:35 -0800278 ui::Rotation orientation = ui::ROTATION_0;
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200279 Rect layerStackSpaceRect;
280 Rect orientedDisplaySpaceRect;
Chia-I Wuff79ef82018-08-22 15:27:32 -0700281
Michael Wright1f6078a2014-06-26 16:01:02 -0700282 uint32_t width, height;
Chia-I Wuff79ef82018-08-22 15:27:32 -0700283
Mathias Agopiane57f2922012-08-09 16:29:12 -0700284 status_t write(Parcel& output) const;
285 status_t read(const Parcel& input);
Mathias Agopian8b33f032012-07-24 20:43:54 -0700286};
287
chaviw273171b2018-12-26 11:46:30 -0800288struct InputWindowCommands {
Vishnu Naire798b472020-07-23 13:52:21 -0700289#ifndef NO_INPUT
290 std::vector<FocusRequest> focusRequests;
291#endif
chaviw291d88a2019-02-14 10:33:58 -0800292 bool syncInputWindows{false};
chaviw273171b2018-12-26 11:46:30 -0800293
Vishnu Naire798b472020-07-23 13:52:21 -0700294 // Merges the passed in commands and returns true if there were any changes.
295 bool merge(const InputWindowCommands& other);
Vishnu Nair958da932020-08-21 17:12:37 -0700296 bool empty() const;
chaviw273171b2018-12-26 11:46:30 -0800297 void clear();
chaviw308ddba2020-08-11 16:23:51 -0700298 status_t write(Parcel& output) const;
299 status_t read(const Parcel& input);
chaviw273171b2018-12-26 11:46:30 -0800300};
301
Marissa Wall11303242018-07-26 13:36:24 -0700302static inline int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
Marissa Wall11303242018-07-26 13:36:24 -0700303 if (lhs.state.surface < rhs.state.surface) return -1;
304 if (lhs.state.surface > rhs.state.surface) return 1;
Robert Carr4cdc58f2017-08-23 14:22:20 -0700305 return 0;
306}
307
Marissa Wall11303242018-07-26 13:36:24 -0700308static inline int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
Robert Carr4cdc58f2017-08-23 14:22:20 -0700309 return compare_type(lhs.token, rhs.token);
310}
311
Steven Thomas62a4cf82020-01-31 12:04:03 -0800312// Returns true if the frameRate and compatibility are valid values, false
313// othwerise. If either of the params are invalid, an error log is printed, and
314// functionName is added to the log to indicate which function call failed.
315// functionName can be null.
316bool ValidateFrameRate(float frameRate, int8_t compatibility, const char* functionName);
317
chaviw618c42d2020-07-24 15:25:08 -0700318struct CaptureArgs {
chaviw4b9d5e12020-08-04 18:30:35 -0700319 const static int32_t UNSET_UID = -1;
chaviw618c42d2020-07-24 15:25:08 -0700320 virtual ~CaptureArgs() = default;
321
322 ui::PixelFormat pixelFormat{ui::PixelFormat::RGBA_8888};
323 Rect sourceCrop;
324 float frameScale{1};
325 bool captureSecureLayers{false};
chaviw4b9d5e12020-08-04 18:30:35 -0700326 int32_t uid{UNSET_UID};
Peiyong Lincd261472020-10-06 18:05:25 -0700327 // Force capture to be in a color space. If the value is ui::Dataspace::UNKNOWN, the captured
328 // result will be in the display's colorspace.
arthurhung79e81aa2020-08-28 00:09:19 +0800329 // The display may use non-RGB dataspace (ex. displayP3) that could cause pixel data could be
Peiyong Lincd261472020-10-06 18:05:25 -0700330 // different from SRGB (byte per color), and failed when checking colors in tests.
331 // NOTE: In normal cases, we want the screen to be captured in display's colorspace.
332 ui::Dataspace dataspace = ui::Dataspace::UNKNOWN;
chaviw618c42d2020-07-24 15:25:08 -0700333
Peiyong Lin05cc0112020-10-14 16:16:37 -0700334 // The receiver of the capture can handle protected buffer. A protected buffer has
335 // GRALLOC_USAGE_PROTECTED usage bit and must not be accessed unprotected behaviour.
336 // Any read/write access from unprotected context will result in undefined behaviour.
337 // Protected contents are typically DRM contents. This has no direct implication to the
338 // secure property of the surface, which is specified by the application explicitly to avoid
339 // the contents being accessed/captured by screenshot or unsecure display.
340 bool allowProtected = false;
341
chaviw618c42d2020-07-24 15:25:08 -0700342 virtual status_t write(Parcel& output) const;
343 virtual status_t read(const Parcel& input);
344};
345
346struct DisplayCaptureArgs : CaptureArgs {
347 sp<IBinder> displayToken;
348 uint32_t width{0};
349 uint32_t height{0};
350 bool useIdentityTransform{false};
chaviw618c42d2020-07-24 15:25:08 -0700351
352 status_t write(Parcel& output) const override;
353 status_t read(const Parcel& input) override;
354};
355
356struct LayerCaptureArgs : CaptureArgs {
357 sp<IBinder> layerHandle;
358 std::unordered_set<sp<IBinder>, ISurfaceComposer::SpHash<IBinder>> excludeHandles;
chaviw70cb6a42020-07-30 13:57:36 -0700359 bool childrenOnly{false};
chaviw618c42d2020-07-24 15:25:08 -0700360
361 status_t write(Parcel& output) const override;
362 status_t read(const Parcel& input) override;
363};
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}; // namespace android
366
Mathias Agopian9cce3252010-02-09 17:46:37 -0800367#endif // ANDROID_SF_LAYER_STATE_H