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