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