Merge branch 'devel/master' into tizen
[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    * Virtual destructor
86    */
87   virtual ~Layer();
88
89   /**
90    * From Node, to convert a node to a layer.
91    * @return The layer.
92    */
93   virtual Layer* GetLayer()
94   {
95     return this;
96   }
97
98   /**
99    * Sets the sort-function of a layer.
100    * @param [in] function The new sort-function.
101    */
102   void SetSortFunction( Dali::Layer::SortFunctionType function );
103
104   /**
105    * Retrieve the function used to sort semi-transparent geometry in this layer.
106    * @return The sort function.
107    */
108   Dali::Layer::SortFunctionType GetSortFunction() const
109   {
110     return mSortFunction;
111   }
112
113   /**
114    * Sets whether clipping is enabled for a layer.
115    * @param [in] enabled True if clipping is enabled.
116    */
117   void SetClipping( bool enabled );
118
119   /**
120    * Query whether clipping is enabled for a layer.
121    * @return True if clipping is enabled.
122    */
123   bool IsClipping() const
124   {
125     return mIsClipping;
126   }
127
128   /**
129    * Sets the clipping box of a layer, in window coordinates.
130    * The contents of the layer will not be visible outside this box, when clipping is
131    * enabled. The default clipping box is empty (0,0,0,0).
132    * @param [in] box The clipping box
133    */
134   void SetClippingBox( const ClippingBox& box );
135
136   /**
137    * Retrieves the clipping box of a layer, in window coordinates.
138    * @return The clipping box
139    */
140   const ClippingBox& GetClippingBox() const
141   {
142     return mClippingBox;
143   }
144
145   /**
146    * Sets the behavior of the layer
147    * @param [in] behavior The behavior of the layer
148    */
149   void SetBehavior( Dali::Layer::Behavior behavior );
150
151   /**
152    * Retrieves the behavior of the layer.
153    * @return The behavior
154    */
155   Dali::Layer::Behavior GetBehavior() const
156   {
157     return mBehavior;
158   }
159
160   /**
161    * @copydoc Dali::Layer::SetDepthTestDisabled()
162    */
163   void SetDepthTestDisabled( bool disable );
164
165   /**
166    * @copydoc Dali::Layer::IsDepthTestDisabled()
167    */
168   bool IsDepthTestDisabled() const;
169
170   /**
171    * Enables the reuse of the model view matrices of all renderers for this layer
172    * @param[in] updateBufferIndex The current update buffer index.
173    * @param value to set
174    */
175   void SetReuseRenderers( BufferIndex updateBufferIndex, bool value )
176   {
177     mAllChildTransformsClean[ updateBufferIndex ] = value;
178   }
179
180   /**
181    * Checks if it is ok to reuse renderers. Renderers can be reused if ModelView transform for all the renderers
182    * has not changed from previous use.
183    * @param[in] camera A pointer to the camera that we want to use to render the list.
184    * @return True if all children transforms have been clean for two consecutive frames and the camera we are going
185    * to use is the same than the one used before ( Otherwise View transform will be different )
186    *
187    */
188   bool CanReuseRenderers( Camera* camera )
189   {
190     bool bReturn( mAllChildTransformsClean[ 0 ] && mAllChildTransformsClean[ 1 ] && camera == mLastCamera );
191     mLastCamera = camera;
192
193     return bReturn;
194   }
195
196   /**
197    * @return True if default sort function is used
198    */
199   bool UsesDefaultSortFunction()
200   {
201     return mIsDefaultSortFunction;
202   }
203
204   /**
205    * Clears all the renderable lists
206    */
207   void ClearRenderables();
208
209 private:
210
211   /**
212    * Private constructor.
213    * See also Layer::New()
214    */
215   Layer();
216
217   // Undefined
218   Layer(const Layer&);
219
220   // Undefined
221   Layer& operator=(const Layer& rhs);
222
223 public: // For update-algorithms
224
225   RenderableContainer stencilRenderables;
226   RenderableContainer colorRenderables;
227   RenderableContainer overlayRenderables;
228
229 private:
230
231   SortFunctionType mSortFunction; ///< Used to sort semi-transparent geometry
232
233   ClippingBox mClippingBox;           ///< The clipping box, in window coordinates
234   Camera* mLastCamera;                ///< Pointer to the last camera that has rendered the layer
235
236   Dali::Layer::Behavior mBehavior;    ///< The behavior of the layer
237
238   bool mAllChildTransformsClean[ 2 ]; ///< True if all child nodes transforms are clean,
239                                       /// double buffered as we need two clean frames before we can reuse N-1 for N+1
240                                       /// this allows us to cache render items when layer is "static"
241   bool mIsClipping:1;                 ///< True when clipping is enabled
242   bool mDepthTestDisabled:1;          ///< Whether depth test is disabled.
243   bool mIsDefaultSortFunction:1;      ///< whether the default depth sort function is used
244
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   typedef MessageValue1< Layer, Dali::Layer::SortFunctionType > LocalType;
257
258   // Reserve some memory inside the message queue
259   unsigned int* 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   typedef MessageValue1< Layer, bool > LocalType;
273
274   // Reserve some memory inside the message queue
275   unsigned int* 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   typedef MessageValue1< Layer, Dali::ClippingBox > LocalType;
289
290   // Reserve some memory inside the message queue
291   unsigned int* 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   typedef MessageValue1< Layer, Dali::Layer::Behavior > LocalType;
307
308   // Reserve some memory inside the message queue
309   unsigned int* 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   typedef MessageValue1< Layer, bool > LocalType;
326
327   // Reserve some memory inside the message queue
328   unsigned int* 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 } // namespace Internal
337
338 } // namespace Dali
339
340 #endif // __DALI_INTERNAL_SCENE_GRAPH_LAYER_H__