Add GetRenderThreadId()
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.cpp
index 2076d17..53514de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
 #include "dali/public-api/common/dali-common.h"
 
 // INTERNAL INCLUDES
-#include <dali/devel-api/adaptor-framework/thread-settings.h>
 #include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
 #include <dali/internal/adaptor/common/combined-update-render-controller-debug.h>
 #include <dali/internal/graphics/common/graphics-interface.h>
 #include <dali/internal/graphics/gles/egl-graphics.h>
-#include <dali/internal/graphics/gles/egl-implementation.h>
 #include <dali/internal/system/common/environment-options.h>
 #include <dali/internal/system/common/time-service.h>
+#include <dali/internal/thread/common/thread-settings-impl.h>
 #include <dali/internal/window-system/common/window-impl.h>
 
 namespace Dali
@@ -106,6 +105,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController(AdaptorInternalSe
   mDefaultHalfFrameNanoseconds(0u),
   mUpdateRequestCount(0u),
   mRunning(FALSE),
+  mThreadId(0),
   mThreadMode(threadMode),
   mUpdateRenderRunCount(0),
   mDestroyUpdateRenderThread(FALSE),
@@ -115,7 +115,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController(AdaptorInternalSe
   mNewSurface(NULL),
   mDeletedSurface(nullptr),
   mPostRendering(FALSE),
-  mSurfaceResized(FALSE),
+  mSurfaceResized(0),
   mForceClear(FALSE),
   mUploadWithoutRendering(FALSE),
   mFirstFrameAfterResume(FALSE)
@@ -366,8 +366,9 @@ void CombinedUpdateRenderController::ResizeSurface()
 
   {
     ConditionalWait::ScopedLock lock(mUpdateRenderThreadWaitCondition);
-    mPostRendering  = FALSE; // Clear the post-rendering flag as Update/Render thread will resize the surface now
-    mSurfaceResized = TRUE;
+    mPostRendering = FALSE; // Clear the post-rendering flag as Update/Render thread will resize the surface now
+    // Surface is resized and the surface resized count is increased.
+    mSurfaceResized++;
     mUpdateRenderThreadWaitCondition.Notify(lock);
   }
 }
@@ -407,6 +408,11 @@ void CombinedUpdateRenderController::AddSurface(Dali::RenderSurfaceInterface* su
   }
 }
 
+int32_t CombinedUpdateRenderController::GetThreadId() const
+{
+  return mThreadId;
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // EVENT THREAD
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -497,7 +503,8 @@ void CombinedUpdateRenderController::ProcessSleepRequest()
 
 void CombinedUpdateRenderController::UpdateRenderThread()
 {
-  SetThreadName("RenderThread\0");
+  ThreadSettings::SetThreadName("RenderThread\0");
+  mThreadId = ThreadSettings::GetThreadId();
 
   // Install a function for logging
   mEnvironmentOptions.InstallLogFunction();
@@ -527,6 +534,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
   // Update time
   uint64_t lastFrameTime;
   TimeService::GetNanoseconds(lastFrameTime);
+  uint64_t lastMemPoolLogTime = lastFrameTime;
 
   LOG_UPDATE_RENDER("THREAD INITIALISED");
 
@@ -535,6 +543,8 @@ void CombinedUpdateRenderController::UpdateRenderThread()
   uint64_t timeToSleepUntil   = 0;
   int      extraFramesDropped = 0;
 
+  const uint64_t memPoolInterval = 1e9 * float(mEnvironmentOptions.GetMemoryPoolInterval());
+
   const unsigned int renderToFboInterval = mEnvironmentOptions.GetRenderToFboInterval();
   const bool         renderToFboEnabled  = 0u != renderToFboInterval;
   unsigned int       frameCount          = 0u;
@@ -543,6 +553,10 @@ void CombinedUpdateRenderController::UpdateRenderThread()
   {
     LOG_UPDATE_RENDER_TRACE;
 
+    // For thread safe
+    bool         uploadOnly     = mUploadWithoutRendering;
+    unsigned int surfaceResized = mSurfaceResized;
+
     // Performance statistics are logged upon a VSYNC tick so use this point for a VSync marker
     AddPerformanceMarker(PerformanceInterface::VSYNC);
 
@@ -573,8 +587,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
       // Then create a new pixmap/window and new surface
       // If the new surface has a different display connection, then the context will be lost
       mAdaptorInterfaces.GetDisplayConnectionInterface().Initialize();
-      newSurface->InitializeGraphics();
-      newSurface->MakeContextCurrent();
+      graphics.ActivateSurfaceContext(newSurface);
       // TODO: ReplaceGraphicsSurface doesn't work, InitializeGraphics()
       // already creates new surface window, the surface and the context.
       // We probably don't need ReplaceGraphicsSurface at all.
@@ -589,8 +602,8 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     // UPDATE
     //////////////////////////////
 
-    const unsigned int currentTime   = currentFrameStartTime / NANOSECONDS_PER_MILLISECOND;
-    const unsigned int nextFrameTime = currentTime + mDefaultFrameDurationMilliseconds;
+    const uint32_t currentTime   = static_cast<uint32_t>(currentFrameStartTime / NANOSECONDS_PER_MILLISECOND);
+    const uint32_t nextFrameTime = currentTime + static_cast<uint32_t>(mDefaultFrameDurationMilliseconds);
 
     uint64_t noOfFramesSinceLastUpdate = 1;
     float    frameDelta                = 0.0f;
@@ -616,12 +629,15 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     Integration::UpdateStatus updateStatus;
 
     AddPerformanceMarker(PerformanceInterface::UPDATE_START);
+    TRACE_UPDATE_RENDER_BEGIN("DALI_UPDATE");
     mCore.Update(frameDelta,
                  currentTime,
                  nextFrameTime,
                  updateStatus,
                  renderToFboEnabled,
-                 isRenderingToFbo);
+                 isRenderingToFbo,
+                 uploadOnly);
+    TRACE_UPDATE_RENDER_END("DALI_UPDATE");
     AddPerformanceMarker(PerformanceInterface::UPDATE_END);
 
     unsigned int keepUpdatingStatus = updateStatus.KeepUpdating();
@@ -633,19 +649,6 @@ void CombinedUpdateRenderController::UpdateRenderThread()
       LOG_UPDATE_RENDER("Notification Triggered");
     }
 
-    // Check resize
-    bool surfaceResized         = false;
-    bool shouldSurfaceBeResized = ShouldSurfaceBeResized();
-    if(DALI_UNLIKELY(shouldSurfaceBeResized))
-    {
-      if(updateStatus.SurfaceRectChanged())
-      {
-        LOG_UPDATE_RENDER_TRACE_FMT("Resizing Surface");
-        SurfaceResized();
-        surfaceResized = true;
-      }
-    }
-
     // Optional logging of update/render status
     mUpdateStatusLogger.Log(keepUpdatingStatus);
 
@@ -653,6 +656,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     // RENDER
     //////////////////////////////
 
+    graphics.FrameStart();
     mAdaptorInterfaces.GetDisplayConnectionInterface().ConsumeEvents();
 
     if(mPreRenderCallback != NULL)
@@ -678,11 +682,12 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     Integration::RenderStatus renderStatus;
 
     AddPerformanceMarker(PerformanceInterface::RENDER_START);
+    TRACE_UPDATE_RENDER_BEGIN("DALI_RENDER");
 
     // Upload shared resources
-    mCore.PreRender(renderStatus, mForceClear, mUploadWithoutRendering);
+    mCore.PreRender(renderStatus, mForceClear);
 
-    if(!mUploadWithoutRendering)
+    if(!uploadOnly || surfaceResized)
     {
       // Go through each window
       WindowContainer windows;
@@ -697,7 +702,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
         {
           Integration::RenderStatus windowRenderStatus;
 
-          windowSurface->InitializeGraphics();
+          const bool sceneSurfaceResized = scene.IsSurfaceRectChanged();
 
           // clear previous frame damaged render items rects, buffer history is tracked on surface level
           mDamagedRects.clear();
@@ -711,25 +716,28 @@ void CombinedUpdateRenderController::UpdateRenderThread()
           Rect<int> clippingRect; // Empty for fbo rendering
 
           // Switch to the context of the surface, merge damaged areas for previous frames
-          windowSurface->PreRender(surfaceResized, mDamagedRects, clippingRect); // Switch GL context
-
-          if(clippingRect.IsEmpty())
-          {
-            mDamagedRects.clear();
-          }
+          windowSurface->PreRender(sceneSurfaceResized, mDamagedRects, clippingRect); // Switch GL context
 
           // Render the surface
           mCore.RenderScene(windowRenderStatus, scene, false, clippingRect);
 
-          if(windowRenderStatus.NeedsPostRender())
+          // Buffer swapping now happens when the surface render target is presented.
+
+          // If surface is resized, the surface resized count is decreased.
+          if(DALI_UNLIKELY(sceneSurfaceResized))
           {
-            windowSurface->PostRender(false, false, surfaceResized, mDamagedRects); // Swap Buffer with damage
+            SurfaceResized();
           }
         }
       }
     }
 
-    mCore.PostRender(mUploadWithoutRendering);
+    if(!uploadOnly)
+    {
+      graphics.PostRender();
+    }
+
+    mCore.PostRender();
 
     //////////////////////////////
     // DELETE SURFACE
@@ -745,8 +753,16 @@ void CombinedUpdateRenderController::UpdateRenderThread()
       SurfaceDeleted();
     }
 
+    TRACE_UPDATE_RENDER_END("DALI_RENDER");
     AddPerformanceMarker(PerformanceInterface::RENDER_END);
 
+    // if the memory pool interval is set and has elapsed, log the graphics memory pools
+    if(0 < memPoolInterval && memPoolInterval < lastFrameTime - lastMemPoolLogTime)
+    {
+      lastMemPoolLogTime = lastFrameTime;
+      graphics.LogMemoryPools();
+    }
+
     mForceClear = false;
 
     // Trigger event thread to request Update/Render thread to sleep if update not required
@@ -912,16 +928,13 @@ void CombinedUpdateRenderController::SurfaceDeleted()
   mSurfaceSemaphore.Release(1);
 }
 
-bool CombinedUpdateRenderController::ShouldSurfaceBeResized()
-{
-  ConditionalWait::ScopedLock lock(mUpdateRenderThreadWaitCondition);
-  return mSurfaceResized;
-}
-
 void CombinedUpdateRenderController::SurfaceResized()
 {
   ConditionalWait::ScopedLock lock(mUpdateRenderThreadWaitCondition);
-  mSurfaceResized = FALSE;
+  if(mSurfaceResized)
+  {
+    mSurfaceResized--;
+  }
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////