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 );
84 if( node.HasAttachment() )
86 // @todo MESH_REWORK Remove dynamic cast.
87 // (Or, if RendererAttachment split into RendererPropertyOwner(?),
88 // do as separate pass as per other mesh objects - see also
89 // UpdateManager::ResetNodeProperty())
90 NodeAttachment& attachment = node.GetAttachment();
91 PropertyOwner* propertyOwner = dynamic_cast< PropertyOwner* >( &attachment );
92 if( propertyOwner != NULL )
94 ConstrainPropertyOwner( *propertyOwner, updateBufferIndex );
99 * Constrain the children next
101 NodeContainer& children = node.GetChildren();
102 const NodeIter endIter = children.End();
103 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
105 Node& child = **iter;
106 ConstrainNodes( child, updateBufferIndex );
110 /******************************************************************************
111 ************************** Update node hierarchy *****************************
112 ******************************************************************************/
114 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
116 if ( nodeDirtyFlags & ColorFlag )
118 rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
122 // Copy previous value, in case it changed in the previous frame
123 rootNode.CopyPreviousWorldColor( updateBufferIndex );
127 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
129 // If opacity needs to be recalculated
130 if ( nodeDirtyFlags & ColorFlag )
132 node.InheritWorldColor( updateBufferIndex );
136 // Copy inherited value, if changed in the previous frame
137 node.CopyPreviousWorldColor( updateBufferIndex );
141 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
143 // If the transform values need to be reinherited
144 if ( nodeDirtyFlags & TransformFlag )
146 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
147 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
148 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
152 // Copy previous value, in case they changed in the previous frame
153 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
154 rootNode.CopyPreviousWorldScale( updateBufferIndex );
155 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
160 * Updates transform values for the given node if the transform flag is dirty.
161 * Note that this will cause the size dirty flag to be set. This is why we pass
162 * the dirty flags in by reference.
163 * @param[in] node The node to update
164 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
165 * @param[in] updateBufferIndex The current index to use for this frame
167 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
169 // If the transform values need to be reinherited
170 if( nodeDirtyFlags & TransformFlag )
172 // With a non-central anchor-point, the world rotation and scale affects the world position.
173 // Therefore the world rotation & scale must be updated before the world position.
174 if( node.IsOrientationInherited() )
176 node.InheritWorldOrientation( updateBufferIndex );
180 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
183 if( node.IsScaleInherited() )
185 node.InheritWorldScale( updateBufferIndex );
189 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
192 node.InheritWorldPosition( updateBufferIndex );
196 // Copy inherited values, if those changed in the previous frame
197 node.CopyPreviousWorldOrientation( updateBufferIndex );
198 node.CopyPreviousWorldScale( updateBufferIndex );
199 node.CopyPreviousWorldPosition( updateBufferIndex );
200 node.CopyPreviousSize( updateBufferIndex );
204 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
206 // If world-matrix needs to be recalculated
207 if ( nodeDirtyFlags & TransformFlag )
209 if( node.GetInhibitLocalTransform() )
211 node.SetWorldMatrix( updateBufferIndex,
212 node.GetWorldScale(updateBufferIndex),
213 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
214 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
218 node.SetWorldMatrix( updateBufferIndex,
219 node.GetWorldScale(updateBufferIndex),
220 node.GetWorldOrientation(updateBufferIndex),
221 node.GetWorldPosition(updateBufferIndex) );
226 node.CopyPreviousWorldMatrix( updateBufferIndex );
230 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
233 * If world-matrix needs to be recalculated.
235 if ( ( nodeDirtyFlags & TransformFlag ) ||
236 updatedRenderable.IsScaleForSizeDirty() )
238 if( updatedRenderable.UsesGeometryScaling() )
240 // TODO: MESH_REWORK : remove scale for size
242 updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
243 if( node.GetInhibitLocalTransform() )
245 node.SetWorldMatrix( updateBufferIndex,
246 node.GetWorldScale(updateBufferIndex) * scaling,
247 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
248 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
252 node.SetWorldMatrix( updateBufferIndex,
253 node.GetWorldScale(updateBufferIndex) * scaling,
254 node.GetWorldOrientation(updateBufferIndex),
255 node.GetWorldPosition(updateBufferIndex) );
260 // no scaling, i.e. Image
261 if( node.GetInhibitLocalTransform() )
263 node.SetWorldMatrix( updateBufferIndex,
264 node.GetWorldScale(updateBufferIndex),
265 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
266 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
270 node.SetWorldMatrix( updateBufferIndex,
271 node.GetWorldScale(updateBufferIndex),
272 node.GetWorldOrientation(updateBufferIndex),
273 node.GetWorldPosition(updateBufferIndex) );
279 node.CopyPreviousWorldMatrix( updateBufferIndex );
284 * Update an attachment.
285 * @return An updated renderable attachment if one was ready.
287 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
289 BufferIndex updateBufferIndex,
290 ResourceManager& resourceManager,
293 // Allow attachments to do specialised processing during updates
294 attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
296 RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
299 // Notify renderables when size has changed
300 // Size can change while node was invisible so we need to check size again if we were previously invisible
301 if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
303 renderable->SizeChanged( updateBufferIndex );
306 // check if node is visible
307 if( renderable->ResolveVisibility( updateBufferIndex ) )
309 renderable->PrepareResources( updateBufferIndex, resourceManager );
315 inline void AddRenderableToLayer( Layer& layer,
316 RenderableAttachment& renderable,
317 BufferIndex updateBufferIndex,
318 int inheritedDrawMode )
320 // The renderables are stored into the opaque list temporarily for PrepareRenderables()
321 // step. The list is cleared by ProcessRenderTasks().
322 layer.opaqueRenderables.push_back( &renderable );
326 * This is called recursively for all children of the root Node
328 inline int UpdateNodesAndAttachments( Node& node,
330 BufferIndex updateBufferIndex,
331 ResourceManager& resourceManager,
332 RenderQueue& renderQueue,
334 int inheritedDrawMode )
336 Layer* layer = ¤tLayer;
338 // Short-circuit for invisible nodes
339 if ( !node.IsVisible( updateBufferIndex ) )
344 // If the node was not previously visible
345 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
346 if ( !node.IsVisible( previousBuffer ) )
348 // The node was skipped in the previous update; it must recalculate everything
349 node.SetAllDirtyFlags();
352 // Some dirty flags are inherited from parent
353 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
355 int cumulativeDirtyFlags = nodeDirtyFlags;
357 if ( node.IsLayer() )
359 // all childs go to this layer
360 layer = node.GetLayer();
362 // assume layer is clean to begin with
363 layer->SetReuseRenderers( updateBufferIndex, true );
365 // Layers do not inherit the DrawMode from their parents
366 inheritedDrawMode = DrawMode::NORMAL;
368 DALI_ASSERT_DEBUG( NULL != layer );
370 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
372 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
373 // It is important that the modified version of these flags are used by the RenderableAttachment.
374 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
376 // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
377 inheritedDrawMode |= node.GetDrawMode();
379 if ( node.HasAttachment() )
382 * Add renderables for the children into the current Layer
384 RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
391 if( NULL != renderable )
393 // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
394 UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
396 // The attachment is ready to render, so it is added to a set of renderables.
397 AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
400 else if( node.IsObserved() )
402 // This node is being used as a property input for an animation, constraint,
403 // camera or bone. Ensure it's matrix is updated
404 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
407 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
408 // also if node has been deleted, dont reuse old render items
409 if( nodeDirtyFlags & RenderableUpdateFlags )
411 layer->SetReuseRenderers( updateBufferIndex, false );
415 NodeContainer& children = node.GetChildren();
416 const NodeIter endIter = children.End();
417 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
419 Node& child = **iter;
420 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
429 return cumulativeDirtyFlags;
433 * The root node is treated separately; it cannot inherit values since it has no parent
435 int UpdateNodesAndAttachments( Layer& rootNode,
436 BufferIndex updateBufferIndex,
437 ResourceManager& resourceManager,
438 RenderQueue& renderQueue )
440 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
442 // Short-circuit for invisible nodes
443 if ( !rootNode.IsVisible( updateBufferIndex ) )
448 // If the root node was not previously visible
449 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
450 if ( !rootNode.IsVisible( previousBuffer ) )
452 // The node was skipped in the previous update; it must recalculate everything
453 rootNode.SetAllDirtyFlags();
456 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
458 int cumulativeDirtyFlags = nodeDirtyFlags;
460 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
462 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
464 DrawMode::Type drawMode( rootNode.GetDrawMode() );
467 NodeContainer& children = rootNode.GetChildren();
468 const NodeIter endIter = children.End();
469 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
471 Node& child = **iter;
472 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
481 return cumulativeDirtyFlags;
484 } // namespace SceneGraph
486 } // namespace Internal