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