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