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