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