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