1 /*
2 * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted
5 * provided that the following conditions are met:
6 *    * Redistributions of source code must retain the above copyright notice, this list of
7 *      conditions and the following disclaimer.
8 *    * Redistributions in binary form must reproduce the above copyright notice, this list of
9 *      conditions and the following disclaimer in the documentation and/or other materials provided
10 *      with the distribution.
11 *    * Neither the name of The Linux Foundation nor the names of its contributors may be used to
12 *      endorse or promote products derived from this software without specific prior written
13 *      permission.
14 *
15 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 /*! @file layer_stack.h
26   @brief File for display layer stack structure which represents a drawing buffer.
27 
28   @details Display layer is a drawing buffer object which will be blended with other drawing buffers
29   under blending rules.
30 */
31 #ifndef __LAYER_STACK_H__
32 #define __LAYER_STACK_H__
33 
34 #include <stdint.h>
35 #include <utils/constants.h>
36 #include <utils/fence.h>
37 
38 #include <vector>
39 #include <utility>
40 #include <unordered_map>
41 #include <memory>
42 #include <bitset>
43 
44 #include "layer_buffer.h"
45 #include "sdm_types.h"
46 
47 namespace sdm {
48 
49 /*! @brief This enum represents display layer blending types.
50 
51   @sa Layer
52 */
53 enum LayerBlending {
54   kBlendingPremultiplied,   //!< Pixel color is expressed using premultiplied alpha in RGBA tuples.
55                             //!< If plane alpha is less than 0xFF, apply modulation as well.
56                             //!<   pixel.rgb = src.rgb + dest.rgb x (1 - src.a)
57 
58   kBlendingOpaque,          //!< Pixel color is expressed using straight alpha in color tuples. It
59                             //!< is constant blend operation. The layer would appear opaque if plane
60                             //!< alpha is 0xFF.
61 
62   kBlendingCoverage,        //!< Pixel color is expressed using straight alpha in color tuples. If
63                             //!< plane alpha is less than 0xff, apply modulation as well.
64                             //!<   pixel.rgb = src.rgb x src.a + dest.rgb x (1 - src.a)
65 };
66 
67 /*! @brief This enum represents display layer composition types.
68 
69   @sa Layer
70 */
71 enum LayerComposition {
72   /* ==== List of composition types set by SDM === */
73   /* These composition types represent SDM composition decision for the layers which need to
74      be blended. Composition types are set during Prepare() by SDM.
75      Client can set default composition type to any of the below before calling into Prepare(),
76      however client's input value is ignored and does not play any role in composition decision.
77   */
78   kCompositionGPU,          //!< This layer will be drawn onto the target buffer by GPU. Display
79                             //!< device will mark the layer for GPU composition if it can not
80                             //!< handle composition for it.
81                             //!< This composition type is used only if GPUTarget layer is provided
82                             //!< in a composition cycle.
83 
84   kCompositionStitch,       //!< This layer will be drawn onto the target buffer by GPU. No blend
85                             //!< required.
86 
87   kCompositionSDE,          //!< This layer will be composed by SDE. It must not be composed by
88                             //!< GPU or Blit.
89 
90   kCompositionCursor,       // This cursor layer can receive async position updates irrespective of
91                             // dedicated h/w cursor usage. It must not be composed by GPU or Blit
92 
93   kCompositionNone,         //!< This layer will not be composed by any hardware.
94 
95   /* === List of composition types set by Client === */
96   /* These composition types represent target buffer layers onto which GPU or Blit will draw if SDM
97      decide to have some or all layers drawn by respective composition engine.
98      Client must provide a target buffer layer, if respective composition type is not disabled by
99      an explicit call to SetCompositionState() method. If a composition type is not disabled,
100      providing a target buffer layer is optional. If SDM is unable to handle layers without support
101      of such a composition engine, Prepare() call will return failure.
102   */
103   kCompositionGPUTarget,    //!< This layer will hold result of composition for layers marked for
104                             //!< GPU composition.
105                             //!< If display device does not set any layer for GPU composition then
106                             //!< this layer would be ignored. Else, this layer will be composed
107                             //!< with other layers marked for SDE composition by SDE.
108                             //!< Only one layer shall be marked as target buffer by the caller.
109                             //!< GPU target layer shall be placed after all application layers
110                             //!< in the layer stack.
111 
112   kCompositionStitchTarget,  //!< This layer will hold result of composition for layers marked fo
113                              //!< Blit composition.
114 };
115 
116 enum LayerUpdate {
117   kSecurity,
118   kMetadataUpdate,
119   kSurfaceDamage,
120   kSurfaceInvalidate,
121   kClientCompRequest,
122   kColorTransformUpdate,
123   kLayerUpdateMax,
124 };
125 
126 /*! @brief This structure defines rotation and flip values for a display layer.
127 
128   @sa Layer
129 */
130 struct LayerTransform {
131   float rotation = 0.0f;  //!< Left most pixel coordinate.
132   bool flip_horizontal = false;  //!< Mirror reversal of the layer across a horizontal axis.
133   bool flip_vertical = false;  //!< Mirror reversal of the layer across a vertical axis.
134 
135   bool operator==(const LayerTransform& transform) const {
136     return (rotation == transform.rotation && flip_horizontal == transform.flip_horizontal &&
137             flip_vertical == transform.flip_vertical);
138   }
139 
140   bool operator!=(const LayerTransform& transform) const {
141     return !operator==(transform);
142   }
143 };
144 
145 /*! @brief This structure defines flags associated with a layer. The 1-bit flag can be set to ON(1)
146   or OFF(0).
147 
148   @sa LayerBuffer
149 */
150 struct LayerFlags {
151   union {
152     struct {
153       uint32_t skip : 1;      //!< This flag shall be set by client to indicate that this layer
154                               //!< will be handled by GPU. Display Device will not consider it
155                               //!< for composition.
156 
157       uint32_t updating : 1;  //!< This flag shall be set by client to indicate that this is
158                               //!< updating non-updating. so strategy manager will mark them for
159                               //!< SDE/GPU composition respectively when the layer stack qualifies
160                               //!< for cache based composition.
161 
162       uint32_t solid_fill : 1;
163                               //!< This flag shall be set by client to indicate that this layer
164                               //!< is for solid fill without input buffer. Display Device will
165                               //!< use SDE HW feature to achieve it.
166 
167       uint32_t cursor : 1;    //!< This flag shall be set by client to indicate that this layer
168                               //!< is a cursor
169                               //!< Display Device may handle this layer using HWCursor
170 
171       uint32_t single_buffer : 1;  //!< This flag shall be set by client to indicate that the layer
172                                    //!< uses only a single buffer that will not be swapped out
173 
174       uint32_t color_transform : 1;  //!< This flag will be set by SDM when the layer
175                                      //!< has a custom matrix
176 
177       uint32_t is_game : 1;  //!< This flag shall be set by client to indicate that this layer
178                              //!< is a game layer.
179 
180       uint32_t sde_preferred : 1;  //! This flag shall be set by client to indicate that this layer
181                                    //! will be composed by display device, layer with this flag
182                                    //! will have highest priority. To be used by OEMs only.
183     };
184 
185     uint32_t flags = 0;       //!< For initialization purpose only.
186                               //!< Client shall not refer it directly.
187   };
188 };
189 
190 /*! @brief This structure defines flags associated with the layer requests. The 1-bit flag can be
191     set to ON(1) or OFF(0).
192 
193   @sa Layer
194 */
195 struct LayerRequestFlags {
196   union {
197     struct {
198       uint32_t tone_map : 1;  //!< This flag will be set by SDM when the layer needs tone map
199       uint32_t secure: 1;  //!< This flag will be set by SDM when the layer must be secure
200       uint32_t flip_buffer: 1;  //!< This flag will be set by SDM when the layer needs FBT flip
201       uint32_t dest_tone_map : 1;  //!< This flag will be set by SDM when the layer needs
202                                    //!< destination tone map
203       uint32_t src_tone_map: 1;    //!< This flag will be set by SDM when the layer needs
204                                    //!< source tone map.
205       uint32_t rc: 1;  //!< This flag will be set by SDM when the layer is drawn by RC HW.
206       uint32_t update_format: 1;   //!< This flag will be set by SDM when layer format is updated
207                                    //!< The buffer format is mentioned in the LayerRequest Format
208       uint32_t update_color_metadata: 1;   //!< This flag will be set by SDM when layer color
209                                            //!< metadata is updated. The color metadata is
210                                            //!< mentioned in the LayerRequest Format
211     };
212     uint32_t request_flags = 0;  //!< For initialization purpose only.
213                                  //!< Shall not be refered directly.
214   };
215 };
216 
217 /*! @brief This structure defines LayerRequest.
218    Includes width/height/format of the LayerRequest.
219 
220    SDM shall set the properties of LayerRequest to be used by the client
221 
222   @sa LayerRequest
223 */
224 struct LayerRequest {
225   LayerRequestFlags flags;  // Flags associated with this request
226   LayerBufferFormat format = kFormatRGBA8888;  // Requested format - Used with tone_map and
227                                                // update_format flags
228   ColorMetaData color_metadata = { .colorPrimaries = ColorPrimaries_BT709_5,
229                                    .range = Range_Full,
230                                    .transfer = Transfer_sRGB };
231                                   // Requested color metadata
232   uint32_t width = 0;   // Requested unaligned width.
233                         // Used with tone_map flag
234   uint32_t height = 0;  // Requested unalighed height
235                         // Used with tone_map flag
236 };
237 
238 /*! @brief This structure defines flags associated with a layer stack. The 1-bit flag can be set to
239   ON(1) or OFF(0).
240 
241   @sa LayerBuffer
242 */
243 struct LayerStackFlags {
244   union {
245     struct {
246       uint32_t geometry_changed : 1;  //!< This flag shall be set by client to indicate that the
247                                       //!< layer set passed to Prepare() has changed by more than
248                                       //!< just the buffer handles and acquire fences.
249 
250       uint32_t skip_present : 1;      //!< This flag will be set to true, if the current layer
251                                       //!< stack contains skip layers.
252 
253       uint32_t video_present : 1;     //!< This flag will be set to true, if current layer stack
254                                       //!< contains video.
255 
256       uint32_t secure_present : 1;    //!< This flag will be set to true, if the current layer
257                                       //!< stack contains secure layers.
258 
259       uint32_t animating : 1;         //!< This flag shall be set by client to indicate that the
260                                       //!<  current frame is animating.i
261 
262       uint32_t attributes_changed : 1;
263                                       //!< This flag shall be set by client to indicate that the
264                                       //!< current frame has some properties changed and
265                                       //!< needs re-config.
266 
267       uint32_t cursor_present : 1;    //!< This flag will be set to true if the current layer
268                                       //!< stack contains cursor layer.
269 
270       uint32_t single_buffered_layer_present : 1;    //!< Set if stack has single buffered layer
271 
272       uint32_t s3d_mode_present : 1;  //!< This flag will be set to true, if the current layer
273                                       //!< stack contains s3d layer, and the layer stack can enter
274                                       //!< s3d mode.
275 
276       uint32_t post_processed_output : 1;  // If output_buffer should contain post processed output
277                                            // This applies only to primary displays currently
278 
279       uint32_t hdr_present : 1;  //!< Set if stack has HDR content
280 
281       uint32_t fast_path : 1;    //!< Preference for fast/slow path draw-cycle, set by client.
282 
283       uint32_t mask_present : 1;  //!< Set if layer stack has mask layers.
284 
285       uint32_t config_changed : 1;  //!< This flag indicates Display config must be validated.
286 
287       uint32_t scaling_rgb_layer_present : 1; //!< This flag indicates scaling rgb layer presense
288     };
289 
290     uint32_t flags = 0;               //!< For initialization purpose only.
291                                       //!< Client shall not refer it directly.
292   };
293 };
294 
295 /*! @brief This structure defines a rectanglular area inside a display layer.
296 
297   @sa LayerRectArray
298 */
299 struct LayerRect {
300   float left   = 0.0f;   //!< Left-most pixel coordinate.
301   float top    = 0.0f;   //!< Top-most pixel coordinate.
302   float right  = 0.0f;   //!< Right-most pixel coordinate.
303   float bottom = 0.0f;   //!< Bottom-most pixel coordinate.
304 
305   LayerRect() = default;
306 
LayerRectLayerRect307   LayerRect(float l, float t, float r, float b) : left(l), top(t), right(r), bottom(b) { }
308 
309   bool operator==(const LayerRect& rect) const {
310     return left == rect.left && right == rect.right && top == rect.top && bottom == rect.bottom;
311   }
312 
313   bool operator!=(const LayerRect& rect) const {
314     return !operator==(rect);
315   }
316 };
317 
318 /*! @brief This structure defines an array of display layer rectangles.
319 
320   @sa LayerRect
321 */
322 struct LayerRectArray {
323   LayerRect *rect = NULL;  //!< Pointer to first element of array.
324   uint32_t count = 0;      //!< Number of elements in the array.
325 };
326 
327 struct LayerStitchInfo {
328   LayerRect dst_rect = {};          //!< The target position where the frame will be
329                                     //!< rendered onto internal FrameBuffer.
330 
331   LayerRect slice_rect = {};        //!<  Target slice that this stitch rect belongs to.
332 };
333 
334 /*! @brief This structure defines solidfill structure.
335 
336   @sa LayerSolidFill
337 */
338 struct LayerSolidFill {
339   uint32_t bit_depth = 0;  //!< Bit depth of solid fill colors
340   uint32_t red = 0;        //!< Red value
341   uint32_t green = 0;      //!< Green value
342   uint32_t blue = 0;       //!< Blue value
343   uint32_t alpha = 0;      //!< Alpha value
344 };
345 
346 struct LayerBufferMap {
347   std::unordered_map<uint64_t, std::shared_ptr<LayerBufferObject>> buffer_map;
348 };
349 
350 /*! @brief This structure defines display layer object which contains layer properties and a drawing
351   buffer.
352 
353   @sa LayerArray
354 */
355 struct Layer {
356   LayerBuffer input_buffer = {};                   //!< Buffer to be composed.
357                                                    //!< If this remains unchanged between two
358                                                    //!< consecutive Prepare() calls and
359                                                    //!< geometry_changed flag is not set for the
360                                                    //!< second call, then the display device will
361                                                    //!< assume that buffer content has not
362                                                    //!< changed.
363 
364   LayerComposition composition = kCompositionGPU;  //!< Composition type which can be set by either
365                                                    //!< the client or the display device. This value
366                                                    //!< should be preserved between Prepare() and
367                                                    //!< Commit() calls.
368 
369   LayerRect src_rect = {};                         //!< Rectangular area of the layer buffer to
370                                                    //!< consider for composition.
371 
372   LayerRect dst_rect = {};                         //!< The target position where the frame will be
373                                                    //!< displayed. Cropping rectangle is scaled to
374                                                    //!< fit into this rectangle. The origin is the
375                                                    //!< top-left corner of the screen.
376 
377   LayerStitchInfo stitch_info = {};                //!< This structure defines all parameters needed
378                                                    //!< for stitching like position to render,
379                                                    //!< boundaries etc;
380 
381   std::vector<LayerRect> visible_regions = {};     //!< Visible rectangular areas in screen space.
382                                                    //!< The visible region includes areas overlapped
383                                                    //!< by a translucent layer.
384 
385   std::vector<LayerRect> dirty_regions = {};       //!< Rectangular areas in the current frames
386                                                    //!< that have changed in comparison to
387                                                    //!< previous frame.
388 
389   LayerBlending blending = kBlendingPremultiplied;  //!< Blending operation which need to be
390                                                     //!< applied on the layer buffer during
391                                                     //!< composition.
392 
393   LayerTransform transform = {};                   //!< Rotation/Flip operations which need to be
394                                                    //!< applied to the layer buffer during
395                                                    //!< composition.
396 
397   uint8_t plane_alpha = 0xff;                      //!< Alpha value applied to the whole layer.
398                                                    //!< Value of each pixel is computed as:
399                                                    //!<    if(kBlendingPremultiplied) {
400                                                    //!<      pixel.RGB = pixel.RGB * planeAlpha/255
401                                                    //!<    }
402                                                    //!<    pixel.a = pixel.a * planeAlpha
403 
404   uint32_t frame_rate = 0;                         //!< Rate at which frames are being updated for
405                                                    //!< this layer.
406 
407   uint32_t solid_fill_color = 0;                   //!< TODO: Remove this field when fb support
408                                                    //!  is deprecated.
409                                                    //!< Solid color used to fill the layer when
410                                                    //!< no content is associated with the layer.
411 
412   LayerFlags flags;                                //!< Flags associated with this layer.
413 
414   LayerRequest request = {};                       //!< o/p - request on this Layer by SDM.
415 
416   Lut3d lut_3d = {};                               //!< o/p - Populated by SDM when tone mapping is
417                                                    //!< needed on this layer.
418   LayerSolidFill solid_fill_info = {};             //!< solid fill info along with depth.
419   std::shared_ptr<LayerBufferMap> buffer_map = nullptr;  //!< Map of handle_id and fb_id.
420   float color_transform_matrix[kColorTransformMatrixSize] = { 1.0, 0.0, 0.0, 0.0,
421                                                               0.0, 1.0, 0.0, 0.0,
422                                                               0.0, 0.0, 1.0, 0.0,
423                                                               0.0, 0.0, 0.0, 1.0 };
424   std::bitset<kLayerUpdateMax> update_mask = 0;
425 };
426 
427 /*! @brief This structure defines the color space + transfer of a given layer.
428 
429   @sa PrimariesTransfer
430 */
431 
432 struct PrimariesTransfer {
433   ColorPrimaries primaries = ColorPrimaries_BT709_5;
434   GammaTransfer transfer = Transfer_sRGB;
435 
436   bool operator==(const PrimariesTransfer& blend_cs) const {
437     return ((primaries == blend_cs.primaries) && (transfer == blend_cs.transfer));
438   }
439 };
440 
441 
442 /*! @brief This structure defines a layer stack that contains layers which need to be composed and
443   rendered onto the target.
444 
445   @sa DisplayInterface::Prepare
446   @sa DisplayInterface::Commit
447 */
448 
449 struct LayerStack {
450   std::vector<Layer *> layers = {};    //!< Vector of layer pointers.
451 
452   shared_ptr<Fence> retire_fence = nullptr;
453                                        //!< File descriptor referring to a sync fence object which
454                                        //!< will be signaled when this composited frame has been
455                                        //!< replaced on screen by a subsequent frame on a physical
456                                        //!< display. The fence object is created and returned during
457                                        //!< Commit(). Client shall close the returned file
458                                        //!< descriptor.
459                                        //!< NOTE: This field applies to a physical display only.
460 
461   LayerBuffer *output_buffer = NULL;   //!< Pointer to the buffer where composed buffer would be
462                                        //!< rendered for virtual displays.
463                                        //!< NOTE: This field applies to a virtual display only.
464 
465   LayerStackFlags flags;               //!< Flags associated with this layer set.
466 
467 
468   PrimariesTransfer blend_cs = {};     //!< o/p - Blending color space of the frame, updated by SDM
469 
470   uint64_t elapse_timestamp = 0;       //!< system time until which display commit needs to be held
471 
472 };
473 
474 }  // namespace sdm
475 
476 #endif  // __LAYER_STACK_H__
477 
478