Merge "Remove old pipeline caches" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
1 /*
2  * Copyright (c) 2023 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/render/common/render-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/threading/thread-pool.h>
26 #include <dali/integration-api/core.h>
27 #include <dali/internal/common/ordered-set.h>
28
29 #include <dali/internal/event/common/scene-impl.h>
30
31 #include <dali/internal/update/common/scene-graph-scene.h>
32 #include <dali/internal/update/nodes/scene-graph-layer.h>
33 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
34
35 #include <dali/internal/common/owner-key-container.h>
36
37 #include <dali/internal/render/common/render-algorithms.h>
38 #include <dali/internal/render/common/render-debug.h>
39 #include <dali/internal/render/common/render-instruction.h>
40 #include <dali/internal/render/common/render-tracker.h>
41 #include <dali/internal/render/queue/render-queue.h>
42 #include <dali/internal/render/renderers/pipeline-cache.h>
43 #include <dali/internal/render/renderers/render-frame-buffer.h>
44 #include <dali/internal/render/renderers/render-texture.h>
45 #include <dali/internal/render/renderers/shader-cache.h>
46 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
47 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
48 #include <dali/internal/render/shaders/program-controller.h>
49
50 #include <memory>
51
52 namespace Dali
53 {
54 namespace Internal
55 {
56 namespace SceneGraph
57 {
58 #if defined(DEBUG_ENABLED)
59 namespace
60 {
61 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
62 } // unnamed namespace
63 #endif
64
65 namespace
66 {
67 inline Graphics::Rect2D RecalculateScissorArea(Graphics::Rect2D scissorArea, int orientation, Rect<int32_t> viewportRect)
68 {
69   Graphics::Rect2D newScissorArea;
70
71   if(orientation == 90)
72   {
73     newScissorArea.x      = viewportRect.height - (scissorArea.y + scissorArea.height);
74     newScissorArea.y      = scissorArea.x;
75     newScissorArea.width  = scissorArea.height;
76     newScissorArea.height = scissorArea.width;
77   }
78   else if(orientation == 180)
79   {
80     newScissorArea.x      = viewportRect.width - (scissorArea.x + scissorArea.width);
81     newScissorArea.y      = viewportRect.height - (scissorArea.y + scissorArea.height);
82     newScissorArea.width  = scissorArea.width;
83     newScissorArea.height = scissorArea.height;
84   }
85   else if(orientation == 270)
86   {
87     newScissorArea.x      = scissorArea.y;
88     newScissorArea.y      = viewportRect.width - (scissorArea.x + scissorArea.width);
89     newScissorArea.width  = scissorArea.height;
90     newScissorArea.height = scissorArea.width;
91   }
92   else
93   {
94     newScissorArea.x      = scissorArea.x;
95     newScissorArea.y      = scissorArea.y;
96     newScissorArea.width  = scissorArea.width;
97     newScissorArea.height = scissorArea.height;
98   }
99   return newScissorArea;
100 }
101 } // namespace
102
103 /**
104  * Structure to contain internal data
105  */
106 struct RenderManager::Impl
107 {
108   Impl(Graphics::Controller&               graphicsController,
109        Integration::DepthBufferAvailable   depthBufferAvailableParam,
110        Integration::StencilBufferAvailable stencilBufferAvailableParam,
111        Integration::PartialUpdateAvailable partialUpdateAvailableParam)
112   : graphicsController(graphicsController),
113     renderAlgorithms(graphicsController),
114     programController(graphicsController),
115     shaderCache(graphicsController),
116     depthBufferAvailable(depthBufferAvailableParam),
117     stencilBufferAvailable(stencilBufferAvailableParam),
118     partialUpdateAvailable(partialUpdateAvailableParam)
119   {
120     // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
121     threadPool = std::make_unique<Dali::ThreadPool>();
122     threadPool->Initialize(1u);
123
124     uniformBufferManager = std::make_unique<Render::UniformBufferManager>(&graphicsController);
125     pipelineCache        = std::make_unique<Render::PipelineCache>(graphicsController);
126   }
127
128   ~Impl()
129   {
130     threadPool.reset(nullptr); // reset now to maintain correct destruction order
131     rendererContainer.Clear(); // clear now before the pipeline cache is deleted
132   }
133
134   void AddRenderTracker(Render::RenderTracker* renderTracker)
135   {
136     DALI_ASSERT_DEBUG(renderTracker != nullptr);
137     mRenderTrackers.PushBack(renderTracker);
138   }
139
140   void RemoveRenderTracker(Render::RenderTracker* renderTracker)
141   {
142     mRenderTrackers.EraseObject(renderTracker);
143   }
144
145   void UpdateTrackers()
146   {
147     for(auto&& iter : mRenderTrackers)
148     {
149       iter->PollSyncObject();
150     }
151   }
152
153   // the order is important for destruction,
154   Graphics::Controller&           graphicsController;
155   RenderQueue                     renderQueue;      ///< A message queue for receiving messages from the update-thread.
156   std::vector<SceneGraph::Scene*> sceneContainer;   ///< List of pointers to the scene graph objects of the scenes
157   Render::RenderAlgorithms        renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
158
159   OrderedSet<Render::Sampler>         samplerContainer;      ///< List of owned samplers
160   OrderedSet<Render::FrameBuffer>     frameBufferContainer;  ///< List of owned framebuffers
161   OrderedSet<Render::VertexBuffer>    vertexBufferContainer; ///< List of owned vertex buffers
162   OrderedSet<Render::Geometry>        geometryContainer;     ///< List of owned Geometries
163   OwnerKeyContainer<Render::Renderer> rendererContainer;     ///< List of owned renderers
164   OwnerKeyContainer<Render::Texture>  textureContainer;      ///< List of owned textures
165
166   OrderedSet<Render::RenderTracker> mRenderTrackers; ///< List of owned render trackers
167
168   ProgramController   programController; ///< Owner of the programs
169   Render::ShaderCache shaderCache;       ///< The cache for the graphics shaders
170
171   std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
172   std::unique_ptr<Render::PipelineCache>        pipelineCache;
173
174   Integration::DepthBufferAvailable   depthBufferAvailable;   ///< Whether the depth buffer is available
175   Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
176   Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
177
178   std::unique_ptr<Dali::ThreadPool> threadPool;            ///< The thread pool
179   Vector<Graphics::Texture*>        boundTextures;         ///< The textures bound for rendering
180   Vector<Graphics::Texture*>        textureDependencyList; ///< The dependency list of bound textures
181   Vector<Render::TextureKey>        updatedTextures{};     ///< The updated texture list
182
183   uint32_t    frameCount{0u};                                                    ///< The current frame count
184   BufferIndex renderBufferIndex{SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX}; ///< The index of the buffer to read from; this is opposite of the "update" buffer
185
186   bool lastFrameWasRendered{false}; ///< Keeps track of the last frame being rendered due to having render instructions
187   bool commandBufferSubmitted{false};
188 };
189
190 RenderManager* RenderManager::New(Graphics::Controller&               graphicsController,
191                                   Integration::DepthBufferAvailable   depthBufferAvailable,
192                                   Integration::StencilBufferAvailable stencilBufferAvailable,
193                                   Integration::PartialUpdateAvailable partialUpdateAvailable)
194 {
195   auto* manager  = new RenderManager;
196   manager->mImpl = new Impl(graphicsController,
197                             depthBufferAvailable,
198                             stencilBufferAvailable,
199                             partialUpdateAvailable);
200   return manager;
201 }
202
203 RenderManager::RenderManager()
204 : mImpl(nullptr)
205 {
206 }
207
208 RenderManager::~RenderManager()
209 {
210   delete mImpl;
211 }
212
213 RenderQueue& RenderManager::GetRenderQueue()
214 {
215   return mImpl->renderQueue;
216 }
217
218 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
219 {
220 }
221
222 void RenderManager::AddRenderer(const Render::RendererKey& renderer)
223 {
224   // Initialize the renderer as we are now in render thread
225   renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get()));
226
227   mImpl->rendererContainer.PushBack(renderer);
228 }
229
230 void RenderManager::RemoveRenderer(const Render::RendererKey& renderer)
231 {
232   mImpl->rendererContainer.EraseKey(renderer);
233 }
234
235 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
236 {
237   sampler->Initialize(mImpl->graphicsController);
238   mImpl->samplerContainer.PushBack(sampler.Release());
239 }
240
241 void RenderManager::RemoveSampler(Render::Sampler* sampler)
242 {
243   mImpl->samplerContainer.EraseObject(sampler);
244 }
245
246 void RenderManager::AddTexture(const Render::TextureKey& textureKey)
247 {
248   DALI_ASSERT_DEBUG(textureKey && "Trying to add empty texture key");
249
250   textureKey->Initialize(mImpl->graphicsController);
251   mImpl->textureContainer.PushBack(textureKey);
252   mImpl->updatedTextures.PushBack(textureKey);
253 }
254
255 void RenderManager::RemoveTexture(const Render::TextureKey& textureKey)
256 {
257   DALI_ASSERT_DEBUG(textureKey && "Trying to remove empty texture key");
258
259   // Find the texture, use std::find so we can do the erase by iterator safely
260   auto iter = std::find(mImpl->textureContainer.Begin(), mImpl->textureContainer.End(), textureKey);
261
262   if(iter != mImpl->textureContainer.End())
263   {
264     textureKey->Destroy();
265     mImpl->textureContainer.Erase(iter); // Texture found; now destroy it
266   }
267 }
268
269 void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Texture::UploadParams& params)
270 {
271   DALI_ASSERT_DEBUG(textureKey && "Trying to upload to empty texture key");
272   textureKey->Upload(pixelData, params);
273
274   mImpl->updatedTextures.PushBack(textureKey);
275 }
276
277 void RenderManager::GenerateMipmaps(const Render::TextureKey& textureKey)
278 {
279   DALI_ASSERT_DEBUG(textureKey && "Trying to generate mipmaps on empty texture key");
280   textureKey->GenerateMipmaps();
281
282   mImpl->updatedTextures.PushBack(textureKey);
283 }
284
285 void RenderManager::SetTextureUpdated(const Render::TextureKey& textureKey)
286 {
287   DALI_ASSERT_DEBUG(textureKey && "Trying to set updated on empty texture key");
288   textureKey->SetUpdated(true);
289
290   mImpl->updatedTextures.PushBack(textureKey);
291 }
292
293 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
294 {
295   sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
296                          static_cast<Dali::FilterMode::Type>(magFilterMode));
297 }
298
299 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
300 {
301   sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
302                        static_cast<Dali::WrapMode::Type>(sWrapMode),
303                        static_cast<Dali::WrapMode::Type>(tWrapMode));
304 }
305
306 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
307 {
308   Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
309   mImpl->frameBufferContainer.PushBack(frameBufferPtr);
310   frameBufferPtr->Initialize(mImpl->graphicsController);
311 }
312
313 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
314 {
315   DALI_ASSERT_DEBUG(nullptr != frameBuffer);
316
317   // Find the framebuffer, use OrderedSet.Find so we can safely do the erase
318   auto iter = mImpl->frameBufferContainer.Find(frameBuffer);
319
320   if(iter != mImpl->frameBufferContainer.End())
321   {
322     frameBuffer->Destroy();
323     mImpl->frameBufferContainer.Erase(iter); // frameBuffer found; now destroy it
324   }
325 }
326
327 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
328 {
329   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
330   mImpl->sceneContainer.push_back(scene);
331 }
332
333 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
334 {
335   auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
336   if(iter != mImpl->sceneContainer.end())
337   {
338     mImpl->sceneContainer.erase(iter);
339   }
340 }
341
342 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
343 {
344   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
345 }
346
347 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
348 {
349   frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
350 }
351
352 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
353 {
354   frameBuffer->AttachDepthTexture(texture, mipmapLevel);
355 }
356
357 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
358 {
359   frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
360 }
361
362 void RenderManager::SetMultiSamplingLevelToFrameBuffer(Render::FrameBuffer* frameBuffer, uint8_t multiSamplingLevel)
363 {
364   frameBuffer->SetMultiSamplingLevel(multiSamplingLevel);
365 }
366
367 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
368 {
369   mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
370 }
371
372 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
373 {
374   mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
375 }
376
377 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
378 {
379   vertexBuffer->SetFormat(format.Release());
380 }
381
382 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
383 {
384   vertexBuffer->SetData(data.Release(), size);
385 }
386
387 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint16ContainerType& indices)
388 {
389   geometry->SetIndexBuffer(indices);
390 }
391
392 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint32ContainerType& indices)
393 {
394   geometry->SetIndexBuffer(indices);
395 }
396
397 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
398 {
399   mImpl->geometryContainer.PushBack(geometry.Release());
400 }
401
402 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
403 {
404   mImpl->geometryContainer.EraseObject(geometry);
405 }
406
407 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
408 {
409   geometry->AddVertexBuffer(vertexBuffer);
410 }
411
412 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
413 {
414   geometry->RemoveVertexBuffer(vertexBuffer);
415 }
416
417 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
418 {
419   geometry->SetType(Render::Geometry::Type(geometryType));
420 }
421
422 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
423 {
424   mImpl->AddRenderTracker(renderTracker);
425 }
426
427 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
428 {
429   mImpl->RemoveRenderTracker(renderTracker);
430 }
431
432 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear)
433 {
434   DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
435
436   // Rollback
437   mImpl->uniformBufferManager->GetUniformBufferViewPool(mImpl->renderBufferIndex)->Rollback();
438
439   // Increment the frame count at the beginning of each frame
440   ++mImpl->frameCount;
441
442   // Process messages queued during previous update
443   mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
444
445   uint32_t count = 0u;
446   for(auto& i : mImpl->sceneContainer)
447   {
448     count += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
449   }
450
451   const bool haveInstructions = count > 0u;
452
453   DALI_LOG_INFO(gLogFilter, Debug::General, "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n", haveInstructions ? "true" : "false", mImpl->lastFrameWasRendered ? "true" : "false", forceClear ? "true" : "false");
454
455   // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
456   if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
457   {
458     DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
459
460     // Upload the geometries
461     for(auto&& geom : mImpl->geometryContainer)
462     {
463       geom->Upload(mImpl->graphicsController);
464     }
465   }
466
467   // Reset pipeline cache before rendering
468   mImpl->pipelineCache->PreRender();
469
470   mImpl->commandBufferSubmitted = false;
471 }
472
473 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
474 {
475   if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
476   {
477     return;
478   }
479
480   Internal::Scene&   sceneInternal = GetImplementation(scene);
481   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
482
483   if(!sceneObject || sceneObject->IsRenderingSkipped())
484   {
485     // We don't need to calculate dirty rects
486     return;
487   }
488
489   class DamagedRectsCleaner
490   {
491   public:
492     explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects, Rect<int>& surfaceRect)
493     : mDamagedRects(damagedRects),
494       mSurfaceRect(surfaceRect),
495       mCleanOnReturn(true)
496     {
497     }
498
499     void SetCleanOnReturn(bool cleanOnReturn)
500     {
501       mCleanOnReturn = cleanOnReturn;
502     }
503
504     ~DamagedRectsCleaner()
505     {
506       if(mCleanOnReturn)
507       {
508         mDamagedRects.clear();
509         mDamagedRects.push_back(mSurfaceRect);
510       }
511     }
512
513   private:
514     std::vector<Rect<int>>& mDamagedRects;
515     Rect<int>               mSurfaceRect;
516     bool                    mCleanOnReturn;
517   };
518
519   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
520
521   // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
522   DamagedRectsCleaner damagedRectCleaner(damagedRects, surfaceRect);
523   bool                cleanDamagedRect = false;
524
525   // Mark previous dirty rects in the std::unordered_map.
526   Scene::ItemsDirtyRectsContainer& itemsDirtyRects = sceneObject->GetItemsDirtyRects();
527   for(auto& dirtyRectPair : itemsDirtyRects)
528   {
529     dirtyRectPair.second.visited = false;
530   }
531
532   uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
533   for(uint32_t i = 0; i < instructionCount; ++i)
534   {
535     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
536
537     if(instruction.mFrameBuffer)
538     {
539       cleanDamagedRect = true;
540       continue; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
541     }
542
543     const Camera* camera = instruction.GetCamera();
544     if(camera && camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
545     {
546       Vector3    position;
547       Vector3    scale;
548       Quaternion orientation;
549       camera->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
550
551       Vector3 orientationAxis;
552       Radian  orientationAngle;
553       orientation.ToAxisAngle(orientationAxis, orientationAngle);
554
555       if(position.x > Math::MACHINE_EPSILON_10000 ||
556          position.y > Math::MACHINE_EPSILON_10000 ||
557          orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
558          orientationAngle != ANGLE_180 ||
559          scale != Vector3(1.0f, 1.0f, 1.0f))
560       {
561         cleanDamagedRect = true;
562         continue;
563       }
564     }
565     else
566     {
567       cleanDamagedRect = true;
568       continue;
569     }
570
571     Rect<int32_t> viewportRect;
572     if(instruction.mIsViewportSet)
573     {
574       const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
575       viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
576       if(viewportRect.IsEmpty() || !viewportRect.IsValid())
577       {
578         cleanDamagedRect = true;
579         continue; // just skip funny use cases for now, empty viewport means it is set somewhere else
580       }
581     }
582     else
583     {
584       viewportRect = surfaceRect;
585     }
586
587     const Matrix* viewMatrix       = instruction.GetViewMatrix(mImpl->renderBufferIndex);
588     const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
589     if(viewMatrix && projectionMatrix)
590     {
591       const RenderListContainer::SizeType count = instruction.RenderListCount();
592       for(RenderListContainer::SizeType index = 0u; index < count; ++index)
593       {
594         const RenderList* renderList = instruction.GetRenderList(index);
595         if(renderList)
596         {
597           if(!renderList->IsEmpty())
598           {
599             const std::size_t listCount = renderList->Count();
600             for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
601             {
602               RenderItem& item = renderList->GetItem(listIndex);
603               // If the item does 3D transformation, make full update
604               if(item.mUpdateArea == Vector4::ZERO)
605               {
606                 cleanDamagedRect = true;
607
608                 // Save the full rect in the damaged list. We need it when this item is removed
609                 DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
610                 auto         dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
611                 if(dirtyRectPos != itemsDirtyRects.end())
612                 {
613                   // Replace the rect
614                   dirtyRectPos->second.visited = true;
615                   dirtyRectPos->second.rect    = surfaceRect;
616                 }
617                 else
618                 {
619                   // Else, just insert the new dirtyrect
620                   itemsDirtyRects.insert({dirtyRectKey, surfaceRect});
621                 }
622                 continue;
623               }
624
625               Rect<int>    rect;
626               DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
627               // If the item refers to updated node or renderer.
628               if(item.mIsUpdated ||
629                  (item.mNode &&
630                   (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex)))))
631               {
632                 item.mIsUpdated = false;
633
634                 Vector4 updateArea = item.mRenderer ? item.mRenderer->GetVisualTransformedUpdateArea(mImpl->renderBufferIndex, item.mUpdateArea) : item.mUpdateArea;
635
636                 rect = RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3(updateArea.x, updateArea.y, 0.0f), Vector3(updateArea.z, updateArea.w, 0.0f), viewportRect.width, viewportRect.height);
637                 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
638                 {
639                   const int left   = rect.x;
640                   const int top    = rect.y;
641                   const int right  = rect.x + rect.width;
642                   const int bottom = rect.y + rect.height;
643                   rect.x           = (left / 16) * 16;
644                   rect.y           = (top / 16) * 16;
645                   rect.width       = ((right + 16) / 16) * 16 - rect.x;
646                   rect.height      = ((bottom + 16) / 16) * 16 - rect.y;
647
648                   // Found valid dirty rect.
649                   auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
650                   if(dirtyRectPos != itemsDirtyRects.end())
651                   {
652                     Rect<int> currentRect = rect;
653
654                     // Same item, merge it with the previous rect
655                     rect.Merge(dirtyRectPos->second.rect);
656
657                     // Replace the rect as current
658                     dirtyRectPos->second.visited = true;
659                     dirtyRectPos->second.rect    = currentRect;
660                   }
661                   else
662                   {
663                     // Else, just insert the new dirtyrect
664                     itemsDirtyRects.insert({dirtyRectKey, rect});
665                   }
666
667                   damagedRects.push_back(rect);
668                 }
669               }
670               else
671               {
672                 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
673                 // 2. Mark the related dirty rects as visited so they will not be removed below.
674                 auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
675                 if(dirtyRectPos != itemsDirtyRects.end())
676                 {
677                   dirtyRectPos->second.visited = true;
678                 }
679                 else
680                 {
681                   // The item is not in the list for some reason. Add it!
682                   itemsDirtyRects.insert({dirtyRectKey, surfaceRect});
683                   cleanDamagedRect = true; // And make full update at this frame
684                 }
685               }
686             }
687           }
688         }
689       }
690     }
691   }
692
693   // Check removed nodes or removed renderers dirty rects
694   // Note, std::unordered_map end iterator is validate if we call erase.
695   for(auto iter = itemsDirtyRects.cbegin(), iterEnd = itemsDirtyRects.cend(); iter != iterEnd;)
696   {
697     if(!iter->second.visited)
698     {
699       damagedRects.push_back(iter->second.rect);
700       iter = itemsDirtyRects.erase(iter);
701     }
702     else
703     {
704       ++iter;
705     }
706   }
707
708   if(!cleanDamagedRect)
709   {
710     damagedRectCleaner.SetCleanOnReturn(false);
711   }
712 }
713
714 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
715 {
716   SceneGraph::Scene* sceneObject = GetImplementation(scene).GetSceneObject();
717   if(!sceneObject)
718   {
719     return;
720   }
721
722   Rect<int> clippingRect = sceneObject->GetSurfaceRect();
723   RenderScene(status, scene, renderToFbo, clippingRect);
724 }
725
726 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
727 {
728   if(mImpl->partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE && !renderToFbo && clippingRect.IsEmpty())
729   {
730     // ClippingRect is empty. Skip rendering
731     return;
732   }
733
734   // Reset main algorithms command buffer
735   mImpl->renderAlgorithms.ResetCommandBuffer();
736
737   auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
738
739   Internal::Scene&   sceneInternal = GetImplementation(scene);
740   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
741   if(!sceneObject)
742   {
743     return;
744   }
745
746   uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
747
748   std::vector<Graphics::RenderTarget*> targetstoPresent;
749
750   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
751   if(clippingRect == surfaceRect)
752   {
753     // Full rendering case
754     // Make clippingRect empty because we're doing full rendering now if the clippingRect is empty.
755     // To reduce side effects, keep this logic now.
756     clippingRect = Rect<int>();
757   }
758
759   // Prepare to lock and map standalone uniform buffer.
760   mImpl->uniformBufferManager->ReadyToLockUniformBuffer(mImpl->renderBufferIndex);
761
762   for(uint32_t i = 0; i < count; ++i)
763   {
764     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
765
766     if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
767     {
768       continue; // skip
769     }
770
771     // Mark that we will require a post-render step to be performed (includes swap-buffers).
772     status.SetNeedsPostRender(true);
773
774     Rect<int32_t> viewportRect;
775
776     int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation() + sceneObject->GetScreenOrientation();
777     if(surfaceOrientation >= 360)
778     {
779       surfaceOrientation -= 360;
780     }
781
782     // @todo Should these be part of scene?
783     Integration::DepthBufferAvailable   depthBufferAvailable   = mImpl->depthBufferAvailable;
784     Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
785
786     Graphics::RenderTarget*           currentRenderTarget = nullptr;
787     Graphics::RenderPass*             currentRenderPass   = nullptr;
788     std::vector<Graphics::ClearValue> currentClearValues{};
789
790     if(instruction.mFrameBuffer)
791     {
792       // Ensure graphics framebuffer is created, bind attachments and create render passes
793       // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
794       // then don't render to this framebuffer.
795       if(!instruction.mFrameBuffer->GetGraphicsObject())
796       {
797         const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
798         if(!created)
799         {
800           continue;
801         }
802       }
803
804       auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
805
806       // Set the clear color for first color attachment
807       if(instruction.mIsClearColorSet && !clearValues.empty())
808       {
809         clearValues[0].color = {
810           instruction.mClearColor.r,
811           instruction.mClearColor.g,
812           instruction.mClearColor.b,
813           instruction.mClearColor.a};
814       }
815
816       currentClearValues = clearValues;
817
818       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
819
820       // offscreen buffer
821       currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
822       currentRenderPass   = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
823     }
824     else // no framebuffer
825     {
826       // surface
827       auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
828
829       if(instruction.mIsClearColorSet)
830       {
831         clearValues[0].color = {
832           instruction.mClearColor.r,
833           instruction.mClearColor.g,
834           instruction.mClearColor.b,
835           instruction.mClearColor.a};
836       }
837
838       currentClearValues = clearValues;
839
840       // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
841       // if the window has a depth/stencil buffer.
842       if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
843           stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
844          (currentClearValues.size() <= 1))
845       {
846         currentClearValues.emplace_back();
847         currentClearValues.back().depthStencil.depth   = 0;
848         currentClearValues.back().depthStencil.stencil = 0;
849       }
850
851       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
852
853       currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
854       currentRenderPass   = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
855     }
856
857     targetstoPresent.emplace_back(currentRenderTarget);
858
859     // reset the program matrices for all programs once per frame
860     // this ensures we will set view and projection matrix once per program per camera
861     mImpl->programController.ResetProgramMatrices();
862
863     if(instruction.mFrameBuffer)
864     {
865       // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
866       for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
867       {
868         mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
869       }
870     }
871
872     if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
873     {
874       // Offscreen buffer rendering
875       if(instruction.mIsViewportSet)
876       {
877         // For Viewport the lower-left corner is (0,0)
878         const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
879         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
880       }
881       else
882       {
883         viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
884       }
885       surfaceOrientation = 0;
886     }
887     else // No Offscreen frame buffer rendering
888     {
889       // Check whether a viewport is specified, otherwise the full surface size is used
890       if(instruction.mIsViewportSet)
891       {
892         // For Viewport the lower-left corner is (0,0)
893         const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
894         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
895       }
896       else
897       {
898         viewportRect = surfaceRect;
899       }
900     }
901
902     // Set surface orientation
903     // @todo Inform graphics impl by another route.
904     // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
905
906     /*** Clear region of framebuffer or surface before drawing ***/
907     bool clearFullFrameRect = (surfaceRect == viewportRect);
908     if(instruction.mFrameBuffer != nullptr)
909     {
910       Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
911       clearFullFrameRect = (frameRect == viewportRect);
912     }
913
914     if(!clippingRect.IsEmpty())
915     {
916       if(!clippingRect.Intersect(viewportRect))
917       {
918         DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
919         clippingRect = Rect<int>();
920       }
921       clearFullFrameRect = false;
922     }
923
924     Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
925     if(instruction.mIsClearColorSet)
926     {
927       if(!clearFullFrameRect)
928       {
929         if(!clippingRect.IsEmpty())
930         {
931           scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
932         }
933       }
934     }
935
936     // Scissor's value should be set based on the default system coordinates.
937     // When the surface is rotated, the input values already were set with the rotated angle.
938     // So, re-calculation is needed.
939     scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, surfaceRect);
940
941     // Begin render pass
942     mainCommandBuffer->BeginRenderPass(
943       currentRenderPass,
944       currentRenderTarget,
945       scissorArea,
946       currentClearValues);
947
948     mainCommandBuffer->SetViewport({float(viewportRect.x),
949                                     float(viewportRect.y),
950                                     float(viewportRect.width),
951                                     float(viewportRect.height)});
952
953     // Clear the list of bound textures
954     mImpl->boundTextures.Clear();
955
956     mImpl->renderAlgorithms.ProcessRenderInstruction(
957       instruction,
958       mImpl->renderBufferIndex,
959       depthBufferAvailable,
960       stencilBufferAvailable,
961       mImpl->boundTextures,
962       viewportRect,
963       clippingRect,
964       surfaceOrientation,
965       Uint16Pair(surfaceRect.width, surfaceRect.height));
966
967     Graphics::SyncObject* syncObject{nullptr};
968     // If the render instruction has an associated render tracker (owned separately)
969     // and framebuffer, create a one shot sync object, and use it to determine when
970     // the render pass has finished executing on GPU.
971     if(instruction.mRenderTracker && instruction.mFrameBuffer)
972     {
973       syncObject                 = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
974       instruction.mRenderTracker = nullptr;
975     }
976     mainCommandBuffer->EndRenderPass(syncObject);
977   }
978
979   // Unlock standalone uniform buffer.
980   mImpl->uniformBufferManager->UnlockUniformBuffer(mImpl->renderBufferIndex);
981
982   mImpl->renderAlgorithms.SubmitCommandBuffer();
983   mImpl->commandBufferSubmitted = true;
984
985   std::sort(targetstoPresent.begin(), targetstoPresent.end());
986
987   Graphics::RenderTarget* rt = nullptr;
988   for(auto& target : targetstoPresent)
989   {
990     if(target != rt)
991     {
992       mImpl->graphicsController.PresentRenderTarget(target);
993       rt = target;
994     }
995   }
996 }
997
998 void RenderManager::PostRender()
999 {
1000   if(!mImpl->commandBufferSubmitted)
1001   {
1002     // Rendering is skipped but there may be pending tasks. Flush them.
1003     Graphics::SubmitInfo submitInfo;
1004     submitInfo.cmdBuffer.clear(); // Only flush
1005     submitInfo.flags = 0 | Graphics::SubmitFlagBits::FLUSH;
1006     mImpl->graphicsController.SubmitCommandBuffers(submitInfo);
1007
1008     mImpl->commandBufferSubmitted = true;
1009   }
1010
1011   // Notify RenderGeometries that rendering has finished
1012   for(auto&& iter : mImpl->geometryContainer)
1013   {
1014     iter->OnRenderFinished();
1015   }
1016
1017   // Notify updated RenderTexture that rendering has finished
1018   for(auto&& iter : mImpl->updatedTextures)
1019   {
1020     iter->OnRenderFinished();
1021   }
1022   mImpl->updatedTextures.Clear();
1023
1024   mImpl->UpdateTrackers();
1025
1026   uint32_t count = 0u;
1027   for(auto& scene : mImpl->sceneContainer)
1028   {
1029     count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
1030   }
1031
1032   const bool haveInstructions = count > 0u;
1033
1034   // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1035   mImpl->lastFrameWasRendered = haveInstructions;
1036
1037   /**
1038    * The rendering has finished; swap to the next buffer.
1039    * Ideally the update has just finished using this buffer; otherwise the render thread
1040    * should block until the update has finished.
1041    */
1042   mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1043
1044   DALI_PRINT_RENDER_END();
1045 }
1046
1047 } // namespace SceneGraph
1048
1049 } // namespace Internal
1050
1051 } // namespace Dali