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/animation/scene-graph-constraint-base.h>
32 #include <dali/internal/update/nodes/scene-graph-layer.h>
33 #include <dali/internal/render/renderers/render-renderer.h>
35 #include <dali/integration-api/debug.h>
46 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gUpdateFilter = Debug::Filter::New(Debug::Concise, false, "LOG_UPDATE_ALGORITHMS");
50 /******************************************************************************
51 *********************** Apply Constraints ************************************
52 ******************************************************************************/
56 * Constrain the local properties of the PropertyOwner.
57 * @param propertyOwner to constrain
58 * @param updateBufferIndex buffer index to use
59 * @return The number of constraints that are still being applied
61 void ConstrainPropertyOwner( PropertyOwner& propertyOwner, BufferIndex updateBufferIndex )
63 ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
65 const ConstraintIter endIter = constraints.End();
66 for( ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter )
68 ConstraintBase& constraint = **iter;
69 constraint.Apply( updateBufferIndex );
74 * Recursively apply the constraints on the nodes
75 * @param node to constraint
76 * @param updateBufferIndex buffer index to use
77 * @return number of active constraints
79 void ConstrainNodes( Node& node, BufferIndex updateBufferIndex )
81 ConstrainPropertyOwner( node, updateBufferIndex );
83 if( node.HasAttachment() )
85 // @todo Remove dynamic cast.
86 NodeAttachment& attachment = node.GetAttachment();
87 PropertyOwner* propertyOwner = dynamic_cast< PropertyOwner* >( &attachment );
88 if( propertyOwner != NULL )
90 ConstrainPropertyOwner( *propertyOwner, updateBufferIndex );
95 * Constrain the children next
97 NodeContainer& children = node.GetChildren();
98 const NodeIter endIter = children.End();
99 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
101 Node& child = **iter;
102 ConstrainNodes( child, updateBufferIndex );
106 /******************************************************************************
107 ************************** Update node hierarchy *****************************
108 ******************************************************************************/
110 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
112 if ( nodeDirtyFlags & ColorFlag )
114 rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
118 // Copy previous value, in case it changed in the previous frame
119 rootNode.CopyPreviousWorldColor( updateBufferIndex );
123 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
125 // If opacity needs to be recalculated
126 if ( nodeDirtyFlags & ColorFlag )
128 node.InheritWorldColor( updateBufferIndex );
132 // Copy inherited value, if changed in the previous frame
133 node.CopyPreviousWorldColor( updateBufferIndex );
137 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
139 // If the transform values need to be reinherited
140 if ( nodeDirtyFlags & TransformFlag )
142 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
143 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
144 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
148 // Copy previous value, in case they changed in the previous frame
149 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
150 rootNode.CopyPreviousWorldScale( updateBufferIndex );
151 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
156 * Updates transform values for the given node if the transform flag is dirty.
157 * Note that this will cause the size dirty flag to be set. This is why we pass
158 * the dirty flags in by reference.
159 * @param[in] node The node to update
160 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
161 * @param[in] updateBufferIndex The current index to use for this frame
163 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
165 // If the transform values need to be reinherited
166 if( nodeDirtyFlags & TransformFlag )
168 // With a non-central anchor-point, the world rotation and scale affects the world position.
169 // Therefore the world rotation & scale must be updated before the world position.
170 if( node.IsOrientationInherited() )
172 node.InheritWorldOrientation( updateBufferIndex );
176 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
179 if( node.IsScaleInherited() )
181 node.InheritWorldScale( updateBufferIndex );
185 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
188 node.InheritWorldPosition( updateBufferIndex );
192 // Copy inherited values, if those changed in the previous frame
193 node.CopyPreviousWorldOrientation( updateBufferIndex );
194 node.CopyPreviousWorldScale( updateBufferIndex );
195 node.CopyPreviousWorldPosition( updateBufferIndex );
196 node.CopyPreviousSize( updateBufferIndex );
200 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
202 // If world-matrix needs to be recalculated
203 if ( nodeDirtyFlags & TransformFlag )
205 if( node.GetInhibitLocalTransform() )
207 node.SetWorldMatrix( updateBufferIndex,
208 node.GetWorldScale(updateBufferIndex),
209 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
210 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
214 node.SetWorldMatrix( updateBufferIndex,
215 node.GetWorldScale(updateBufferIndex),
216 node.GetWorldOrientation(updateBufferIndex),
217 node.GetWorldPosition(updateBufferIndex) );
222 node.CopyPreviousWorldMatrix( updateBufferIndex );
227 * This is called recursively for all children of the root Node
229 inline int UpdateNodesAndAttachments( Node& node,
231 BufferIndex updateBufferIndex,
232 ResourceManager& resourceManager,
233 RenderQueue& renderQueue,
235 int inheritedDrawMode )
237 Layer* layer = ¤tLayer;
239 // Short-circuit for invisible nodes
240 if ( !node.IsVisible( updateBufferIndex ) )
245 // If the node was not previously visible
246 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
247 if ( !node.IsVisible( previousBuffer ) )
249 // The node was skipped in the previous update; it must recalculate everything
250 node.SetAllDirtyFlags();
253 // Some dirty flags are inherited from parent
254 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
256 int cumulativeDirtyFlags = nodeDirtyFlags;
258 if ( node.IsLayer() )
260 // all childs go to this layer
261 layer = node.GetLayer();
263 // assume layer is clean to begin with
264 layer->SetReuseRenderers( updateBufferIndex, true );
266 // Layers do not inherit the DrawMode from their parents
267 inheritedDrawMode = DrawMode::NORMAL;
269 DALI_ASSERT_DEBUG( NULL != layer );
271 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
273 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function
274 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
276 // Setting STENCIL will override OVERLAY_2D, if that would otherwise have been inherited.
277 inheritedDrawMode |= node.GetDrawMode();
279 if ( node.HasAttachment() )
281 node.GetAttachment().Update( updateBufferIndex, node, nodeDirtyFlags );
283 else if( node.IsObserved() || node.GetRendererCount() )
285 // This node is being used as a property input for an animation, constraint,
286 // camera or bone. Ensure it's matrix is updated
287 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
290 if( node.ResolveVisibility(updateBufferIndex) )
292 node.PrepareRender( updateBufferIndex );
296 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
297 // also if node has been deleted, dont reuse old render items
298 if( nodeDirtyFlags & RenderableUpdateFlags )
300 layer->SetReuseRenderers( updateBufferIndex, false );
304 NodeContainer& children = node.GetChildren();
305 const NodeIter endIter = children.End();
306 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
308 Node& child = **iter;
309 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
318 return cumulativeDirtyFlags;
322 * The root node is treated separately; it cannot inherit values since it has no parent
324 int UpdateNodesAndAttachments( Layer& rootNode,
325 BufferIndex updateBufferIndex,
326 ResourceManager& resourceManager,
327 RenderQueue& renderQueue )
329 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
331 // Short-circuit for invisible nodes
332 if ( !rootNode.IsVisible( updateBufferIndex ) )
337 // If the root node was not previously visible
338 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
339 if ( !rootNode.IsVisible( previousBuffer ) )
341 // The node was skipped in the previous update; it must recalculate everything
342 rootNode.SetAllDirtyFlags();
345 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
347 int cumulativeDirtyFlags = nodeDirtyFlags;
349 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
351 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
353 DrawMode::Type drawMode( rootNode.GetDrawMode() );
356 NodeContainer& children = rootNode.GetChildren();
357 const NodeIter endIter = children.End();
358 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
360 Node& child = **iter;
361 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
370 return cumulativeDirtyFlags;
373 } // namespace SceneGraph
375 } // namespace Internal