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