Updated all code to new format
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / update-algorithms.cpp
1 /*
2  * Copyright (c) 2021 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/internal/render/renderers/render-renderer.h>
26 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
27 #include <dali/internal/update/nodes/node.h>
28 #include <dali/internal/update/nodes/scene-graph-layer.h>
29 #include <dali/public-api/actors/draw-mode.h>
30 #include <dali/public-api/math/matrix.h>
31 #include <dali/public-api/math/vector3.h>
32
33 #include <dali/integration-api/debug.h>
34
35 namespace Dali
36 {
37 namespace Internal
38 {
39 namespace SceneGraph
40 {
41 #if defined(DEBUG_ENABLED)
42 Debug::Filter* gUpdateFilter = Debug::Filter::New(Debug::Concise, false, "LOG_UPDATE_ALGORITHMS");
43 #endif
44
45 /******************************************************************************
46  *********************** Apply Constraints ************************************
47  ******************************************************************************/
48
49 /**
50  * Constrain the local properties of the PropertyOwner.
51  * @param propertyOwner to constrain
52  * @param updateBufferIndex buffer index to use
53  */
54 void ConstrainPropertyOwner(PropertyOwner& propertyOwner, BufferIndex updateBufferIndex)
55 {
56   ConstraintOwnerContainer& constraints = propertyOwner.GetConstraints();
57
58   const ConstraintIter endIter = constraints.End();
59   for(ConstraintIter iter = constraints.Begin(); iter != endIter; ++iter)
60   {
61     ConstraintBase& constraint = **iter;
62     constraint.Apply(updateBufferIndex);
63   }
64 }
65
66 /******************************************************************************
67  ************************** Update node hierarchy *****************************
68  ******************************************************************************/
69
70 inline void UpdateRootNodeOpacity(Layer& rootNode, NodePropertyFlags nodeDirtyFlags, BufferIndex updateBufferIndex)
71 {
72   if(nodeDirtyFlags & NodePropertyFlags::COLOR)
73   {
74     rootNode.SetWorldColor(rootNode.GetColor(updateBufferIndex), updateBufferIndex);
75   }
76   else
77   {
78     // Copy previous value, in case it changed in the previous frame
79     rootNode.CopyPreviousWorldColor(updateBufferIndex);
80   }
81 }
82
83 inline void UpdateNodeOpacity(Node& node, NodePropertyFlags nodeDirtyFlags, BufferIndex updateBufferIndex)
84 {
85   // If opacity needs to be recalculated
86   if(nodeDirtyFlags & NodePropertyFlags::COLOR)
87   {
88     node.InheritWorldColor(updateBufferIndex);
89   }
90   else
91   {
92     // Copy inherited value, if changed in the previous frame
93     node.CopyPreviousWorldColor(updateBufferIndex);
94   }
95 }
96
97 /**
98  * This is called recursively for all children of the root Node
99  */
100 inline NodePropertyFlags UpdateNodes(Node&             node,
101                                      NodePropertyFlags parentFlags,
102                                      BufferIndex       updateBufferIndex,
103                                      RenderQueue&      renderQueue,
104                                      Layer&            currentLayer,
105                                      uint32_t          inheritedDrawMode)
106 {
107   // Apply constraints to the node
108   ConstrainPropertyOwner(node, updateBufferIndex);
109
110   // Some dirty flags are inherited from parent
111   NodePropertyFlags nodeDirtyFlags = node.GetInheritedDirtyFlags(parentFlags);
112
113   NodePropertyFlags cumulativeDirtyFlags = nodeDirtyFlags;
114
115   Layer* layer = &currentLayer;
116   Layer* nodeIsLayer(node.GetLayer());
117   if(nodeIsLayer)
118   {
119     // all childs go to this layer
120     layer = nodeIsLayer;
121
122     // assume layer is clean to begin with
123     layer->SetReuseRenderers(updateBufferIndex, true);
124
125     // Layers do not inherit the DrawMode from their parents
126     inheritedDrawMode = DrawMode::NORMAL;
127   }
128   DALI_ASSERT_DEBUG(NULL != layer);
129
130   UpdateNodeOpacity(node, nodeDirtyFlags, updateBufferIndex);
131
132   // Draw mode inheritance is treated as or-ing the modes together (as they are a bit-mask).
133   inheritedDrawMode |= node.GetDrawMode();
134
135   node.PrepareRender(updateBufferIndex);
136
137   // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
138   // also if node has been deleted, dont reuse old render items
139   if(nodeDirtyFlags & RenderableUpdateFlags)
140   {
141     layer->SetReuseRenderers(updateBufferIndex, false);
142   }
143
144   // recurse children
145   NodeContainer& children = node.GetChildren();
146   const NodeIter endIter  = children.End();
147   for(NodeIter iter = children.Begin(); iter != endIter; ++iter)
148   {
149     Node& child = **iter;
150     cumulativeDirtyFlags |= UpdateNodes(child,
151                                         nodeDirtyFlags,
152                                         updateBufferIndex,
153                                         renderQueue,
154                                         *layer,
155                                         inheritedDrawMode);
156   }
157
158   return cumulativeDirtyFlags;
159 }
160
161 /**
162  * The root node is treated separately; it cannot inherit values since it has no parent
163  */
164 NodePropertyFlags UpdateNodeTree(Layer&       rootNode,
165                                  BufferIndex  updateBufferIndex,
166                                  RenderQueue& renderQueue)
167 {
168   DALI_ASSERT_DEBUG(rootNode.IsRoot());
169
170   // Short-circuit for invisible nodes
171   if(DALI_UNLIKELY(!rootNode.IsVisible(updateBufferIndex))) // almost never ever true
172   {
173     return NodePropertyFlags::NOTHING;
174   }
175
176   // If the root node was not previously visible
177   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
178   if(DALI_UNLIKELY(!rootNode.IsVisible(previousBuffer))) // almost never ever true
179   {
180     // The node was skipped in the previous update; it must recalculate everything
181     rootNode.SetAllDirtyFlags();
182   }
183
184   NodePropertyFlags nodeDirtyFlags(rootNode.GetDirtyFlags());
185
186   NodePropertyFlags cumulativeDirtyFlags = nodeDirtyFlags;
187
188   UpdateRootNodeOpacity(rootNode, nodeDirtyFlags, updateBufferIndex);
189
190   DrawMode::Type drawMode(rootNode.GetDrawMode());
191
192   // recurse children
193   NodeContainer& children = rootNode.GetChildren();
194   const NodeIter endIter  = children.End();
195   for(NodeIter iter = children.Begin(); iter != endIter; ++iter)
196   {
197     Node& child = **iter;
198     cumulativeDirtyFlags |= UpdateNodes(child,
199                                         nodeDirtyFlags,
200                                         updateBufferIndex,
201                                         renderQueue,
202                                         rootNode,
203                                         drawMode);
204   }
205
206   return cumulativeDirtyFlags;
207 }
208
209 } // namespace SceneGraph
210
211 } // namespace Internal
212
213 } // namespace Dali