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