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