[Tizen] Call PostRender method of the surface if needed
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.cpp
index 36fc133..3291c4b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
 // EXTERNAL INCLUDES
 #include <errno.h>
 #include <dali/integration-api/platform-abstraction.h>
+#include <unistd.h>
 
 // INTERNAL INCLUDES
-#include <dali/integration-api/adaptors/trigger-event-factory.h>
+#include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
 #include <dali/devel-api/adaptor-framework/thread-settings.h>
 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
 #include <dali/internal/adaptor/common/combined-update-render-controller-debug.h>
@@ -32,6 +33,7 @@
 #include <dali/internal/graphics/common/graphics-interface.h>
 #include <dali/internal/system/common/environment-options.h>
 #include <dali/internal/system/common/time-service.h>
+#include <dali/internal/window-system/common/window-impl.h>
 
 namespace Dali
 {
@@ -91,6 +93,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
 : mFpsTracker( environmentOptions ),
   mUpdateStatusLogger( environmentOptions ),
   mEventThreadSemaphore(),
+  mGraphicsInitializeSemaphore(),
   mUpdateRenderThreadWaitCondition(),
   mAdaptorInterfaces( adaptorInterfaces ),
   mPerformanceInterface( adaptorInterfaces.GetPerformanceInterface() ),
@@ -131,10 +134,11 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
     currentSurface->SetThreadSynchronization( *this );
   }
 
-  TriggerEventFactoryInterface& triggerFactory = mAdaptorInterfaces.GetTriggerEventFactoryInterface();
-  mSleepTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &CombinedUpdateRenderController::ProcessSleepRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
+  mSleepTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &CombinedUpdateRenderController::ProcessSleepRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
 
-  sem_init( &mEventThreadSemaphore, 0, 0 ); // Initialize to 0 so that it just waits if sem_post has not been called
+  // Initialize to 0 so that it just waits if sem_post has not been called
+  sem_init( &mEventThreadSemaphore, 0, 0 );
+  sem_init( &mGraphicsInitializeSemaphore, 0, 0 );
 }
 
 CombinedUpdateRenderController::~CombinedUpdateRenderController()
@@ -175,7 +179,7 @@ void CombinedUpdateRenderController::Start()
     sem_wait( &mEventThreadSemaphore );
   }
 
-  Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
   if( currentSurface )
   {
     currentSurface->StartRender();
@@ -232,7 +236,7 @@ void CombinedUpdateRenderController::Stop()
   LOG_EVENT_TRACE;
 
   // Stop Rendering and the Update/Render Thread
-  Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
   if( currentSurface )
   {
     currentSurface->StopRender();
@@ -343,6 +347,21 @@ void CombinedUpdateRenderController::DeleteSurface( Dali::RenderSurfaceInterface
   }
 }
 
+void CombinedUpdateRenderController::WaitForGraphicsInitialization()
+{
+  LOG_EVENT_TRACE;
+
+  if( mUpdateRenderThread )
+  {
+    LOG_EVENT( "Waiting for graphics initialisation, event-thread blocked" );
+
+    // Wait until the graphics has been initialised
+    sem_wait( &mGraphicsInitializeSemaphore );
+
+    LOG_EVENT( "graphics initialised, event-thread continuing" );
+  }
+}
+
 void CombinedUpdateRenderController::ResizeSurface()
 {
   LOG_EVENT_TRACE;
@@ -381,6 +400,17 @@ void CombinedUpdateRenderController::SetPreRenderCallback( CallbackBase* callbac
   mPreRenderCallback = callback;
 }
 
+void CombinedUpdateRenderController::AddSurface( Dali::RenderSurfaceInterface* surface )
+{
+  LOG_EVENT_TRACE;
+  LOG_EVENT( "Surface is added" );
+  if( mUpdateRenderThread )
+  {
+    // Set the ThreadSyncronizationInterface on the added surface
+    surface->SetThreadSynchronization( *this );
+  }
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // EVENT THREAD
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -457,6 +487,9 @@ void CombinedUpdateRenderController::UpdateRenderThread()
   Dali::DisplayConnection& displayConnection = mAdaptorInterfaces.GetDisplayConnectionInterface();
   displayConnection.Initialize();
 
+  // EGL has been initialised at this point
+  NotifyGraphicsInitialised();
+
   RenderSurfaceInterface* currentSurface = nullptr;
 
   GraphicsInterface& graphics = mAdaptorInterfaces.GetGraphicsInterface();
@@ -544,19 +577,21 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     // REPLACE SURFACE
     //////////////////////////////
 
-    Integration::RenderSurface* newSurface = ShouldSurfaceBeReplaced();
+    Dali::RenderSurfaceInterface* newSurface = ShouldSurfaceBeReplaced();
     if( DALI_UNLIKELY( newSurface ) )
     {
       LOG_UPDATE_RENDER_TRACE_FMT( "Replacing Surface" );
-
       // This is designed for replacing pixmap surfaces, but should work for window as well
       // we need to delete the surface and renderable (pixmap / window)
       // 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->ReplaceGraphicsSurface();
+      newSurface->MakeContextCurrent();
+      // TODO: ReplaceGraphicsSurface doesn't work, InitializeGraphics()
+      // already creates new surface window, the surface and the context.
+      // We probably don't need ReplaceGraphicsSurface at all.
+      // newSurface->ReplaceGraphicsSurface();
       SurfaceReplaced();
     }
 
@@ -602,6 +637,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     }
 
     // Check resize
+    bool surfaceResized = false;
     bool shouldSurfaceBeResized = ShouldSurfaceBeResized();
     if( DALI_UNLIKELY( shouldSurfaceBeResized ) )
     {
@@ -609,6 +645,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
       {
         LOG_UPDATE_RENDER_TRACE_FMT( "Resizing Surface" );
         SurfaceResized();
+        surfaceResized = true;
       }
     }
 
@@ -648,18 +685,54 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     Integration::RenderStatus renderStatus;
 
     AddPerformanceMarker( PerformanceInterface::RENDER_START );
-    mCore.Render( renderStatus, mForceClear, mUploadWithoutRendering );
+
+    // Upload shared resources
+    mCore.PreRender( renderStatus, mForceClear, mUploadWithoutRendering );
+
+    if ( !mUploadWithoutRendering )
+    {
+      // Go through each window
+      WindowContainer windows;
+      mAdaptorInterfaces.GetWindowContainerInterface( windows );
+
+      for( auto&& window : windows )
+      {
+        Dali::Integration::Scene scene = window->GetScene();
+        Dali::RenderSurfaceInterface* windowSurface = window->GetSurface();
+
+        if ( scene && windowSurface )
+        {
+          windowSurface->InitializeGraphics();
+
+          // Render off-screen frame buffers first if any
+          mCore.RenderScene( scene, true );
+
+          // Switch to the EGL context of the surface
+          windowSurface->PreRender( surfaceResized ); // Switch GL context
+
+          // Render the surface
+          mCore.RenderScene( scene, false );
+
+          if( renderStatus.NeedsPostRender() )
+          {
+            windowSurface->PostRender( false, false, surfaceResized ); // Swap Buffer
+          }
+        }
+      }
+    }
+
+    mCore.PostRender( mUploadWithoutRendering );
 
     //////////////////////////////
     // DELETE SURFACE
     //////////////////////////////
 
-    Integration::RenderSurface* deletedSurface = ShouldSurfaceBeDeleted();
+    Dali::RenderSurfaceInterface* deletedSurface = ShouldSurfaceBeDeleted();
     if( DALI_UNLIKELY( deletedSurface ) )
     {
       LOG_UPDATE_RENDER_TRACE_FMT( "Deleting Surface" );
 
-      mCore.SurfaceDeleted( deletedSurface );
+      deletedSurface->DestroySurface();
 
       SurfaceDeleted();
     }
@@ -724,7 +797,7 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     }
   }
 
-  // Inform core of context destruction & shutdown EGL
+  // Inform core of context destruction
   mCore.ContextDestroyed();
   currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
   if( currentSurface )
@@ -733,6 +806,9 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     currentSurface = nullptr;
   }
 
+  // Shutdown EGL
+  eglInterface->TerminateGles();
+
   LOG_UPDATE_RENDER( "THREAD DESTROYED" );
 
   // Uninstall the logging function
@@ -793,11 +869,11 @@ bool CombinedUpdateRenderController::UpdateRenderReady( bool& useElapsedTime, bo
   return ! mDestroyUpdateRenderThread;
 }
 
-Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
+Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
 {
   ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
 
-  Integration::RenderSurface* newSurface = mNewSurface;
+  Dali::RenderSurfaceInterface* newSurface = mNewSurface;
   mNewSurface = NULL;
 
   return newSurface;
@@ -809,11 +885,11 @@ void CombinedUpdateRenderController::SurfaceReplaced()
   sem_post( &mEventThreadSemaphore );
 }
 
-Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
+Dali::RenderSurfaceInterface* CombinedUpdateRenderController::ShouldSurfaceBeDeleted()
 {
   ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
 
-  Integration::RenderSurface* deletedSurface = mDeletedSurface;
+  Dali::RenderSurfaceInterface* deletedSurface = mDeletedSurface;
   mDeletedSurface = NULL;
 
   return deletedSurface;
@@ -847,6 +923,11 @@ void CombinedUpdateRenderController::NotifyThreadInitialised()
   sem_post( &mEventThreadSemaphore );
 }
 
+void CombinedUpdateRenderController::NotifyGraphicsInitialised()
+{
+  sem_post( &mGraphicsInitializeSemaphore );
+}
+
 void CombinedUpdateRenderController::AddPerformanceMarker( PerformanceInterface::MarkerType type )
 {
   if( mPerformanceInterface )
@@ -882,7 +963,6 @@ void CombinedUpdateRenderController::PostRenderWaitForCompletion()
   while( mPostRendering &&
          ! mNewSurface &&                // We should NOT wait if we're replacing the surface
          ! mDeletedSurface &&            // We should NOT wait if we're deleting the surface
-         ! mSurfaceResized &&            // We should NOT wait if we're resizing the surface
          ! mDestroyUpdateRenderThread )
   {
     mUpdateRenderThreadWaitCondition.Wait( lock );