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