Merge "Prevent API generate warning log" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / render-task-processor.cpp
1 /*
2  * Copyright (c) 2016 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  * Returns true if all renderers have finished acquiring resources.
90  *
91  * @param[in] updateBufferIndex The current update buffer index.
92  * @param[in] node The current node of the scene-graph.
93  * @param[in] currentLayer The current layer containing lists of opaque/transparent renderables.
94  * @param[in] renderTask The current render-task.
95  * @param[in] inheritedDrawMode The draw mode of the parent
96  * @param[in] parentDepthIndex The inherited parent node depth index
97  * @param[in] currentClippingId The current Clipping Id
98  *              Note: ClippingId is passed by reference, so it is permanently modified when traversing back up the tree for uniqueness.
99  * @param[in] clippingDepth The current clipping depth
100  */
101 bool AddRenderablesForTask( BufferIndex updateBufferIndex,
102                             Node& node,
103                             Layer& currentLayer,
104                             RenderTask& renderTask,
105                             int inheritedDrawMode,
106                             uint32_t& currentClippingId,
107                             uint32_t clippingDepth )
108 {
109   bool resourcesFinished = true;
110
111   // Short-circuit for invisible nodes
112   if( !node.IsVisible( updateBufferIndex ) )
113   {
114     return resourcesFinished;
115   }
116
117   // Check whether node is exclusive to a different render-task
118   const RenderTask* exclusiveTo = node.GetExclusiveRenderTask();
119   if( exclusiveTo && ( exclusiveTo != &renderTask ) )
120   {
121     return resourcesFinished;
122   }
123
124   // Assume all children go to this layer (if this node is a layer).
125   Layer* layer = node.GetLayer();
126   if( layer )
127   {
128     // Layers do not inherit the DrawMode from their parents
129     inheritedDrawMode = node.GetDrawMode();
130   }
131   else
132   {
133     // This node is not a layer.
134     layer = &currentLayer;
135     inheritedDrawMode |= node.GetDrawMode();
136   }
137
138   DALI_ASSERT_DEBUG( NULL != layer );
139
140   // Update the clipping Id and depth for this node (if clipping is enabled).
141   if( DALI_UNLIKELY( node.GetClippingMode() != ClippingMode::DISABLED ) )
142   {
143     ++currentClippingId; // This modifies the reference passed in as well as the local value, causing the value to be global to the recursion.
144     ++clippingDepth;     // This only modifies the local value (which is passed in when the method recurses).
145   }
146   // Set the information in the node.
147   node.SetClippingInformation( currentClippingId, clippingDepth );
148
149   const unsigned int count = node.GetRendererCount();
150   for( unsigned int i = 0; i < count; ++i )
151   {
152     SceneGraph::Renderer* renderer = node.GetRendererAt( i );
153     bool ready = false;
154     bool complete = false;
155     renderer->GetReadyAndComplete( ready, complete );
156
157     DALI_LOG_INFO( gRenderTaskLogFilter, Debug::General, "Testing renderable:%p ready:%s complete:%s\n", renderer, ready ? "T" : "F", complete ? "T" : "F" );
158
159     resourcesFinished &= complete;
160
161     if( ready ) // IE. should be rendered (all resources are available)
162     {
163       // Normal is the more-likely draw mode to occur.
164       if( DALI_LIKELY( inheritedDrawMode == DrawMode::NORMAL ) )
165       {
166         layer->colorRenderables.PushBack( Renderable( &node, renderer ) );
167       }
168       else
169       {
170         layer->overlayRenderables.PushBack( Renderable( &node, renderer ) );
171       }
172     }
173   }
174
175   // Recurse children.
176   NodeContainer& children = node.GetChildren();
177   const NodeIter endIter = children.End();
178   for( NodeIter iter = children.Begin(); iter != endIter; ++iter )
179   {
180     Node& child = **iter;
181     bool childResourcesComplete = AddRenderablesForTask( updateBufferIndex, child, *layer, renderTask, inheritedDrawMode, currentClippingId, clippingDepth );
182     resourcesFinished &= childResourcesComplete;
183   }
184
185   return resourcesFinished;
186 }
187
188 } // Anonymous namespace.
189
190
191 RenderTaskProcessor::RenderTaskProcessor()
192 {
193 }
194
195 RenderTaskProcessor::~RenderTaskProcessor()
196 {
197 }
198
199 void RenderTaskProcessor::Process( BufferIndex updateBufferIndex,
200                                    RenderTaskList& renderTasks,
201                                    Layer& rootNode,
202                                    SortedLayerPointers& sortedLayers,
203                                    GeometryBatcher& geometryBatcher,
204                                    RenderInstructionContainer& instructions )
205 {
206   RenderTaskList::RenderTaskContainer& taskContainer = renderTasks.GetTasks();
207
208   if( taskContainer.IsEmpty() )
209   {
210     // Early-exit if there are no tasks to process
211     return;
212   }
213
214   // For each render-task:
215   //   1) Prepare the render-task
216   //   2) Clear the layer-stored lists of renderers (TODO check if the layer is not changed and don't clear in this case)
217   //   3) Traverse the scene-graph, filling the lists for the current render-task
218   //   4) Prepare render-instructions
219
220   DALI_LOG_INFO( gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Offscreens first\n" );
221
222   // First process off screen render tasks - we may need the results of these for the on screen renders
223   uint32_t clippingId = 0u;
224   bool hasClippingNodes = false;
225
226   RenderTaskList::RenderTaskContainer::ConstIterator endIter = taskContainer.End();
227   for( RenderTaskList::RenderTaskContainer::Iterator iter = taskContainer.Begin(); endIter != iter; ++iter )
228   {
229     RenderTask& renderTask = **iter;
230
231     // Off screen only.
232     if( ( ( 0 == renderTask.GetFrameBufferId() ) && ( renderTask.GetFrameBuffer() == 0 ) ) ||
233         ( !renderTask.ReadyToRender( updateBufferIndex ) ) )
234     {
235       // Skip to next task.
236       continue;
237     }
238
239     Node* sourceNode = renderTask.GetSourceNode();
240     DALI_ASSERT_DEBUG( NULL != sourceNode ); // Otherwise Prepare() should return false
241
242     // Check that the source node is not exclusive to another task.
243     if( ! CheckExclusivity( *sourceNode, renderTask ) )
244     {
245       continue;
246     }
247
248     Layer* layer = FindLayer( *sourceNode );
249     if( !layer )
250     {
251       // Skip to next task as no layer.
252       continue;
253     }
254
255     bool resourcesFinished = false;
256     if( renderTask.IsRenderRequired() )
257     {
258       size_t layerCount( sortedLayers.size() );
259       for( size_t i(0); i<layerCount; ++i )
260       {
261         sortedLayers[i]->ClearRenderables();
262       }
263
264       resourcesFinished = AddRenderablesForTask( updateBufferIndex,
265                                                  *sourceNode,
266                                                  *layer,
267                                                  renderTask,
268                                                  sourceNode->GetDrawMode(),
269                                                  clippingId,
270                                                  0u );
271
272       renderTask.SetResourcesFinished( resourcesFinished );
273
274       // If the clipping Id is still 0 after adding all Renderables, there is no clipping required for this RenderTaskList.
275       hasClippingNodes = clippingId != 0u;
276
277       mRenderInstructionProcessor.Prepare( updateBufferIndex,
278                                   sortedLayers,
279                                   renderTask,
280                                   renderTask.GetCullMode(),
281                                   geometryBatcher,
282                                   hasClippingNodes,
283                                   instructions );
284     }
285     else
286     {
287       renderTask.SetResourcesFinished( resourcesFinished );
288     }
289   }
290
291   DALI_LOG_INFO( gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Onscreen\n" );
292
293   // Now that the off screen renders are done we can process on screen render tasks.
294   // Reset the clipping Id for the OnScreen render tasks.
295   clippingId = 0u;
296   for ( RenderTaskList::RenderTaskContainer::Iterator iter = taskContainer.Begin(); endIter != iter; ++iter )
297   {
298     RenderTask& renderTask = **iter;
299
300     // On screen only.
301     if( ( 0 != renderTask.GetFrameBufferId() ) ||
302         ( renderTask.GetFrameBuffer() != 0 )   ||
303         ( !renderTask.ReadyToRender( updateBufferIndex ) ) )
304     {
305       // Skip to next task.
306       continue;
307     }
308
309     Node* sourceNode = renderTask.GetSourceNode();
310     DALI_ASSERT_DEBUG( NULL != sourceNode ); // Otherwise Prepare() should return false.
311
312     // Check that the source node is not exclusive to another task.
313     if( ! CheckExclusivity( *sourceNode, renderTask ) )
314     {
315       continue;
316     }
317
318     Layer* layer = FindLayer( *sourceNode );
319     if( !layer )
320     {
321       // Skip to next task as no layer.
322       continue;
323     }
324
325     bool resourcesFinished = false;
326     if( renderTask.IsRenderRequired() )
327     {
328       size_t layerCount( sortedLayers.size() );
329       for( size_t i(0); i < layerCount; ++i )
330       {
331         sortedLayers[i]->ClearRenderables();
332       }
333
334       resourcesFinished = AddRenderablesForTask( updateBufferIndex,
335                                                  *sourceNode,
336                                                  *layer,
337                                                  renderTask,
338                                                  sourceNode->GetDrawMode(),
339                                                  clippingId,
340                                                  0u );
341
342       // If the clipping Id is still 0 after adding all Renderables, there is no clipping required for this RenderTaskList.
343       hasClippingNodes = clippingId != 0;
344
345       mRenderInstructionProcessor.Prepare( updateBufferIndex,
346                                   sortedLayers,
347                                   renderTask,
348                                   renderTask.GetCullMode(),
349                                   geometryBatcher,
350                                   hasClippingNodes,
351                                   instructions );
352     }
353
354     renderTask.SetResourcesFinished( resourcesFinished );
355   }
356 }
357
358
359 } // SceneGraph
360
361 } // Internal
362
363 } // Dali