Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / combined-update-render-controller.cpp
index 31cf0fd..4c7b79f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
@@ -28,6 +28,7 @@
 #include <dali/internal/system/common/environment-options.h>
 #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>
 
 namespace Dali
 {
@@ -40,6 +41,7 @@ namespace Adaptor
 
 namespace
 {
+
 const unsigned int CREATED_THREAD_COUNT = 1u;
 
 const int CONTINUOUS = -1;
@@ -85,7 +87,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 ),
@@ -94,6 +95,7 @@ CombinedUpdateRenderController::CombinedUpdateRenderController( AdaptorInternalS
   mEnvironmentOptions( environmentOptions ),
   mNotificationTrigger( adaptorInterfaces.GetProcessCoreEventsTrigger() ),
   mSleepTrigger( NULL ),
+  mPreRenderCallback( NULL ),
   mUpdateRenderThread( NULL ),
   mDefaultFrameDelta( 0.0f ),
   mDefaultFrameDurationMilliseconds( 0u ),
@@ -135,6 +137,7 @@ CombinedUpdateRenderController::~CombinedUpdateRenderController()
 
   Stop();
 
+  delete mPreRenderCallback;
   delete mSleepTrigger;
 }
 
@@ -150,7 +153,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
 }
 
@@ -166,7 +169,11 @@ void CombinedUpdateRenderController::Start()
     sem_wait( &mEventThreadSemaphore );
   }
 
-  mRenderHelper.Start();
+  RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->StartRender();
+  }
 
   mRunning = TRUE;
 
@@ -208,7 +215,11 @@ void CombinedUpdateRenderController::Stop()
   LOG_EVENT_TRACE;
 
   // Stop Rendering and the Update/Render Thread
-  mRenderHelper.Stop();
+  RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->StopRender();
+  }
 
   StopUpdateRenderThread();
 
@@ -291,20 +302,14 @@ void CombinedUpdateRenderController::ResizeSurface()
 {
   LOG_EVENT_TRACE;
 
-  LOG_EVENT( "Starting to resize the surface, event-thread blocked" );
+  LOG_EVENT( "Resize the surface" );
 
-  // Start resizing the surface.
   {
     ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
     mPostRendering = FALSE; // Clear the post-rendering flag as Update/Render thread will resize the surface now
     mSurfaceResized = TRUE;
     mUpdateRenderThreadWaitCondition.Notify( lock );
   }
-
-  // Wait until the surface has been resized
-  sem_wait( &mEventThreadSemaphore );
-
-  LOG_EVENT( "Surface resized, event-thread continuing" );
 }
 
 void CombinedUpdateRenderController::SetRenderRefreshRate( unsigned int numberOfFramesPerRender )
@@ -318,6 +323,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
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -379,14 +397,23 @@ void CombinedUpdateRenderController::ProcessSleepRequest()
 
 void CombinedUpdateRenderController::UpdateRenderThread()
 {
+  SetThreadName("RenderThread\0");
+
   // Install a function for logging
   mEnvironmentOptions.InstallLogFunction();
 
+  // Install a function for tracing
+  mEnvironmentOptions.InstallTraceFunction();
+
   LOG_UPDATE_RENDER( "THREAD CREATED" );
 
-  mRenderHelper.InitializeEgl();
+  RenderSurface* currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+  if( currentSurface )
+  {
+    currentSurface->InitializeGraphics( mAdaptorInterfaces.GetGraphicsInterface(), mAdaptorInterfaces.GetDisplayConnectionInterface() );
+  }
 
-  // tell core it has a context
+  // Tell core it has a context
   mCore.ContextCreated();
 
   NotifyThreadInitialised();
@@ -435,26 +462,21 @@ void CombinedUpdateRenderController::UpdateRenderThread()
     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( mAdaptorInterfaces.GetGraphicsInterface(), mAdaptorInterfaces.GetDisplayConnectionInterface() );
+      newSurface->ReplaceGraphicsSurface();
       SurfaceReplaced();
     }
 
-    //////////////////////////////
-    // RESIZE SURFACE
-    //////////////////////////////
-
     const bool isRenderingToFbo = renderToFboEnabled && ( ( 0u == frameCount ) || ( 0u != frameCount % renderToFboInterval ) );
     ++frameCount;
 
-    // The resizing will be applied in the next loop
-    bool surfaceResized = ShouldSurfaceBeResized();
-    if( DALI_UNLIKELY( surfaceResized ) )
-    {
-      LOG_UPDATE_RENDER_TRACE_FMT( "Resizing Surface" );
-      mRenderHelper.ResizeSurface();
-      SurfaceResized();
-    }
-
     //////////////////////////////
     // UPDATE
     //////////////////////////////
@@ -493,6 +515,19 @@ 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 );
 
@@ -500,8 +535,23 @@ 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;
+      }
+    }
+
+    currentSurface = mAdaptorInterfaces.GetRenderSurfaceInterface();
+    if( currentSurface )
+    {
+      currentSurface->PreRender( surfaceResized );
+    }
 
     Integration::RenderStatus renderStatus;
 
@@ -513,12 +563,14 @@ void CombinedUpdateRenderController::UpdateRenderThread()
 
     if( renderStatus.NeedsPostRender() )
     {
-      mRenderHelper.PostRender( isRenderingToFbo );
+      if( currentSurface )
+      {
+        currentSurface->PostRender( isRenderingToFbo, ( mNewSurface != NULL ), surfaceResized );
+      }
     }
 
     // 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;
@@ -575,7 +627,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" );
 
@@ -653,17 +710,13 @@ void CombinedUpdateRenderController::SurfaceReplaced()
 bool CombinedUpdateRenderController::ShouldSurfaceBeResized()
 {
   ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
-
-  bool surfaceSized = mSurfaceResized;
-  mSurfaceResized = FALSE;
-
-  return surfaceSized;
+  return mSurfaceResized;
 }
 
 void CombinedUpdateRenderController::SurfaceResized()
 {
-  // Just increment the semaphore
-  sem_post( &mEventThreadSemaphore );
+  ConditionalWait::ScopedLock lock( mUpdateRenderThreadWaitCondition );
+  mSurfaceResized = FALSE;
 }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////