Merge branch 'tizen' of platform/core/uifw/dali-core into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / update-algorithms.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/manager/update-algorithms.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/actors/draw-mode.h>
26 #include <dali/public-api/math/matrix.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/internal/update/resources/resource-manager.h>
29 #include <dali/internal/update/nodes/node.h>
30 #include <dali/internal/update/node-attachments/node-attachment.h>
31 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
32 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
33 #include <dali/internal/update/nodes/scene-graph-layer.h>
34 #include <dali/internal/render/renderers/scene-graph-renderer.h>
35
36 #include <dali/integration-api/debug.h>
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace SceneGraph
45 {
46
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gUpdateFilter = Debug::Filter::New(Debug::Concise, false, "LOG_UPDATE_ALGORITHMS");
49 #endif
50
51 /******************************************************************************
52  *********************** Apply Constraints ************************************
53  ******************************************************************************/
54
55
56 /**
57  * Constrain the local properties of the PropertyOwner.
58  * @param propertyOwner to constrain
59  * @param updateBufferIndex buffer index to use
60  * @return The number of constraints that are still being applied
61  */
62 unsigned int ConstrainPropertyOwner( PropertyOwner& propertyOwner, BufferIndex updateBufferIndex )
63 {
64   unsigned int activeCount = 0;
65
66   ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
67
68   const ConstraintIter endIter = constraints.End();
69   for( ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter )
70   {
71     ConstraintBase& constraint = **iter;
72     constraint.Apply( updateBufferIndex );
73
74     if( constraint.mWeight[updateBufferIndex] < 1.0f )
75     {
76       // this constraint is still being applied
77       ++activeCount;
78     }
79   }
80
81   return activeCount;
82 }
83
84 /**
85  * Recursively apply the constraints on the nodes
86  * @param node to constraint
87  * @param updateBufferIndex buffer index to use
88  * @return number of active constraints
89  */
90 unsigned int ConstrainNodes( Node& node, BufferIndex updateBufferIndex )
91 {
92   unsigned int activeCount = ConstrainPropertyOwner( node, updateBufferIndex );
93
94   /**
95    *  Constrain the children next
96    */
97   NodeContainer& children = node.GetChildren();
98   const NodeIter endIter = children.End();
99   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
100   {
101     Node& child = **iter;
102     activeCount += ConstrainNodes( child, updateBufferIndex );
103   }
104   return activeCount;
105 }
106
107 /******************************************************************************
108  ************************** Update node hierarchy *****************************
109  ******************************************************************************/
110
111 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
112 {
113   if ( nodeDirtyFlags & ColorFlag )
114   {
115     rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
116   }
117   else
118   {
119     // Copy previous value, in case it changed in the previous frame
120     rootNode.CopyPreviousWorldColor( updateBufferIndex );
121   }
122 }
123
124 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
125 {
126   // If opacity needs to be recalculated
127   if ( nodeDirtyFlags & ColorFlag )
128   {
129     node.InheritWorldColor( updateBufferIndex );
130   }
131   else
132   {
133     // Copy inherited value, if changed in the previous frame
134     node.CopyPreviousWorldColor( updateBufferIndex );
135   }
136 }
137
138 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
139 {
140   // If the transform values need to be reinherited
141   if ( nodeDirtyFlags & TransformFlag )
142   {
143     rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
144     rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
145     rootNode.SetWorldScale   ( updateBufferIndex, rootNode.GetScale   ( updateBufferIndex ) );
146   }
147   else
148   {
149     // Copy previous value, in case they changed in the previous frame
150     rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
151     rootNode.CopyPreviousWorldScale( updateBufferIndex );
152     rootNode.CopyPreviousWorldPosition( updateBufferIndex );
153   }
154 }
155
156 /**
157  * Updates transform values for the given node if the transform flag is dirty.
158  * This includes applying a new size should the SizeMode require it.
159  * Note that this will cause the size dirty flag to be set. This is why we pass
160  * the dirty flags in by reference.
161  * @param[in]     node The node to update
162  * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
163  * @param[in]     updateBufferIndex The current index to use for this frame
164  */
165 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
166 {
167   // If the transform values need to be reinherited
168   if( nodeDirtyFlags & TransformFlag )
169   {
170     // With a non-central anchor-point, the world rotation and scale affects the world position.
171     // Therefore the world rotation & scale must be updated before the world position.
172     if( node.IsOrientationInherited() )
173     {
174       node.InheritWorldOrientation( updateBufferIndex );
175     }
176     else
177     {
178       node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
179     }
180
181     if( node.IsScaleInherited() )
182     {
183       node.InheritWorldScale( updateBufferIndex );
184     }
185     else
186     {
187       node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
188     }
189
190     node.InheritWorldPosition( updateBufferIndex );
191   }
192   else
193   {
194     // Copy inherited values, if those changed in the previous frame
195     node.CopyPreviousWorldOrientation( updateBufferIndex );
196     node.CopyPreviousWorldScale( updateBufferIndex );
197     node.CopyPreviousWorldPosition( updateBufferIndex );
198     node.CopyPreviousSize( updateBufferIndex );
199   }
200 }
201
202 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
203 {
204   // If world-matrix needs to be recalculated
205   if ( nodeDirtyFlags & TransformFlag )
206   {
207     if( node.GetInhibitLocalTransform() )
208     {
209       node.SetWorldMatrix( updateBufferIndex,
210                            node.GetWorldScale(updateBufferIndex),
211                            node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
212                            node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
213     }
214     else
215     {
216       node.SetWorldMatrix( updateBufferIndex,
217                            node.GetWorldScale(updateBufferIndex),
218                            node.GetWorldOrientation(updateBufferIndex),
219                            node.GetWorldPosition(updateBufferIndex) );
220     }
221   }
222   else
223   {
224     node.CopyPreviousWorldMatrix( updateBufferIndex );
225   }
226 }
227
228 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
229 {
230   /**
231    * If world-matrix needs to be recalculated.
232    */
233   if ( ( nodeDirtyFlags & TransformFlag ) ||
234          updatedRenderable.IsScaleForSizeDirty() )
235   {
236     if( updatedRenderable.UsesGeometryScaling() )
237     {
238       Vector3 scaling;
239       updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
240       if( node.GetInhibitLocalTransform() )
241       {
242         node.SetWorldMatrix( updateBufferIndex,
243                              node.GetWorldScale(updateBufferIndex) * scaling,
244                              node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
245                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
246       }
247       else
248       {
249         node.SetWorldMatrix( updateBufferIndex,
250                              node.GetWorldScale(updateBufferIndex) * scaling,
251                              node.GetWorldOrientation(updateBufferIndex),
252                              node.GetWorldPosition(updateBufferIndex) );
253       }
254     }
255     else
256     {
257       // no scaling, i.e. Image
258       if( node.GetInhibitLocalTransform() )
259       {
260         node.SetWorldMatrix( updateBufferIndex,
261                              node.GetWorldScale(updateBufferIndex),
262                              node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
263                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
264       }
265       else
266       {
267         node.SetWorldMatrix( updateBufferIndex,
268                              node.GetWorldScale(updateBufferIndex),
269                              node.GetWorldOrientation(updateBufferIndex),
270                              node.GetWorldPosition(updateBufferIndex) );
271       }
272     }
273   }
274   else
275   {
276     node.CopyPreviousWorldMatrix( updateBufferIndex );
277   }
278 }
279
280 /**
281  * Update an attachment.
282  * @return An updated renderable attachment if one was ready.
283  */
284 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
285                                                Node& node,
286                                                BufferIndex updateBufferIndex,
287                                                ResourceManager& resourceManager,
288                                                int nodeDirtyFlags )
289 {
290   // Allow attachments to do specialised processing during updates
291   attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
292
293   RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
294   if( renderable )
295   {
296     // Notify renderables when size has changed
297     // Size can change while node was invisible so we need to check size again if we were previously invisible
298     if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
299     {
300       renderable->SizeChanged( updateBufferIndex );
301     }
302
303     // check if node is visible
304     if( renderable->ResolveVisibility( updateBufferIndex ) )
305     {
306       renderable->PrepareResources( updateBufferIndex, resourceManager );
307     }
308   }
309   return renderable;
310 }
311
312 inline void AddRenderableToLayer( Layer& layer,
313                                   RenderableAttachment& renderable,
314                                   BufferIndex updateBufferIndex,
315                                   int inheritedDrawMode )
316 {
317   // The renderables are stored into the opaque list temporarily for PrepareRenderables()
318   // step. The list is cleared by ProcessRenderTasks().
319   layer.opaqueRenderables.push_back( &renderable );
320 }
321
322 /**
323  * This is called recursively for all children of the root Node
324  */
325 inline int UpdateNodesAndAttachments( Node& node,
326                                       int parentFlags,
327                                       BufferIndex updateBufferIndex,
328                                       ResourceManager& resourceManager,
329                                       RenderQueue& renderQueue,
330                                       Layer& currentLayer,
331                                       int inheritedDrawMode )
332 {
333   Layer* layer = &currentLayer;
334
335   // Short-circuit for invisible nodes
336   if ( !node.IsVisible( updateBufferIndex ) )
337   {
338     return 0;
339   }
340
341   // If the node was not previously visible
342   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
343   if ( !node.IsVisible( previousBuffer ) )
344   {
345     // The node was skipped in the previous update; it must recalculate everything
346     node.SetAllDirtyFlags();
347   }
348
349   // Some dirty flags are inherited from parent
350   int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
351
352   int cumulativeDirtyFlags = nodeDirtyFlags;
353
354   if ( node.IsLayer() )
355   {
356     // all childs go to this layer
357     layer = node.GetLayer();
358
359     // assume layer is clean to begin with
360     layer->SetReuseRenderers( updateBufferIndex, true );
361
362     // Layers do not inherit the DrawMode from their parents
363     inheritedDrawMode = DrawMode::NORMAL;
364   }
365   DALI_ASSERT_DEBUG( NULL != layer );
366
367   UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
368
369   // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
370   // It is important that the modified version of these flags are used by the RenderableAttachment.
371   UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
372
373   // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
374   inheritedDrawMode |= node.GetDrawMode();
375
376   if ( node.HasAttachment() )
377   {
378     /*
379      * Add renderables for the children into the current Layer
380      */
381     RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
382                                                          node,
383                                                          updateBufferIndex,
384                                                          resourceManager,
385                                                          nodeDirtyFlags );
386
387
388     if( NULL != renderable )
389     {
390       // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
391       UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
392
393       // The attachment is ready to render, so it is added to a set of renderables.
394       AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
395     }
396   }
397   else if( node.IsObserved() )
398   {
399     // This node is being used as a property input for an animation, constraint,
400     // camera or bone. Ensure it's matrix is updated
401     UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
402   }
403
404   // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
405   // also if node has been deleted, dont reuse old render items
406   if( nodeDirtyFlags & RenderableUpdateFlags )
407   {
408     layer->SetReuseRenderers( updateBufferIndex, false );
409   }
410
411   // recurse children
412   NodeContainer& children = node.GetChildren();
413   const NodeIter endIter = children.End();
414   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
415   {
416     Node& child = **iter;
417     cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
418                                                       nodeDirtyFlags,
419                                                       updateBufferIndex,
420                                                       resourceManager,
421                                                       renderQueue,
422                                                       *layer,
423                                                       inheritedDrawMode );
424   }
425
426   return cumulativeDirtyFlags;
427 }
428
429 /**
430  * The root node is treated separately; it cannot inherit values since it has no parent
431  */
432 int UpdateNodesAndAttachments( Layer& rootNode,
433                                BufferIndex updateBufferIndex,
434                                ResourceManager& resourceManager,
435                                RenderQueue& renderQueue )
436 {
437   DALI_ASSERT_DEBUG( rootNode.IsRoot() );
438
439   // Short-circuit for invisible nodes
440   if ( !rootNode.IsVisible( updateBufferIndex ) )
441   {
442     return 0;
443   }
444
445   // If the root node was not previously visible
446   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
447   if ( !rootNode.IsVisible( previousBuffer ) )
448   {
449     // The node was skipped in the previous update; it must recalculate everything
450     rootNode.SetAllDirtyFlags();
451   }
452
453   int nodeDirtyFlags( rootNode.GetDirtyFlags() );
454
455   int cumulativeDirtyFlags = nodeDirtyFlags;
456
457   UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
458
459   UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
460
461   DrawMode::Type drawMode( rootNode.GetDrawMode() );
462
463   // recurse children
464   NodeContainer& children = rootNode.GetChildren();
465   const NodeIter endIter = children.End();
466   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
467   {
468     Node& child = **iter;
469     cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
470                                                        nodeDirtyFlags,
471                                                        updateBufferIndex,
472                                                        resourceManager,
473                                                        renderQueue,
474                                                        rootNode,
475                                                        drawMode );
476   }
477
478   return cumulativeDirtyFlags;
479 }
480
481 } // namespace SceneGraph
482
483 } // namespace Internal
484
485 } // namespace Dali