2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/manager/render-task-processor.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/render/common/render-instruction-container.h>
24 #include <dali/internal/render/common/render-instruction.h>
25 #include <dali/internal/render/common/render-item.h>
26 #include <dali/internal/render/common/render-tracker.h>
27 #include <dali/internal/render/renderers/render-renderer.h>
28 #include <dali/internal/update/manager/sorted-layers.h>
29 #include <dali/internal/update/nodes/scene-graph-layer.h>
30 #include <dali/internal/update/render-tasks/scene-graph-render-task-list.h>
31 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
33 #if defined(DEBUG_ENABLED)
34 extern Debug::Filter* gRenderTaskLogFilter;
43 namespace //Unnamed namespace
45 // Return false if the node or it's parents are exclusive to another render-task.
46 bool CheckExclusivity(const Node& node, const RenderTask& task)
48 const RenderTask* exclusiveTo = node.GetExclusiveRenderTask();
51 return (exclusiveTo == &task);
54 const Node* parent = node.GetParent();
57 return CheckExclusivity(*parent, task);
60 // No exclusive flags set.
64 Layer* FindLayer(Node& node)
66 Node* currentNode(&node);
67 Layer* layer(nullptr);
70 if((layer = currentNode->GetLayer()) != nullptr)
75 currentNode = currentNode->GetParent();
82 * Rebuild the Layer::colorRenderables and overlayRenderables members,
83 * including only renderers which are included in the current render-task.
85 * @param[in] updateBufferIndex The current update buffer index.
86 * @param[in] node The current node of the scene-graph.
87 * @param[in] currentLayer The current layer containing lists of opaque/transparent renderables.
88 * @param[in] renderTask The current render-task.
89 * @param[in] inheritedDrawMode The draw mode of the parent
90 * @param[in] parentDepthIndex The inherited parent node depth index
91 * @param[in] currentClippingId The current Clipping Id
92 * Note: ClippingId is passed by reference, so it is permanently modified when traversing back up the tree for uniqueness.
93 * @param[in] clippingDepth The current stencil clipping depth
94 * @param[in] clippingDepth The current scissor clipping depth
95 * @param[out] clippingUsed Gets set to true if any clipping nodes have been found
96 * @return true if rendering should be kept, false otherwise.
98 bool AddRenderablesForTask(BufferIndex updateBufferIndex,
101 RenderTask& renderTask,
102 int inheritedDrawMode,
103 uint32_t& currentClippingId,
104 uint32_t clippingDepth,
105 uint32_t scissorDepth,
108 bool keepRendering = false;
110 // Short-circuit for invisible nodes
111 if(!node.IsVisible(updateBufferIndex))
113 node.GetPartialRenderingData().mVisible = false;
114 return keepRendering;
117 // If the node was not previously visible
118 if(!node.GetPartialRenderingData().mVisible)
120 node.GetPartialRenderingData().mVisible = true;
121 node.SetUpdatedTree(true);
124 // Check whether node is exclusive to a different render-task
125 const RenderTask* exclusiveTo = node.GetExclusiveRenderTask();
126 if(exclusiveTo && (exclusiveTo != &renderTask))
128 return keepRendering;
131 // Assume all children go to this layer (if this node is a layer).
132 Layer* layer = node.GetLayer();
135 // Layers do not inherit the DrawMode from their parents
136 inheritedDrawMode = node.GetDrawMode();
140 // This node is not a layer.
141 layer = ¤tLayer;
142 inheritedDrawMode |= node.GetDrawMode();
145 DALI_ASSERT_DEBUG(NULL != layer);
147 const uint32_t count = node.GetRendererCount();
149 // Update the clipping Id and depth for this node (if clipping is enabled).
150 const Dali::ClippingMode::Type clippingMode = node.GetClippingMode();
151 if(DALI_UNLIKELY(clippingMode != ClippingMode::DISABLED))
153 if(DALI_LIKELY(clippingMode == ClippingMode::CLIP_TO_BOUNDING_BOX))
155 ++scissorDepth; // This only modifies the local value (which is passed in when the method recurses).
156 // If we do not have any renderers, create one to house the scissor operation.
159 layer->colorRenderables.PushBack(Renderable(&node, nullptr));
164 // We only need clipping Id for stencil clips. This means we can deliberately avoid modifying it for bounding box clips,
165 // thus allowing bounding box clipping to still detect clip depth changes without turning on the stencil buffer for non-clipped nodes.
166 ++currentClippingId; // This modifies the reference passed in as well as the local value, causing the value to be global to the recursion.
167 ++clippingDepth; // This only modifies the local value (which is passed in when the method recurses).
171 // Set the information in the node.
172 node.SetClippingInformation(currentClippingId, clippingDepth, scissorDepth);
174 RenderableContainer& target = DALI_LIKELY(inheritedDrawMode == DrawMode::NORMAL) ? layer->colorRenderables : layer->overlayRenderables;
175 for(uint32_t i = 0; i < count; ++i)
177 SceneGraph::Renderer* renderer = node.GetRendererAt(i);
178 target.PushBack(Renderable(&node, renderer));
180 keepRendering = keepRendering || (renderer->GetRenderingBehavior() == DevelRenderer::Rendering::CONTINUOUSLY);
184 NodeContainer& children = node.GetChildren();
185 const NodeIter endIter = children.End();
186 for(NodeIter iter = children.Begin(); iter != endIter; ++iter)
188 Node& child = **iter;
189 keepRendering |= AddRenderablesForTask(updateBufferIndex, child, *layer, renderTask, inheritedDrawMode, currentClippingId, clippingDepth, scissorDepth, clippingUsed);
192 return keepRendering;
196 * Process the list of render-tasks; the output is a series of render instructions.
197 * @note When ProcessRenderTasks is called, the layers should already the transparent/opaque renderers which are ready to render.
198 * If there is only one default render-task, then no further processing is required.
199 * @param[in] updateBufferIndex The current update buffer index.
200 * @param[in] taskContainer The container of render-tasks.
201 * @param[in] rootNode The root node of the scene-graph.
202 * @param[in] sortedLayers The layers containing lists of opaque / transparent renderables.
203 * @param[out] instructions The instructions for rendering the next frame.
204 * @param[in] renderInstructionProcessor An instance of the RenderInstructionProcessor used to sort and handle the renderers for each layer.
205 * @param[in] renderToFboEnabled Whether rendering into the Frame Buffer Object is enabled (used to measure FPS above 60)
206 * @param[in] isRenderingToFbo Whether this frame is being rendered into the Frame Buffer Object (used to measure FPS above 60)
207 * @param[in] processOffscreen Whether the offscreen render tasks are the ones processed. Otherwise it processes the onscreen tasks.
208 * @return true if rendering should be kept, false otherwise.
210 bool ProcessTasks(BufferIndex updateBufferIndex,
211 RenderTaskList::RenderTaskContainer& taskContainer,
213 SortedLayerPointers& sortedLayers,
214 RenderInstructionContainer& instructions,
215 RenderInstructionProcessor& renderInstructionProcessor,
216 bool renderToFboEnabled,
217 bool isRenderingToFbo,
218 bool processOffscreen)
220 uint32_t clippingId = 0u;
221 bool hasClippingNodes = false;
223 bool isFirstRenderTask = true;
224 bool keepRendering = false;
225 for(RenderTaskList::RenderTaskContainer::Iterator iter = taskContainer.Begin(), endIter = taskContainer.End(); endIter != iter; ++iter)
227 RenderTask& renderTask = **iter;
229 const bool hasFrameBuffer = nullptr != renderTask.GetFrameBuffer();
230 const bool isDefaultRenderTask = isFirstRenderTask;
231 isFirstRenderTask = false;
233 if((!renderToFboEnabled && ((!processOffscreen && hasFrameBuffer) ||
234 (processOffscreen && !hasFrameBuffer))) ||
235 (renderToFboEnabled && ((processOffscreen && !hasFrameBuffer) ||
236 (isDefaultRenderTask && processOffscreen) ||
237 (!isDefaultRenderTask && !processOffscreen && hasFrameBuffer))) ||
238 !renderTask.ReadyToRender(updateBufferIndex))
240 // Skip to next task.
244 Node* sourceNode = renderTask.GetSourceNode();
245 DALI_ASSERT_DEBUG(NULL != sourceNode); // Otherwise Prepare() should return false
247 // Check that the source node is not exclusive to another task.
248 if(!CheckExclusivity(*sourceNode, renderTask))
253 Layer* layer = FindLayer(*sourceNode);
256 // Skip to next task as no layer.
260 const uint32_t currentNumberOfInstructions = instructions.Count(updateBufferIndex);
262 if(renderTask.IsRenderRequired())
264 for(auto&& sortedLayer : sortedLayers)
266 sortedLayer->ClearRenderables();
269 keepRendering |= AddRenderablesForTask(updateBufferIndex,
273 sourceNode->GetDrawMode(),
279 renderInstructionProcessor.Prepare(updateBufferIndex,
282 renderTask.GetCullMode(),
287 if(!processOffscreen && isDefaultRenderTask && renderToFboEnabled && !isRenderingToFbo && hasFrameBuffer)
289 // Traverse the instructions of the default render task and mark them to be rendered into the frame buffer.
290 const uint32_t count = instructions.Count(updateBufferIndex);
291 for(uint32_t index = currentNumberOfInstructions; index < count; ++index)
293 RenderInstruction& instruction = instructions.At(updateBufferIndex, index);
294 instruction.mIgnoreRenderToFbo = true;
299 return keepRendering;
302 } // Anonymous namespace.
304 RenderTaskProcessor::RenderTaskProcessor() = default;
306 RenderTaskProcessor::~RenderTaskProcessor() = default;
308 bool RenderTaskProcessor::Process(BufferIndex updateBufferIndex,
309 RenderTaskList& renderTasks,
311 SortedLayerPointers& sortedLayers,
312 RenderInstructionContainer& instructions,
313 bool renderToFboEnabled,
314 bool isRenderingToFbo)
316 RenderTaskList::RenderTaskContainer& taskContainer = renderTasks.GetTasks();
317 bool keepRendering = false;
319 if(taskContainer.IsEmpty())
321 // Early-exit if there are no tasks to process
322 return keepRendering;
325 // For each render-task:
326 // 1) Prepare the render-task
327 // 2) Clear the layer-stored lists of renderers (TODO check if the layer is not changed and don't clear in this case)
328 // 3) Traverse the scene-graph, filling the lists for the current render-task
329 // 4) Prepare render-instructions
331 DALI_LOG_INFO(gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Offscreens first\n");
333 // First process off screen render tasks - we may need the results of these for the on screen renders
335 keepRendering = ProcessTasks(updateBufferIndex,
340 mRenderInstructionProcessor,
345 DALI_LOG_INFO(gRenderTaskLogFilter, Debug::General, "RenderTaskProcessor::Process() Onscreen\n");
347 // Now that the off screen renders are done we can process on screen render tasks.
348 // Reset the clipping Id for the OnScreen render tasks.
350 keepRendering |= ProcessTasks(updateBufferIndex,
355 mRenderInstructionProcessor,
360 return keepRendering;
363 } // namespace SceneGraph
365 } // namespace Internal