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