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