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 node.SetWorldMatrix( updateBufferIndex,
206 node.GetWorldScale(updateBufferIndex),
207 node.GetWorldOrientation(updateBufferIndex),
208 node.GetWorldPosition(updateBufferIndex) );
212 node.CopyPreviousWorldMatrix( updateBufferIndex );
217 * This is called recursively for all children of the root Node
219 inline int UpdateNodesAndAttachments( Node& node,
221 BufferIndex updateBufferIndex,
222 ResourceManager& resourceManager,
223 RenderQueue& renderQueue,
225 int inheritedDrawMode )
227 Layer* layer = ¤tLayer;
229 // Short-circuit for invisible nodes
230 if ( !node.IsVisible( updateBufferIndex ) )
235 // If the node was not previously visible
236 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
237 if ( !node.IsVisible( previousBuffer ) )
239 // The node was skipped in the previous update; it must recalculate everything
240 node.SetAllDirtyFlags();
243 // Some dirty flags are inherited from parent
244 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
246 int cumulativeDirtyFlags = nodeDirtyFlags;
248 Layer* nodeIsLayer( node.GetLayer() );
251 // all childs go to this layer
254 // assume layer is clean to begin with
255 layer->SetReuseRenderers( updateBufferIndex, true );
257 // Layers do not inherit the DrawMode from their parents
258 inheritedDrawMode = DrawMode::NORMAL;
260 DALI_ASSERT_DEBUG( NULL != layer );
262 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
264 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function
265 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
267 // Setting STENCIL will override OVERLAY_2D, if that would otherwise have been inherited.
268 inheritedDrawMode |= node.GetDrawMode();
270 if ( node.HasAttachment() )
272 node.GetAttachment().Update( updateBufferIndex, node, nodeDirtyFlags );
274 else if( node.IsObserved() || node.GetRendererCount() )
276 // This node is being used as a property input for an animation, constraint,
277 // camera or bone. Ensure it's matrix is updated
278 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
281 node.PrepareRender( updateBufferIndex );
284 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
285 // also if node has been deleted, dont reuse old render items
286 if( nodeDirtyFlags & RenderableUpdateFlags )
288 layer->SetReuseRenderers( updateBufferIndex, false );
292 NodeContainer& children = node.GetChildren();
293 const NodeIter endIter = children.End();
294 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
296 Node& child = **iter;
297 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
306 return cumulativeDirtyFlags;
310 * The root node is treated separately; it cannot inherit values since it has no parent
312 int UpdateNodesAndAttachments( Layer& rootNode,
313 BufferIndex updateBufferIndex,
314 ResourceManager& resourceManager,
315 RenderQueue& renderQueue )
317 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
319 // Short-circuit for invisible nodes
320 if ( DALI_UNLIKELY( !rootNode.IsVisible( updateBufferIndex ) ) ) // almost never ever true
325 // If the root node was not previously visible
326 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
327 if ( DALI_UNLIKELY( !rootNode.IsVisible( previousBuffer ) ) ) // almost never ever true
329 // The node was skipped in the previous update; it must recalculate everything
330 rootNode.SetAllDirtyFlags();
333 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
335 int cumulativeDirtyFlags = nodeDirtyFlags;
337 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
339 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
341 DrawMode::Type drawMode( rootNode.GetDrawMode() );
344 NodeContainer& children = rootNode.GetChildren();
345 const NodeIter endIter = children.End();
346 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
348 Node& child = **iter;
349 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
358 return cumulativeDirtyFlags;
361 } // namespace SceneGraph
363 } // namespace Internal