Revert "[Tizen] Remove the calling of ecore_wl2_display_screen_size_get() in render...
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-render-surface.cpp
old mode 100644 (file)
new mode 100755 (executable)
index e8d8367..eff8745
@@ -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.
 // INTERNAL INCLUDES
 #include <dali/integration-api/trigger-event-factory-interface.h>
 #include <dali/integration-api/thread-synchronization-interface.h>
-#include <dali/internal/graphics/gles20/egl-implementation.h>
+#include <dali/internal/graphics/gles/egl-implementation.h>
 #include <dali/internal/adaptor/common/adaptor-impl.h>
+#include <dali/internal/adaptor/common/adaptor-internal-services.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
 {
@@ -42,6 +46,7 @@ namespace
 {
 
 const int MINIMUM_DIMENSION_CHANGE( 1 ); ///< Minimum change for window to be considered to have moved
+const int TILE_SIZE = 16u;  ///< Unit of tile size at GPU driver
 
 #if defined(DEBUG_ENABLED)
 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
@@ -50,20 +55,30 @@ Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose
 } // unnamed namespace
 
 WindowRenderSurface::WindowRenderSurface( Dali::PositionSize positionSize, Any surface, bool isTransparent )
-: mPositionSize( positionSize ),
+: mEGL( nullptr ),
+  mDisplayConnection( nullptr ),
+  mPositionSize( positionSize ),
   mWindowBase(),
   mThreadSynchronization( NULL ),
   mRenderNotification( NULL ),
   mRotationTrigger( NULL ),
+  mGraphics( nullptr ),
+  mEGLSurface( nullptr ),
+  mEGLContext( nullptr ),
   mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
   mOutputTransformedSignal(),
   mRotationAngle( 0 ),
   mScreenRotationAngle( 0 ),
+  mBufferAge( 0 ),
+  mPreBufferAge( 0 ),
   mOwnSurface( false ),
   mRotationSupported( false ),
   mRotationFinished( true ),
   mScreenRotationFinished( true ),
-  mResizeFinished( true )
+  mResizeFinished( true ),
+  mDpiHorizontal( 0 ),
+  mDpiVertical( 0 ),
+  mPreDamagedRect()
 {
   DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n" );
   Initialize( surface );
@@ -75,19 +90,24 @@ WindowRenderSurface::~WindowRenderSurface()
   {
     delete mRotationTrigger;
   }
+
+  if ( mEGLSurface )
+  {
+    DestroySurface();
+  }
 }
 
 void WindowRenderSurface::Initialize( Any surface )
 {
-  // if width or height are zero, go full screen.
- if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
- {
-   // Default window size == screen size
-   mPositionSize.x = 0;
-   mPositionSize.y = 0;
+  // If width or height are zero, go full screen.
 if ( (mPositionSize.width == 0) || (mPositionSize.height == 0) )
 {
+    // Default window size == screen size
+    mPositionSize.x = 0;
+    mPositionSize.y = 0;
 
-   WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
- }
+    WindowSystem::GetScreenSize( mPositionSize.width, mPositionSize.height );
 }
 
   // Create a window base
   auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
@@ -101,6 +121,7 @@ void WindowRenderSurface::Initialize( Any surface )
   if( mScreenRotationAngle != 0 )
   {
     mScreenRotationFinished = false;
+    mResizeFinished = false;
   }
 }
 
@@ -131,12 +152,6 @@ void WindowRenderSurface::SetTransparency( bool transparent )
 
 void WindowRenderSurface::RequestRotation( int angle, int width, int height )
 {
-  if( !mRotationSupported )
-  {
-    DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: Rotation is not supported!\n" );
-    return;
-  }
-
   if( !mRotationTrigger )
   {
     TriggerEventFactoryInterface& triggerFactory = Internal::Adaptor::Adaptor::GetImplementation( Adaptor::Get() ).GetTriggerEventFactoryInterface();
@@ -171,19 +186,50 @@ 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;
+}
+
+int WindowRenderSurface::GetOrientation() const
+{
+  return mWindowBase->GetOrientation();
 }
 
-void WindowRenderSurface::InitializeEgl( EglInterface& eglIf )
+void WindowRenderSurface::InitializeGraphics()
 {
-  DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
-  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
+  mGraphics = &mAdaptor->GetGraphicsInterface();
+
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+  mEGL = &eglGraphics->GetEglInterface();
+
+  if ( mEGLContext == NULL )
+  {
+    // Create the OpenGL context for this window
+    Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
+    eglImpl.ChooseConfig(true, mColorDepth);
+    eglImpl.CreateWindowContext( mEGLContext );
 
-  eglImpl.ChooseConfig(true, mColorDepth);
+    // Create the OpenGL surface
+    CreateSurface();
+  }
 }
 
-void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
+void WindowRenderSurface::CreateSurface()
 {
   DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
@@ -199,29 +245,36 @@ void WindowRenderSurface::CreateEglSurface( EglInterface& eglIf )
     height = mPositionSize.width;
   }
 
-  // create the EGL window
+  // Create the EGL window
   EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
 
-  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
-  eglImpl.CreateSurfaceWindow( window, mColorDepth );
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+
+  Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+  mEGLSurface = eglImpl.CreateSurfaceWindow( window, mColorDepth );
 
   // Check rotation capability
   mRotationSupported = mWindowBase->IsEglWindowRotationSupported();
 
-  DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::CreateEglSurface: w = %d h = %d angle = %d screen rotation = %d\n", mPositionSize.width, mPositionSize.height, mRotationAngle, mScreenRotationAngle );
+  int screenWidth, screenHeight;
+  WindowSystem::GetScreenSize( screenWidth, screenHeight );
+  DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: w = %d h = %d screenWidth = %d screenHeight = %d angle = %d screen rotation = %d\n",
+      mPositionSize.width, mPositionSize.height, screenWidth, screenHeight, mRotationAngle, mScreenRotationAngle );
 }
 
-void WindowRenderSurface::DestroyEglSurface( EglInterface& eglIf )
+void WindowRenderSurface::DestroySurface()
 {
   DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
-  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( eglIf );
-  eglImpl.DestroySurface();
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+
+  Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+  eglImpl.DestroySurface( mEGLSurface );
 
   mWindowBase->DestroyEglWindow();
 }
 
-bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
+bool WindowRenderSurface::ReplaceGraphicsSurface()
 {
   DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
@@ -240,14 +293,16 @@ bool WindowRenderSurface::ReplaceEGLSurface( EglInterface& egl )
     height = mPositionSize.width;
   }
 
-  // create the EGL window
+  // Create the EGL window
   EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
 
   // Set screen rotation
   mScreenRotationFinished = false;
 
-  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
-  return eglImpl.ReplaceSurfaceWindow( window );
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+
+  Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+  return eglImpl.ReplaceSurfaceWindow( window, mEGLSurface, mEGLContext );
 }
 
 void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
@@ -255,14 +310,14 @@ void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
   bool needToMove = false;
   bool needToResize = false;
 
-  // check moving
+  // Check moving
   if( (fabs(positionSize.x - mPositionSize.x) > MINIMUM_DIMENSION_CHANGE) ||
       (fabs(positionSize.y - mPositionSize.y) > MINIMUM_DIMENSION_CHANGE) )
   {
     needToMove = true;
   }
 
-  // check resizing
+  // Check resizing
   if( (fabs(positionSize.width - mPositionSize.width) > MINIMUM_DIMENSION_CHANGE) ||
       (fabs(positionSize.height - mPositionSize.height) > MINIMUM_DIMENSION_CHANGE) )
   {
@@ -296,25 +351,21 @@ void WindowRenderSurface::MoveResize( Dali::PositionSize positionSize )
   DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height );
 }
 
-void WindowRenderSurface::SetViewMode( ViewMode viewMode )
-{
-  mWindowBase->SetViewMode( viewMode );
-}
-
 void WindowRenderSurface::StartRender()
 {
 }
 
-bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, bool resizingSurface )
+bool WindowRenderSurface::PreRender( bool resizingSurface )
 {
+  MakeContextCurrent();
+
   if( resizingSurface )
   {
-#ifdef OVER_TIZEN_VERSION_4
+    int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
+
     // Window rotate or screen rotate
     if( !mRotationFinished || !mScreenRotationFinished )
     {
-      int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
-
       mWindowBase->SetEglWindowRotation( totalAngle );
       mWindowBase->SetEglWindowBufferTransform( totalAngle );
 
@@ -329,45 +380,171 @@ bool WindowRenderSurface::PreRender( EglInterface& egl, Integration::GlAbstracti
     {
       mWindowBase->SetEglWindowTransform( mRotationAngle );
     }
-#endif
 
     // Resize case
-    if( !mResizeFinished )
+    if ( !mResizeFinished )
     {
-      mWindowBase->ResizeEglWindow( mPositionSize );
+      Dali::PositionSize positionSize;
+      positionSize.x = mPositionSize.x;
+      positionSize.y = mPositionSize.y;
+      if( totalAngle == 0 || totalAngle == 180 )
+      {
+        positionSize.width = mPositionSize.width;
+        positionSize.height = mPositionSize.height;
+      }
+      else
+      {
+        positionSize.width = mPositionSize.height;
+        positionSize.height = mPositionSize.width;
+      }
+
+      mWindowBase->ResizeEglWindow( positionSize );
       mResizeFinished = true;
 
       DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PreRender: Set resize\n" );
     }
   }
 
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+  if ( eglGraphics )
+  {
+    GlImplementation& mGLES = eglGraphics->GetGlesInterface();
+    mGLES.PreRender();
+  }
+
   return true;
 }
 
-void WindowRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstraction& glAbstraction, Dali::DisplayConnection* displayConnection, bool replacingSurface, bool resizingSurface )
+std::vector<int32_t> WindowRenderSurface::MergeRect( const Rect<int32_t>& damagedRect, int bufferAge )
 {
-  if( resizingSurface )
+  std::vector<int32_t> mergedRectArray;
+  // merge bounding
+  int dx1 = mPositionSize.width, dx2 = 0, dy1 = mPositionSize.height, dy2 = 0;
+  int checkWidth = mPositionSize.width - TILE_SIZE;
+  int checkHeight = mPositionSize.height - TILE_SIZE;
+
+  dx1 = std::min( damagedRect.x, dx1 );
+  dx2 = std::max( damagedRect.x + damagedRect.width, dx2);
+  dy1 = std::min( damagedRect.y, dy1 );
+  dy2 = std::max( damagedRect.y + damagedRect.height, dy2 );
+
+  for( int j = 0; j <= bufferAge; j++ )
   {
-    if( !mRotationFinished )
+    if( !mPreDamagedRect[j].IsEmpty() )
     {
-      DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
-
-      mRotationTrigger->Trigger();
+      dx1 = std::min( mPreDamagedRect[j].x, dx1 );
+      dx2 = std::max( mPreDamagedRect[j].x + mPreDamagedRect[j].width, dx2);
+      dy1 = std::min( mPreDamagedRect[j].y, dy1 );
+      dy2 = std::max( mPreDamagedRect[j].y + mPreDamagedRect[j].height, dy2 );
 
-      if( mThreadSynchronization )
+      if( dx1 < TILE_SIZE && dx2 > checkWidth && dy1 < TILE_SIZE && dy2 > checkHeight )
       {
-        // Wait until the event-thread complete the rotation event processing
-        mThreadSynchronization->PostRenderWaitForCompletion();
+        dx1 = 0, dx2 = mPositionSize.width, dy1 = 0, dy2 = mPositionSize.height;
+        break;
       }
     }
   }
 
-  Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
-  eglImpl.SwapBuffers();
+  dx1 = TILE_SIZE * (dx1 / TILE_SIZE);
+  dy1 = TILE_SIZE * (dy1 / TILE_SIZE);
+  dx2 = TILE_SIZE * ((dx2 + TILE_SIZE - 1) / TILE_SIZE);
+  dy2 = TILE_SIZE * ((dy2 + TILE_SIZE - 1) / TILE_SIZE);
 
-  if( mRenderNotification )
+  mergedRectArray.push_back( dx1 );
+  mergedRectArray.push_back( dy1 );
+  mergedRectArray.push_back( dx2 - dx1 );
+  mergedRectArray.push_back( dy2 - dy1 );
+
+  return mergedRectArray;
+}
+
+
+Rect<int32_t> WindowRenderSurface::SetDamagedRect( const Rect<int32_t>& damagedRect )
+{
+  auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
+  std::vector<int32_t> rectArray;
+  Rect<int32_t> mergedDamagedRect;
+  if( eglGraphics )
+  {
+    Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+
+    rectArray = MergeRect( damagedRect, mBufferAge );
+
+    mPreDamagedRect[4] = std::move( mPreDamagedRect[3] );
+    mPreDamagedRect[3] = std::move( mPreDamagedRect[2] );
+    mPreDamagedRect[2] = std::move( mPreDamagedRect[1] );
+    mPreDamagedRect[1] = std::move( mPreDamagedRect[0] );
+    mPreDamagedRect[0] = std::move( damagedRect );
+
+    eglImpl.SetDamagedRect( rectArray, mEGLSurface );
+  }
+
+  if( !rectArray.empty() )
+  {
+    mergedDamagedRect.x = rectArray[0];
+    mergedDamagedRect.y = rectArray[1];
+    mergedDamagedRect.width = rectArray[2];
+    mergedDamagedRect.height = rectArray[3];
+  }
+
+  return mergedDamagedRect;
+}
+
+int32_t WindowRenderSurface::GetBufferAge()
+{
+  int result = mBufferAge = 0;
+  auto eglGraphics = static_cast<EglGraphics *>( mGraphics );
+  if( eglGraphics )
   {
-    mRenderNotification->Trigger();
+    Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+    mBufferAge = eglImpl.GetBufferAge( mEGLSurface );;
+    result = ( mBufferAge != mPreBufferAge ) ? 0 : mBufferAge;
+    mPreBufferAge = mBufferAge;
+  }
+  return result;
+}
+
+void WindowRenderSurface::PostRender( bool renderToFbo, bool replacingSurface, bool resizingSurface )
+{
+  // Inform the gl implementation that rendering has finished before informing the surface
+  auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
+  if ( eglGraphics )
+  {
+    GlImplementation& mGLES = eglGraphics->GetGlesInterface();
+    mGLES.PostRender();
+
+    if( renderToFbo )
+    {
+      mGLES.Flush();
+      mGLES.Finish();
+    }
+    else
+    {
+      if( resizingSurface )
+      {
+        if( !mRotationFinished )
+        {
+          DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::PostRender: Trigger rotation event\n" );
+
+          mRotationTrigger->Trigger();
+
+          if( mThreadSynchronization )
+          {
+            // Wait until the event-thread complete the rotation event processing
+            mThreadSynchronization->PostRenderWaitForCompletion();
+          }
+        }
+      }
+    }
+
+    Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
+
+    eglImpl.SwapBuffers( mEGLSurface );
+
+    if( mRenderNotification )
+    {
+      mRenderNotification->Trigger();
+    }
   }
 }
 
@@ -387,11 +564,29 @@ void WindowRenderSurface::ReleaseLock()
   // Nothing to do.
 }
 
-RenderSurface::Type WindowRenderSurface::GetSurfaceType()
+Integration::RenderSurface::Type WindowRenderSurface::GetSurfaceType()
 {
   return RenderSurface::WINDOW_RENDER_SURFACE;
 }
 
+void WindowRenderSurface::MakeContextCurrent()
+{
+  if ( mEGL != nullptr )
+  {
+    mEGL->MakeContextCurrent( mEGLSurface, mEGLContext );
+  }
+}
+
+Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
+{
+  return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
+}
+
+Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
+{
+  return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
+}
+
 void WindowRenderSurface::OutputTransformed()
 {
   int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
@@ -400,6 +595,7 @@ void WindowRenderSurface::OutputTransformed()
   {
     mScreenRotationAngle = screenRotationAngle;
     mScreenRotationFinished = false;
+    mResizeFinished = false;
 
     mOutputTransformedSignal.Emit();