Revert "[Tizen] Add screen and client rotation itself function"
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-render-surface.cpp
index dacb89e..7326047 100644 (file)
@@ -60,11 +60,14 @@ 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 ),
@@ -104,6 +107,13 @@ void WindowRenderSurface::Initialize( Any surface )
 
   // Connect signals
   mWindowBase->OutputTransformedSignal().Connect( this, &WindowRenderSurface::OutputTransformed );
+
+  // Check screen rotation
+  mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
+  if( mScreenRotationAngle != 0 )
+  {
+    mScreenRotationFinished = false;
+  }
 }
 
 Any WindowRenderSurface::GetNativeWindow()
@@ -133,6 +143,12 @@ 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 )
   {
     mRotationTrigger = TriggerEventFactory::CreateTriggerEvent( MakeCallback( this, &WindowRenderSurface::ProcessRotationRequest ), TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER );
@@ -184,11 +200,6 @@ void WindowRenderSurface::GetDpi( unsigned int& dpiHorizontal, unsigned int& dpi
   dpiVertical = mDpiVertical;
 }
 
-int WindowRenderSurface::GetOrientation() const
-{
-  return mWindowBase->GetOrientation();
-}
-
 void WindowRenderSurface::InitializeGraphics()
 {
   mGraphics = &mAdaptor->GetGraphicsInterface();
@@ -214,8 +225,20 @@ void WindowRenderSurface::CreateSurface()
 {
   DALI_LOG_TRACE_METHOD( gWindowRenderSurfaceLogFilter );
 
+  int width, height;
+  if( mScreenRotationAngle == 0 || mScreenRotationAngle == 180 )
+  {
+    width = mPositionSize.width;
+    height = mPositionSize.height;
+  }
+  else
+  {
+    width = mPositionSize.height;
+    height = mPositionSize.width;
+  }
+
   // Create the EGL window
-  EGLNativeWindowType window = mWindowBase->CreateEglWindow( mPositionSize.width, mPositionSize.height );
+  EGLNativeWindowType window = mWindowBase->CreateEglWindow( width, height );
 
   auto eglGraphics = static_cast<EglGraphics *>(mGraphics);
 
@@ -332,17 +355,83 @@ 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 )
   {
-    int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
-
     // Window rotate or screen rotate
     if( !mRotationFinished || !mScreenRotationFinished )
     {
+      int totalAngle = (mRotationAngle + mScreenRotationAngle) % 360;
+
       mWindowBase->SetEglWindowRotation( totalAngle );
       mWindowBase->SetEglWindowBufferTransform( totalAngle );
 
@@ -359,39 +448,31 @@ bool WindowRenderSurface::PreRender( bool resizingSurface )
     }
 
     // Resize case
-    if ( !mResizeFinished )
+    if( !mResizeFinished )
     {
-      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 );
+      mWindowBase->ResizeEglWindow( mPositionSize );
       mResizeFinished = true;
 
       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);
@@ -431,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 )
     {
@@ -487,15 +568,14 @@ void WindowRenderSurface::OutputTransformed()
   {
     mScreenRotationAngle = screenRotationAngle;
     mScreenRotationFinished = false;
-    mResizeFinished = false;
 
     mOutputTransformedSignal.Emit();
 
-    DALI_LOG_RELEASE_INFO( "WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mRotationAngle, mScreenRotationAngle );
+    DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: angle = %d screen rotation = %d\n", mRotationAngle, mScreenRotationAngle );
   }
   else
   {
-    DALI_LOG_RELEASE_INFO( "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
+    DALI_LOG_INFO( gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle );
   }
 }
 
@@ -513,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