1/*
2* Copyright (c) 2014 - 2016, 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
37#include <vector>
38
39#include "layer_buffer.h"
40#include "sdm_types.h"
41
42namespace sdm {
43
44/*! @brief This enum represents display layer blending types.
45
46  @sa Layer
47*/
48enum LayerBlending {
49  kBlendingPremultiplied,   //!< Pixel color is expressed using premultiplied alpha in RGBA tuples.
50                            //!< If plane alpha is less than 0xFF, apply modulation as well.
51                            //!<   pixel.rgb = src.rgb + dest.rgb x (1 - src.a)
52
53  kBlendingOpaque,          //!< Pixel color is expressed using straight alpha in color tuples. It
54                            //!< is constant blend operation. The layer would appear opaque if plane
55                            //!< alpha is 0xFF.
56
57  kBlendingCoverage,        //!< Pixel color is expressed using straight alpha in color tuples. If
58                            //!< plane alpha is less than 0xff, apply modulation as well.
59                            //!<   pixel.rgb = src.rgb x src.a + dest.rgb x (1 - src.a)
60};
61
62/*! @brief This enum represents display layer composition types.
63
64  @sa Layer
65*/
66enum LayerComposition {
67  /* ==== List of composition types set by SDM === */
68  /* These composition types represent SDM composition decision for the layers which need to
69     be blended. Composition types are set during Prepare() by SDM.
70     Client can set default composition type to any of the below before calling into Prepare(),
71     however client's input value is ignored and does not play any role in composition decision.
72  */
73  kCompositionGPU,          //!< This layer will be drawn onto the target buffer by GPU. Display
74                            //!< device will mark the layer for GPU composition if it can not
75                            //!< handle composition for it.
76                            //!< This composition type is used only if GPUTarget layer is provided
77                            //!< in a composition cycle.
78
79  kCompositionGPUS3D,       //!< This layer will be drawn onto the target buffer in s3d mode by GPU.
80                            //!< Display device will mark the layer for GPU composition if it can
81                            //!< not handle composition for it.
82                            //!< This composition type is used only if GPUTarget layer is provided
83                            //!< in a composition cycle.
84
85  kCompositionSDE,          //!< This layer will be composed by SDE. It must not be composed by
86                            //!< GPU or Blit.
87
88  kCompositionHWCursor,     //!< This layer will be composed by SDE using HW Cursor. It must not be
89                            //!< composed by GPU or Blit.
90
91  kCompositionHybrid,       //!< This layer will be drawn by a blit engine and SDE together.
92                            //!< Display device will split the layer, update the blit rectangle
93                            //!< that need to be composed by a blit engine and update original
94                            //!< source rectangle that will be composed by SDE.
95                            //!< This composition type is used only if GPUTarget and BlitTarget
96                            //!< layers are provided in a composition cycle.
97
98  kCompositionBlit,         //!< This layer will be composed using Blit Engine.
99                            //!< This composition type is used only if BlitTarget layer is provided
100                            //!< in a composition cycle.
101
102  /* === List of composition types set by Client === */
103  /* These composition types represent target buffer layers onto which GPU or Blit will draw if SDM
104     decide to have some or all layers drawn by respective composition engine.
105     Client must provide a target buffer layer, if respective composition type is not disabled by
106     an explicit call to SetCompositionState() method. If a composition type is not disabled,
107     providing a target buffer layer is optional. If SDM is unable to handle layers without support
108     of such a composition engine, Prepare() call will return failure.
109  */
110  kCompositionGPUTarget,    //!< This layer will hold result of composition for layers marked for
111                            //!< GPU composition.
112                            //!< If display device does not set any layer for GPU composition then
113                            //!< this layer would be ignored. Else, this layer will be composed
114                            //!< with other layers marked for SDE composition by SDE.
115                            //!< Only one layer shall be marked as target buffer by the caller.
116                            //!< GPU target layer shall be placed after all application layers
117                            //!< in the layer stack.
118
119  kCompositionBlitTarget,   //!< This layer will hold result of composition for blit rectangles
120                            //!< from the layers marked for hybrid composition. Nth blit rectangle
121                            //!< in a layer shall be composed onto Nth blit target.
122                            //!< If display device does not set any layer for hybrid composition
123                            //!< then this would be ignored.
124                            //!< Blit target layers shall be placed after GPUTarget in the layer
125                            //!< stack.
126};
127
128/*! @brief This structure defines rotation and flip values for a display layer.
129
130  @sa Layer
131*/
132struct LayerTransform {
133  float rotation = 0.0f;  //!< Left most pixel coordinate.
134  bool flip_horizontal = false;  //!< Mirror reversal of the layer across a horizontal axis.
135  bool flip_vertical = false;  //!< Mirror reversal of the layer across a vertical axis.
136
137  bool operator==(const LayerTransform& transform) const {
138    return (rotation == transform.rotation && flip_horizontal == transform.flip_horizontal &&
139            flip_vertical == transform.flip_vertical);
140  }
141
142  bool operator!=(const LayerTransform& transform) const {
143    return !operator==(transform);
144  }
145};
146
147/*! @brief This structure defines flags associated with a layer. The 1-bit flag can be set to ON(1)
148  or OFF(0).
149
150  @sa LayerBuffer
151*/
152struct LayerFlags {
153  union {
154    struct {
155      uint32_t skip : 1;      //!< This flag shall be set by client to indicate that this layer
156                              //!< will be handled by GPU. Display Device will not consider it
157                              //!< for composition.
158
159      uint32_t updating : 1;  //!< This flag shall be set by client to indicate that this is
160                              //!< updating non-updating. so strategy manager will mark them for
161                              //!< SDE/GPU composition respectively when the layer stack qualifies
162                              //!< for cache based composition.
163
164      uint32_t solid_fill : 1;
165                              //!< This flag shall be set by client to indicate that this layer
166                              //!< is for solid fill without input buffer. Display Device will
167                              //!< use SDE HW feature to achieve it.
168
169      uint32_t cursor : 1;    //!< This flag shall be set by client to indicate that this layer
170                              //!< is a cursor
171                              //!< Display Device may handle this layer using HWCursor
172
173      uint32_t single_buffer : 1;  //!< This flag shall be set by client to indicate that the layer
174                                   //!< uses only a single buffer that will not be swapped out
175    };
176
177    uint32_t flags = 0;       //!< For initialization purpose only.
178                              //!< Client shall not refer it directly.
179  };
180};
181
182/*! @brief This structure defines flags associated with the layer requests. The 1-bit flag can be
183    set to ON(1) or OFF(0).
184
185  @sa Layer
186*/
187struct LayerRequestFlags {
188  union {
189    struct {
190      uint32_t tone_map : 1;  //!< This flag will be set by SDM when the layer needs tone map
191      uint32_t secure: 1;  //!< This flag will be set by SDM when the layer must be secure
192    };
193    uint32_t request_flags = 0;  //!< For initialization purpose only.
194                                 //!< Shall not be refered directly.
195  };
196};
197
198/*! @brief This structure defines LayerRequest.
199   Includes width/height/format of the LayerRequest.
200
201   SDM shall set the properties of LayerRequest to be used by the client
202
203  @sa LayerRequest
204*/
205struct LayerRequest {
206  LayerRequestFlags flags;  // Flags associated with this request
207  LayerBufferFormat format = kFormatRGBA8888;  // Requested format
208  uint32_t width = 0;  // Requested unaligned width.
209  uint32_t height = 0;  // Requested unalighed height
210};
211
212/*! @brief This structure defines flags associated with a layer stack. The 1-bit flag can be set to
213  ON(1) or OFF(0).
214
215  @sa LayerBuffer
216*/
217struct LayerStackFlags {
218  union {
219    struct {
220      uint32_t geometry_changed : 1;  //!< This flag shall be set by client to indicate that the
221                                      //!< layer set passed to Prepare() has changed by more than
222                                      //!< just the buffer handles and acquire fences.
223
224      uint32_t skip_present : 1;      //!< This flag will be set to true, if the current layer
225                                      //!< stack contains skip layers.
226
227      uint32_t video_present : 1;     //!< This flag will be set to true, if current layer stack
228                                      //!< contains video.
229
230      uint32_t secure_present : 1;    //!< This flag will be set to true, if the current layer
231                                      //!< stack contains secure layers.
232
233      uint32_t animating : 1;         //!< This flag shall be set by client to indicate that the
234                                      //!<  current frame is animating.i
235
236      uint32_t attributes_changed : 1;
237                                      //!< This flag shall be set by client to indicate that the
238                                      //!< current frame has some properties changed and
239                                      //!< needs re-config.
240
241      uint32_t cursor_present : 1;    //!< This flag will be set to true if the current layer
242                                      //!< stack contains cursor layer.
243
244      uint32_t single_buffered_layer_present : 1;    //!< Set if stack has single buffered layer
245
246      uint32_t s3d_mode_present : 1;  //!< This flag will be set to true, if the current layer
247                                      //!< stack contains s3d layer, and the layer stack can enter
248                                      //!< s3d mode.
249
250      uint32_t post_processed_output : 1;  // If output_buffer should contain post processed output
251                                           // This applies only to primary displays currently
252
253      uint32_t hdr_present : 1;  //!< Set if stack has HDR content
254    };
255
256    uint32_t flags = 0;               //!< For initialization purpose only.
257                                      //!< Client shall not refer it directly.
258  };
259};
260
261/*! @brief This structure defines a rectanglular area inside a display layer.
262
263  @sa LayerRectArray
264*/
265struct LayerRect {
266  float left   = 0.0f;   //!< Left-most pixel coordinate.
267  float top    = 0.0f;   //!< Top-most pixel coordinate.
268  float right  = 0.0f;   //!< Right-most pixel coordinate.
269  float bottom = 0.0f;   //!< Bottom-most pixel coordinate.
270
271  LayerRect() = default;
272
273  LayerRect(float l, float t, float r, float b) : left(l), top(t), right(r), bottom(b) { }
274
275  bool operator==(const LayerRect& rect) const {
276    return left == rect.left && right == rect.right && top == rect.top && bottom == rect.bottom;
277  }
278
279  bool operator!=(const LayerRect& rect) const {
280    return !operator==(rect);
281  }
282};
283
284/*! @brief This structure defines an array of display layer rectangles.
285
286  @sa LayerRect
287*/
288struct LayerRectArray {
289  LayerRect *rect = NULL;  //!< Pointer to first element of array.
290  uint32_t count = 0;      //!< Number of elements in the array.
291};
292
293/*! @brief This structure defines display layer object which contains layer properties and a drawing
294  buffer.
295
296  @sa LayerArray
297*/
298struct Layer {
299  LayerBuffer input_buffer = {};                   //!< Buffer to be composed.
300                                                   //!< If this remains unchanged between two
301                                                   //!< consecutive Prepare() calls and
302                                                   //!< geometry_changed flag is not set for the
303                                                   //!< second call, then the display device will
304                                                   //!< assume that buffer content has not
305                                                   //!< changed.
306
307  LayerComposition composition = kCompositionGPU;  //!< Composition type which can be set by either
308                                                   //!< the client or the display device. This value
309                                                   //!< should be preserved between Prepare() and
310                                                   //!< Commit() calls.
311
312  LayerRect src_rect = {};                         //!< Rectangular area of the layer buffer to
313                                                   //!< consider for composition.
314
315  LayerRect dst_rect = {};                         //!< The target position where the frame will be
316                                                   //!< displayed. Cropping rectangle is scaled to
317                                                   //!< fit into this rectangle. The origin is the
318                                                   //!< top-left corner of the screen.
319
320  std::vector<LayerRect> visible_regions = {};     //!< Visible rectangular areas in screen space.
321                                                   //!< The visible region includes areas overlapped
322                                                   //!< by a translucent layer.
323
324  std::vector<LayerRect> dirty_regions = {};       //!< Rectangular areas in the current frames
325                                                   //!< that have changed in comparison to
326                                                   //!< previous frame.
327
328  std::vector<LayerRect> blit_regions = {};        //!< Rectangular areas of this layer which need
329                                                   //!< to be composed to blit target. Display
330                                                   //!< device will update blit rectangles if a
331                                                   //!< layer composition is set as hybrid. Nth blit
332                                                   //!< rectangle shall be composed onto Nth blit
333                                                   //!< target.
334
335  LayerBlending blending = kBlendingPremultiplied;  //!< Blending operation which need to be
336                                                    //!< applied on the layer buffer during
337                                                    //!< composition.
338
339  LayerTransform transform = {};                   //!< Rotation/Flip operations which need to be
340                                                   //!< applied to the layer buffer during
341                                                   //!< composition.
342
343  uint8_t plane_alpha = 0xff;                      //!< Alpha value applied to the whole layer.
344                                                   //!< Value of each pixel is computed as:
345                                                   //!<    if(kBlendingPremultiplied) {
346                                                   //!<      pixel.RGB = pixel.RGB * planeAlpha/255
347                                                   //!<    }
348                                                   //!<    pixel.a = pixel.a * planeAlpha
349
350  uint32_t frame_rate = 0;                         //!< Rate at which frames are being updated for
351                                                   //!< this layer.
352
353  uint32_t solid_fill_color = 0;                   //!< Solid color used to fill the layer when
354                                                   //!< no content is associated with the layer.
355
356  LayerFlags flags;                                //!< Flags associated with this layer.
357
358  LayerRequest request = {};                       //!< o/p - request on this Layer by SDM.
359
360  Lut3d lut_3d = {};                               //!< o/p - Populated by SDM when tone mapping is
361                                                   //!< needed on this layer.
362};
363
364/*! @brief This structure defines a layer stack that contains layers which need to be composed and
365  rendered onto the target.
366
367  @sa DisplayInterface::Prepare
368  @sa DisplayInterface::Commit
369*/
370struct LayerStack {
371  std::vector<Layer *> layers = {};    //!< Vector of layer pointers.
372
373  int retire_fence_fd = -1;            //!< File descriptor referring to a sync fence object which
374                                       //!< will be signaled when this composited frame has been
375                                       //!< replaced on screen by a subsequent frame on a physical
376                                       //!< display. The fence object is created and returned during
377                                       //!< Commit(). Client shall close the returned file
378                                       //!< descriptor.
379                                       //!< NOTE: This field applies to a physical display only.
380
381  LayerBuffer *output_buffer = NULL;   //!< Pointer to the buffer where composed buffer would be
382                                       //!< rendered for virtual displays.
383                                       //!< NOTE: This field applies to a virtual display only.
384
385  LayerStackFlags flags;               //!< Flags associated with this layer set.
386};
387
388}  // namespace sdm
389
390#endif  // __LAYER_STACK_H__
391
392