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