[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / nodes / scene-graph-layer.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_LAYER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_LAYER_H
3
4 /*
5  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/common/event-thread-services.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/public-api/actors/layer.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 // value types used by messages
31 template<>
32 struct ParameterType<Dali::Layer::SortFunctionType>
33 : public BasicType<Dali::Layer::SortFunctionType>
34 {
35 };
36 template<>
37 struct ParameterType<Dali::Layer::Behavior>
38 : public BasicType<Dali::Layer::Behavior>
39 {
40 };
41
42 namespace SceneGraph
43 {
44 class Camera;
45
46 /**
47  * Pair of node-renderer
48  */
49 struct Renderable
50 {
51   Renderable() = default;
52
53   Renderable(Node* node, RendererKey renderer)
54   : mNode(node),
55     mRenderer(renderer)
56   {
57   }
58
59   Node*       mNode{nullptr};
60   RendererKey mRenderer{};
61 };
62
63 } // namespace SceneGraph
64 } // Namespace Internal
65
66 /// Enable Renderable to be used as a trivial type in Dali::Vector.
67 template<>
68 struct TypeTraits<Internal::SceneGraph::Renderable> : public Dali::BasicTypes<Internal::SceneGraph::Renderable>
69 {
70   enum
71   {
72     IS_TRIVIAL_TYPE = true
73   };
74 };
75
76 namespace Internal
77 {
78 namespace SceneGraph
79 {
80 using RenderableContainer = Dali::Vector<Renderable>;
81
82 /**
83  * Layers have a "depth" relative to all other layers in the scene-graph.
84  * Non-layer child nodes are considered part of the layer.
85  *
86  * Layers are rendered separately, and by default the depth buffer is cleared before each layer is rendered.
87  * Objects in higher layers, are rendered after (in front of) objects in lower layers.
88  */
89 class Layer : public Node
90 {
91 public:
92   using SortFunctionType = Dali::Layer::SortFunctionType;
93
94   // Creation methods
95
96   /**
97    * Construct a new Layer.
98    * @return A smart-pointer to a newly allocated Node
99    */
100   static SceneGraph::Layer* New();
101
102   /**
103    * Virtual destructor
104    */
105   ~Layer() override;
106
107   /**
108    * From Node, to convert a node to a layer.
109    * @return The layer.
110    */
111   Layer* GetLayer() override
112   {
113     return this;
114   }
115
116   /**
117    * Sets the sort-function of a layer.
118    * @param [in] function The new sort-function.
119    */
120   void SetSortFunction(Dali::Layer::SortFunctionType function);
121
122   /**
123    * Retrieve the function used to sort semi-transparent geometry in this layer.
124    * @return The sort function.
125    */
126   Dali::Layer::SortFunctionType GetSortFunction() const
127   {
128     return mSortFunction;
129   }
130
131   /**
132    * Sets whether clipping is enabled for a layer.
133    * @param [in] enabled True if clipping is enabled.
134    */
135   void SetClipping(bool enabled);
136
137   /**
138    * Query whether clipping is enabled for a layer.
139    * @return True if clipping is enabled.
140    */
141   bool IsClipping() const
142   {
143     return mIsClipping;
144   }
145
146   /**
147    * Sets the clipping box of a layer, in window coordinates.
148    * The contents of the layer will not be visible outside this box, when clipping is
149    * enabled. The default clipping box is empty (0,0,0,0).
150    * @param [in] box The clipping box
151    */
152   void SetClippingBox(const ClippingBox& box);
153
154   /**
155    * Retrieves the clipping box of a layer, in window coordinates.
156    * @return The clipping box
157    */
158   const ClippingBox& GetClippingBox() const
159   {
160     return mClippingBox;
161   }
162
163   /**
164    * Sets the behavior of the layer
165    * @param [in] behavior The behavior of the layer
166    */
167   void SetBehavior(Dali::Layer::Behavior behavior);
168
169   /**
170    * Retrieves the behavior of the layer.
171    * @return The behavior
172    */
173   Dali::Layer::Behavior GetBehavior() const
174   {
175     return mBehavior;
176   }
177
178   /**
179    * @copydoc Dali::Layer::SetDepthTestDisabled()
180    */
181   void SetDepthTestDisabled(bool disable);
182
183   /**
184    * @copydoc Dali::Layer::IsDepthTestDisabled()
185    */
186   bool IsDepthTestDisabled() const;
187
188   /**
189    * Enables the reuse of the model view matrices of all renderers for this layer
190    * @param[in] updateBufferIndex The current update buffer index.
191    * @param[in] value to set
192    */
193   void SetReuseRenderers(BufferIndex updateBufferIndex, bool value)
194   {
195     mAllChildTransformsClean[updateBufferIndex] = value;
196   }
197
198   /**
199    * Get the reuse of the model view matrices of all renderers for this layer is enabled.
200    * @param[in] updateBufferIndex The current update buffer index.
201    * @return Whether all child transform was clean or not.
202    */
203   bool GetReuseRenderers(BufferIndex updateBufferIndex) const
204   {
205     return mAllChildTransformsClean[updateBufferIndex];
206   }
207
208   /**
209    * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
210    * has not changed from previous use.
211    * @param[in] camera A pointer to the camera that we want to use to render the list.
212    * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
213    * to use is the same than the one used before ( Otherwise View transform will be different )
214    *
215    */
216   bool CanReuseRenderers(const Camera* camera)
217   {
218     bool bReturn(mAllChildTransformsClean[0] && mAllChildTransformsClean[1] && camera == mLastCamera);
219     mLastCamera = camera;
220
221     return bReturn;
222   }
223
224   /**
225    * @return True if default sort function is used
226    */
227   bool UsesDefaultSortFunction()
228   {
229     return mIsDefaultSortFunction;
230   }
231
232   /**
233    * Clears all the renderable lists
234    */
235   void ClearRenderables();
236
237 private:
238   /**
239    * Private constructor.
240    * See also Layer::New()
241    */
242   Layer();
243
244   // Delete copy and move
245   Layer(const Layer&) = delete;
246   Layer(Layer&&)      = delete;
247   Layer& operator=(const Layer& rhs) = delete;
248   Layer& operator=(Layer&& rhs) = delete;
249
250 public: // For update-algorithms
251   RenderableContainer colorRenderables;
252   RenderableContainer overlayRenderables;
253
254 private:
255   SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
256
257   ClippingBox   mClippingBox; ///< The clipping box, in window coordinates
258   const Camera* mLastCamera;  ///< Pointer to the last camera that has rendered the layer
259
260   Dali::Layer::Behavior mBehavior; ///< The behavior of the layer
261
262   bool mAllChildTransformsClean[2]; ///< True if all child nodes transforms are clean,
263                                     ///  double buffered as we need two clean frames before we can reuse N-1 for N+1
264                                     ///  this allows us to cache render items when layer is "static"
265   bool mIsClipping : 1;             ///< True when clipping is enabled
266   bool mDepthTestDisabled : 1;      ///< Whether depth test is disabled.
267   bool mIsDefaultSortFunction : 1;  ///< whether the default depth sort function is used
268 };
269
270 // Messages for Layer
271
272 /**
273  * Create a message to set the sort-function of a layer
274  * @param[in] layer The layer
275  * @param[in] function The new sort-function.
276  */
277 inline void SetSortFunctionMessage(EventThreadServices& eventThreadServices, const Layer& layer, Dali::Layer::SortFunctionType function)
278 {
279   using LocalType = MessageValue1<Layer, Dali::Layer::SortFunctionType>;
280
281   // Reserve some memory inside the message queue
282   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
283
284   // Construct message in the message queue memory; note that delete should not be called on the return value
285   new(slot) LocalType(&layer, &Layer::SetSortFunction, function);
286 }
287
288 /**
289  * Create a message for enabling/disabling layer clipping
290  * @param[in] layer The layer
291  * @param[in] enabled True if clipping is enabled
292  */
293 inline void SetClippingMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool enabled)
294 {
295   using LocalType = MessageValue1<Layer, bool>;
296
297   // Reserve some memory inside the message queue
298   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
299
300   // Construct message in the message queue memory; note that delete should not be called on the return value
301   new(slot) LocalType(&layer, &Layer::SetClipping, enabled);
302 }
303
304 /**
305  * Create a message to set the clipping box of a layer
306  * @param[in] layer The layer
307  * @param[in] clippingbox The clipping box
308  */
309 inline void SetClippingBoxMessage(EventThreadServices& eventThreadServices, const Layer& layer, const Dali::ClippingBox& clippingbox)
310 {
311   using LocalType = MessageValue1<Layer, Dali::ClippingBox>;
312
313   // Reserve some memory inside the message queue
314   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
315
316   // Construct message in the message queue memory; note that delete should not be called on the return value
317   new(slot) LocalType(&layer, &Layer::SetClippingBox, clippingbox);
318 }
319
320 /**
321  * Create a message to set the behavior of a layer
322  * @param[in] layer The layer
323  * @param[in] behavior The behavior
324  */
325 inline void SetBehaviorMessage(EventThreadServices&  eventThreadServices,
326                                const Layer&          layer,
327                                Dali::Layer::Behavior behavior)
328 {
329   using LocalType = MessageValue1<Layer, Dali::Layer::Behavior>;
330
331   // Reserve some memory inside the message queue
332   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
333
334   // Construct message in the message queue memory; note that delete should not be called on the return value
335   new(slot) LocalType(&layer, &Layer::SetBehavior, behavior);
336 }
337
338 /**
339  * Create a message for disabling/enabling depth test.
340  *
341  * @see Dali::Layer::SetDepthTestDisabled().
342  *
343  * @param[in] layer The layer
344  * @param[in] disable \e true disables depth test. \e false sets the default behavior.
345  */
346 inline void SetDepthTestDisabledMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool disable)
347 {
348   using LocalType = MessageValue1<Layer, bool>;
349
350   // Reserve some memory inside the message queue
351   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
352
353   // Construct message in the message queue memory; note that delete should not be called on the return value
354   new(slot) LocalType(&layer, &Layer::SetDepthTestDisabled, disable);
355 }
356
357 } // namespace SceneGraph
358
359 // Template specialisation for OwnerPointer<Layer>, because delete is protected
360 template<>
361 inline void OwnerPointer<Dali::Internal::SceneGraph::Layer>::Reset()
362 {
363   if(mObject != nullptr)
364   {
365     Dali::Internal::SceneGraph::Node::Delete(mObject);
366     mObject = nullptr;
367   }
368 }
369 } // namespace Internal
370
371 } // namespace Dali
372
373 #endif // DALI_INTERNAL_SCENE_GRAPH_LAYER_H