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