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 UpdateNodeGeometry( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
140 if ( nodeDirtyFlags & SizeFlag )
142 Vector3 geometryScale( 1.0f, 1.0f, 1.0f );
144 if ( node.GetTransmitGeometryScaling() )
146 const Vector3& requiredSize = node.GetSize( updateBufferIndex );
147 geometryScale = FitKeepAspectRatio( requiredSize, node.GetInitialVolume() );
150 if ( node.GetGeometryScale() != geometryScale )
152 node.SetGeometryScale( geometryScale );
157 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
159 // If the transform values need to be reinherited
160 if ( nodeDirtyFlags & TransformFlag )
162 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
163 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
164 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
168 // Copy previous value, in case they changed in the previous frame
169 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
170 rootNode.CopyPreviousWorldScale( updateBufferIndex );
171 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
176 * Updates transform values for the given node if the transform flag is dirty.
177 * This includes applying a new size should the SizeMode require it.
178 * Note that this will cause the size dirty flag to be set. This is why we pass
179 * the dirty flags in by reference.
180 * @param[in] node The node to update
181 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
182 * @param[in] updateBufferIndex The current index to use for this frame
184 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
186 // If the transform values need to be reinherited
187 if( nodeDirtyFlags & TransformFlag )
189 // With a non-central anchor-point, the world rotation and scale affects the world position.
190 // Therefore the world rotation & scale must be updated before the world position.
191 if( node.IsOrientationInherited() )
193 node.InheritWorldOrientation( updateBufferIndex );
197 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
200 if( node.IsScaleInherited() )
202 node.InheritWorldScale( updateBufferIndex );
206 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
209 node.InheritWorldPosition( updateBufferIndex );
213 // Copy inherited values, if those changed in the previous frame
214 node.CopyPreviousWorldOrientation( updateBufferIndex );
215 node.CopyPreviousWorldScale( updateBufferIndex );
216 node.CopyPreviousWorldPosition( updateBufferIndex );
217 node.CopyPreviousSize( updateBufferIndex );
221 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
223 // If world-matrix needs to be recalculated
224 if ( nodeDirtyFlags & TransformFlag )
226 if( node.GetInhibitLocalTransform() )
228 node.SetWorldMatrix( updateBufferIndex,
229 node.GetWorldScale(updateBufferIndex),
230 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
231 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
235 node.SetWorldMatrix( updateBufferIndex,
236 node.GetWorldScale(updateBufferIndex),
237 node.GetWorldOrientation(updateBufferIndex),
238 node.GetWorldPosition(updateBufferIndex) );
243 node.CopyPreviousWorldMatrix( updateBufferIndex );
247 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
250 * If world-matrix needs to be recalculated.
252 if ( ( nodeDirtyFlags & TransformFlag ) ||
253 updatedRenderable.IsScaleForSizeDirty() )
255 if( updatedRenderable.UsesGeometryScaling() )
257 // scaling, i.e. Mesh
259 updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
260 if( node.GetInhibitLocalTransform() )
262 node.SetWorldMatrix( updateBufferIndex,
263 node.GetWorldScale(updateBufferIndex) * scaling,
264 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
265 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
269 node.SetWorldMatrix( updateBufferIndex,
270 node.GetWorldScale(updateBufferIndex) * scaling,
271 node.GetWorldOrientation(updateBufferIndex),
272 node.GetWorldPosition(updateBufferIndex) );
277 // no scaling, i.e. Image
278 if( node.GetInhibitLocalTransform() )
280 node.SetWorldMatrix( updateBufferIndex,
281 node.GetWorldScale(updateBufferIndex),
282 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
283 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
287 node.SetWorldMatrix( updateBufferIndex,
288 node.GetWorldScale(updateBufferIndex),
289 node.GetWorldOrientation(updateBufferIndex),
290 node.GetWorldPosition(updateBufferIndex) );
296 node.CopyPreviousWorldMatrix( updateBufferIndex );
301 * Update an attachment.
302 * @return An updated renderable attachment if one was ready.
304 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
306 BufferIndex updateBufferIndex,
307 ResourceManager& resourceManager,
310 // Allow attachments to do specialised processing during updates
311 attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
313 RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
316 // Notify renderables when size has changed
317 // Size can change while node was invisible so we need to check size again if we were previously invisible
318 if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
320 renderable->SizeChanged( updateBufferIndex );
323 // check if node is visible
324 if( renderable->ResolveVisibility( updateBufferIndex ) )
326 renderable->PrepareResources( updateBufferIndex, resourceManager );
332 inline void AddRenderableToLayer( Layer& layer,
333 RenderableAttachment& renderable,
334 BufferIndex updateBufferIndex,
335 int inheritedDrawMode )
337 // The renderables are stored into the opaque list temporarily for PrepareRenderables()
338 // step. The list is cleared by ProcessRenderTasks().
339 layer.opaqueRenderables.push_back( &renderable );
343 * This is called recursively for all children of the root Node
345 inline int UpdateNodesAndAttachments( Node& node,
347 BufferIndex updateBufferIndex,
348 ResourceManager& resourceManager,
349 RenderQueue& renderQueue,
351 int inheritedDrawMode )
353 Layer* layer = ¤tLayer;
355 // Short-circuit for invisible nodes
356 if ( !node.IsVisible( updateBufferIndex ) )
361 // If the node was not previously visible
362 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
363 if ( !node.IsVisible( previousBuffer ) )
365 // The node was skipped in the previous update; it must recalculate everything
366 node.SetAllDirtyFlags();
369 // Some dirty flags are inherited from parent
370 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
372 int cumulativeDirtyFlags = nodeDirtyFlags;
374 if ( node.IsLayer() )
376 // all childs go to this layer
377 layer = node.GetLayer();
379 // assume layer is clean to begin with
380 layer->SetReuseRenderers( updateBufferIndex, true );
382 // Layers do not inherit the DrawMode from their parents
383 inheritedDrawMode = DrawMode::NORMAL;
385 DALI_ASSERT_DEBUG( NULL != layer );
387 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
389 UpdateNodeGeometry( node, nodeDirtyFlags, updateBufferIndex );
391 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
392 // It is important that the modified version of these flags are used by the RenderableAttachment.
393 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
395 // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
396 inheritedDrawMode |= node.GetDrawMode();
398 if ( node.HasAttachment() )
401 * Add renderables for the children into the current Layer
403 RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
410 if( NULL != renderable )
412 // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
413 UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
415 // The attachment is ready to render, so it is added to a set of renderables.
416 AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
419 else if( node.IsObserved() )
421 // This node is being used as a property input for an animation, constraint,
422 // camera or bone. Ensure it's matrix is updated
423 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
426 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
427 // also if node has been deleted, dont reuse old render items
428 if( nodeDirtyFlags & RenderableUpdateFlags )
430 layer->SetReuseRenderers( updateBufferIndex, false );
434 NodeContainer& children = node.GetChildren();
435 const NodeIter endIter = children.End();
436 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
438 Node& child = **iter;
439 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
448 return cumulativeDirtyFlags;
452 * The root node is treated separately; it cannot inherit values since it has no parent
454 int UpdateNodesAndAttachments( Layer& rootNode,
455 BufferIndex updateBufferIndex,
456 ResourceManager& resourceManager,
457 RenderQueue& renderQueue )
459 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
461 // Short-circuit for invisible nodes
462 if ( !rootNode.IsVisible( updateBufferIndex ) )
467 // If the root node was not previously visible
468 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
469 if ( !rootNode.IsVisible( previousBuffer ) )
471 // The node was skipped in the previous update; it must recalculate everything
472 rootNode.SetAllDirtyFlags();
475 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
477 int cumulativeDirtyFlags = nodeDirtyFlags;
479 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
481 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
483 DrawMode::Type drawMode( rootNode.GetDrawMode() );
486 NodeContainer& children = rootNode.GetChildren();
487 const NodeIter endIter = children.End();
488 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
490 Node& child = **iter;
491 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
500 return cumulativeDirtyFlags;
503 } // namespace SceneGraph
505 } // namespace Internal