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