Merge "Replace DALI_COMPILE_TIME_ASSERT with C++11 static_assert" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / render-task-processor.cpp
1 /*
2  * Copyright (c) 2017 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/render-task-processor.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/sorted-layers.h>
23 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
24 #include <dali/internal/update/render-tasks/scene-graph-render-task-list.h>
25 #include <dali/internal/update/nodes/scene-graph-layer.h>
26 #include <dali/internal/render/common/render-item.h>
27 #include <dali/internal/render/common/render-tracker.h>
28 #include <dali/internal/render/common/render-instruction.h>
29 #include <dali/internal/render/common/render-instruction-container.h>
30 #include <dali/internal/render/renderers/render-renderer.h>
31 #include <dali/integration-api/debug.h>
32
33 #if defined(DEBUG_ENABLED)
34 extern Debug::Filter* gRenderTaskLogFilter;
35 #endif
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43
44 namespace SceneGraph
45 {
46
47 namespace //Unnamed namespace
48 {
49
50 // Return false if the node or it's parents are exclusive to another render-task.
51 bool CheckExclusivity( const Node& node, const RenderTask& task )
52 {
53   const RenderTask* exclusiveTo = node.GetExclusiveRenderTask();
54   if( exclusiveTo )
55   {
56     return ( exclusiveTo == &task );
57   }
58
59   const Node* parent = node.GetParent();
60   if ( parent )
61   {
62     return CheckExclusivity( *parent, task );
63   }
64
65   // No exclusive flags set.
66   return true;
67 }
68
69 Layer* FindLayer( Node& node )
70 {
71   Node* currentNode( &node );
72   Layer* layer( NULL );
73   while( currentNode )
74   {
75     if( ( layer = currentNode->GetLayer() ) != NULL )
76     {
77       return layer;
78     }
79
80     currentNode = currentNode->GetParent();
81   }
82
83   return NULL;
84 }
85
86 /**
87  * Rebuild the Layer::colorRenderables and overlayRenderables members,
88  * including only renderers which are included in the current render-task.
89  *
90  * @param[in] updateBufferIndex The current update buffer index.
91  * @param[in] node The current node of the scene-graph.
92  * @param[in] currentLayer The current layer containing lists of opaque/transparent renderables.
93  * @param[in] renderTask The current render-task.
94  * @param[in] inheritedDrawMode The draw mode of the parent
95  * @param[in] parentDepthIndex The inherited parent node depth index
96  * @param[in] currentClippingId The current Clipping Id
97  *              Note: ClippingId is passed by reference, so it is permanently modified when traversing back up the tree for uniqueness.
98  * @param[in] clippingDepth The current clipping depth
99  */
100 void AddRenderablesForTask( BufferIndex updateBufferIndex,
101                             Node& node,
102                             Layer& currentLayer,
103                             RenderTask& renderTask,
104                             int inheritedDrawMode,
105                             uint32_t& currentClippingId,
106                             uint32_t clippingDepth )
107 {
108   // Short-circuit for invisible nodes
109   if( !node.IsVisible( updateBufferIndex ) )
110   {
111     return;
112   }
113
114   // Check whether node is exclusive to a different render-task
115   const RenderTask* exclusiveTo = node.GetExclusiveRenderTask();
116   if( exclusiveTo && ( exclusiveTo != &renderTask ) )
117   {
118     return;
119   }
120
121   // Assume all children go to this layer (if this node is a layer).
122   Layer* layer = node.GetLayer();
123   if( layer )
124   {
125     // Layers do not inherit the DrawMode from their parents
126     inheritedDrawMode = node.GetDrawMode();
127   }
128   else
129   {
130     // This node is not a layer.
131     layer = &currentLayer;
132     inheritedDrawMode |= node.GetDrawMode();
133   }
134
135   DALI_ASSERT_DEBUG( NULL != layer );
136
137   // Update the clipping Id and depth for this node (if clipping is enabled).
138   if( DALI_UNLIKELY( node.GetClippingMode() != ClippingMode::DISABLED ) )
139   {
140     ++currentClippingId; // This modifies the reference passed in as well as the local value, causing the value to be global to the recursion.
141     ++clippingDepth;     // This only modifies the local value (which is passed in when the method recurses).
142   }
143   // Set the information in the node.
144   node.SetClippingInformation( currentClippingId, clippingDepth );
145
146   const unsigned int count = node.GetRendererCount();
147   for( unsigned int i = 0; i < count; ++i )
148   {
149     SceneGraph::Renderer* renderer = node.GetRendererAt( i );
150
151     // Normal is the more-likely draw mode to occur.
152     if( DALI_LIKELY( inheritedDrawMode == DrawMode::NORMAL ) )
153     {
154       layer->colorRenderables.PushBack( Renderable( &node, renderer ) );
155     }
156     else
157     {
158       layer->overlayRenderables.PushBack( Renderable( &node, renderer ) );
159     }
160   }
161
162   // Recurse children.
163   NodeContainer& children = node.GetChildren();
164   const NodeIter endIter = children.End();
165   for( NodeIter iter = children.Begin(); iter != endIter; ++iter )
166   {
167     Node& child = **iter;
168     AddRenderablesForTask( updateBufferIndex, child, *layer, renderTask, inheritedDrawMode, currentClippingId, clippingDepth );
169   }
170 }
171
172 } // Anonymous namespace.
173
174
175 RenderTaskProcessor::RenderTaskProcessor()
176 {
177 }
178
179 RenderTaskProcessor::~RenderTaskProcessor()
180 {
181 }
182
183 void RenderTaskProcessor::Process( BufferIndex updateBufferIndex,
184                                    RenderTaskList& renderTasks,
185                                    Layer& rootNode,
186                                    SortedLayerPointers& sortedLayers,
187                                    RenderInstructionContainer& instructions )
188 {
189   RenderTaskList::RenderTaskContainer& taskContainer = renderTasks.GetTasks();
190
191   if( taskContainer.IsEmpty() )
192   {
193     // Early-exit if there are no tasks to process
194     return;
195   }
196
197   // For each render-task:
198   //   1) Prepare the render-task
199   //   2) Clear the layer-stored lists of renderers (TODO check if the layer is not changed and don't clear in this case)
200   //   3) Traverse the scene-graph, filling the lists for the current render-task
201   //   4) Prepare render-instructions
202
203   DALI_LOG_INFO( gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Offscreens first\n" );
204
205   // First process off screen render tasks - we may need the results of these for the on screen renders
206   uint32_t clippingId = 0u;
207   bool hasClippingNodes = false;
208
209   RenderTaskList::RenderTaskContainer::ConstIterator endIter = taskContainer.End();
210   for( RenderTaskList::RenderTaskContainer::Iterator iter = taskContainer.Begin(); endIter != iter; ++iter )
211   {
212     RenderTask& renderTask = **iter;
213
214     // Off screen only.
215     if( ( renderTask.GetFrameBuffer() == 0 ) || ( !renderTask.ReadyToRender( updateBufferIndex ) ) )
216     {
217       // Skip to next task.
218       continue;
219     }
220
221     Node* sourceNode = renderTask.GetSourceNode();
222     DALI_ASSERT_DEBUG( NULL != sourceNode ); // Otherwise Prepare() should return false
223
224     // Check that the source node is not exclusive to another task.
225     if( ! CheckExclusivity( *sourceNode, renderTask ) )
226     {
227       continue;
228     }
229
230     Layer* layer = FindLayer( *sourceNode );
231     if( !layer )
232     {
233       // Skip to next task as no layer.
234       continue;
235     }
236
237     if( renderTask.IsRenderRequired() )
238     {
239       size_t layerCount( sortedLayers.size() );
240       for( size_t i(0); i<layerCount; ++i )
241       {
242         sortedLayers[i]->ClearRenderables();
243       }
244
245       AddRenderablesForTask( updateBufferIndex,
246                              *sourceNode,
247                              *layer,
248                              renderTask,
249                              sourceNode->GetDrawMode(),
250                              clippingId,
251                              0u );
252
253       // If the clipping Id is still 0 after adding all Renderables, there is no clipping required for this RenderTaskList.
254       hasClippingNodes = clippingId != 0u;
255
256       mRenderInstructionProcessor.Prepare( updateBufferIndex,
257                                   sortedLayers,
258                                   renderTask,
259                                   renderTask.GetCullMode(),
260                                   hasClippingNodes,
261                                   instructions );
262     }
263   }
264
265   DALI_LOG_INFO( gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Onscreen\n" );
266
267   // Now that the off screen renders are done we can process on screen render tasks.
268   // Reset the clipping Id for the OnScreen render tasks.
269   clippingId = 0u;
270   for ( RenderTaskList::RenderTaskContainer::Iterator iter = taskContainer.Begin(); endIter != iter; ++iter )
271   {
272     RenderTask& renderTask = **iter;
273
274     // On screen only.
275     if( ( renderTask.GetFrameBuffer() != 0 )   || ( !renderTask.ReadyToRender( updateBufferIndex ) ) )
276     {
277       // Skip to next task.
278       continue;
279     }
280
281     Node* sourceNode = renderTask.GetSourceNode();
282     DALI_ASSERT_DEBUG( NULL != sourceNode ); // Otherwise Prepare() should return false.
283
284     // Check that the source node is not exclusive to another task.
285     if( ! CheckExclusivity( *sourceNode, renderTask ) )
286     {
287       continue;
288     }
289
290     Layer* layer = FindLayer( *sourceNode );
291     if( !layer )
292     {
293       // Skip to next task as no layer.
294       continue;
295     }
296
297     if( renderTask.IsRenderRequired() )
298     {
299       size_t layerCount( sortedLayers.size() );
300       for( size_t i(0); i < layerCount; ++i )
301       {
302         sortedLayers[i]->ClearRenderables();
303       }
304
305       AddRenderablesForTask( updateBufferIndex,
306                              *sourceNode,
307                              *layer,
308                              renderTask,
309                              sourceNode->GetDrawMode(),
310                              clippingId,
311                              0u );
312
313       // If the clipping Id is still 0 after adding all Renderables, there is no clipping required for this RenderTaskList.
314       hasClippingNodes = clippingId != 0;
315
316       mRenderInstructionProcessor.Prepare( updateBufferIndex,
317                                   sortedLayers,
318                                   renderTask,
319                                   renderTask.GetCullMode(),
320                                   hasClippingNodes,
321                                   instructions );
322     }
323   }
324 }
325
326
327 } // SceneGraph
328
329 } // Internal
330
331 } // Dali