Revert "[Tizen] Fix rendering issue by tbm_surface and eglSwapBuffers in native rende...
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / native-render-surface-tizen.cpp
index 3799b0e..7af6a74 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
@@ -26,6 +26,7 @@
 #include <Ecore_Wayland.h>
 #include <tbm_bufmgr.h>
 #include <tbm_surface_queue.h>
+#include <tbm_surface_internal.h>
 
 // INTERNAL INCLUDES
 #include <trigger-event.h>
@@ -49,6 +50,8 @@ struct NativeRenderSurface::Impl
     mColorDepth( isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24 ),
     mTbmFormat( isTransparent ? TBM_FORMAT_ARGB8888 : TBM_FORMAT_RGB888 ),
     mOwnSurface( false ),
+    mDrawableCompleted( false ),
+    mTbmQueue( NULL ),
     mConsumeSurface( NULL ),
     mThreadSynchronization( NULL )
   {
@@ -60,6 +63,7 @@ struct NativeRenderSurface::Impl
   ColorDepth mColorDepth;
   tbm_format mTbmFormat;
   bool mOwnSurface;
+  bool mDrawableCompleted;
 
   tbm_surface_queue_h mTbmQueue;
   tbm_surface_h mConsumeSurface;
@@ -82,12 +86,7 @@ NativeRenderSurface::~NativeRenderSurface()
   // release the surface if we own one
   if( mImpl->mOwnSurface )
   {
-
-    if( mImpl->mConsumeSurface )
-    {
-      tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
-      mImpl->mConsumeSurface = NULL;
-    }
+    ReleaseDrawable();
 
     if( mImpl->mTbmQueue )
     {
@@ -107,8 +106,6 @@ void NativeRenderSurface::SetRenderNotification( TriggerEventInterface* renderNo
 
 tbm_surface_h NativeRenderSurface::GetDrawable()
 {
-  ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
-
   return mImpl->mConsumeSurface;
 }
 
@@ -133,7 +130,7 @@ void NativeRenderSurface::CreateEglSurface( EglInterface& egl )
 
   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
 
-  eglImpl.CreateSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue, mImpl->mColorDepth );
+  eglImpl.CreateSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ), mImpl->mColorDepth );
 }
 
 void NativeRenderSurface::DestroyEglSurface( EglInterface& egl )
@@ -148,19 +145,6 @@ bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
 {
   DALI_LOG_TRACE_METHOD( gRenderSurfaceLogFilter );
 
-  if( mImpl->mConsumeSurface )
-  {
-    tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
-    mImpl->mConsumeSurface = NULL;
-  }
-
-  if( mImpl->mTbmQueue )
-  {
-    tbm_surface_queue_destroy( mImpl->mTbmQueue );
-  }
-
-  CreateNativeRenderable();
-
   if( !mImpl->mTbmQueue )
   {
     return false;
@@ -168,7 +152,7 @@ bool NativeRenderSurface::ReplaceEGLSurface( EglInterface& egl )
 
   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
 
-  return eglImpl.ReplaceSurfaceWindow( (EGLNativeWindowType)mImpl->mTbmQueue ); // reinterpret_cast does not compile
+  return eglImpl.ReplaceSurfaceWindow( reinterpret_cast< EGLNativeWindowType >( mImpl->mTbmQueue ) );
 }
 
 void NativeRenderSurface::StartRender()
@@ -186,26 +170,45 @@ void NativeRenderSurface::PostRender( EglInterface& egl, Integration::GlAbstract
   Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>( egl );
   eglImpl.SwapBuffers();
 
+  if( mImpl->mThreadSynchronization )
   {
-    ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
+    mImpl->mThreadSynchronization->PostRenderStarted();
+  }
 
-    if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
+  if( tbm_surface_queue_can_acquire( mImpl->mTbmQueue, 1 ) )
+  {
+    if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
     {
-      if( tbm_surface_queue_acquire( mImpl->mTbmQueue, &mImpl->mConsumeSurface ) != TBM_SURFACE_QUEUE_ERROR_NONE )
-      {
-        DALI_LOG_ERROR( "Failed aquire consume tbm_surface\n" );
-        return;
-      }
+      DALI_LOG_ERROR( "Failed to aquire a tbm_surface\n" );
+      return;
     }
   }
 
+  tbm_surface_internal_ref( mImpl->mConsumeSurface );
+
+  if( replacingSurface )
+  {
+    ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
+    mImpl->mDrawableCompleted = true;
+    mImpl->mTbmSurfaceCondition.Notify( lock );
+  }
+
  // create damage for client applications which wish to know the update timing
-  if( mImpl->mRenderNotification )
+  if( !replacingSurface && mImpl->mRenderNotification )
   {
     // use notification trigger
     // Tell the event-thread to render the tbm_surface
     mImpl->mRenderNotification->Trigger();
   }
+
+  if( mImpl->mThreadSynchronization )
+  {
+    // wait until the event-thread completed to use the tbm_surface
+    mImpl->mThreadSynchronization->PostRenderWaitForCompletion();
+  }
+
+  // release the consumed surface after post render was completed
+  ReleaseDrawable();
 }
 
 void NativeRenderSurface::StopRender()
@@ -239,7 +242,7 @@ RenderSurface::Type NativeRenderSurface::GetSurfaceType()
 void NativeRenderSurface::CreateNativeRenderable()
 {
   // check we're creating one with a valid size
-  DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "Pixmap size is invalid" );
+  DALI_ASSERT_ALWAYS( mImpl->mPosition.width > 0 && mImpl->mPosition.height > 0 && "tbm_surface size is invalid" );
 
   mImpl->mTbmQueue = tbm_surface_queue_create( 3, mImpl->mPosition.width, mImpl->mPosition.height, mImpl->mTbmFormat, TBM_BO_DEFAULT );
 
@@ -253,21 +256,36 @@ void NativeRenderSurface::CreateNativeRenderable()
   }
 }
 
-void NativeRenderSurface::ReleaseSurface()
+void NativeRenderSurface::ReleaseLock()
+{
+  if( mImpl->mThreadSynchronization )
+  {
+    mImpl->mThreadSynchronization->PostRenderComplete();
+  }
+}
+
+void NativeRenderSurface::WaitUntilSurfaceReplaced()
 {
   ConditionalWait::ScopedLock lock( mImpl->mTbmSurfaceCondition );
-  if( mImpl->mConsumeSurface )
+  while( !mImpl->mDrawableCompleted )
   {
-    tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
-    mImpl->mConsumeSurface = NULL;
+    mImpl->mTbmSurfaceCondition.Wait( lock );
   }
+
+  mImpl->mDrawableCompleted = false;
 }
 
-void NativeRenderSurface::ReleaseLock()
+void NativeRenderSurface::ReleaseDrawable()
 {
-  if( mImpl->mThreadSynchronization )
+  if( mImpl->mConsumeSurface )
   {
-    mImpl->mThreadSynchronization->PostRenderComplete();
+    tbm_surface_internal_unref( mImpl->mConsumeSurface );
+
+    if( tbm_surface_internal_is_valid( mImpl->mConsumeSurface ) )
+    {
+      tbm_surface_queue_release( mImpl->mTbmQueue, mImpl->mConsumeSurface );
+    }
+    mImpl->mConsumeSurface = NULL;
   }
 }