[Tizen] Print logs if dali skip rendering
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
index afef026..7a934f1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
 #include <memory.h>
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/threading/thread-pool.h>
 #include <dali/integration-api/core.h>
 #include <dali/internal/common/ordered-set.h>
 
@@ -42,7 +41,6 @@
 #include <dali/internal/render/renderers/pipeline-cache.h>
 #include <dali/internal/render/renderers/render-frame-buffer.h>
 #include <dali/internal/render/renderers/render-texture.h>
-#include <dali/internal/render/renderers/shader-cache.h>
 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
 #include <dali/internal/render/renderers/uniform-buffer.h>
 #include <dali/internal/render/shaders/program-controller.h>
@@ -64,6 +62,9 @@ Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_REN
 
 namespace
 {
+// TODO : Cache clean logic have some problem now. Just block it until bug resolved
+//constexpr uint32_t CACHE_CLEAN_FRAME_COUNT = 600u; // 60fps * 10sec
+
 inline Graphics::Rect2D RecalculateScissorArea(const Graphics::Rect2D& scissorArea, int orientation, const Rect<int32_t>& viewportRect)
 {
   Graphics::Rect2D newScissorArea;
@@ -139,22 +140,16 @@ struct RenderManager::Impl
   : graphicsController(graphicsController),
     renderAlgorithms(graphicsController),
     programController(graphicsController),
-    shaderCache(graphicsController),
     depthBufferAvailable(depthBufferAvailableParam),
     stencilBufferAvailable(stencilBufferAvailableParam),
     partialUpdateAvailable(partialUpdateAvailableParam)
   {
-    // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
-    threadPool = std::make_unique<Dali::ThreadPool>();
-    threadPool->Initialize(1u);
-
     uniformBufferManager = std::make_unique<Render::UniformBufferManager>(&graphicsController);
     pipelineCache        = std::make_unique<Render::PipelineCache>(graphicsController);
   }
 
   ~Impl()
   {
-    threadPool.reset(nullptr); // reset now to maintain correct destruction order
     rendererContainer.Clear(); // clear now before the pipeline cache is deleted
   }
 
@@ -192,8 +187,9 @@ struct RenderManager::Impl
 
   OrderedSet<Render::RenderTracker> mRenderTrackers; ///< List of owned render trackers
 
-  ProgramController   programController; ///< Owner of the programs
-  Render::ShaderCache shaderCache;       ///< The cache for the graphics shaders
+  OwnerKeyContainer<Render::Texture> textureDiscardQueue; ///< Discarded textures
+
+  ProgramController programController; ///< Owner of the programs
 
   std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
   std::unique_ptr<Render::PipelineCache>        pipelineCache;
@@ -202,10 +198,7 @@ struct RenderManager::Impl
   Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
   Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
 
-  std::unique_ptr<Dali::ThreadPool> threadPool;            ///< The thread pool
-  Vector<Graphics::Texture*>        boundTextures;         ///< The textures bound for rendering
-  Vector<Graphics::Texture*>        textureDependencyList; ///< The dependency list of bound textures
-  Vector<Render::TextureKey>        updatedTextures{};     ///< The updated texture list
+  Vector<Render::TextureKey> updatedTextures{}; ///< The updated texture list
 
   uint32_t    frameCount{0u};                                                    ///< The current frame count
   BufferIndex renderBufferIndex{SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX}; ///< The index of the buffer to read from;
@@ -249,7 +242,7 @@ void RenderManager::SetShaderSaver(ShaderSaver& upstream)
 void RenderManager::AddRenderer(const Render::RendererKey& renderer)
 {
   // Initialize the renderer as we are now in render thread
-  renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get()));
+  renderer->Initialize(mImpl->graphicsController, mImpl->programController, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get()));
 
   mImpl->rendererContainer.PushBack(renderer);
 }
@@ -274,7 +267,7 @@ void RenderManager::AddTexture(const Render::TextureKey& textureKey)
 {
   DALI_ASSERT_DEBUG(textureKey && "Trying to add empty texture key");
 
-  textureKey->Initialize(mImpl->graphicsController);
+  textureKey->Initialize(mImpl->graphicsController, *this);
   mImpl->textureContainer.PushBack(textureKey);
   mImpl->updatedTextures.PushBack(textureKey);
 }
@@ -288,12 +281,15 @@ void RenderManager::RemoveTexture(const Render::TextureKey& textureKey)
 
   if(iter != mImpl->textureContainer.End())
   {
+    // Destroy texture.
     textureKey->Destroy();
-    mImpl->textureContainer.Erase(iter); // Texture found; now destroy it
+
+    // Transfer ownership to the discard queue, this keeps the object alive, until the render-thread has finished with it
+    mImpl->textureDiscardQueue.PushBack(mImpl->textureContainer.Release(iter));
   }
 }
 
-void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Texture::UploadParams& params)
+void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Graphics::UploadParams& params)
 {
   DALI_ASSERT_DEBUG(textureKey && "Trying to upload to empty texture key");
   textureKey->Upload(pixelData, params);
@@ -309,6 +305,19 @@ void RenderManager::GenerateMipmaps(const Render::TextureKey& textureKey)
   mImpl->updatedTextures.PushBack(textureKey);
 }
 
+void RenderManager::SetTextureSize(const Render::TextureKey& textureKey, const Dali::ImageDimensions& size)
+{
+  DALI_ASSERT_DEBUG(textureKey && "Trying to set size on empty texture key");
+  textureKey->SetWidth(size.GetWidth());
+  textureKey->SetHeight(size.GetHeight());
+}
+
+void RenderManager::SetTextureFormat(const Render::TextureKey& textureKey, Dali::Pixel::Format pixelFormat)
+{
+  DALI_ASSERT_DEBUG(textureKey && "Trying to set pixel format on empty texture key");
+  textureKey->SetPixelFormat(pixelFormat);
+}
+
 void RenderManager::SetTextureUpdated(const Render::TextureKey& textureKey)
 {
   DALI_ASSERT_DEBUG(textureKey && "Trying to set updated on empty texture key");
@@ -499,6 +508,15 @@ void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear
   // Reset pipeline cache before rendering
   mImpl->pipelineCache->PreRender();
 
+  // Let we collect reference counts during CACHE_CLEAN_FRAME_COUNT frames.
+  // TODO : Cache clean logic have some problem now. Just block it until bug resolved
+  /*
+  if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 1)
+  {
+    mImpl->programController.ResetReferenceCount();
+  }
+  */
+
   mImpl->commandBufferSubmitted = false;
 }
 
@@ -515,6 +533,14 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>&
   if(!sceneObject || sceneObject->IsRenderingSkipped())
   {
     // We don't need to calculate dirty rects
+    if(!sceneObject)
+    {
+      DALI_LOG_ERROR("Scene was empty handle. Skip pre-rendering\n");
+    }
+    else
+    {
+      DALI_LOG_RELEASE_INFO("RenderingSkipped set true. Skip pre-rendering\n");
+    }
     return;
   }
 
@@ -771,6 +797,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
   if(mImpl->partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE && !renderToFbo && clippingRect.IsEmpty())
   {
     // ClippingRect is empty. Skip rendering
+    DALI_LOG_DEBUG_INFO("ClippingRect is empty. Skip rendering\n");
     return;
   }
 
@@ -783,6 +810,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
   if(!sceneObject)
   {
+    DALI_LOG_ERROR("Scene was empty handle. Skip rendering\n");
     return;
   }
 
@@ -823,7 +851,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
             auto program = item.mRenderer->PrepareProgram(instruction);
             if(program)
             {
-              auto memoryRequirements = program->GetUniformBlocksMemoryRequirements();
+              const auto& memoryRequirements = program->GetUniformBlocksMemoryRequirements();
 
               totalSizeCPU += memoryRequirements.totalCpuSizeRequired;
               totalSizeGPU += memoryRequirements.totalGpuSizeRequired;
@@ -966,19 +994,6 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
 
     targetstoPresent.emplace_back(currentRenderTarget);
 
-    // reset the program matrices for all programs once per frame
-    // this ensures we will set view and projection matrix once per program per camera
-    mImpl->programController.ResetProgramMatrices();
-
-    if(instruction.mFrameBuffer)
-    {
-      // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
-      for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
-      {
-        mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
-      }
-    }
-
     if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
     {
       // Offscreen buffer rendering
@@ -1060,15 +1075,11 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
                                     float(viewportRect.width),
                                     float(viewportRect.height)});
 
-    // Clear the list of bound textures
-    mImpl->boundTextures.Clear();
-
     mImpl->renderAlgorithms.ProcessRenderInstruction(
       instruction,
       mImpl->renderBufferIndex,
       depthBufferAvailable,
       stencilBufferAvailable,
-      mImpl->boundTextures,
       viewportRect,
       clippingRect,
       surfaceOrientation,
@@ -1088,7 +1099,6 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::
 
   // Flush UBOs
   mImpl->uniformBufferManager->Flush(sceneObject, renderToFbo);
-
   mImpl->renderAlgorithms.SubmitCommandBuffer();
   mImpl->commandBufferSubmitted = true;
 
@@ -1131,6 +1141,9 @@ void RenderManager::PostRender()
   }
   mImpl->updatedTextures.Clear();
 
+  // Remove discarded textures after OnRenderFinished called
+  mImpl->textureDiscardQueue.Clear();
+
   mImpl->UpdateTrackers();
 
   uint32_t count = 0u;
@@ -1139,6 +1152,15 @@ void RenderManager::PostRender()
     count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
   }
 
+  // Remove unused shader and programs during CACHE_CLEAN_FRAME_COUNT frames.
+  // TODO : Cache clean logic have some problem now. Just block it until bug resolved
+  /*
+  if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 0)
+  {
+    mImpl->programController.ClearUnusedCache();
+  }
+  */
+
   const bool haveInstructions = count > 0u;
 
   // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.