Merge "Changed STEREO_HORIZONTAL view mode to work with landscape orientation" into...
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / update-algorithms.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/manager/update-algorithms.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
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>
35
36 #include <dali/integration-api/debug.h>
37
38
39 namespace Dali
40 {
41
42 namespace Internal
43 {
44
45 namespace SceneGraph
46 {
47
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gUpdateFilter = Debug::Filter::New(Debug::Concise, false, "LOG_UPDATE_ALGORITHMS");
50 #endif
51
52 /******************************************************************************
53  *********************** Apply Constraints ************************************
54  ******************************************************************************/
55
56
57 /**
58  * Constrain the local properties of the PropertyOwner.
59  * @param propertyOwner to constrain
60  * @param updateBufferIndex buffer index to use
61  * @return The number of constraints that are still being applied
62  */
63 unsigned int ConstrainPropertyOwner( PropertyOwner& propertyOwner, BufferIndex updateBufferIndex )
64 {
65   unsigned int activeCount = 0;
66
67   ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
68
69   const ConstraintIter endIter = constraints.End();
70   for( ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter )
71   {
72     ConstraintBase& constraint = **iter;
73     constraint.Apply( updateBufferIndex );
74
75     if( constraint.mWeight[updateBufferIndex] < 1.0f )
76     {
77       // this constraint is still being applied
78       ++activeCount;
79     }
80   }
81
82   return activeCount;
83 }
84
85 /**
86  * Recursively apply the constraints on the nodes
87  * @param node to constraint
88  * @param updateBufferIndex buffer index to use
89  * @return number of active constraints
90  */
91 unsigned int ConstrainNodes( Node& node, BufferIndex updateBufferIndex )
92 {
93   unsigned int activeCount = ConstrainPropertyOwner( node, updateBufferIndex );
94
95   /**
96    *  Constrain the children next
97    */
98   NodeContainer& children = node.GetChildren();
99   const NodeIter endIter = children.End();
100   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
101   {
102     Node& child = **iter;
103     activeCount += ConstrainNodes( child, updateBufferIndex );
104   }
105   return activeCount;
106 }
107
108 /******************************************************************************
109  ************************** Update node hierarchy *****************************
110  ******************************************************************************/
111
112 inline void UpdateRootNodeOpacity( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
113 {
114   if ( nodeDirtyFlags & ColorFlag )
115   {
116     rootNode.SetWorldColor( rootNode.GetColor( updateBufferIndex ), updateBufferIndex );
117   }
118   else
119   {
120     // Copy previous value, in case it changed in the previous frame
121     rootNode.CopyPreviousWorldColor( updateBufferIndex );
122   }
123 }
124
125 inline void UpdateNodeOpacity( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
126 {
127   // If opacity needs to be recalculated
128   if ( nodeDirtyFlags & ColorFlag )
129   {
130     node.InheritWorldColor( updateBufferIndex );
131   }
132   else
133   {
134     // Copy inherited value, if changed in the previous frame
135     node.CopyPreviousWorldColor( updateBufferIndex );
136   }
137 }
138
139 inline void UpdateNodeGeometry( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
140 {
141   if ( nodeDirtyFlags & SizeFlag )
142   {
143     Vector3 geometryScale( 1.0f, 1.0f, 1.0f );
144
145     if ( node.GetTransmitGeometryScaling() )
146     {
147       const Vector3& requiredSize = node.GetSize( updateBufferIndex );
148       geometryScale = FitKeepAspectRatio( requiredSize, node.GetInitialVolume() );
149     }
150
151     if ( node.GetGeometryScale() != geometryScale )
152     {
153       node.SetGeometryScale( geometryScale );
154     }
155   }
156 }
157
158 inline void UpdateRootNodeTransformValues( Layer& rootNode, int nodeDirtyFlags, BufferIndex updateBufferIndex )
159 {
160   // If the transform values need to be reinherited
161   if ( nodeDirtyFlags & TransformFlag )
162   {
163     rootNode.SetWorldPosition( updateBufferIndex, rootNode.GetPosition( updateBufferIndex ) );
164     rootNode.SetWorldRotation( updateBufferIndex, rootNode.GetRotation( updateBufferIndex ) );
165     rootNode.SetWorldScale   ( updateBufferIndex, rootNode.GetScale   ( updateBufferIndex ) );
166   }
167   else
168   {
169     // Copy previous value, in case they changed in the previous frame
170     rootNode.CopyPreviousWorldRotation( updateBufferIndex );
171     rootNode.CopyPreviousWorldScale( updateBufferIndex );
172     rootNode.CopyPreviousWorldPosition( updateBufferIndex );
173   }
174 }
175
176 inline void UpdateNodeTransformValues( Node& node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
177 {
178   // If the transform values need to be reinherited
179   if ( nodeDirtyFlags & TransformFlag )
180   {
181     // With a non-central anchor-point, the world rotation and scale affects the world position.
182     // Therefore the world rotation & scale must be updated before the world position.
183
184     if( node.IsRotationInherited() )
185     {
186       node.InheritWorldRotation( updateBufferIndex );
187     }
188     else
189     {
190       node.SetWorldRotation( updateBufferIndex, node.GetRotation( updateBufferIndex ) );
191     }
192
193     if( node.IsScaleInherited() )
194     {
195       node.InheritWorldScale( updateBufferIndex );
196     }
197     else
198     {
199       node.SetWorldScale( updateBufferIndex, node.GetScale( updateBufferIndex ) );
200     }
201
202     node.InheritWorldPosition( updateBufferIndex );
203   }
204   else
205   {
206     // Copy inherited values, if those changed in the previous frame
207     node.CopyPreviousWorldRotation( updateBufferIndex );
208     node.CopyPreviousWorldScale( updateBufferIndex );
209     node.CopyPreviousWorldPosition( updateBufferIndex );
210   }
211 }
212
213 inline void UpdateNodeWorldMatrix( Node &node, int nodeDirtyFlags, BufferIndex updateBufferIndex )
214 {
215   // If world-matrix needs to be recalculated
216   if ( nodeDirtyFlags & TransformFlag )
217   {
218     if( node.GetInhibitLocalTransform() )
219     {
220       node.SetWorldMatrix( updateBufferIndex,
221                            node.GetWorldScale(updateBufferIndex),
222                            node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
223                            node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
224     }
225     else
226     {
227       node.SetWorldMatrix( updateBufferIndex,
228                            node.GetWorldScale(updateBufferIndex),
229                            node.GetWorldRotation(updateBufferIndex),
230                            node.GetWorldPosition(updateBufferIndex) );
231     }
232   }
233   else
234   {
235     node.CopyPreviousWorldMatrix( updateBufferIndex );
236   }
237 }
238
239 inline void UpdateNodeWorldMatrix( Node& node, RenderableAttachment& updatedRenderable, int nodeDirtyFlags, BufferIndex updateBufferIndex )
240 {
241   /**
242    * If world-matrix needs to be recalculated.
243    */
244   if ( ( nodeDirtyFlags & TransformFlag ) ||
245          updatedRenderable.IsScaleForSizeDirty() )
246   {
247     if( updatedRenderable.UsesGeometryScaling() )
248     {
249       // scaling, i.e. Text or Mesh
250       Vector3 scaling;
251       updatedRenderable.GetScaleForSize( node.GetSize( updateBufferIndex ), scaling );
252       if( node.GetInhibitLocalTransform() )
253       {
254         node.SetWorldMatrix( updateBufferIndex,
255                              node.GetWorldScale(updateBufferIndex) * scaling,
256                              node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
257                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
258       }
259       else
260       {
261         node.SetWorldMatrix( updateBufferIndex,
262                              node.GetWorldScale(updateBufferIndex) * scaling,
263                              node.GetWorldRotation(updateBufferIndex),
264                              node.GetWorldPosition(updateBufferIndex) );
265       }
266     }
267     else
268     {
269       // no scaling, i.e. Image
270       if( node.GetInhibitLocalTransform() )
271       {
272         node.SetWorldMatrix( updateBufferIndex,
273                              node.GetWorldScale(updateBufferIndex),
274                              node.GetWorldRotation(updateBufferIndex) / node.GetRotation(updateBufferIndex),
275                              node.GetWorldPosition(updateBufferIndex) - node.GetPosition(updateBufferIndex) );
276       }
277       else
278       {
279         node.SetWorldMatrix( updateBufferIndex,
280                              node.GetWorldScale(updateBufferIndex),
281                              node.GetWorldRotation(updateBufferIndex),
282                              node.GetWorldPosition(updateBufferIndex) );
283       }
284     }
285   }
286   else
287   {
288     node.CopyPreviousWorldMatrix( updateBufferIndex );
289   }
290 }
291
292 /**
293  * Update an attachment.
294  * @return An updated renderable attachment if one was ready.
295  */
296 inline RenderableAttachment* UpdateAttachment( NodeAttachment& attachment,
297                                                Node& node,
298                                                BufferIndex updateBufferIndex,
299                                                ResourceManager& resourceManager,
300                                                int nodeDirtyFlags )
301 {
302   // Allow attachments to do specialised processing during updates
303   attachment.Update( updateBufferIndex, node, nodeDirtyFlags );
304
305   RenderableAttachment* renderable = attachment.GetRenderable(); // not all scene objects render
306   if( renderable )
307   {
308     // Notify renderables when size has changed
309     // Size can change while node was invisible so we need to check size again if we were previously invisible
310     if( nodeDirtyFlags & (SizeFlag|VisibleFlag) )
311     {
312       renderable->SizeChanged( updateBufferIndex );
313     }
314
315     // check if node is visible
316     if( renderable->ResolveVisibility( updateBufferIndex ) )
317     {
318       renderable->PrepareResources( updateBufferIndex, resourceManager );
319     }
320   }
321   return renderable;
322 }
323
324 inline void AddRenderableToLayer( Layer& layer,
325                                   RenderableAttachment& renderable,
326                                   BufferIndex updateBufferIndex,
327                                   int inheritedDrawMode )
328 {
329   // The renderables are stored into the opaque list temporarily for PrepareRenderables()
330   // step. The list is cleared by ProcessRenderTasks().
331   layer.opaqueRenderables.push_back( &renderable );
332 }
333
334 /**
335  * This is called recursively for all children of the root Node
336  */
337 inline int UpdateNodesAndAttachments( Node& node,
338                                       int parentFlags,
339                                       BufferIndex updateBufferIndex,
340                                       ResourceManager& resourceManager,
341                                       RenderQueue& renderQueue,
342                                       Layer& currentLayer,
343                                       int inheritedDrawMode )
344 {
345   Layer* layer = &currentLayer;
346
347   // Short-circuit for invisible nodes
348   if ( !node.IsVisible( updateBufferIndex ) )
349   {
350     return 0;
351   }
352
353   // If the node was not previously visible
354   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
355   if ( !node.IsVisible( previousBuffer ) )
356   {
357     // The node was skipped in the previous update; it must recalculate everything
358     node.SetAllDirtyFlags();
359   }
360
361   // Some dirty flags are inherited from parent
362   int nodeDirtyFlags( node.GetDirtyFlags() | ( parentFlags & InheritedDirtyFlags ) );
363
364   int cumulativeDirtyFlags = nodeDirtyFlags;
365
366   if ( node.IsLayer() )
367   {
368     // all childs go to this layer
369     layer = node.GetLayer();
370
371     // assume layer is clean to begin with
372     layer->SetReuseRenderers( updateBufferIndex, true );
373
374     // Layers do not inherit the DrawMode from their parents
375     inheritedDrawMode = DrawMode::NORMAL;
376   }
377   DALI_ASSERT_DEBUG( NULL != layer );
378
379   UpdateNodeOpacity( node, nodeDirtyFlags, updateBufferIndex );
380
381   UpdateNodeGeometry( node, nodeDirtyFlags, updateBufferIndex );
382
383   UpdateNodeTransformValues( node, nodeDirtyFlags, updateBufferIndex );
384
385   // Setting STENCIL will override OVERLAY, if that would otherwise have been inherited.
386   inheritedDrawMode |= node.GetDrawMode();
387
388   if ( node.HasAttachment() )
389   {
390     /*
391      * Add renderables for the children into the current Layer
392      */
393     RenderableAttachment* renderable = UpdateAttachment( node.GetAttachment(),
394                                                          node,
395                                                          updateBufferIndex,
396                                                          resourceManager,
397                                                          nodeDirtyFlags );
398
399
400     if( NULL != renderable )
401     {
402       // Update the world matrix after renderable update; the ScaleForSize property should now be calculated
403       UpdateNodeWorldMatrix( node, *renderable, nodeDirtyFlags, updateBufferIndex );
404
405       // The attachment is ready to render, so it is added to a set of renderables.
406       AddRenderableToLayer( *layer, *renderable, updateBufferIndex, inheritedDrawMode );
407     }
408   }
409   else if( node.IsObserved() )
410   {
411     // This node is being used as a property input for an animation, constraint,
412     // camera or bone. Ensure it's matrix is updated
413     UpdateNodeWorldMatrix( node, nodeDirtyFlags, updateBufferIndex );
414   }
415
416   // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
417   // also if node has been deleted, dont reuse old render items
418   if( nodeDirtyFlags & RenderableUpdateFlags )
419   {
420     layer->SetReuseRenderers( updateBufferIndex, false );
421   }
422
423   // recurse children
424   NodeContainer& children = node.GetChildren();
425   const NodeIter endIter = children.End();
426   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
427   {
428     Node& child = **iter;
429     cumulativeDirtyFlags |=UpdateNodesAndAttachments( child,
430                                                       nodeDirtyFlags,
431                                                       updateBufferIndex,
432                                                       resourceManager,
433                                                       renderQueue,
434                                                       *layer,
435                                                       inheritedDrawMode );
436   }
437
438   return cumulativeDirtyFlags;
439 }
440
441 /**
442  * The root node is treated separately; it cannot inherit values since it has no parent
443  */
444 int UpdateNodesAndAttachments( Layer& rootNode,
445                                BufferIndex updateBufferIndex,
446                                ResourceManager& resourceManager,
447                                RenderQueue& renderQueue )
448 {
449   DALI_ASSERT_DEBUG( rootNode.IsRoot() );
450
451   // Short-circuit for invisible nodes
452   if ( !rootNode.IsVisible( updateBufferIndex ) )
453   {
454     return 0;
455   }
456
457   // If the root node was not previously visible
458   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
459   if ( !rootNode.IsVisible( previousBuffer ) )
460   {
461     // The node was skipped in the previous update; it must recalculate everything
462     rootNode.SetAllDirtyFlags();
463   }
464
465   int nodeDirtyFlags( rootNode.GetDirtyFlags() );
466
467   int cumulativeDirtyFlags = nodeDirtyFlags;
468
469   UpdateRootNodeOpacity( rootNode, nodeDirtyFlags, updateBufferIndex );
470
471   UpdateRootNodeTransformValues( rootNode, nodeDirtyFlags, updateBufferIndex );
472
473   DrawMode::Type drawMode( rootNode.GetDrawMode() );
474
475   // recurse children
476   NodeContainer& children = rootNode.GetChildren();
477   const NodeIter endIter = children.End();
478   for ( NodeIter iter = children.Begin(); iter != endIter; ++iter )
479   {
480     Node& child = **iter;
481     cumulativeDirtyFlags |= UpdateNodesAndAttachments( child,
482                                                        nodeDirtyFlags,
483                                                        updateBufferIndex,
484                                                        resourceManager,
485                                                        renderQueue,
486                                                        rootNode,
487                                                        drawMode );
488   }
489
490   return cumulativeDirtyFlags;
491 }
492
493 } // namespace SceneGraph
494
495 } // namespace Internal
496
497 } // namespace Dali