[dali_2.2.11] 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) 2022 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
81 using RenderableContainer = Dali::Vector<Renderable>;
82
83 /**
84  * Layers have a "depth" relative to all other layers in the scene-graph.
85  * Non-layer child nodes are considered part of the layer.
86  *
87  * Layers are rendered separately, and by default the depth buffer is cleared before each layer is rendered.
88  * Objects in higher layers, are rendered after (in front of) objects in lower layers.
89  */
90 class Layer : public Node
91 {
92 public:
93   using SortFunctionType = Dali::Layer::SortFunctionType;
94
95   // Creation methods
96
97   /**
98    * Construct a new Layer.
99    * @return A smart-pointer to a newly allocated Node
100    */
101   static SceneGraph::Layer* New();
102
103   /**
104    * Virtual destructor
105    */
106   ~Layer() override;
107
108   /**
109    * From Node, to convert a node to a layer.
110    * @return The layer.
111    */
112   Layer* GetLayer() override
113   {
114     return this;
115   }
116
117   /**
118    * Sets the sort-function of a layer.
119    * @param [in] function The new sort-function.
120    */
121   void SetSortFunction(Dali::Layer::SortFunctionType function);
122
123   /**
124    * Retrieve the function used to sort semi-transparent geometry in this layer.
125    * @return The sort function.
126    */
127   Dali::Layer::SortFunctionType GetSortFunction() const
128   {
129     return mSortFunction;
130   }
131
132   /**
133    * Sets whether clipping is enabled for a layer.
134    * @param [in] enabled True if clipping is enabled.
135    */
136   void SetClipping(bool enabled);
137
138   /**
139    * Query whether clipping is enabled for a layer.
140    * @return True if clipping is enabled.
141    */
142   bool IsClipping() const
143   {
144     return mIsClipping;
145   }
146
147   /**
148    * Sets the clipping box of a layer, in window coordinates.
149    * The contents of the layer will not be visible outside this box, when clipping is
150    * enabled. The default clipping box is empty (0,0,0,0).
151    * @param [in] box The clipping box
152    */
153   void SetClippingBox(const ClippingBox& box);
154
155   /**
156    * Retrieves the clipping box of a layer, in window coordinates.
157    * @return The clipping box
158    */
159   const ClippingBox& GetClippingBox() const
160   {
161     return mClippingBox;
162   }
163
164   /**
165    * Sets the behavior of the layer
166    * @param [in] behavior The behavior of the layer
167    */
168   void SetBehavior(Dali::Layer::Behavior behavior);
169
170   /**
171    * Retrieves the behavior of the layer.
172    * @return The behavior
173    */
174   Dali::Layer::Behavior GetBehavior() const
175   {
176     return mBehavior;
177   }
178
179   /**
180    * @copydoc Dali::Layer::SetDepthTestDisabled()
181    */
182   void SetDepthTestDisabled(bool disable);
183
184   /**
185    * @copydoc Dali::Layer::IsDepthTestDisabled()
186    */
187   bool IsDepthTestDisabled() const;
188
189   /**
190    * Enables the reuse of the model view matrices of all renderers for this layer
191    * @param[in] updateBufferIndex The current update buffer index.
192    * @param value to set
193    */
194   void SetReuseRenderers(BufferIndex updateBufferIndex, bool value)
195   {
196     mAllChildTransformsClean[updateBufferIndex] = value;
197   }
198
199   /**
200    * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
201    * has not changed from previous use.
202    * @param[in] camera A pointer to the camera that we want to use to render the list.
203    * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
204    * to use is the same than the one used before ( Otherwise View transform will be different )
205    *
206    */
207   bool CanReuseRenderers(const Camera* camera)
208   {
209     bool bReturn(mAllChildTransformsClean[0] && mAllChildTransformsClean[1] && camera == mLastCamera);
210     mLastCamera = camera;
211
212     return bReturn;
213   }
214
215   /**
216    * @return True if default sort function is used
217    */
218   bool UsesDefaultSortFunction()
219   {
220     return mIsDefaultSortFunction;
221   }
222
223   /**
224    * Clears all the renderable lists
225    */
226   void ClearRenderables();
227
228 private:
229   /**
230    * Private constructor.
231    * See also Layer::New()
232    */
233   Layer();
234
235   // Delete copy and move
236   Layer(const Layer&)                = delete;
237   Layer(Layer&&)                     = delete;
238   Layer& operator=(const Layer& rhs) = delete;
239   Layer& operator=(Layer&& rhs)      = delete;
240
241 public: // For update-algorithms
242   RenderableContainer colorRenderables;
243   RenderableContainer overlayRenderables;
244
245 private:
246   SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
247
248   ClippingBox   mClippingBox; ///< The clipping box, in window coordinates
249   const Camera* mLastCamera;  ///< Pointer to the last camera that has rendered the layer
250
251   Dali::Layer::Behavior mBehavior; ///< The behavior of the layer
252
253   bool mAllChildTransformsClean[2]; ///< True if all child nodes transforms are clean,
254                                     ///  double buffered as we need two clean frames before we can reuse N-1 for N+1
255                                     ///  this allows us to cache render items when layer is "static"
256   bool mIsClipping : 1;             ///< True when clipping is enabled
257   bool mDepthTestDisabled : 1;      ///< Whether depth test is disabled.
258   bool mIsDefaultSortFunction : 1;  ///< whether the default depth sort function is used
259 };
260
261 // Messages for Layer
262
263 /**
264  * Create a message to set the sort-function of a layer
265  * @param[in] layer The layer
266  * @param[in] function The new sort-function.
267  */
268 inline void SetSortFunctionMessage(EventThreadServices& eventThreadServices, const Layer& layer, Dali::Layer::SortFunctionType function)
269 {
270   using LocalType = MessageValue1<Layer, Dali::Layer::SortFunctionType>;
271
272   // Reserve some memory inside the message queue
273   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
274
275   // Construct message in the message queue memory; note that delete should not be called on the return value
276   new(slot) LocalType(&layer, &Layer::SetSortFunction, function);
277 }
278
279 /**
280  * Create a message for enabling/disabling layer clipping
281  * @param[in] layer The layer
282  * @param[in] enabled True if clipping is enabled
283  */
284 inline void SetClippingMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool enabled)
285 {
286   using LocalType = MessageValue1<Layer, bool>;
287
288   // Reserve some memory inside the message queue
289   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
290
291   // Construct message in the message queue memory; note that delete should not be called on the return value
292   new(slot) LocalType(&layer, &Layer::SetClipping, enabled);
293 }
294
295 /**
296  * Create a message to set the clipping box of a layer
297  * @param[in] layer The layer
298  * @param[in] clippingbox The clipping box
299  */
300 inline void SetClippingBoxMessage(EventThreadServices& eventThreadServices, const Layer& layer, const Dali::ClippingBox& clippingbox)
301 {
302   using LocalType = MessageValue1<Layer, Dali::ClippingBox>;
303
304   // Reserve some memory inside the message queue
305   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
306
307   // Construct message in the message queue memory; note that delete should not be called on the return value
308   new(slot) LocalType(&layer, &Layer::SetClippingBox, clippingbox);
309 }
310
311 /**
312  * Create a message to set the behavior of a layer
313  * @param[in] layer The layer
314  * @param[in] behavior The behavior
315  */
316 inline void SetBehaviorMessage(EventThreadServices&  eventThreadServices,
317                                const Layer&          layer,
318                                Dali::Layer::Behavior behavior)
319 {
320   using LocalType = MessageValue1<Layer, Dali::Layer::Behavior>;
321
322   // Reserve some memory inside the message queue
323   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
324
325   // Construct message in the message queue memory; note that delete should not be called on the return value
326   new(slot) LocalType(&layer, &Layer::SetBehavior, behavior);
327 }
328
329 /**
330  * Create a message for disabling/enabling depth test.
331  *
332  * @see Dali::Layer::SetDepthTestDisabled().
333  *
334  * @param[in] layer The layer
335  * @param[in] disable \e true disables depth test. \e false sets the default behavior.
336  */
337 inline void SetDepthTestDisabledMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool disable)
338 {
339   using LocalType = MessageValue1<Layer, bool>;
340
341   // Reserve some memory inside the message queue
342   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
343
344   // Construct message in the message queue memory; note that delete should not be called on the return value
345   new(slot) LocalType(&layer, &Layer::SetDepthTestDisabled, disable);
346 }
347
348 } // namespace SceneGraph
349
350 // Template specialisation for OwnerPointer<Layer>, because delete is protected
351 template<>
352 inline void OwnerPointer<Dali::Internal::SceneGraph::Layer>::Reset()
353 {
354   if(mObject != nullptr)
355   {
356     Dali::Internal::SceneGraph::Node::Delete(mObject);
357     mObject = nullptr;
358   }
359 }
360 } // namespace Internal
361
362 } // namespace Dali
363
364 #endif // DALI_INTERNAL_SCENE_GRAPH_LAYER_H