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