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