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 );
199 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
201 // If world-matrix needs to be recalculated
202 if ( nodeDirtyFlags & TransformFlag )
204 node.SetWorldMatrix( updateBufferIndex,
205 node.GetWorldScale(updateBufferIndex),
206 node.GetWorldOrientation(updateBufferIndex),
207 node.GetWorldPosition(updateBufferIndex) );
211 node.CopyPreviousWorldMatrix( updateBufferIndex );
216 * This is called recursively for all children of the root Node
218 inline int UpdateNodesAndAttachments( Node& node,
220 BufferIndex updateBufferIndex,
221 ResourceManager& resourceManager,
222 RenderQueue& renderQueue,
224 int inheritedDrawMode )
226 Layer* layer = ¤tLayer;
228 // Short-circuit for invisible nodes
229 if ( !node.IsVisible( updateBufferIndex ) )
234 // If the node was not previously visible
235 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
236 if ( !node.IsVisible( previousBuffer ) )
238 // The node was skipped in the previous update; it must recalculate everything
239 node.SetAllDirtyFlags();
242 // Some dirty flags are inherited from parent
243 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
245 int cumulativeDirtyFlags = nodeDirtyFlags;
247 Layer* nodeIsLayer( node.GetLayer() );
250 // all childs go to this layer
253 // assume layer is clean to begin with
254 layer->SetReuseRenderers( updateBufferIndex, true );
256 // Layers do not inherit the DrawMode from their parents
257 inheritedDrawMode = DrawMode::NORMAL;
259 DALI_ASSERT_DEBUG( NULL != layer );
261 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
263 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function
264 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
266 // Setting STENCIL will override OVERLAY_2D, if that would otherwise have been inherited.
267 inheritedDrawMode |= node.GetDrawMode();
269 if ( node.HasAttachment() )
271 node.GetAttachment().Update( updateBufferIndex, node, nodeDirtyFlags );
273 else if( node.IsObserved() || node.GetRendererCount() )
275 // This node is being used as a property input for an animation, constraint,
276 // camera or bone. Ensure it's matrix is updated
277 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
280 node.PrepareRender( updateBufferIndex );
283 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
284 // also if node has been deleted, dont reuse old render items
285 if( nodeDirtyFlags & RenderableUpdateFlags )
287 layer->SetReuseRenderers( updateBufferIndex, false );
291 NodeContainer& children = node.GetChildren();
292 const NodeIter endIter = children.End();
293 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
295 Node& child = **iter;
296 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
305 return cumulativeDirtyFlags;
309 * The root node is treated separately; it cannot inherit values since it has no parent
311 int UpdateNodesAndAttachments( Layer& rootNode,
312 BufferIndex updateBufferIndex,
313 ResourceManager& resourceManager,
314 RenderQueue& renderQueue )
316 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
318 // Short-circuit for invisible nodes
319 if ( DALI_UNLIKELY( !rootNode.IsVisible( updateBufferIndex ) ) ) // almost never ever true
324 // If the root node was not previously visible
325 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
326 if ( DALI_UNLIKELY( !rootNode.IsVisible( previousBuffer ) ) ) // almost never ever true
328 // The node was skipped in the previous update; it must recalculate everything
329 rootNode.SetAllDirtyFlags();
332 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
334 int cumulativeDirtyFlags = nodeDirtyFlags;
336 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
338 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
340 DrawMode::Type drawMode( rootNode.GetDrawMode() );
343 NodeContainer& children = rootNode.GetChildren();
344 const NodeIter endIter = children.End();
345 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
347 Node& child = **iter;
348 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
357 return cumulativeDirtyFlags;
360 } // namespace SceneGraph
362 } // namespace Internal