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 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 );
85 * Constrain the children next
87 NodeContainer& children = node.GetChildren();
88 const NodeIter endIter = children.End();
89 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
92 ConstrainNodes( child, updateBufferIndex );
96 /******************************************************************************
97 ************************** Update node hierarchy *****************************
98 ******************************************************************************/
100 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
102 if ( nodeDirtyFlags & ColorFlag )
104 rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
108 // Copy previous value, in case it changed in the previous frame
109 rootNode.CopyPreviousWorldColor( updateBufferIndex );
113 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
115 // If opacity needs to be recalculated
116 if ( nodeDirtyFlags & ColorFlag )
118 node.InheritWorldColor( updateBufferIndex );
122 // Copy inherited value, if changed in the previous frame
123 node.CopyPreviousWorldColor( updateBufferIndex );
127 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
129 // If the transform values need to be reinherited
130 if ( nodeDirtyFlags & TransformFlag )
132 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
133 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
134 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
138 // Copy previous value, in case they changed in the previous frame
139 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
140 rootNode.CopyPreviousWorldScale( updateBufferIndex );
141 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
146 * Updates transform values for the given node if the transform flag is dirty.
147 * Note that this will cause the size dirty flag to be set. This is why we pass
148 * the dirty flags in by reference.
149 * @param[in] node The node to update
150 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
151 * @param[in] updateBufferIndex The current index to use for this frame
153 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
155 // If the transform values need to be reinherited
156 if( nodeDirtyFlags & TransformFlag )
158 // With a non-central anchor-point, the world rotation and scale affects the world position.
159 // Therefore the world rotation & scale must be updated before the world position.
160 if( node.IsOrientationInherited() )
162 node.InheritWorldOrientation( updateBufferIndex );
166 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
169 if( node.IsScaleInherited() )
171 node.InheritWorldScale( updateBufferIndex );
175 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
178 node.InheritWorldPosition( updateBufferIndex );
182 // Copy inherited values, if those changed in the previous frame
183 node.CopyPreviousWorldOrientation( updateBufferIndex );
184 node.CopyPreviousWorldScale( updateBufferIndex );
185 node.CopyPreviousWorldPosition( updateBufferIndex );
186 node.CopyPreviousSize( updateBufferIndex );
190 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
192 // If world-matrix needs to be recalculated
193 if ( nodeDirtyFlags & TransformFlag )
195 if( node.GetInhibitLocalTransform() )
197 node.SetWorldMatrix( updateBufferIndex,
198 node.GetWorldScale(updateBufferIndex),
199 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
200 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
204 node.SetWorldMatrix( updateBufferIndex,
205 node.GetWorldScale(updateBufferIndex),
206 node.GetWorldOrientation(updateBufferIndex),
207 node.GetWorldPosition(updateBufferIndex) );
212 node.CopyPreviousWorldMatrix( updateBufferIndex );
216 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
219 * If world-matrix needs to be recalculated.
221 if ( ( nodeDirtyFlags & TransformFlag ) ||
222 updatedRenderable.IsScaleForSizeDirty() )
224 if( updatedRenderable.UsesGeometryScaling() )
226 // TODO: MESH_REWORK : remove scale for size
228 updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
229 if( node.GetInhibitLocalTransform() )
231 node.SetWorldMatrix( updateBufferIndex,
232 node.GetWorldScale(updateBufferIndex) * scaling,
233 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
234 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
238 node.SetWorldMatrix( updateBufferIndex,
239 node.GetWorldScale(updateBufferIndex) * scaling,
240 node.GetWorldOrientation(updateBufferIndex),
241 node.GetWorldPosition(updateBufferIndex) );
246 // no scaling, i.e. Image
247 if( node.GetInhibitLocalTransform() )
249 node.SetWorldMatrix( updateBufferIndex,
250 node.GetWorldScale(updateBufferIndex),
251 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
252 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
256 node.SetWorldMatrix( updateBufferIndex,
257 node.GetWorldScale(updateBufferIndex),
258 node.GetWorldOrientation(updateBufferIndex),
259 node.GetWorldPosition(updateBufferIndex) );
265 node.CopyPreviousWorldMatrix( updateBufferIndex );
270 * Update an attachment.
271 * @return An updated renderable attachment if one was ready.
273 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
275 BufferIndex updateBufferIndex,
276 ResourceManager& resourceManager,
279 // Allow attachments to do specialised processing during updates
280 attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
282 RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
285 // Notify renderables when size has changed
286 // Size can change while node was invisible so we need to check size again if we were previously invisible
287 if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
289 renderable->SizeChanged( updateBufferIndex );
292 // check if node is visible
293 if( renderable->ResolveVisibility( updateBufferIndex ) )
295 renderable->PrepareResources( updateBufferIndex, resourceManager );
301 inline void AddRenderableToLayer( Layer& layer,
302 RenderableAttachment& renderable,
303 BufferIndex updateBufferIndex,
304 int inheritedDrawMode )
306 // The renderables are stored into the opaque list temporarily for PrepareRenderables()
307 // step. The list is cleared by ProcessRenderTasks().
308 layer.opaqueRenderables.push_back( &renderable );
312 * This is called recursively for all children of the root Node
314 inline int UpdateNodesAndAttachments( Node& node,
316 BufferIndex updateBufferIndex,
317 ResourceManager& resourceManager,
318 RenderQueue& renderQueue,
320 int inheritedDrawMode )
322 Layer* layer = ¤tLayer;
324 // Short-circuit for invisible nodes
325 if ( !node.IsVisible( updateBufferIndex ) )
330 // If the node was not previously visible
331 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
332 if ( !node.IsVisible( previousBuffer ) )
334 // The node was skipped in the previous update; it must recalculate everything
335 node.SetAllDirtyFlags();
338 // Some dirty flags are inherited from parent
339 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
341 int cumulativeDirtyFlags = nodeDirtyFlags;
343 if ( node.IsLayer() )
345 // all childs go to this layer
346 layer = node.GetLayer();
348 // assume layer is clean to begin with
349 layer->SetReuseRenderers( updateBufferIndex, true );
351 // Layers do not inherit the DrawMode from their parents
352 inheritedDrawMode = DrawMode::NORMAL;
354 DALI_ASSERT_DEBUG( NULL != layer );
356 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
358 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
359 // It is important that the modified version of these flags are used by the RenderableAttachment.
360 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
362 // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
363 inheritedDrawMode |= node.GetDrawMode();
365 if ( node.HasAttachment() )
368 * Add renderables for the children into the current Layer
370 RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
377 if( NULL != renderable )
379 // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
380 UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
382 // The attachment is ready to render, so it is added to a set of renderables.
383 AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
386 else if( node.IsObserved() )
388 // This node is being used as a property input for an animation, constraint,
389 // camera or bone. Ensure it's matrix is updated
390 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
393 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
394 // also if node has been deleted, dont reuse old render items
395 if( nodeDirtyFlags & RenderableUpdateFlags )
397 layer->SetReuseRenderers( updateBufferIndex, false );
401 NodeContainer& children = node.GetChildren();
402 const NodeIter endIter = children.End();
403 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
405 Node& child = **iter;
406 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
415 return cumulativeDirtyFlags;
419 * The root node is treated separately; it cannot inherit values since it has no parent
421 int UpdateNodesAndAttachments( Layer& rootNode,
422 BufferIndex updateBufferIndex,
423 ResourceManager& resourceManager,
424 RenderQueue& renderQueue )
426 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
428 // Short-circuit for invisible nodes
429 if ( !rootNode.IsVisible( updateBufferIndex ) )
434 // If the root node was not previously visible
435 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
436 if ( !rootNode.IsVisible( previousBuffer ) )
438 // The node was skipped in the previous update; it must recalculate everything
439 rootNode.SetAllDirtyFlags();
442 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
444 int cumulativeDirtyFlags = nodeDirtyFlags;
446 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
448 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
450 DrawMode::Type drawMode( rootNode.GetDrawMode() );
453 NodeContainer& children = rootNode.GetChildren();
454 const NodeIter endIter = children.End();
455 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
457 Node& child = **iter;
458 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
467 return cumulativeDirtyFlags;
470 } // namespace SceneGraph
472 } // namespace Internal