[dali_1.0.32] Merge branch 'tizen'
[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 UpdateNodeGeometry( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
139 {
140   if ( nodeDirtyFlags & SizeFlag )
141   {
142     Vector3 geometryScale( 1.0f, 1.0f, 1.0f );
143
144     if ( node.GetTransmitGeometryScaling() )
145     {
146       const Vector3& requiredSize = node.GetSize( updateBufferIndex );
147       geometryScale = FitKeepAspectRatio( requiredSize, node.GetInitialVolume() );
148     }
149
150     if ( node.GetGeometryScale() != geometryScale )
151     {
152       node.SetGeometryScale( geometryScale );
153     }
154   }
155 }
156
157 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
158 {
159   // If the transform values need to be reinherited
160   if ( nodeDirtyFlags & TransformFlag )
161   {
162     rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
163     rootNode.SetWorldRotation( updateBufferIndex, rootNode.GetRotation( updateBufferIndex ) );
164     rootNode.SetWorldScale   ( updateBufferIndex, rootNode.GetScale   ( updateBufferIndex ) );
165   }
166   else
167   {
168     // Copy previous value, in case they changed in the previous frame
169     rootNode.CopyPreviousWorldRotation( updateBufferIndex );
170     rootNode.CopyPreviousWorldScale( updateBufferIndex );
171     rootNode.CopyPreviousWorldPosition( updateBufferIndex );
172   }
173 }
174
175 /**
176  * Updates transform values for the given node if the transform flag is dirty.
177  * This includes applying a new size should the SizeMode require it.
178  * Note that this will cause the size dirty flag to be set. This is why we pass
179  * the dirty flags in by reference.
180  * @param[in]     node The node to update
181  * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
182  * @param[in]     updateBufferIndex The current index to use for this frame
183  */
184 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
185 {
186   // If the transform values need to be reinherited
187   if( nodeDirtyFlags & TransformFlag )
188   {
189     // Handle size relative to parent modes.
190     // This must be delt with before rotation/translation as otherwise anything
191     // anchored to a corner of this child would appear at the wrong position.
192     // The size dirty flag is modified if the size is being overridden.
193     // Note: Switch is in order of use-case commonality.
194     switch( node.GetSizeMode() )
195     {
196       case USE_OWN_SIZE:
197       {
198         // Completely ignore the parents size.
199         break;
200       }
201
202       case SIZE_EQUAL_TO_PARENT:
203       {
204         // Set the nodes size to that of the parent.
205         node.SetSize( updateBufferIndex, node.GetParent()->GetSize( updateBufferIndex ) );
206         nodeDirtyFlags |= SizeFlag;
207         break;
208       }
209
210       case SIZE_RELATIVE_TO_PARENT:
211       {
212         // Set the nodes size to the parents multiplied by a user defined value.
213         node.SetSize( updateBufferIndex, node.GetSizeModeFactor() * node.GetParent()->GetSize( updateBufferIndex ) );
214         nodeDirtyFlags |= SizeFlag;
215         break;
216       }
217
218       case SIZE_FIXED_OFFSET_FROM_PARENT:
219       {
220         // Set the nodes size to the parents plus a user defined value.
221         node.SetSize( updateBufferIndex, node.GetSizeModeFactor() + node.GetParent()->GetSize( updateBufferIndex ) );
222         nodeDirtyFlags |= SizeFlag;
223         break;
224       }
225     }
226
227     // With a non-central anchor-point, the world rotation and scale affects the world position.
228     // Therefore the world rotation & scale must be updated before the world position.
229     if( node.IsRotationInherited() )
230     {
231       node.InheritWorldRotation( updateBufferIndex );
232     }
233     else
234     {
235       node.SetWorldRotation( updateBufferIndex, node.GetRotation( updateBufferIndex ) );
236     }
237
238     if( node.IsScaleInherited() )
239     {
240       node.InheritWorldScale( updateBufferIndex );
241     }
242     else
243     {
244       node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
245     }
246
247     node.InheritWorldPosition( updateBufferIndex );
248   }
249   else
250   {
251     // Copy inherited values, if those changed in the previous frame
252     node.CopyPreviousWorldRotation( updateBufferIndex );
253     node.CopyPreviousWorldScale( updateBufferIndex );
254     node.CopyPreviousWorldPosition( updateBufferIndex );
255     node.CopyPreviousSize( updateBufferIndex );
256   }
257 }
258
259 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
260 {
261   // If world-matrix needs to be recalculated
262   if ( nodeDirtyFlags & TransformFlag )
263   {
264     if( node.GetInhibitLocalTransform() )
265     {
266       node.SetWorldMatrix( updateBufferIndex,
267                            node.GetWorldScale(updateBufferIndex),
268                            node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
269                            node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
270     }
271     else
272     {
273       node.SetWorldMatrix( updateBufferIndex,
274                            node.GetWorldScale(updateBufferIndex),
275                            node.GetWorldRotation(updateBufferIndex),
276                            node.GetWorldPosition(updateBufferIndex) );
277     }
278   }
279   else
280   {
281     node.CopyPreviousWorldMatrix( updateBufferIndex );
282   }
283 }
284
285 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
286 {
287   /**
288    * If world-matrix needs to be recalculated.
289    */
290   if ( ( nodeDirtyFlags & TransformFlag ) ||
291          updatedRenderable.IsScaleForSizeDirty() )
292   {
293     if( updatedRenderable.UsesGeometryScaling() )
294     {
295       // scaling, i.e. Text or Mesh
296       Vector3 scaling;
297       updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
298       if( node.GetInhibitLocalTransform() )
299       {
300         node.SetWorldMatrix( updateBufferIndex,
301                              node.GetWorldScale(updateBufferIndex) * scaling,
302                              node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
303                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
304       }
305       else
306       {
307         node.SetWorldMatrix( updateBufferIndex,
308                              node.GetWorldScale(updateBufferIndex) * scaling,
309                              node.GetWorldRotation(updateBufferIndex),
310                              node.GetWorldPosition(updateBufferIndex) );
311       }
312     }
313     else
314     {
315       // no scaling, i.e. Image
316       if( node.GetInhibitLocalTransform() )
317       {
318         node.SetWorldMatrix( updateBufferIndex,
319                              node.GetWorldScale(updateBufferIndex),
320                              node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
321                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
322       }
323       else
324       {
325         node.SetWorldMatrix( updateBufferIndex,
326                              node.GetWorldScale(updateBufferIndex),
327                              node.GetWorldRotation(updateBufferIndex),
328                              node.GetWorldPosition(updateBufferIndex) );
329       }
330     }
331   }
332   else
333   {
334     node.CopyPreviousWorldMatrix( updateBufferIndex );
335   }
336 }
337
338 /**
339  * Update an attachment.
340  * @return An updated renderable attachment if one was ready.
341  */
342 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
343                                                Node& node,
344                                                BufferIndex updateBufferIndex,
345                                                ResourceManager& resourceManager,
346                                                int nodeDirtyFlags )
347 {
348   // Allow attachments to do specialised processing during updates
349   attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
350
351   RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
352   if( renderable )
353   {
354     // Notify renderables when size has changed
355     // Size can change while node was invisible so we need to check size again if we were previously invisible
356     if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
357     {
358       renderable->SizeChanged( updateBufferIndex );
359     }
360
361     // check if node is visible
362     if( renderable->ResolveVisibility( updateBufferIndex ) )
363     {
364       renderable->PrepareResources( updateBufferIndex, resourceManager );
365     }
366   }
367   return renderable;
368 }
369
370 inline void AddRenderableToLayer( Layer& layer,
371                                   RenderableAttachment& renderable,
372                                   BufferIndex updateBufferIndex,
373                                   int inheritedDrawMode )
374 {
375   // The renderables are stored into the opaque list temporarily for PrepareRenderables()
376   // step. The list is cleared by ProcessRenderTasks().
377   layer.opaqueRenderables.push_back( &renderable );
378 }
379
380 /**
381  * This is called recursively for all children of the root Node
382  */
383 inline int UpdateNodesAndAttachments( Node& node,
384                                       int parentFlags,
385                                       BufferIndex updateBufferIndex,
386                                       ResourceManager& resourceManager,
387                                       RenderQueue& renderQueue,
388                                       Layer& currentLayer,
389                                       int inheritedDrawMode )
390 {
391   Layer* layer = &currentLayer;
392
393   // Short-circuit for invisible nodes
394   if ( !node.IsVisible( updateBufferIndex ) )
395   {
396     return 0;
397   }
398
399   // If the node was not previously visible
400   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
401   if ( !node.IsVisible( previousBuffer ) )
402   {
403     // The node was skipped in the previous update; it must recalculate everything
404     node.SetAllDirtyFlags();
405   }
406
407   // Some dirty flags are inherited from parent
408   int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
409
410   int cumulativeDirtyFlags = nodeDirtyFlags;
411
412   if ( node.IsLayer() )
413   {
414     // all childs go to this layer
415     layer = node.GetLayer();
416
417     // assume layer is clean to begin with
418     layer->SetReuseRenderers( updateBufferIndex, true );
419
420     // Layers do not inherit the DrawMode from their parents
421     inheritedDrawMode = DrawMode::NORMAL;
422   }
423   DALI_ASSERT_DEBUG( NULL != layer );
424
425   UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
426
427   UpdateNodeGeometry( node, nodeDirtyFlags, updateBufferIndex );
428
429   // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
430   // It is important that the modified version of these flags are used by the RenderableAttachment.
431   UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
432
433   // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
434   inheritedDrawMode |= node.GetDrawMode();
435
436   if ( node.HasAttachment() )
437   {
438     /*
439      * Add renderables for the children into the current Layer
440      */
441     RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
442                                                          node,
443                                                          updateBufferIndex,
444                                                          resourceManager,
445                                                          nodeDirtyFlags );
446
447
448     if( NULL != renderable )
449     {
450       // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
451       UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
452
453       // The attachment is ready to render, so it is added to a set of renderables.
454       AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
455     }
456   }
457   else if( node.IsObserved() )
458   {
459     // This node is being used as a property input for an animation, constraint,
460     // camera or bone. Ensure it's matrix is updated
461     UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
462   }
463
464   // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
465   // also if node has been deleted, dont reuse old render items
466   if( nodeDirtyFlags & RenderableUpdateFlags )
467   {
468     layer->SetReuseRenderers( updateBufferIndex, false );
469   }
470
471   // recurse children
472   NodeContainer& children = node.GetChildren();
473   const NodeIter endIter = children.End();
474   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
475   {
476     Node& child = **iter;
477     cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
478                                                       nodeDirtyFlags,
479                                                       updateBufferIndex,
480                                                       resourceManager,
481                                                       renderQueue,
482                                                       *layer,
483                                                       inheritedDrawMode );
484   }
485
486   return cumulativeDirtyFlags;
487 }
488
489 /**
490  * The root node is treated separately; it cannot inherit values since it has no parent
491  */
492 int UpdateNodesAndAttachments( Layer& rootNode,
493                                BufferIndex updateBufferIndex,
494                                ResourceManager& resourceManager,
495                                RenderQueue& renderQueue )
496 {
497   DALI_ASSERT_DEBUG( rootNode.IsRoot() );
498
499   // Short-circuit for invisible nodes
500   if ( !rootNode.IsVisible( updateBufferIndex ) )
501   {
502     return 0;
503   }
504
505   // If the root node was not previously visible
506   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
507   if ( !rootNode.IsVisible( previousBuffer ) )
508   {
509     // The node was skipped in the previous update; it must recalculate everything
510     rootNode.SetAllDirtyFlags();
511   }
512
513   int nodeDirtyFlags( rootNode.GetDirtyFlags() );
514
515   int cumulativeDirtyFlags = nodeDirtyFlags;
516
517   UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
518
519   UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
520
521   DrawMode::Type drawMode( rootNode.GetDrawMode() );
522
523   // recurse children
524   NodeContainer& children = rootNode.GetChildren();
525   const NodeIter endIter = children.End();
526   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
527   {
528     Node& child = **iter;
529     cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
530                                                        nodeDirtyFlags,
531                                                        updateBufferIndex,
532                                                        resourceManager,
533                                                        renderQueue,
534                                                        rootNode,
535                                                        drawMode );
536   }
537
538   return cumulativeDirtyFlags;
539 }
540
541 } // namespace SceneGraph
542
543 } // namespace Internal
544
545 } // namespace Dali