Revert "[Tizen] Fix video underlay issue"
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-implementation.cpp
index dca3bd0..76fa78a 100755 (executable)
@@ -20,8 +20,8 @@
 #include <dali/internal/graphics/gles/egl-implementation.h>
 
 // EXTERNAL INCLUDES
+#include <sstream>
 #include <dali/integration-api/debug.h>
-
 #include <dali/public-api/common/dali-vector.h>
 
 // INTERNAL INCLUDES
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 
+namespace
+{
+  const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
+}
+
 namespace Dali
 {
 
@@ -64,13 +69,16 @@ EglImplementation::EglImplementation( int multiSamplingLevel,
   mEglConfig( 0 ),
   mEglContext( 0 ),
   mCurrentEglSurface( 0 ),
+  mCurrentEglContext( EGL_NO_CONTEXT ),
   mMultiSamplingLevel( multiSamplingLevel ),
+  mGlesVersion( 30 ),
   mColorDepth( COLOR_DEPTH_24 ),
   mGlesInitialized( false ),
   mIsOwnSurface( true ),
   mIsWindow( true ),
   mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
-  mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
+  mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
+  mIsSurfacelessContextSupported( false )
 {
 }
 
@@ -102,30 +110,23 @@ bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwn
     }
     eglBindAPI(EGL_OPENGL_ES_API);
 
-    mContextAttribs.Clear();
-
-#if DALI_GLES_VERSION >= 30
-
-    mContextAttribs.Reserve(5);
-    mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
-    mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
-    mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
-    mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
-
-#else // DALI_GLES_VERSION >= 30
-
-    mContextAttribs.Reserve(3);
-    mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
-    mContextAttribs.PushBack( 2 );
-
-#endif // DALI_GLES_VERSION >= 30
-
-    mContextAttribs.PushBack( EGL_NONE );
-
     mGlesInitialized = true;
     mIsOwnSurface = isOwnSurface;
   }
 
+  // Query EGL extensions to check whether surfaceless context is supported
+  const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
+  std::istringstream stream(extensionStr);
+  std::string currentExtension;
+  while ( std::getline( stream, currentExtension, ' ' ) )
+  {
+    if ( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
+    {
+      mIsSurfacelessContextSupported = true;
+      break;
+    }
+  }
+
   // We want to display this information all the time, so use the LogMessage directly
   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
       "            Vendor:        %s\n"
@@ -135,7 +136,7 @@ bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwn
       eglQueryString( mEglDisplay, EGL_VENDOR ),
       eglQueryString( mEglDisplay, EGL_VERSION ),
       eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
-      eglQueryString( mEglDisplay, EGL_EXTENSIONS ));
+      extensionStr);
 
   return mGlesInitialized;
 }
@@ -201,11 +202,20 @@ void EglImplementation::DestroySurface( EGLSurface& eglSurface )
 
 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
 {
+  if (mCurrentEglContext == eglContext)
+  {
+    return;
+  }
+
   mCurrentEglSurface = eglSurface;
 
   if(mIsOwnSurface)
   {
+    glFinish();
+
     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
+
+    mCurrentEglContext = eglContext;
   }
 
   EGLint error = eglGetError();
@@ -220,12 +230,21 @@ void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eg
 
 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
 {
+  if (mCurrentEglContext == mEglContext)
+  {
+    return;
+  }
+
   mCurrentEglNativePixmap = pixmap;
   mCurrentEglSurface = eglSurface;
 
   if(mIsOwnSurface)
   {
+    glFinish();
+
     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
+
+    mCurrentEglContext = mEglContext;
   }
 
   EGLint error = eglGetError();
@@ -242,6 +261,7 @@ void EglImplementation::MakeContextNull()
 {
   // clear the current context
   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
+  mCurrentEglContext = EGL_NO_CONTEXT;
 }
 
 void EglImplementation::TerminateGles()
@@ -270,6 +290,7 @@ void EglImplementation::TerminateGles()
     mEglConfig  = NULL;
     mEglContext = NULL;
     mCurrentEglSurface = NULL;
+    mCurrentEglContext = EGL_NO_CONTEXT;
 
     mGlesInitialized = false;
   }
@@ -298,15 +319,16 @@ void EglImplementation::WaitGL()
   eglWaitGL();
 }
 
-void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
+bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
 {
   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
   {
-    return;
+    return true;
   }
 
   bool isTransparent = ( depth == COLOR_DEPTH_32 );
 
+  mColorDepth = depth;
   mIsWindow = isWindowType;
 
   EGLint numConfigs;
@@ -326,29 +348,22 @@ void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
 
   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
 
-#if DALI_GLES_VERSION >= 30
-
+  if( mGlesVersion >= 30 )
+  {
 #ifdef _ARCH_ARM_
-  configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
+    configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
 #else
-  // There is a bug in the desktop emulator
-  // Requesting for ES3 causes eglCreateContext even though it allows to ask
-  // for a configuration that supports GLES 3.0
-  configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
+    configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
 #endif // _ARCH_ARM_
+  }
+  else
+  {
+    configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
+  }
 
-#else // DALI_GLES_VERSION >= 30
-
-  Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
-  configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
-
-#endif //DALI_GLES_VERSION >= 30
-
-#if DALI_GLES_VERSION >= 30
 // TODO: enable this flag when it becomes supported
 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
-#endif //DALI_GLES_VERSION >= 30
 
   configAttribs.PushBack( EGL_RED_SIZE );
   configAttribs.PushBack( 8 );
@@ -386,8 +401,25 @@ void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
 #endif // DALI_PROFILE_UBUNTU
   configAttribs.PushBack( EGL_NONE );
 
-  if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
+  // Ensure number of configs is set to 1 as on some drivers,
+  // eglChooseConfig succeeds but does not actually create a proper configuration.
+  if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
+       ( numConfigs != 1 ) )
   {
+    if( mGlesVersion >= 30 )
+    {
+      mEglConfig = NULL;
+      DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retring to use OpenGL es 2.0.");
+      return false;
+    }
+
+    if ( numConfigs != 1 )
+    {
+      DALI_LOG_ERROR("No configurations found.\n");
+
+      TEST_EGL_ERROR("eglChooseConfig");
+    }
+
     EGLint error = eglGetError();
     switch (error)
     {
@@ -417,14 +449,28 @@ void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
       }
     }
     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
+    return false;
   }
+  Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
 
-  if ( numConfigs != 1 )
+  mContextAttribs.Clear();
+  if( mGlesVersion >= 30 )
   {
-    DALI_LOG_ERROR("No configurations found.\n");
-
-    TEST_EGL_ERROR("eglChooseConfig");
+    mContextAttribs.Reserve(5);
+    mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
+    mContextAttribs.PushBack( mGlesVersion / 10 );
+    mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
+    mContextAttribs.PushBack( mGlesVersion % 10 );
   }
+  else
+  {
+    mContextAttribs.Reserve(3);
+    mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
+    mContextAttribs.PushBack( 2 );
+  }
+  mContextAttribs.PushBack( EGL_NONE );
+
+  return true;
 }
 
 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
@@ -473,10 +519,10 @@ bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSur
   DestroySurface( eglSurface );
 
   // create the EGL surface
-  CreateSurfaceWindow( window, mColorDepth );
+  EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
 
   // set the context to be current with the new surface
-  MakeContextCurrent( eglSurface, eglContext );
+  MakeContextCurrent( newEglSurface, eglContext );
 
   return contextLost;
 }
@@ -495,6 +541,11 @@ bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSur
   return contextLost;
 }
 
+void EglImplementation::SetGlesVersion( const int32_t glesVersion )
+{
+  mGlesVersion = glesVersion;
+}
+
 EGLDisplay EglImplementation::GetDisplay() const
 {
   return mEglDisplay;
@@ -505,6 +556,16 @@ EGLContext EglImplementation::GetContext() const
   return mEglContext;
 }
 
+int32_t EglImplementation::GetGlesVersion() const
+{
+  return mGlesVersion;
+}
+
+bool EglImplementation::IsSurfacelessContextSupported() const
+{
+  return mIsSurfacelessContextSupported;
+}
+
 } // namespace Adaptor
 
 } // namespace Internal