Change Layer's ReuseRenderers check only if we need
[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[in] value to set
193    */
194   void SetReuseRenderers(BufferIndex updateBufferIndex, bool value)
195   {
196     mAllChildTransformsClean[updateBufferIndex] = value;
197   }
198
199   /**
200    * Get the reuse of the model view matrices of all renderers for this layer is enabled.
201    * @param[in] updateBufferIndex The current update buffer index.
202    * @return Whether all child transform was clean or not.
203    */
204   bool GetReuseRenderers(BufferIndex updateBufferIndex)
205   {
206     return mAllChildTransformsClean[updateBufferIndex];
207   }
208
209   /**
210    * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
211    * has not changed from previous use.
212    * @param[in] camera A pointer to the camera that we want to use to render the list.
213    * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
214    * to use is the same than the one used before ( Otherwise View transform will be different )
215    *
216    */
217   bool CanReuseRenderers(const Camera* camera)
218   {
219     bool bReturn(mAllChildTransformsClean[0] && mAllChildTransformsClean[1] && camera == mLastCamera);
220     mLastCamera = camera;
221
222     return bReturn;
223   }
224
225   /**
226    * @return True if default sort function is used
227    */
228   bool UsesDefaultSortFunction()
229   {
230     return mIsDefaultSortFunction;
231   }
232
233   /**
234    * Clears all the renderable lists
235    */
236   void ClearRenderables();
237
238 private:
239   /**
240    * Private constructor.
241    * See also Layer::New()
242    */
243   Layer();
244
245   // Delete copy and move
246   Layer(const Layer&)                = delete;
247   Layer(Layer&&)                     = delete;
248   Layer& operator=(const Layer& rhs) = delete;
249   Layer& operator=(Layer&& rhs)      = delete;
250
251 public: // For update-algorithms
252   RenderableContainer colorRenderables;
253   RenderableContainer overlayRenderables;
254
255 private:
256   SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
257
258   ClippingBox   mClippingBox; ///< The clipping box, in window coordinates
259   const Camera* mLastCamera;  ///< Pointer to the last camera that has rendered the layer
260
261   Dali::Layer::Behavior mBehavior; ///< The behavior of the layer
262
263   bool mAllChildTransformsClean[2]; ///< True if all child nodes transforms are clean,
264                                     ///  double buffered as we need two clean frames before we can reuse N-1 for N+1
265                                     ///  this allows us to cache render items when layer is "static"
266   bool mIsClipping : 1;             ///< True when clipping is enabled
267   bool mDepthTestDisabled : 1;      ///< Whether depth test is disabled.
268   bool mIsDefaultSortFunction : 1;  ///< whether the default depth sort function is used
269 };
270
271 // Messages for Layer
272
273 /**
274  * Create a message to set the sort-function of a layer
275  * @param[in] layer The layer
276  * @param[in] function The new sort-function.
277  */
278 inline void SetSortFunctionMessage(EventThreadServices& eventThreadServices, const Layer& layer, Dali::Layer::SortFunctionType function)
279 {
280   using LocalType = MessageValue1<Layer, Dali::Layer::SortFunctionType>;
281
282   // Reserve some memory inside the message queue
283   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
284
285   // Construct message in the message queue memory; note that delete should not be called on the return value
286   new(slot) LocalType(&layer, &Layer::SetSortFunction, function);
287 }
288
289 /**
290  * Create a message for enabling/disabling layer clipping
291  * @param[in] layer The layer
292  * @param[in] enabled True if clipping is enabled
293  */
294 inline void SetClippingMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool enabled)
295 {
296   using LocalType = MessageValue1<Layer, bool>;
297
298   // Reserve some memory inside the message queue
299   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
300
301   // Construct message in the message queue memory; note that delete should not be called on the return value
302   new(slot) LocalType(&layer, &Layer::SetClipping, enabled);
303 }
304
305 /**
306  * Create a message to set the clipping box of a layer
307  * @param[in] layer The layer
308  * @param[in] clippingbox The clipping box
309  */
310 inline void SetClippingBoxMessage(EventThreadServices& eventThreadServices, const Layer& layer, const Dali::ClippingBox& clippingbox)
311 {
312   using LocalType = MessageValue1<Layer, Dali::ClippingBox>;
313
314   // Reserve some memory inside the message queue
315   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
316
317   // Construct message in the message queue memory; note that delete should not be called on the return value
318   new(slot) LocalType(&layer, &Layer::SetClippingBox, clippingbox);
319 }
320
321 /**
322  * Create a message to set the behavior of a layer
323  * @param[in] layer The layer
324  * @param[in] behavior The behavior
325  */
326 inline void SetBehaviorMessage(EventThreadServices&  eventThreadServices,
327                                const Layer&          layer,
328                                Dali::Layer::Behavior behavior)
329 {
330   using LocalType = MessageValue1<Layer, Dali::Layer::Behavior>;
331
332   // Reserve some memory inside the message queue
333   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
334
335   // Construct message in the message queue memory; note that delete should not be called on the return value
336   new(slot) LocalType(&layer, &Layer::SetBehavior, behavior);
337 }
338
339 /**
340  * Create a message for disabling/enabling depth test.
341  *
342  * @see Dali::Layer::SetDepthTestDisabled().
343  *
344  * @param[in] layer The layer
345  * @param[in] disable \e true disables depth test. \e false sets the default behavior.
346  */
347 inline void SetDepthTestDisabledMessage(EventThreadServices& eventThreadServices, const Layer& layer, bool disable)
348 {
349   using LocalType = MessageValue1<Layer, bool>;
350
351   // Reserve some memory inside the message queue
352   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
353
354   // Construct message in the message queue memory; note that delete should not be called on the return value
355   new(slot) LocalType(&layer, &Layer::SetDepthTestDisabled, disable);
356 }
357
358 } // namespace SceneGraph
359
360 // Template specialisation for OwnerPointer<Layer>, because delete is protected
361 template<>
362 inline void OwnerPointer<Dali::Internal::SceneGraph::Layer>::Reset()
363 {
364   if(mObject != nullptr)
365   {
366     Dali::Internal::SceneGraph::Node::Delete(mObject);
367     mObject = nullptr;
368   }
369 }
370 } // namespace Internal
371
372 } // namespace Dali
373
374 #endif // DALI_INTERNAL_SCENE_GRAPH_LAYER_H