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