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 UpdateNodeGeometry( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
129 if ( nodeDirtyFlags & SizeFlag )
131 Vector3 geometryScale( 1.0f, 1.0f, 1.0f );
133 if ( node.GetTransmitGeometryScaling() )
135 const Vector3& requiredSize = node.GetSize( updateBufferIndex );
136 geometryScale = FitKeepAspectRatio( requiredSize, node.GetInitialVolume() );
139 if ( node.GetGeometryScale() != geometryScale )
141 node.SetGeometryScale( geometryScale );
146 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
148 // If the transform values need to be reinherited
149 if ( nodeDirtyFlags & TransformFlag )
151 rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
152 rootNode.SetWorldOrientation( updateBufferIndex, rootNode.GetOrientation( updateBufferIndex ) );
153 rootNode.SetWorldScale ( updateBufferIndex, rootNode.GetScale ( updateBufferIndex ) );
157 // Copy previous value, in case they changed in the previous frame
158 rootNode.CopyPreviousWorldOrientation( updateBufferIndex );
159 rootNode.CopyPreviousWorldScale( updateBufferIndex );
160 rootNode.CopyPreviousWorldPosition( updateBufferIndex );
165 * Updates transform values for the given node if the transform flag is dirty.
166 * Note that this will cause the size dirty flag to be set. This is why we pass
167 * the dirty flags in by reference.
168 * @param[in] node The node to update
169 * @param[in,out] nodeDirtyFlags A reference to the dirty flags, these may be modified by this function
170 * @param[in] updateBufferIndex The current index to use for this frame
172 inline void UpdateNodeTransformValues( Node& node, int& nodeDirtyFlags, BufferIndex updateBufferIndex )
174 // If the transform values need to be reinherited
175 if( nodeDirtyFlags & TransformFlag )
177 // With a non-central anchor-point, the world rotation and scale affects the world position.
178 // Therefore the world rotation & scale must be updated before the world position.
179 if( node.IsOrientationInherited() )
181 node.InheritWorldOrientation( updateBufferIndex );
185 node.SetWorldOrientation( updateBufferIndex, node.GetOrientation( updateBufferIndex ) );
188 if( node.IsScaleInherited() )
190 node.InheritWorldScale( updateBufferIndex );
194 node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
197 node.InheritWorldPosition( updateBufferIndex );
201 // Copy inherited values, if those changed in the previous frame
202 node.CopyPreviousWorldOrientation( updateBufferIndex );
203 node.CopyPreviousWorldScale( updateBufferIndex );
204 node.CopyPreviousWorldPosition( updateBufferIndex );
205 node.CopyPreviousSize( updateBufferIndex );
209 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
211 // If world-matrix needs to be recalculated
212 if ( nodeDirtyFlags & TransformFlag )
214 if( node.GetInhibitLocalTransform() )
216 node.SetWorldMatrix( updateBufferIndex,
217 node.GetWorldScale(updateBufferIndex),
218 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
219 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
223 node.SetWorldMatrix( updateBufferIndex,
224 node.GetWorldScale(updateBufferIndex),
225 node.GetWorldOrientation(updateBufferIndex),
226 node.GetWorldPosition(updateBufferIndex) );
231 node.CopyPreviousWorldMatrix( updateBufferIndex );
235 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
238 * If world-matrix needs to be recalculated.
240 if ( ( nodeDirtyFlags & TransformFlag ) ||
241 updatedRenderable.IsScaleForSizeDirty() )
243 if( updatedRenderable.UsesGeometryScaling() )
245 // scaling, i.e. Text or Mesh
247 updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
248 if( node.GetInhibitLocalTransform() )
250 node.SetWorldMatrix( updateBufferIndex,
251 node.GetWorldScale(updateBufferIndex) * scaling,
252 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
253 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
257 node.SetWorldMatrix( updateBufferIndex,
258 node.GetWorldScale(updateBufferIndex) * scaling,
259 node.GetWorldOrientation(updateBufferIndex),
260 node.GetWorldPosition(updateBufferIndex) );
265 // no scaling, i.e. Image
266 if( node.GetInhibitLocalTransform() )
268 node.SetWorldMatrix( updateBufferIndex,
269 node.GetWorldScale(updateBufferIndex),
270 node.GetWorldOrientation(updateBufferIndex) / node.GetOrientation(updateBufferIndex),
271 node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
275 node.SetWorldMatrix( updateBufferIndex,
276 node.GetWorldScale(updateBufferIndex),
277 node.GetWorldOrientation(updateBufferIndex),
278 node.GetWorldPosition(updateBufferIndex) );
284 node.CopyPreviousWorldMatrix( updateBufferIndex );
289 * Update an attachment.
290 * @return An updated renderable attachment if one was ready.
292 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
294 BufferIndex updateBufferIndex,
295 ResourceManager& resourceManager,
298 // Allow attachments to do specialised processing during updates
299 attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
301 RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
304 // Notify renderables when size has changed
305 // Size can change while node was invisible so we need to check size again if we were previously invisible
306 if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
308 renderable->SizeChanged( updateBufferIndex );
311 // check if node is visible
312 if( renderable->ResolveVisibility( updateBufferIndex ) )
314 renderable->PrepareResources( updateBufferIndex, resourceManager );
320 inline void AddRenderableToLayer( Layer& layer,
321 RenderableAttachment& renderable,
322 BufferIndex updateBufferIndex,
323 int inheritedDrawMode )
325 // The renderables are stored into the opaque list temporarily for PrepareRenderables()
326 // step. The list is cleared by ProcessRenderTasks().
327 layer.opaqueRenderables.push_back( &renderable );
331 * This is called recursively for all children of the root Node
333 inline int UpdateNodesAndAttachments( Node& node,
335 BufferIndex updateBufferIndex,
336 ResourceManager& resourceManager,
337 RenderQueue& renderQueue,
339 int inheritedDrawMode )
341 Layer* layer = ¤tLayer;
343 // Short-circuit for invisible nodes
344 if ( !node.IsVisible( updateBufferIndex ) )
349 // If the node was not previously visible
350 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
351 if ( !node.IsVisible( previousBuffer ) )
353 // The node was skipped in the previous update; it must recalculate everything
354 node.SetAllDirtyFlags();
357 // Some dirty flags are inherited from parent
358 int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
360 int cumulativeDirtyFlags = nodeDirtyFlags;
362 if ( node.IsLayer() )
364 // all childs go to this layer
365 layer = node.GetLayer();
367 // assume layer is clean to begin with
368 layer->SetReuseRenderers( updateBufferIndex, true );
370 // Layers do not inherit the DrawMode from their parents
371 inheritedDrawMode = DrawMode::NORMAL;
373 DALI_ASSERT_DEBUG( NULL != layer );
375 UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
377 UpdateNodeGeometry( node, nodeDirtyFlags, updateBufferIndex );
379 // Note: nodeDirtyFlags are passed in by reference and may be modified by the following function.
380 // It is important that the modified version of these flags are used by the RenderableAttachment.
381 UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
383 // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
384 inheritedDrawMode |= node.GetDrawMode();
386 if ( node.HasAttachment() )
389 * Add renderables for the children into the current Layer
391 RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
398 if( NULL != renderable )
400 // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
401 UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
403 // The attachment is ready to render, so it is added to a set of renderables.
404 AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
407 else if( node.IsObserved() )
409 // This node is being used as a property input for an animation, constraint,
410 // camera or bone. Ensure it's matrix is updated
411 UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
414 // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
415 // also if node has been deleted, dont reuse old render items
416 if( nodeDirtyFlags & RenderableUpdateFlags )
418 layer->SetReuseRenderers( updateBufferIndex, false );
422 NodeContainer& children = node.GetChildren();
423 const NodeIter endIter = children.End();
424 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
426 Node& child = **iter;
427 cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
436 return cumulativeDirtyFlags;
440 * The root node is treated separately; it cannot inherit values since it has no parent
442 int UpdateNodesAndAttachments( Layer& rootNode,
443 BufferIndex updateBufferIndex,
444 ResourceManager& resourceManager,
445 RenderQueue& renderQueue )
447 DALI_ASSERT_DEBUG( rootNode.IsRoot() );
449 // Short-circuit for invisible nodes
450 if ( !rootNode.IsVisible( updateBufferIndex ) )
455 // If the root node was not previously visible
456 BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
457 if ( !rootNode.IsVisible( previousBuffer ) )
459 // The node was skipped in the previous update; it must recalculate everything
460 rootNode.SetAllDirtyFlags();
463 int nodeDirtyFlags( rootNode.GetDirtyFlags() );
465 int cumulativeDirtyFlags = nodeDirtyFlags;
467 UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
469 UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
471 DrawMode::Type drawMode( rootNode.GetDrawMode() );
474 NodeContainer& children = rootNode.GetChildren();
475 const NodeIter endIter = children.End();
476 for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
478 Node& child = **iter;
479 cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
488 return cumulativeDirtyFlags;
491 } // namespace SceneGraph
493 } // namespace Internal