2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/manager/update-algorithms.h>
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/render-renderer.h>
36 #include <dali/integration-api/debug.h>
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gUpdateFilter = Debug::Filter::New(Debug::Concise, false, "LOG_UPDATE_ALGORITHMS");
51 /******************************************************************************
52 *********************** Apply Constraints ************************************
53 ******************************************************************************/
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
62 void ConstrainPropertyOwner( PropertyOwner& propertyOwner, BufferIndex updateBufferIndex )
64 ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
66 const ConstraintIter endIter = constraints.End();
67 for( ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter )
69 ConstraintBase& constraint = **iter;
70 constraint.Apply( updateBufferIndex );
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
80 void ConstrainNodes( Node& node, BufferIndex updateBufferIndex )
82 ConstrainPropertyOwner( node, updateBufferIndex );
84 if( node.HasAttachment() )
86 // @todo Remove dynamic cast.
87 NodeAttachment& attachment = node.GetAttachment();
88 PropertyOwner* propertyOwner = dynamic_cast< PropertyOwner* >( &attachment );
89 if( propertyOwner != NULL )
91 ConstrainPropertyOwner( *propertyOwner, updateBufferIndex );
96 * Constrain the children next
98 NodeContainer& children = node.GetChildren();
99 const NodeIter endIter = children.End();
100 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
102 Node& child = **iter;
103 ConstrainNodes( child, updateBufferIndex );
107 /******************************************************************************
108 ************************** Update node hierarchy *****************************
109 ******************************************************************************/
111 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
113 if ( nodeDirtyFlags & ColorFlag )
115 rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
119 // Copy previous value, in case it changed in the previous frame
120 rootNode.CopyPreviousWorldColor( updateBufferIndex );
124 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
126 // If opacity needs to be recalculated
127 if ( nodeDirtyFlags & ColorFlag )
129 node.InheritWorldColor( updateBufferIndex );
133 // Copy inherited value, if changed in the previous frame
134 node.CopyPreviousWorldColor( updateBufferIndex );
138 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
140 // If the transform values need to be reinherited
141 if ( nodeDirtyFlags & TransformFlag )
143 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
144 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
145 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
149 // Copy previous value, in case they changed in the previous frame
150 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
151 rootNode.CopyPreviousWorldScale( updateBufferIndex );
152 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
157 * Updates transform values for the given node if the transform flag is dirty.
158 * Note that this will cause the size dirty flag to be set. This is why we pass
159 * the dirty flags in by reference.
160 * @param[in] node The node to update
161 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
162 * @param[in] updateBufferIndex The current index to use for this frame
164 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
166 // If the transform values need to be reinherited
167 if( nodeDirtyFlags & TransformFlag )
169 // With a non-central anchor-point, the world rotation and scale affects the world position.
170 // Therefore the world rotation & scale must be updated before the world position.
171 if( node.IsOrientationInherited() )
173 node.InheritWorldOrientation( updateBufferIndex );
177 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
180 if( node.IsScaleInherited() )
182 node.InheritWorldScale( updateBufferIndex );
186 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
189 node.InheritWorldPosition( updateBufferIndex );
193 // Copy inherited values, if those changed in the previous frame
194 node.CopyPreviousWorldOrientation( updateBufferIndex );
195 node.CopyPreviousWorldScale( updateBufferIndex );
196 node.CopyPreviousWorldPosition( updateBufferIndex );
197 node.CopyPreviousSize( updateBufferIndex );
201 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
203 // If world-matrix needs to be recalculated
204 if ( nodeDirtyFlags & TransformFlag )
206 if( node.GetInhibitLocalTransform() )
208 node.SetWorldMatrix( updateBufferIndex,
209 node.GetWorldScale(updateBufferIndex),
210 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
211 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
215 node.SetWorldMatrix( updateBufferIndex,
216 node.GetWorldScale(updateBufferIndex),
217 node.GetWorldOrientation(updateBufferIndex),
218 node.GetWorldPosition(updateBufferIndex) );
223 node.CopyPreviousWorldMatrix( updateBufferIndex );
227 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
230 * If world-matrix needs to be recalculated.
232 if ( ( nodeDirtyFlags & TransformFlag ) ||
233 updatedRenderable.IsScaleForSizeDirty() )
235 if( updatedRenderable.UsesGeometryScaling() )
237 // TODO: MESH_REWORK : remove scale for size
239 updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
240 if( node.GetInhibitLocalTransform() )
242 node.SetWorldMatrix( updateBufferIndex,
243 node.GetWorldScale(updateBufferIndex) * scaling,
244 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
245 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
249 node.SetWorldMatrix( updateBufferIndex,
250 node.GetWorldScale(updateBufferIndex) * scaling,
251 node.GetWorldOrientation(updateBufferIndex),
252 node.GetWorldPosition(updateBufferIndex) );
257 // no scaling, i.e. Image
258 if( node.GetInhibitLocalTransform() )
260 node.SetWorldMatrix( updateBufferIndex,
261 node.GetWorldScale(updateBufferIndex),
262 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
263 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
267 node.SetWorldMatrix( updateBufferIndex,
268 node.GetWorldScale(updateBufferIndex),
269 node.GetWorldOrientation(updateBufferIndex),
270 node.GetWorldPosition(updateBufferIndex) );
276 node.CopyPreviousWorldMatrix( updateBufferIndex );
281 * Update an attachment.
282 * @return An updated renderable attachment if one was ready.
284 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
286 BufferIndex updateBufferIndex,
287 ResourceManager& resourceManager,
290 // Allow attachments to do specialised processing during updates
291 attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
293 RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
296 // Notify renderables when size has changed
297 // Size can change while node was invisible so we need to check size again if we were previously invisible
298 if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
300 renderable->SizeChanged( updateBufferIndex );
303 // check if node is visible
304 if( renderable->ResolveVisibility( updateBufferIndex ) )
306 renderable->PrepareResources( updateBufferIndex, resourceManager );
312 inline void AddRenderableToLayer( Layer& layer,
313 RenderableAttachment& renderable,
314 BufferIndex updateBufferIndex,
315 int inheritedDrawMode )
317 // The renderables are stored into the opaque list temporarily for PrepareRenderables()
318 // step. The list is cleared by ProcessRenderTasks().
319 layer.colorRenderables.push_back( &renderable );
323 * This is called recursively for all children of the root Node
325 inline int UpdateNodesAndAttachments( Node& node,
327 BufferIndex updateBufferIndex,
328 ResourceManager& resourceManager,
329 RenderQueue& renderQueue,
331 int inheritedDrawMode )
333 Layer* layer = ¤tLayer;
335 // Short-circuit for invisible nodes
336 if ( !node.IsVisible( updateBufferIndex ) )
341 // If the node was not previously visible
342 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
343 if ( !node.IsVisible( previousBuffer ) )
345 // The node was skipped in the previous update; it must recalculate everything
346 node.SetAllDirtyFlags();
349 // Some dirty flags are inherited from parent
350 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
352 int cumulativeDirtyFlags = nodeDirtyFlags;
354 if ( node.IsLayer() )
356 // all childs go to this layer
357 layer = node.GetLayer();
359 // assume layer is clean to begin with
360 layer->SetReuseRenderers( updateBufferIndex, true );
362 // Layers do not inherit the DrawMode from their parents
363 inheritedDrawMode = DrawMode::NORMAL;
365 DALI_ASSERT_DEBUG( NULL != layer );
367 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
369 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
370 // It is important that the modified version of these flags are used by the RenderableAttachment.
371 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
373 // Setting STENCIL will override OVERLAY_2D, if that would otherwise have been inherited.
374 inheritedDrawMode |= node.GetDrawMode();
376 if ( node.HasAttachment() )
379 * Add renderables for the children into the current Layer
381 RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
388 if( NULL != renderable )
390 // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
391 UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
393 // The attachment is ready to render, so it is added to a set of renderables.
394 AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
397 else if( node.IsObserved() || node.GetRendererCount() )
399 // This node is being used as a property input for an animation, constraint,
400 // camera or bone. Ensure it's matrix is updated
401 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
404 if( node.ResolveVisibility(updateBufferIndex) )
406 node.PrepareRender( updateBufferIndex );
410 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
411 // also if node has been deleted, dont reuse old render items
412 if( nodeDirtyFlags & RenderableUpdateFlags )
414 layer->SetReuseRenderers( updateBufferIndex, false );
418 NodeContainer& children = node.GetChildren();
419 const NodeIter endIter = children.End();
420 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
422 Node& child = **iter;
423 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
432 return cumulativeDirtyFlags;
436 * The root node is treated separately; it cannot inherit values since it has no parent
438 int UpdateNodesAndAttachments( Layer& rootNode,
439 BufferIndex updateBufferIndex,
440 ResourceManager& resourceManager,
441 RenderQueue& renderQueue )
443 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
445 // Short-circuit for invisible nodes
446 if ( !rootNode.IsVisible( updateBufferIndex ) )
451 // If the root node was not previously visible
452 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
453 if ( !rootNode.IsVisible( previousBuffer ) )
455 // The node was skipped in the previous update; it must recalculate everything
456 rootNode.SetAllDirtyFlags();
459 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
461 int cumulativeDirtyFlags = nodeDirtyFlags;
463 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
465 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
467 DrawMode::Type drawMode( rootNode.GetDrawMode() );
470 NodeContainer& children = rootNode.GetChildren();
471 const NodeIter endIter = children.End();
472 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
474 Node& child = **iter;
475 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
484 return cumulativeDirtyFlags;
487 } // namespace SceneGraph
489 } // namespace Internal