Fix WindowRenderSurface crash
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-render-surface.cpp
index 04828cc..7326047 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.
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
-#include <dali/integration-api/trigger-event-factory-interface.h>
-#include <dali/integration-api/thread-synchronization-interface.h>
-#include <dali/internal/graphics/gles/egl-implementation.h>
+#include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
+#include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
 #include <dali/internal/adaptor/common/adaptor-impl.h>
 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
+#include <dali/internal/graphics/gles/egl-graphics.h>
+#include <dali/internal/graphics/gles/egl-implementation.h>
 #include <dali/internal/window-system/common/window-base.h>
 #include <dali/internal/window-system/common/window-factory.h>
 #include <dali/internal/window-system/common/window-system.h>
-#include <dali/internal/graphics/gles/egl-graphics.h>
-
+#include <dali/internal/system/common/environment-variables.h>
 
 namespace Dali
 {
@@ -60,18 +60,23 @@ WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any s
   mThreadSynchronization( NULL ),
   mRenderNotification( NULL ),
   mRotationTrigger( NULL ),
+  mFrameRenderedTrigger(),
   mGraphics( nullptr ),
   mEGLSurface( nullptr ),
   mEGLContext( nullptr ),
   mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
   mOutputTransformedSignal(),
+  mFrameCallbackInfoContainer(),
+  mMutex(),
   mRotationAngle( 0 ),
   mScreenRotationAngle( 0 ),
   mOwnSurface( false ),
   mRotationSupported( false ),
   mRotationFinished( true ),
   mScreenRotationFinished( true ),
-  mResizeFinished( true )
+  mResizeFinished( true ),
+  mDpiHorizontal( 0 ),
+  mDpiVertical( 0 )
 {
   DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
   Initialize( surface );
@@ -93,7 +98,6 @@ void WindowRenderSurface::Initialize( Any surface )
     // Default window size == screen size
     mPositionSize.x = 0;
     mPositionSize.y = 0;
-
     WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
   }
 
@@ -147,8 +151,7 @@ void WindowRenderSurface::RequestRotation( int angle, int width, int height )
 
   if( !mRotationTrigger )
   {
-    TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
-    mRotationTrigger = triggerFactory.CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
+    mRotationTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
   }
 
   mPositionSize.width = width;
@@ -179,14 +182,30 @@ PositionSize WindowRenderSurface::GetPositionSize() const
 
 void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpiVertical )
 {
-  mWindowBase->GetDpi( dpiHorizontal, dpiVertical );
+  if( mDpiHorizontal == 0 || mDpiVertical == 0 )
+  {
+    const char* environmentDpiHorizontal = std::getenv( DALI_ENV_DPI_HORIZONTAL );
+    mDpiHorizontal = environmentDpiHorizontal ? std::atoi( environmentDpiHorizontal ) : 0;
+
+    const char* environmentDpiVertical = std::getenv( DALI_ENV_DPI_VERTICAL );
+    mDpiVertical = environmentDpiVertical ? std::atoi( environmentDpiVertical ) : 0;
+
+    if( mDpiHorizontal == 0 || mDpiVertical == 0 )
+    {
+      mWindowBase->GetDpi( mDpiHorizontal, mDpiVertical );
+    }
+  }
+
+  dpiHorizontal = mDpiHorizontal;
+  dpiVertical = mDpiVertical;
 }
 
 void WindowRenderSurface::InitializeGraphics()
 {
-
   mGraphics = &mAdaptor->GetGraphicsInterface();
 
+  DALI_ASSERT_ALWAYS( mGraphics && "Graphics interface is not created" );
+
   auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
   mEGL = &eglGraphics->GetEglInterface();
 
@@ -229,7 +248,8 @@ void WindowRenderSurface::CreateSurface()
   // Check rotation capability
   mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
 
-  DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
+  DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
+      mWindowBase->GetNativeWindowId(), mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
 }
 
 void WindowRenderSurface::DestroySurface()
@@ -237,11 +257,21 @@ void WindowRenderSurface::DestroySurface()
   DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
   auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+  if( eglGraphics )
+  {
+    DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId() );
 
-  Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
-  eglImpl.DestroySurface( mEGLSurface );
+    Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
 
-  mWindowBase->DestroyEglWindow();
+    eglImpl.DestroySurface( mEGLSurface );
+    mEGLSurface = nullptr;
+
+    // Destroy context also
+    eglImpl.DestroyContext( mEGLContext );
+    mEGLContext = nullptr;
+
+    mWindowBase->DestroyEglWindow();
+  }
 }
 
 bool WindowRenderSurface::ReplaceGraphicsSurface()
@@ -325,10 +355,76 @@ void WindowRenderSurface::StartRender()
 {
 }
 
-bool WindowRenderSurface::PreRender( bool resizingSurface )
+bool WindowRenderSurface::PreRender( bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
 {
+  Dali::Integration::Scene::FrameCallbackContainer callbacks;
+
+  Dali::Integration::Scene scene = mScene.GetHandle();
+  if( scene )
+  {
+    bool needFrameRenderedTrigger = false;
+
+    scene.GetFrameRenderedCallback( callbacks );
+    if( !callbacks.empty() )
+    {
+      int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
+      if( frameRenderedSync != -1 )
+      {
+        Dali::Mutex::ScopedLock lock( mMutex );
+
+        DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync );
+
+        mFrameCallbackInfoContainer.push_back( std::unique_ptr< FrameCallbackInfo >( new FrameCallbackInfo( callbacks, frameRenderedSync ) ) );
+
+        needFrameRenderedTrigger = true;
+      }
+      else
+      {
+        DALI_LOG_ERROR( "WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n" );
+      }
+
+      // Clear callbacks
+      callbacks.clear();
+    }
+
+    scene.GetFramePresentedCallback( callbacks );
+    if( !callbacks.empty() )
+    {
+      int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
+      if( framePresentedSync != -1 )
+      {
+        Dali::Mutex::ScopedLock lock( mMutex );
+
+        DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync );
+
+        mFrameCallbackInfoContainer.push_back( std::unique_ptr< FrameCallbackInfo >( new FrameCallbackInfo( callbacks, framePresentedSync ) ) );
+
+        needFrameRenderedTrigger = true;
+      }
+      else
+      {
+        DALI_LOG_ERROR( "WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n" );
+      }
+
+      // Clear callbacks
+      callbacks.clear();
+    }
+
+    if( needFrameRenderedTrigger )
+    {
+      if( !mFrameRenderedTrigger )
+      {
+        mFrameRenderedTrigger = std::unique_ptr< TriggerEventInterface >( TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessFrameCallback ),
+                                                                                                                   TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER ) );
+      }
+      mFrameRenderedTrigger->Trigger();
+    }
+  }
+
   MakeContextCurrent();
 
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+
   if( resizingSurface )
   {
     // Window rotate or screen rotate
@@ -359,19 +455,24 @@ bool WindowRenderSurface::PreRender( bool resizingSurface )
 
       DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
     }
+
+    if (eglGraphics)
+    {
+      Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+      eglImpl.SetFullSwapNextFrame();
+    }
   }
 
-  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
-  if ( eglGraphics )
+  if (eglGraphics)
   {
-    GlImplementation& mGLES = eglGraphics->GetGlesInterface();
-    mGLES.PreRender();
+    Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+    eglImpl.SetDamage( mEGLSurface, damagedRects, clippingRect );
   }
 
   return true;
 }
 
-void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
+void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface, const std::vector<Rect<int>>& damagedRects )
 {
   // Inform the gl implementation that rendering has finished before informing the surface
   auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
@@ -391,7 +492,13 @@ void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, b
       {
         if( !mRotationFinished )
         {
-          DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
+          if( mThreadSynchronization )
+          {
+            // Enable PostRender flag
+            mThreadSynchronization->PostRenderStarted();
+          }
+
+          DALI_LOG_RELEASE_INFO("WindowRenderSurface::PostRender: Trigger rotation event\n" );
 
           mRotationTrigger->Trigger();
 
@@ -405,7 +512,7 @@ void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, b
     }
 
     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
-    eglImpl.SwapBuffers( mEGLSurface );
+    eglImpl.SwapBuffers( mEGLSurface, damagedRects );
 
     if( mRenderNotification )
     {
@@ -430,9 +537,9 @@ void WindowRenderSurface::ReleaseLock()
   // Nothing to do.
 }
 
-Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
+Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
 {
-  return RenderSurface::WINDOW_RENDER_SURFACE;
+  return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
 }
 
 void WindowRenderSurface::MakeContextCurrent()
@@ -486,6 +593,59 @@ void WindowRenderSurface::ProcessRotationRequest()
   }
 }
 
+void WindowRenderSurface::ProcessFrameCallback()
+{
+  Dali::Mutex::ScopedLock lock( mMutex );
+
+  for( auto&& iter : mFrameCallbackInfoContainer )
+  {
+    if( !iter->fileDescriptorMonitor )
+    {
+      iter->fileDescriptorMonitor = std::unique_ptr< FileDescriptorMonitor >( new FileDescriptorMonitor( iter->fileDescriptor,
+                                                                             MakeCallback( this, &WindowRenderSurface::OnFileDescriptorEventDispatched ), FileDescriptorMonitor::FD_READABLE ) );
+
+      DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor );
+    }
+  }
+}
+
+void WindowRenderSurface::OnFileDescriptorEventDispatched( FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor )
+{
+  if( !( eventBitMask & FileDescriptorMonitor::FD_READABLE ) )
+  {
+    DALI_LOG_ERROR( "WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask );
+    close( fileDescriptor );
+    return;
+  }
+
+  DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor );
+
+  std::unique_ptr< FrameCallbackInfo > callbackInfo;
+  {
+    Dali::Mutex::ScopedLock lock( mMutex );
+    auto frameCallbackInfo = std::find_if( mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(),
+                                      [fileDescriptor]( std::unique_ptr< FrameCallbackInfo >& callbackInfo )
+                                      {
+                                        return callbackInfo->fileDescriptor == fileDescriptor;
+                                      } );
+    if( frameCallbackInfo != mFrameCallbackInfoContainer.end() )
+    {
+      callbackInfo = std::move( *frameCallbackInfo );
+
+      mFrameCallbackInfoContainer.erase( frameCallbackInfo );
+    }
+  }
+
+  // Call the connected callback
+  if( callbackInfo )
+  {
+    for( auto&& iter : ( callbackInfo )->callbacks )
+    {
+      CallbackBase::Execute( *( iter.first ), iter.second );
+    }
+  }
+}
+
 } // namespace Adaptor
 
 } // namespace internal