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/gui/DisplayCaptureArgs.h>
25 #include <android/gui/IWindowInfosReportedListener.h>
26 #include <android/gui/LayerCaptureArgs.h>
27 #include <android/gui/TrustedPresentationThresholds.h>
28 #include <android/native_window.h>
29 #include <gui/DisplayLuts.h>
30 #include <gui/IGraphicBufferProducer.h>
31 #include <gui/ITransactionCompletedListener.h>
32 #include <math/mat4.h>
33
34 #include <android/gui/DropInputMode.h>
35 #include <android/gui/EdgeExtensionParameters.h>
36 #include <android/gui/FocusRequest.h>
37 #include <android/gui/TrustedOverlay.h>
38
39 #include <ftl/flags.h>
40 #include <gui/BufferReleaseChannel.h>
41 #include <gui/ISurfaceComposer.h>
42 #include <gui/LayerMetadata.h>
43 #include <gui/SpHash.h>
44 #include <gui/SurfaceControl.h>
45 #include <gui/WindowInfo.h>
46 #include <math/vec3.h>
47 #include <ui/BlurRegion.h>
48 #include <ui/GraphicTypes.h>
49 #include <ui/LayerStack.h>
50 #include <ui/PictureProfileHandle.h>
51 #include <ui/Rect.h>
52 #include <ui/Region.h>
53 #include <ui/Rotation.h>
54 #include <ui/StretchEffect.h>
55 #include <ui/Transform.h>
56 #include <utils/Errors.h>
57
58 namespace android {
59
60 class Parcel;
61
62 using gui::ISurfaceComposerClient;
63 using gui::LayerMetadata;
64
65 using gui::TrustedPresentationThresholds;
66
67 struct 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
isValidclient_cache_t73 bool isValid() const { return token != nullptr; }
74 };
75
76 class TrustedPresentationListener : public Parcelable {
77 public:
78 sp<ITransactionCompletedListener> callbackInterface;
79 int callbackId = -1;
80
invoke(bool presentedWithinThresholds)81 void invoke(bool presentedWithinThresholds) {
82 callbackInterface->onTrustedPresentationChanged(callbackId, presentedWithinThresholds);
83 }
84
85 status_t writeToParcel(Parcel* parcel) const;
86 status_t readFromParcel(const Parcel* parcel);
87 };
88
89 class BufferData : public Parcelable {
90 public:
91 virtual ~BufferData() = default;
hasBuffer()92 virtual bool hasBuffer() const { return buffer != nullptr; }
hasSameBuffer(const BufferData & other)93 virtual bool hasSameBuffer(const BufferData& other) const {
94 return buffer == other.buffer && frameNumber == other.frameNumber;
95 }
getWidth()96 virtual uint32_t getWidth() const { return buffer->getWidth(); }
getHeight()97 virtual uint32_t getHeight() const { return buffer->getHeight(); }
getBounds()98 Rect getBounds() const {
99 return {0, 0, static_cast<int32_t>(getWidth()), static_cast<int32_t>(getHeight())};
100 }
getId()101 virtual uint64_t getId() const { return buffer->getId(); }
getPixelFormat()102 virtual PixelFormat getPixelFormat() const { return buffer->getPixelFormat(); }
getUsage()103 virtual uint64_t getUsage() const { return buffer->getUsage(); }
104
105 enum class BufferDataChange : uint32_t {
106 fenceChanged = 0x01,
107 frameNumberChanged = 0x02,
108 cachedBufferChanged = 0x04,
109 };
110
111 sp<GraphicBuffer> buffer;
112 sp<Fence> acquireFence;
113
114 // Used by BlastBufferQueue to forward the framenumber generated by the
115 // graphics producer.
116 uint64_t frameNumber = 0;
117 bool hasBarrier = false;
118 uint64_t barrierFrameNumber = 0;
119 uint32_t producerId = 0;
120
121 // Listens to when the buffer is safe to be released. This is used for blast
122 // layers only. The callback includes a release fence as well as the graphic
123 // buffer id to identify the buffer.
124 sp<ITransactionCompletedListener> releaseBufferListener = nullptr;
125
126 // Stores which endpoint the release information should be sent to. We don't want to send the
127 // releaseCallbackId and release fence to all listeners so we store which listener the setBuffer
128 // was called with.
129 sp<IBinder> releaseBufferEndpoint;
130
131 ftl::Flags<BufferDataChange> flags;
132
133 client_cache_t cachedBuffer;
134
135 nsecs_t dequeueTime;
136
137 // Generates the release callback id based on the buffer id and frame number.
138 // This is used as an identifier when release callbacks are invoked.
139 ReleaseCallbackId generateReleaseCallbackId() const;
140
141 status_t writeToParcel(Parcel* parcel) const override;
142 status_t readFromParcel(const Parcel* parcel) override;
143 };
144
145 /*
146 * Used to communicate layer information between SurfaceFlinger and its clients.
147 */
148 struct layer_state_t {
149 enum Permission {
150 ACCESS_SURFACE_FLINGER = 0x1,
151 ROTATE_SURFACE_FLINGER = 0x2,
152 INTERNAL_SYSTEM_WINDOW = 0x4,
153 };
154
155 enum {
156 eLayerHidden = 0x01, // SURFACE_HIDDEN in SurfaceControl.java
157 eLayerOpaque = 0x02, // SURFACE_OPAQUE
158 eLayerSkipScreenshot = 0x40, // SKIP_SCREENSHOT
159 eLayerSecure = 0x80, // SECURE
160 // Queue up layer buffers instead of dropping the oldest buffer when this flag is
161 // set. This blocks the client until all the buffers have been presented. If the buffers
162 // have presentation timestamps, then we may drop buffers.
163 eEnableBackpressure = 0x100, // ENABLE_BACKPRESSURE
164 eLayerIsDisplayDecoration = 0x200, // DISPLAY_DECORATION
165 // Ignore any destination frame set on the layer. This is used when the buffer scaling mode
166 // is freeze and the destination frame is applied asynchronously with the buffer submission.
167 // This is needed to maintain compatibility for SurfaceView scaling behavior.
168 // See SurfaceView scaling behavior for more details.
169 eIgnoreDestinationFrame = 0x400,
170 eLayerIsRefreshRateIndicator = 0x800, // REFRESH_RATE_INDICATOR
171 // Sets a property on this layer indicating that its visible region should be considered
172 // when computing TrustedPresentation Thresholds.
173 eCanOccludePresentation = 0x1000,
174 // Indicates that the SurfaceControl should recover from buffer stuffing when
175 // possible. This is the case when the SurfaceControl is the root SurfaceControl
176 // owned by ViewRootImpl.
177 eRecoverableFromBufferStuffing = 0x2000,
178 };
179
180 enum {
181 ePositionChanged = 0x00000001,
182 eLayerChanged = 0x00000002,
183 eTrustedPresentationInfoChanged = 0x00000004,
184 eAlphaChanged = 0x00000008,
185 eMatrixChanged = 0x00000010,
186 eTransparentRegionChanged = 0x00000020,
187 eFlagsChanged = 0x00000040,
188 eLayerStackChanged = 0x00000080,
189 eFlushJankData = 0x00000100,
190 eCachingHintChanged = 0x00000200,
191 eDimmingEnabledChanged = 0x00000400,
192 eShadowRadiusChanged = 0x00000800,
193 eLutsChanged = 0x00001000,
194 eBufferCropChanged = 0x00002000,
195 eRelativeLayerChanged = 0x00004000,
196 eReparent = 0x00008000,
197 eColorChanged = 0x00010000,
198 eFrameRateCategoryChanged = 0x00020000,
199 eBufferTransformChanged = 0x00040000,
200 eTransformToDisplayInverseChanged = 0x00080000,
201 eCropChanged = 0x00100000,
202 eBufferChanged = 0x00200000,
203 eDefaultFrameRateCompatibilityChanged = 0x00400000,
204 eDataspaceChanged = 0x00800000,
205 eHdrMetadataChanged = 0x01000000,
206 eSurfaceDamageRegionChanged = 0x02000000,
207 eApiChanged = 0x04000000,
208 eSidebandStreamChanged = 0x08000000,
209 eColorTransformChanged = 0x10000000,
210 eHasListenerCallbacksChanged = 0x20000000,
211 eInputInfoChanged = 0x40000000,
212 eCornerRadiusChanged = 0x80000000,
213 eDestinationFrameChanged = 0x1'00000000,
214 eFrameRateSelectionStrategyChanged = 0x2'00000000,
215 eBackgroundColorChanged = 0x4'00000000,
216 eMetadataChanged = 0x8'00000000,
217 eColorSpaceAgnosticChanged = 0x10'00000000,
218 eFrameRateSelectionPriority = 0x20'00000000,
219 eFrameRateChanged = 0x40'00000000,
220 eBackgroundBlurRadiusChanged = 0x80'00000000,
221 eProducerDisconnect = 0x100'00000000,
222 eFixedTransformHintChanged = 0x200'00000000,
223 eDesiredHdrHeadroomChanged = 0x400'00000000,
224 eBlurRegionsChanged = 0x800'00000000,
225 eAutoRefreshChanged = 0x1000'00000000,
226 eStretchChanged = 0x2000'00000000,
227 eTrustedOverlayChanged = 0x4000'00000000,
228 eDropInputModeChanged = 0x8000'00000000,
229 eExtendedRangeBrightnessChanged = 0x10000'00000000,
230 eEdgeExtensionChanged = 0x20000'00000000,
231 eBufferReleaseChannelChanged = 0x40000'00000000,
232 ePictureProfileHandleChanged = 0x80000'00000000,
233 eAppContentPriorityChanged = 0x100000'00000000,
234 };
235
236 layer_state_t();
237
238 void merge(const layer_state_t& other);
239 status_t write(Parcel& output) const;
240 status_t read(const Parcel& input);
241 // Compares two layer_state_t structs and returns a set of change flags describing all the
242 // states that are different.
243 uint64_t diff(const layer_state_t& other) const;
244 bool hasBufferChanges() const;
245
246 // Layer hierarchy updates.
247 static constexpr uint64_t HIERARCHY_CHANGES = layer_state_t::eLayerChanged |
248 layer_state_t::eRelativeLayerChanged | layer_state_t::eReparent |
249 layer_state_t::eLayerStackChanged;
250
251 // Geometry updates.
252 static constexpr uint64_t GEOMETRY_CHANGES = layer_state_t::eBufferCropChanged |
253 layer_state_t::eBufferTransformChanged | layer_state_t::eCornerRadiusChanged |
254 layer_state_t::eCropChanged | layer_state_t::eDestinationFrameChanged |
255 layer_state_t::eMatrixChanged | layer_state_t::ePositionChanged |
256 layer_state_t::eTransformToDisplayInverseChanged |
257 layer_state_t::eTransparentRegionChanged | layer_state_t::eEdgeExtensionChanged;
258
259 // Buffer and related updates.
260 static constexpr uint64_t BUFFER_CHANGES = layer_state_t::eApiChanged |
261 layer_state_t::eBufferChanged | layer_state_t::eBufferCropChanged |
262 layer_state_t::eBufferTransformChanged | layer_state_t::eDataspaceChanged |
263 layer_state_t::eSidebandStreamChanged | layer_state_t::eSurfaceDamageRegionChanged |
264 layer_state_t::eTransformToDisplayInverseChanged |
265 layer_state_t::eTransparentRegionChanged |
266 layer_state_t::eExtendedRangeBrightnessChanged |
267 layer_state_t::eDesiredHdrHeadroomChanged | layer_state_t::eLutsChanged;
268
269 // Content updates.
270 static constexpr uint64_t CONTENT_CHANGES = layer_state_t::BUFFER_CHANGES |
271 layer_state_t::eAlphaChanged | layer_state_t::eAutoRefreshChanged |
272 layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBackgroundColorChanged |
273 layer_state_t::eBlurRegionsChanged | layer_state_t::eColorChanged |
274 layer_state_t::eColorSpaceAgnosticChanged | layer_state_t::eColorTransformChanged |
275 layer_state_t::eCornerRadiusChanged | layer_state_t::eDimmingEnabledChanged |
276 layer_state_t::eHdrMetadataChanged | layer_state_t::eShadowRadiusChanged |
277 layer_state_t::eStretchChanged | layer_state_t::ePictureProfileHandleChanged |
278 layer_state_t::eAppContentPriorityChanged;
279
280 // Changes which invalidates the layer's visible region in CE.
281 static constexpr uint64_t CONTENT_DIRTY = layer_state_t::CONTENT_CHANGES |
282 layer_state_t::GEOMETRY_CHANGES | layer_state_t::HIERARCHY_CHANGES;
283
284 // Changes affecting child states.
285 static constexpr uint64_t AFFECTS_CHILDREN = layer_state_t::GEOMETRY_CHANGES |
286 layer_state_t::HIERARCHY_CHANGES | layer_state_t::eAlphaChanged |
287 layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged |
288 layer_state_t::eColorTransformChanged | layer_state_t::eCornerRadiusChanged |
289 layer_state_t::eFlagsChanged | layer_state_t::eTrustedOverlayChanged |
290 layer_state_t::eFrameRateChanged | layer_state_t::eFrameRateCategoryChanged |
291 layer_state_t::eFrameRateSelectionStrategyChanged |
292 layer_state_t::eFrameRateSelectionPriority | layer_state_t::eFixedTransformHintChanged;
293
294 // Changes affecting data sent to input.
295 static constexpr uint64_t INPUT_CHANGES = layer_state_t::eAlphaChanged |
296 layer_state_t::eInputInfoChanged | layer_state_t::eDropInputModeChanged |
297 layer_state_t::eTrustedOverlayChanged | layer_state_t::eLayerStackChanged;
298
299 // Changes that affect the visible region on a display.
300 static constexpr uint64_t VISIBLE_REGION_CHANGES = layer_state_t::GEOMETRY_CHANGES |
301 layer_state_t::HIERARCHY_CHANGES | layer_state_t::eAlphaChanged;
302
303 bool hasValidBuffer() const;
304 void sanitize(int32_t permissions);
305
306 struct matrix22_t {
307 float dsdx{0};
308 float dtdx{0};
309 float dtdy{0};
310 float dsdy{0};
311 status_t write(Parcel& output) const;
312 status_t read(const Parcel& input);
313 inline bool operator==(const matrix22_t& other) const {
314 return std::tie(dsdx, dtdx, dtdy, dsdy) ==
315 std::tie(other.dsdx, other.dtdx, other.dtdy, other.dsdy);
316 }
317 inline bool operator!=(const matrix22_t& other) const { return !(*this == other); }
318 };
319 sp<IBinder> surface;
320 int32_t layerId;
321 uint64_t what;
322 float x;
323 float y;
324 int32_t z;
325 ui::LayerStack layerStack = ui::DEFAULT_LAYER_STACK;
326 uint32_t flags;
327 uint32_t mask;
328 uint8_t reserved;
329 matrix22_t matrix;
330 float cornerRadius;
331 uint32_t backgroundBlurRadius;
332
333 sp<SurfaceControl> relativeLayerSurfaceControl;
334
335 sp<SurfaceControl> parentSurfaceControlForChild;
336
337 half4 color;
338
339 // non POD must be last. see write/read
340 Region transparentRegion;
341 uint32_t bufferTransform;
342 bool transformToDisplayInverse;
343 FloatRect crop;
344 std::shared_ptr<BufferData> bufferData = nullptr;
345 ui::Dataspace dataspace;
346 HdrMetadata hdrMetadata;
347 Region surfaceDamageRegion;
348 int32_t api;
349 sp<NativeHandle> sidebandStream;
350 mat4 colorTransform;
351 std::vector<BlurRegion> blurRegions;
352
353 sp<gui::WindowInfoHandle> windowInfoHandle = sp<gui::WindowInfoHandle>::make();
354
355 LayerMetadata metadata;
356
357 // The following refer to the alpha, and dataspace, respectively of
358 // the background color layer
359 half4 bgColor;
360 ui::Dataspace bgColorDataspace;
361
362 // A color space agnostic layer means the color of this layer can be
363 // interpreted in any color space.
364 bool colorSpaceAgnostic;
365
366 std::vector<ListenerCallbacks> listeners;
367
368 // Draws a shadow around the surface.
369 float shadowRadius;
370
371 // Priority of the layer assigned by Window Manager.
372 int32_t frameRateSelectionPriority;
373
374 // Layer frame rate and compatibility. See ANativeWindow_setFrameRate().
375 float frameRate;
376 int8_t frameRateCompatibility;
377 int8_t changeFrameRateStrategy;
378
379 // Default frame rate compatibility used to set the layer refresh rate votetype.
380 int8_t defaultFrameRateCompatibility;
381
382 // Frame rate category to suggest what frame rate range a surface should run.
383 int8_t frameRateCategory;
384 bool frameRateCategorySmoothSwitchOnly;
385
386 // Strategy of the layer for frame rate selection.
387 int8_t frameRateSelectionStrategy;
388
389 // Set by window manager indicating the layer and all its children are
390 // in a different orientation than the display. The hint suggests that
391 // the graphic producers should receive a transform hint as if the
392 // display was in this orientation. When the display changes to match
393 // the layer orientation, the graphic producer may not need to allocate
394 // a buffer of a different size. -1 means the transform hint is not set,
395 // otherwise the value will be a valid ui::Rotation.
396 ui::Transform::RotationFlags fixedTransformHint;
397
398 // Indicates that the consumer should acquire the next frame as soon as it
399 // can and not wait for a frame to become available. This is only relevant
400 // in shared buffer mode.
401 bool autoRefresh;
402
403 // An inherited state that indicates that this surface control and its children
404 // should be trusted for input occlusion detection purposes
405 gui::TrustedOverlay trustedOverlay;
406
407 // Stretch effect to be applied to this layer
408 StretchEffect stretchEffect;
409
410 // Edge extension effect to be applied to this layer
411 gui::EdgeExtensionParameters edgeExtensionParameters;
412
413 Rect bufferCrop;
414 Rect destinationFrame;
415
416 // Force inputflinger to drop all input events for the layer and its children.
417 gui::DropInputMode dropInputMode;
418
419 bool dimmingEnabled;
420 float currentHdrSdrRatio = 1.f;
421 float desiredHdrSdrRatio = 1.f;
422
423 // Enhance the quality of the buffer contents by configurating a picture processing pipeline
424 // with values as specified by this picture profile.
425 PictureProfileHandle pictureProfileHandle{PictureProfileHandle::NONE};
426
427 // A value indicating the significance of the layer's content to the app's desired user
428 // experience. A lower priority will result in more likelihood of getting access to limited
429 // resources, such as picture processing hardware.
430 int32_t appContentPriority = 0;
431
432 gui::CachingHint cachingHint = gui::CachingHint::Enabled;
433
434 TrustedPresentationThresholds trustedPresentationThresholds;
435 TrustedPresentationListener trustedPresentationListener;
436
437 std::shared_ptr<gui::BufferReleaseChannel::ProducerEndpoint> bufferReleaseChannel;
438
439 std::shared_ptr<gui::DisplayLuts> luts;
440 };
441
442 class ComposerState {
443 public:
444 layer_state_t state;
445 status_t write(Parcel& output) const;
446 status_t read(const Parcel& input);
447 };
448
449 struct DisplayState {
450 enum : uint32_t {
451 eSurfaceChanged = 0x01,
452 eLayerStackChanged = 0x02,
453 eDisplayProjectionChanged = 0x04,
454 eDisplaySizeChanged = 0x08,
455 eFlagsChanged = 0x10,
456
457 eAllChanged = ~0u
458 };
459
460 // Not for direct use. Prefer constructor below for new displays.
461 DisplayState();
462
DisplayStateDisplayState463 DisplayState(sp<IBinder> token, ui::LayerStack layerStack)
464 : what(eAllChanged),
465 token(std::move(token)),
466 layerStack(layerStack),
467 layerStackSpaceRect(Rect::INVALID_RECT),
468 orientedDisplaySpaceRect(Rect::INVALID_RECT) {}
469
470 void merge(const DisplayState& other);
471 void sanitize(int32_t permissions);
472
473 uint32_t what = 0;
474 uint32_t flags = 0;
475 sp<IBinder> token;
476
477 ui::LayerStack layerStack = ui::DEFAULT_LAYER_STACK;
478
479 // These states define how layers are projected onto the physical or virtual display.
480 //
481 // Layers are first clipped to `layerStackSpaceRect'. They are then translated and
482 // scaled from `layerStackSpaceRect' to `orientedDisplaySpaceRect'. Finally, they are rotated
483 // according to `orientation', `width', and `height'.
484 //
485 // For example, assume layerStackSpaceRect is Rect(0, 0, 200, 100), orientedDisplaySpaceRect is
486 // Rect(20, 10, 420, 210), and the size of the display is WxH. When orientation is 0, layers
487 // will be scaled by a factor of 2 and translated by (20, 10). When orientation is 1, layers
488 // will be additionally rotated by 90 degrees around the origin clockwise and translated by (W,
489 // 0).
490 //
491 // Rect::INVALID_RECT sizes the space to the active resolution of the physical display, or the
492 // default dimensions of the virtual display surface.
493 //
494 ui::Rotation orientation = ui::ROTATION_0;
495 Rect layerStackSpaceRect = Rect::EMPTY_RECT;
496 Rect orientedDisplaySpaceRect = Rect::EMPTY_RECT;
497
498 // Exclusive to virtual displays: The sink surface into which the virtual display is rendered,
499 // and an optional resolution that overrides its default dimensions.
500 sp<IGraphicBufferProducer> surface;
501 uint32_t width = 0;
502 uint32_t height = 0;
503
504 status_t write(Parcel& output) const;
505 status_t read(const Parcel& input);
506 };
507
508 struct InputWindowCommands {
509 std::vector<gui::FocusRequest> focusRequests;
510 std::unordered_set<sp<gui::IWindowInfosReportedListener>,
511 SpHash<gui::IWindowInfosReportedListener>>
512 windowInfosReportedListeners;
513
514 // Merges the passed in commands and returns true if there were any changes.
515 bool merge(const InputWindowCommands& other);
516 bool empty() const;
517 void clear();
518 status_t write(Parcel& output) const;
519 status_t read(const Parcel& input);
520 };
521
compare_type(const ComposerState & lhs,const ComposerState & rhs)522 static inline int compare_type(const ComposerState& lhs, const ComposerState& rhs) {
523 if (lhs.state.surface < rhs.state.surface) return -1;
524 if (lhs.state.surface > rhs.state.surface) return 1;
525 return 0;
526 }
527
compare_type(const DisplayState & lhs,const DisplayState & rhs)528 static inline int compare_type(const DisplayState& lhs, const DisplayState& rhs) {
529 return compare_type(lhs.token, rhs.token);
530 }
531
532 }; // namespace android
533
534 #endif // ANDROID_SF_LAYER_STATE_H
535