Revert "[Tizen] Revert "Support multiple window rendering""
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.cpp
index 72aa9da..25555a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 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.
@@ -29,6 +29,9 @@
 #include <dali/internal/system/common/time-service.h>
 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
 #include <dali/devel-api/adaptor-framework/thread-settings.h>
+#include <dali/internal/graphics/gles/egl-graphics.h>
+#include <dali/internal/graphics/gles/egl-implementation.h>
+#include <dali/internal/graphics/common/graphics-interface.h>
 
 namespace Dali
 {
@@ -87,7 +90,6 @@ const unsigned int MAXIMUM_UPDATE_REQUESTS = 2;
 CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalServices& adaptorInterfaces, const EnvironmentOptions& environmentOptions )
 : mFpsTracker( environmentOptions ),
   mUpdateStatusLogger( environmentOptions ),
-  mRenderHelper( adaptorInterfaces ),
   mEventThreadSemaphore(),
   mUpdateRenderThreadWaitCondition(),
   mAdaptorInterfaces( adaptorInterfaces ),
@@ -96,6 +98,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
   mEnvironmentOptions( environmentOptions ),
   mNotificationTrigger( adaptorInterfaces.GetProcessCoreEventsTrigger() ),
   mSleepTrigger( NULL ),
+  mPreRenderCallback( NULL ),
   mUpdateRenderThread( NULL ),
   mDefaultFrameDelta( 0.0f ),
   mDefaultFrameDurationMilliseconds( 0u ),
@@ -119,7 +122,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
   SetRenderRefreshRate( environmentOptions.GetRenderRefreshRate() );
 
   // Set the thread-synchronization interface on the render-surface
-  RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  Dali::RenderSurfaceInterface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
   if( currentSurface )
   {
     currentSurface->SetThreadSynchronization( *this );
@@ -137,6 +140,7 @@ CombinedUpdateRenderController::~CombinedUpdateRenderController()
 
   Stop();
 
+  delete mPreRenderCallback;
   delete mSleepTrigger;
 }
 
@@ -152,7 +156,7 @@ void CombinedUpdateRenderController::Initialize()
   int error = pthread_create( mUpdateRenderThread, NULL, InternalUpdateRenderThreadEntryFunc, this );
   DALI_ASSERT_ALWAYS( !error && "Return code from pthread_create() when creating UpdateRenderThread" );
 
-  // The Update/Render thread will now run and initialise EGL etc. and will then wait for Start to be called
+  // The Update/Render thread will now run and initialise the graphics interface etc. and will then wait for Start to be called
   // When this function returns, the application initialisation on the event thread should occur
 }
 
@@ -168,7 +172,11 @@ void CombinedUpdateRenderController::Start()
     sem_wait( &mEventThreadSemaphore );
   }
 
-  mRenderHelper.Start();
+  Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->StartRender();
+  }
 
   mRunning = TRUE;
 
@@ -210,7 +218,11 @@ void CombinedUpdateRenderController::Stop()
   LOG_EVENT_TRACE;
 
   // Stop Rendering and the Update/Render Thread
-  mRenderHelper.Stop();
+  Integration::RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->StopRender();
+  }
 
   StopUpdateRenderThread();
 
@@ -266,7 +278,7 @@ void CombinedUpdateRenderController::RequestUpdateOnce()
   }
 }
 
-void CombinedUpdateRenderController::ReplaceSurface( RenderSurface* newSurface )
+void CombinedUpdateRenderController::ReplaceSurface( Dali::RenderSurfaceInterface* newSurface )
 {
   LOG_EVENT_TRACE;
 
@@ -314,6 +326,19 @@ void CombinedUpdateRenderController::SetRenderRefreshRate( unsigned int numberOf
   LOG_EVENT( "mDefaultFrameDelta(%.6f), mDefaultFrameDurationMilliseconds(%lld), mDefaultFrameDurationNanoseconds(%lld)", mDefaultFrameDelta, mDefaultFrameDurationMilliseconds, mDefaultFrameDurationNanoseconds );
 }
 
+void CombinedUpdateRenderController::SetPreRenderCallback( CallbackBase* callback )
+{
+  LOG_EVENT_TRACE;
+  LOG_EVENT( "Set PreRender Callback" );
+
+  ConditionalWait::ScopedLock updateLock( mUpdateRenderThreadWaitCondition );
+  if( mPreRenderCallback )
+  {
+    delete mPreRenderCallback;
+  }
+  mPreRenderCallback = callback;
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // EVENT THREAD
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -385,9 +410,39 @@ void CombinedUpdateRenderController::UpdateRenderThread()
 
   LOG_UPDATE_RENDER( "THREAD CREATED" );
 
-  mRenderHelper.InitializeEgl();
+  // Initialize EGL & OpenGL
+  Dali::DisplayConnection& displayConnection = mAdaptorInterfaces.GetDisplayConnectionInterface();
+  displayConnection.Initialize();
+
+  RenderSurfaceInterface* currentSurface = nullptr;
+
+#if DALI_GLES_VERSION >= 30
+
+  GraphicsInterface& graphics = mAdaptorInterfaces.GetGraphicsInterface();
+  EglGraphics* eglGraphics = static_cast<EglGraphics *>(&graphics);
+
+  // This will only be created once
+  EglInterface* eglInterface = &eglGraphics->GetEglInterface();
+
+  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( *eglInterface );
+  eglImpl.ChooseConfig( true, COLOR_DEPTH_32 ); // Always use this for shared context???
+
+  // Create a surfaceless OpenGL context for shared resources
+  eglImpl.CreateContext();
+  eglImpl.MakeContextCurrent( EGL_NO_SURFACE, eglImpl.GetContext() );
 
-  // tell core it has a context
+#else // DALI_GLES_VERSION >= 30
+
+  currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->InitializeGraphics();
+    currentSurface->MakeContextCurrent();
+  }
+
+#endif
+
+  // Tell core it has a context
   mCore.ContextCreated();
 
   NotifyThreadInitialised();
@@ -432,11 +487,19 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     // REPLACE SURFACE
     //////////////////////////////
 
-    RenderSurface* newSurface = ShouldSurfaceBeReplaced();
+    Integration::RenderSurface* newSurface = ShouldSurfaceBeReplaced();
     if( DALI_UNLIKELY( newSurface ) )
     {
       LOG_UPDATE_RENDER_TRACE_FMT( "Replacing Surface" );
-      mRenderHelper.ReplaceSurface( newSurface );
+
+      // 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();
       SurfaceReplaced();
     }
 
@@ -482,14 +545,12 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     }
 
     // Check resize
-    bool surfaceResized = ShouldSurfaceBeResized();
-    if( DALI_UNLIKELY( surfaceResized ) )
+    bool shouldSurfaceBeResized = ShouldSurfaceBeResized();
+    if( DALI_UNLIKELY( shouldSurfaceBeResized ) )
     {
-      // RenderHelper::ResizeSurface() should be called right after a viewport is changed.
       if( updateStatus.SurfaceRectChanged() )
       {
         LOG_UPDATE_RENDER_TRACE_FMT( "Resizing Surface" );
-        mRenderHelper.ResizeSurface();
         SurfaceResized();
       }
     }
@@ -501,8 +562,22 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     // RENDER
     //////////////////////////////
 
-    mRenderHelper.ConsumeEvents();
-    mRenderHelper.PreRender();
+    mAdaptorInterfaces.GetDisplayConnectionInterface().ConsumeEvents();
+
+    if( mPreRenderCallback != NULL )
+    {
+      bool keepCallback = CallbackBase::ExecuteReturn<bool>(*mPreRenderCallback);
+      if( ! keepCallback )
+      {
+        delete mPreRenderCallback;
+        mPreRenderCallback = NULL;
+      }
+    }
+
+#if DALI_GLES_VERSION >= 30
+    // Make the shared surfaceless context as current before rendering
+    eglImpl.MakeContextCurrent( EGL_NO_SURFACE, eglImpl.GetContext() );
+#endif
 
     Integration::RenderStatus renderStatus;
 
@@ -512,14 +587,8 @@ void CombinedUpdateRenderController::UpdateRenderThread()
 
     mForceClear = false;
 
-    if( renderStatus.NeedsPostRender() )
-    {
-      mRenderHelper.PostRender( isRenderingToFbo );
-    }
-
     // Trigger event thread to request Update/Render thread to sleep if update not required
-    if( ( Integration::KeepUpdating::NOT_REQUESTED == keepUpdatingStatus ) &&
-        ! renderStatus.NeedsUpdate() )
+    if( ( Integration::KeepUpdating::NOT_REQUESTED == keepUpdatingStatus ) && !renderStatus.NeedsUpdate() )
     {
       mSleepTrigger->Trigger();
       updateRequired = false;
@@ -576,7 +645,12 @@ void CombinedUpdateRenderController::UpdateRenderThread()
 
   // Inform core of context destruction & shutdown EGL
   mCore.ContextDestroyed();
-  mRenderHelper.ShutdownEgl();
+  currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->DestroySurface();
+    currentSurface = nullptr;
+  }
 
   LOG_UPDATE_RENDER( "THREAD DESTROYED" );
 
@@ -635,11 +709,11 @@ bool CombinedUpdateRenderController::UpdateRenderReady( bool& useElapsedTime, bo
   return ! mDestroyUpdateRenderThread;
 }
 
-RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
+Integration::RenderSurface* CombinedUpdateRenderController::ShouldSurfaceBeReplaced()
 {
   ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
 
-  RenderSurface* newSurface = mNewSurface;
+  Integration::RenderSurface* newSurface = mNewSurface;
   mNewSurface = NULL;
 
   return newSurface;