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/scene-graph-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 unsigned int ConstrainPropertyOwner( PropertyOwner& propertyOwner, BufferIndex updateBufferIndex )
64 unsigned int activeCount = 0;
66 ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
68 const ConstraintIter endIter = constraints.End();
69 for( ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter )
71 ConstraintBase& constraint = **iter;
72 constraint.Apply( updateBufferIndex );
74 if( constraint.mWeight[updateBufferIndex] < 1.0f )
76 // this constraint is still being applied
85 * Recursively apply the constraints on the nodes
86 * @param node to constraint
87 * @param updateBufferIndex buffer index to use
88 * @return number of active constraints
90 unsigned int ConstrainNodes( Node& node, BufferIndex updateBufferIndex )
92 unsigned int activeCount = ConstrainPropertyOwner( node, 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 activeCount += 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 * This includes applying a new size should the SizeMode require it.
159 * Note that this will cause the size dirty flag to be set. This is why we pass
160 * the dirty flags in by reference.
161 * @param[in] node The node to update
162 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
163 * @param[in] updateBufferIndex The current index to use for this frame
165 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
167 // If the transform values need to be reinherited
168 if( nodeDirtyFlags & TransformFlag )
170 // With a non-central anchor-point, the world rotation and scale affects the world position.
171 // Therefore the world rotation & scale must be updated before the world position.
172 if( node.IsOrientationInherited() )
174 node.InheritWorldOrientation( updateBufferIndex );
178 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
181 if( node.IsScaleInherited() )
183 node.InheritWorldScale( updateBufferIndex );
187 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
190 node.InheritWorldPosition( updateBufferIndex );
194 // Copy inherited values, if those changed in the previous frame
195 node.CopyPreviousWorldOrientation( updateBufferIndex );
196 node.CopyPreviousWorldScale( updateBufferIndex );
197 node.CopyPreviousWorldPosition( updateBufferIndex );
198 node.CopyPreviousSize( updateBufferIndex );
202 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
204 // If world-matrix needs to be recalculated
205 if ( nodeDirtyFlags & TransformFlag )
207 if( node.GetInhibitLocalTransform() )
209 node.SetWorldMatrix( updateBufferIndex,
210 node.GetWorldScale(updateBufferIndex),
211 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
212 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
216 node.SetWorldMatrix( updateBufferIndex,
217 node.GetWorldScale(updateBufferIndex),
218 node.GetWorldOrientation(updateBufferIndex),
219 node.GetWorldPosition(updateBufferIndex) );
224 node.CopyPreviousWorldMatrix( updateBufferIndex );
228 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
231 * If world-matrix needs to be recalculated.
233 if ( ( nodeDirtyFlags & TransformFlag ) ||
234 updatedRenderable.IsScaleForSizeDirty() )
236 if( updatedRenderable.UsesGeometryScaling() )
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.opaqueRenderables.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, 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() )
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 any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
405 // also if node has been deleted, dont reuse old render items
406 if( nodeDirtyFlags & RenderableUpdateFlags )
408 layer->SetReuseRenderers( updateBufferIndex, false );
412 NodeContainer& children = node.GetChildren();
413 const NodeIter endIter = children.End();
414 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
416 Node& child = **iter;
417 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
426 return cumulativeDirtyFlags;
430 * The root node is treated separately; it cannot inherit values since it has no parent
432 int UpdateNodesAndAttachments( Layer& rootNode,
433 BufferIndex updateBufferIndex,
434 ResourceManager& resourceManager,
435 RenderQueue& renderQueue )
437 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
439 // Short-circuit for invisible nodes
440 if ( !rootNode.IsVisible( updateBufferIndex ) )
445 // If the root node was not previously visible
446 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
447 if ( !rootNode.IsVisible( previousBuffer ) )
449 // The node was skipped in the previous update; it must recalculate everything
450 rootNode.SetAllDirtyFlags();
453 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
455 int cumulativeDirtyFlags = nodeDirtyFlags;
457 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
459 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
461 DrawMode::Type drawMode( rootNode.GetDrawMode() );
464 NodeContainer& children = rootNode.GetChildren();
465 const NodeIter endIter = children.End();
466 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
468 Node& child = **iter;
469 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
478 return cumulativeDirtyFlags;
481 } // namespace SceneGraph
483 } // namespace Internal