db310dfba66d18136a4f9ec70fd840c0b6ffddf9
[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                                      bool              updated)
107 {
108   // Apply constraints to the node
109   ConstrainPropertyOwner(node, updateBufferIndex);
110
111   // Some dirty flags are inherited from parent
112   NodePropertyFlags nodeDirtyFlags = node.GetInheritedDirtyFlags(parentFlags);
113
114   NodePropertyFlags cumulativeDirtyFlags = nodeDirtyFlags;
115
116   Layer* layer = &currentLayer;
117   Layer* nodeIsLayer(node.GetLayer());
118   if(nodeIsLayer)
119   {
120     // all childs go to this layer
121     layer = nodeIsLayer;
122
123     // assume layer is clean to begin with
124     layer->SetReuseRenderers(updateBufferIndex, true);
125
126     // Layers do not inherit the DrawMode from their parents
127     inheritedDrawMode = DrawMode::NORMAL;
128   }
129   DALI_ASSERT_DEBUG(NULL != layer);
130
131   UpdateNodeOpacity(node, nodeDirtyFlags, updateBufferIndex);
132
133   // Draw mode inheritance is treated as or-ing the modes together (as they are a bit-mask).
134   inheritedDrawMode |= node.GetDrawMode();
135
136   node.PrepareRender(updateBufferIndex);
137
138   // if any child node has moved or had its sort modifier changed, layer is not clean and old frame cannot be reused
139   // also if node has been deleted, dont reuse old render items
140   if(nodeDirtyFlags & RenderableUpdateFlags)
141   {
142     layer->SetReuseRenderers(updateBufferIndex, false);
143   }
144
145   // For partial update, mark all children of an animating node as updated.
146   if(updated) // Only set to updated if parent was updated.
147   {
148     node.SetUpdated(true);
149   }
150   else if(node.Updated()) // Only propagate updated==true downwards.
151   {
152     updated = true;
153   }
154
155   // recurse children
156   NodeContainer& children = node.GetChildren();
157   const NodeIter endIter  = children.End();
158   for(NodeIter iter = children.Begin(); iter != endIter; ++iter)
159   {
160     Node& child = **iter;
161     cumulativeDirtyFlags |= UpdateNodes(child,
162                                         nodeDirtyFlags,
163                                         updateBufferIndex,
164                                         renderQueue,
165                                         *layer,
166                                         inheritedDrawMode,
167                                         updated);
168   }
169
170   return cumulativeDirtyFlags;
171 }
172
173 /**
174  * The root node is treated separately; it cannot inherit values since it has no parent
175  */
176 NodePropertyFlags UpdateNodeTree(Layer&       rootNode,
177                                  BufferIndex  updateBufferIndex,
178                                  RenderQueue& renderQueue)
179 {
180   DALI_ASSERT_DEBUG(rootNode.IsRoot());
181
182   // Short-circuit for invisible nodes
183   if(DALI_UNLIKELY(!rootNode.IsVisible(updateBufferIndex))) // almost never ever true
184   {
185     return NodePropertyFlags::NOTHING;
186   }
187
188   // If the root node was not previously visible
189   BufferIndex previousBuffer = updateBufferIndex ? 0u : 1u;
190   if(DALI_UNLIKELY(!rootNode.IsVisible(previousBuffer))) // almost never ever true
191   {
192     // The node was skipped in the previous update; it must recalculate everything
193     rootNode.SetAllDirtyFlags();
194   }
195
196   NodePropertyFlags nodeDirtyFlags(rootNode.GetDirtyFlags());
197
198   NodePropertyFlags cumulativeDirtyFlags = nodeDirtyFlags;
199
200   UpdateRootNodeOpacity(rootNode, nodeDirtyFlags, updateBufferIndex);
201
202   DrawMode::Type drawMode(rootNode.GetDrawMode());
203
204   bool updated = rootNode.Updated();
205
206   // recurse children
207   NodeContainer& children = rootNode.GetChildren();
208   const NodeIter endIter  = children.End();
209   for(NodeIter iter = children.Begin(); iter != endIter; ++iter)
210   {
211     Node& child = **iter;
212     cumulativeDirtyFlags |= UpdateNodes(child,
213                                         nodeDirtyFlags,
214                                         updateBufferIndex,
215                                         renderQueue,
216                                         rootNode,
217                                         drawMode,
218                                         updated);
219   }
220
221   return cumulativeDirtyFlags;
222 }
223
224 } // namespace SceneGraph
225
226 } // namespace Internal
227
228 } // namespace Dali