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