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