(Partial update) Fix surface damage area
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-implementation.cpp
index 783c06a..6e39644 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 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.
@@ -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 uint32_t THRESHOLD_SWAPBUFFER_COUNT = 5;
+  const uint32_t CHECK_EXTENSION_NUMBER = 2;
+  const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
+  const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
+}
+
 namespace Dali
 {
 
@@ -55,7 +63,8 @@ namespace Adaptor
 
 EglImplementation::EglImplementation( int multiSamplingLevel,
                                       Integration::DepthBufferAvailable depthBufferRequired,
-                                      Integration::StencilBufferAvailable stencilBufferRequired )
+                                      Integration::StencilBufferAvailable stencilBufferRequired ,
+                                      Integration::PartialUpdateAvailable partialUpdateRequired )
 : mContextAttribs(),
   mEglNativeDisplay( 0 ),
   mEglNativeWindow( 0 ),
@@ -64,14 +73,22 @@ 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 ),
-  mContextCurrent( false ),
   mIsWindow( true ),
   mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
-  mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
+  mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
+  mPartialUpdateRequired( partialUpdateRequired == Integration::PartialUpdateAvailable::TRUE ),
+  mIsSurfacelessContextSupported( false ),
+  mIsKhrCreateContextSupported( false ),
+  mSwapBufferCountAfterResume( 0 ),
+  mEglSetDamageRegionKHR( 0 ),
+  mEglSwapBuffersWithDamageKHR( 0 ),
+  mFullSwapNextFrame( true )
 {
 }
 
@@ -86,12 +103,18 @@ bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwn
   {
     mEglNativeDisplay = display;
 
-    //@todo see if we can just EGL_DEFAULT_DISPLAY instead
-    mEglDisplay = eglGetDisplay(mEglNativeDisplay);
-    EGLint error = eglGetError();
+    // Try to get the display connection for the native display first
+    mEglDisplay = eglGetDisplay( mEglNativeDisplay );
+
+    if( mEglDisplay == EGL_NO_DISPLAY )
+    {
+      // If failed, try to get the default display connection
+      mEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
+    }
 
-    if( mEglDisplay == NULL && error != EGL_SUCCESS )
+    if( mEglDisplay == EGL_NO_DISPLAY )
     {
+      // Still failed to get a display connection
       throw Dali::DaliException( "", "OpenGL ES is not supported");
     }
 
@@ -103,29 +126,42 @@ 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 );
+    mIsOwnSurface = isOwnSurface;
+  }
 
-#endif // DALI_GLES_VERSION >= 30
+  // 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;
+  uint32_t extensionCheckCount = 0;
+  while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
+  {
+    if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
+    {
+      mIsSurfacelessContextSupported = true;
+      extensionCheckCount++;
+    }
+    if( currentExtension == EGL_KHR_CREATE_CONTEXT )
+    {
+      mIsKhrCreateContextSupported = true;
+      extensionCheckCount++;
+    }
+  }
 
-    mContextAttribs.PushBack( EGL_NONE );
+  mGlesInitialized = true;
 
-    mGlesInitialized = true;
-    mIsOwnSurface = isOwnSurface;
-  }
+  // We want to display this information all the time, so use the LogMessage directly
+  Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
+      "            PartialUpdate  %d\n"
+      "            Vendor:        %s\n"
+      "            Version:       %s\n"
+      "            Client APIs:   %s\n"
+      "            Extensions:    %s\n",
+      mPartialUpdateRequired,
+      eglQueryString( mEglDisplay, EGL_VENDOR ),
+      eglQueryString( mEglDisplay, EGL_VERSION ),
+      eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
+      extensionStr);
 
   return mGlesInitialized;
 }
@@ -146,35 +182,88 @@ bool EglImplementation::CreateContext()
   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
 
+  mEglSetDamageRegionKHR = reinterpret_cast<PFNEGLSETDAMAGEREGIONKHRPROC>(eglGetProcAddress("eglSetDamageRegionKHR"));
+  if (!mEglSetDamageRegionKHR)
+  {
+    DALI_LOG_ERROR("Coudn't find eglSetDamageRegionKHR!\n");
+    mPartialUpdateRequired = false;
+  }
+  mEglSwapBuffersWithDamageKHR = reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>(eglGetProcAddress("eglSwapBuffersWithDamageKHR"));
+  if (!mEglSwapBuffersWithDamageKHR)
+  {
+    DALI_LOG_ERROR("Coudn't find eglSwapBuffersWithDamageKHR!\n");
+    mPartialUpdateRequired = false;
+  }
   return true;
 }
 
-void EglImplementation::DestroyContext()
+bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
 {
-  DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
+  // make sure a context isn't created twice
+  DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
+
+  eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
+  TEST_EGL_ERROR("eglCreateContext render thread");
+
+  DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
+
+  DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
+  DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
+  DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
+  DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
+  DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
 
-  eglDestroyContext( mEglDisplay, mEglContext );
-  mEglContext = 0;
+  mEglWindowContexts.push_back( eglContext );
+
+  mEglSetDamageRegionKHR = reinterpret_cast<PFNEGLSETDAMAGEREGIONKHRPROC>(eglGetProcAddress("eglSetDamageRegionKHR"));
+  if (!mEglSetDamageRegionKHR)
+  {
+    DALI_LOG_ERROR("Coudn't find eglSetDamageRegionKHR!\n");
+    mPartialUpdateRequired = false;
+  }
+  mEglSwapBuffersWithDamageKHR = reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>(eglGetProcAddress("eglSwapBuffersWithDamageKHR"));
+  if (!mEglSwapBuffersWithDamageKHR)
+  {
+    DALI_LOG_ERROR("Coudn't find eglSwapBuffersWithDamageKHR!\n");
+    mPartialUpdateRequired = false;
+  }
+  return true;
 }
 
-void EglImplementation::DestroySurface()
+void EglImplementation::DestroyContext( EGLContext& eglContext )
 {
-  if(mIsOwnSurface && mCurrentEglSurface)
+  if( eglContext )
+  {
+    eglDestroyContext( mEglDisplay, eglContext );
+    eglContext = 0;
+  }
+}
+
+void EglImplementation::DestroySurface( EGLSurface& eglSurface )
+{
+  if(mIsOwnSurface && eglSurface)
   {
     // Make context null to prevent crash in driver side
     MakeContextNull();
-    eglDestroySurface( mEglDisplay, mCurrentEglSurface );
-    mCurrentEglSurface = 0;
+    eglDestroySurface( mEglDisplay, eglSurface );
+    eglSurface = 0;
   }
 }
 
-void EglImplementation::MakeContextCurrent()
+void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
 {
-  mContextCurrent = true;
+  if (mCurrentEglContext == eglContext)
+  {
+    return;
+  }
+
+  mCurrentEglSurface = eglSurface;
 
   if(mIsOwnSurface)
   {
-    eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
+    eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
+
+    mCurrentEglContext = eglContext;
   }
 
   EGLint error = eglGetError();
@@ -185,27 +274,23 @@ void EglImplementation::MakeContextCurrent()
 
     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
   }
-
-  // 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"
-      "            Version:       %s\n"
-      "            Client APIs:   %s\n"
-      "            Extensions:    %s\n",
-      eglQueryString(mEglDisplay, EGL_VENDOR),
-      eglQueryString(mEglDisplay, EGL_VERSION),
-      eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
-      eglQueryString(mEglDisplay, EGL_EXTENSIONS));
 }
 
 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
 {
+  if (mCurrentEglContext == mEglContext)
+  {
+    return;
+  }
+
   mCurrentEglNativePixmap = pixmap;
   mCurrentEglSurface = eglSurface;
 
   if(mIsOwnSurface)
   {
-    eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
+    eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
+
+    mCurrentEglContext = mEglContext;
   }
 
   EGLint error = eglGetError();
@@ -220,9 +305,9 @@ void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglS
 
 void EglImplementation::MakeContextNull()
 {
-  mContextCurrent = false;
   // clear the current context
   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
+  mCurrentEglContext = EGL_NO_CONTEXT;
 }
 
 void EglImplementation::TerminateGles()
@@ -232,11 +317,18 @@ void EglImplementation::TerminateGles()
     // Make context null to prevent crash in driver side
     MakeContextNull();
 
-    if(mIsOwnSurface && mCurrentEglSurface)
+    for ( auto eglSurface : mEglWindowSurfaces )
     {
-      eglDestroySurface(mEglDisplay, mCurrentEglSurface);
+      if(mIsOwnSurface && eglSurface)
+      {
+        eglDestroySurface(mEglDisplay, eglSurface);
+      }
     }
     eglDestroyContext(mEglDisplay, mEglContext);
+    for ( auto eglContext : mEglWindowContexts )
+    {
+      eglDestroyContext(mEglDisplay, eglContext);
+    }
 
     eglTerminate(mEglDisplay);
 
@@ -244,6 +336,7 @@ void EglImplementation::TerminateGles()
     mEglConfig  = NULL;
     mEglContext = NULL;
     mCurrentEglSurface = NULL;
+    mCurrentEglContext = EGL_NO_CONTEXT;
 
     mGlesInitialized = false;
   }
@@ -254,14 +347,233 @@ bool EglImplementation::IsGlesInitialized() const
   return mGlesInitialized;
 }
 
-void EglImplementation::SwapBuffers()
+void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
 {
-  eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
+  if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
+  {
+#ifndef DALI_PROFILE_UBUNTU
+    if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
+    {
+      DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
+    }
+#endif //DALI_PROFILE_UBUNTU
+
+    // DALI_LOG_ERROR("EglImplementation::SwapBuffers()\n");
+    eglSwapBuffers( mEglDisplay, eglSurface );
+    mFullSwapNextFrame = false;
+
+#ifndef DALI_PROFILE_UBUNTU
+    if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
+    {
+      DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
+      mSwapBufferCountAfterResume++;
+    }
+#endif //DALI_PROFILE_UBUNTU
+  }
 }
 
-void EglImplementation::CopyBuffers()
+EGLint EglImplementation::GetBufferAge(EGLSurface& eglSurface) const
 {
-  eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
+  EGLint age = 0;
+  eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_EXT, &age);
+  if (age < 0)
+  {
+    DALI_LOG_ERROR("eglQuerySurface(%d)\n", eglGetError());
+    age = 0;
+  }
+
+  // 0 - invalid buffer
+  // 1, 2, 3
+  if (age > 3)
+  {
+    DALI_LOG_ERROR("EglImplementation::GetBufferAge() buffer age %d > 3\n", age);
+    age = 0; // shoudn't be more than 3 back buffers, if there is just reset, I don't want to add extra history level
+  }
+
+  return age;
+}
+
+void EglImplementation::SetFullSwapNextFrame()
+{
+  mFullSwapNextFrame = true;
+}
+
+void mergeRects(Rect<int>& mergingRect, const std::vector<Rect<int>>& rects)
+{
+  uint32_t i = 0;
+  if (mergingRect.IsEmpty())
+  {
+    for (;i < rects.size(); i++)
+    {
+      if (!rects[i].IsEmpty())
+      {
+        mergingRect = rects[i];
+        break;
+      }
+    }
+  }
+
+  for (;i < rects.size(); i++)
+  {
+    mergingRect.Merge(rects[i]);
+  }
+}
+
+void insertRects(std::list<std::vector<Rect<int>>>& damagedRectsList, const std::vector<Rect<int>>& damagedRects)
+{
+  damagedRectsList.push_front(damagedRects);
+  if (damagedRectsList.size() > 4) // past triple buffers + current
+  {
+    damagedRectsList.pop_back();
+  }
+}
+
+void EglImplementation::SetDamage( EGLSurface& eglSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
+{
+  if (!mPartialUpdateRequired)
+  {
+    return;
+  }
+
+  if (eglSurface != EGL_NO_SURFACE) // skip if using surfaceless context
+  {
+    EGLint width = 0;
+    EGLint height = 0;
+    eglQuerySurface(mEglDisplay, eglSurface, EGL_WIDTH, &width);
+    eglQuerySurface(mEglDisplay, eglSurface, EGL_HEIGHT, &height);
+    Rect<int> surfaceRect(0, 0, width, height);
+
+    mSurfaceRect = surfaceRect;
+
+    if (mFullSwapNextFrame)
+    {
+      insertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
+      clippingRect = Rect<int>();
+      return;
+    }
+
+    EGLint bufferAge = GetBufferAge(eglSurface);
+
+    // Buffer age 0 means the back buffer in invalid and requires full swap
+    if (!damagedRects.size() || bufferAge == 0)
+    {
+      // No damage or buffer is out of order or buffer age is reset
+      insertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
+      clippingRect = Rect<int>();
+      return;
+    }
+
+    // We push current frame damaged rects here, zero index for current frame
+    insertRects(mBufferDamagedRects, damagedRects);
+
+    // Merge damaged rects into clipping rect
+    auto bufferDamagedRects = mBufferDamagedRects.begin();
+    while (bufferAge-- >= 0 && bufferDamagedRects != mBufferDamagedRects.end())
+    {
+      const std::vector<Rect<int>>& rects = *bufferDamagedRects++;
+      mergeRects(clippingRect, rects);
+    }
+
+    if (!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * 0.8)
+    {
+      // clipping area too big or doesn't intersect surface rect
+      clippingRect = Rect<int>();
+      return;
+    }
+
+    // DALI_LOG_ERROR("eglSetDamageRegionKHR(%d, %d, %d, %d)\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
+    EGLBoolean result = mEglSetDamageRegionKHR(mEglDisplay, eglSurface, reinterpret_cast<int*>(&clippingRect), 1);
+    if (result == EGL_FALSE)
+    {
+      DALI_LOG_ERROR("eglSetDamageRegionKHR(%d)\n", eglGetError());
+    }
+  }
+}
+
+void EglImplementation::SwapBuffers(EGLSurface& eglSurface, const std::vector<Rect<int>>& damagedRects)
+{
+  if (eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
+  {
+    if (!mPartialUpdateRequired || mFullSwapNextFrame || !damagedRects.size() || (damagedRects[0].Area() > mSurfaceRect.Area() * 0.8) )
+    {
+      SwapBuffers(eglSurface);
+      return;
+    }
+
+#ifndef DALI_PROFILE_UBUNTU
+    if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
+    {
+      DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
+    }
+#endif //DALI_PROFILE_UBUNTU
+
+    std::vector< Rect< int > > mergedRects = damagedRects;
+
+    // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
+    // Could be optional and can be removed, needs to be checked with and without on platform
+    const int n = mergedRects.size();
+    for(int i = 0; i < n-1; i++)
+    {
+      if (mergedRects[i].IsEmpty())
+      {
+        continue;
+      }
+
+      for (int j = i+1; j < n; j++)
+      {
+        if (mergedRects[j].IsEmpty())
+        {
+          continue;
+        }
+
+        if (mergedRects[i].Intersects(mergedRects[j]))
+        {
+          mergedRects[i].Merge(mergedRects[j]);
+          mergedRects[j].width = 0;
+          mergedRects[j].height = 0;
+        }
+      }
+    }
+
+    int j = 0;
+    for (int i = 0; i < n; i++)
+    {
+      if (!mergedRects[i].IsEmpty())
+      {
+        mergedRects[j++] = mergedRects[i];
+      }
+    }
+
+    if (j != 0)
+    {
+      mergedRects.resize(j);
+    }
+
+    if (!mergedRects.size() || (mergedRects[0].Area() > mSurfaceRect.Area() * 0.8))
+    {
+      SwapBuffers(eglSurface);
+      return;
+    }
+
+    EGLBoolean result = mEglSwapBuffersWithDamageKHR(mEglDisplay, eglSurface, reinterpret_cast<int*>(mergedRects.data()), mergedRects.size());
+    if (result == EGL_FALSE)
+    {
+      DALI_LOG_ERROR("eglSwapBuffersWithDamageKHR(%d)\n", eglGetError());
+    }
+
+#ifndef DALI_PROFILE_UBUNTU
+    if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
+    {
+      DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
+      mSwapBufferCountAfterResume++;
+    }
+#endif //DALI_PROFILE_UBUNTU
+  }
+}
+
+void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
+{
+  eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
 }
 
 void EglImplementation::WaitGL()
@@ -269,13 +581,14 @@ 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;
   }
 
+  mColorDepth = depth;
   mIsWindow = isWindowType;
 
   EGLint numConfigs;
@@ -295,29 +608,18 @@ void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
 
   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
 
-#if DALI_GLES_VERSION >= 30
-
-#ifdef _ARCH_ARM_
-  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 );
-#endif // _ARCH_ARM_
-
-#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( mGlesVersion >= 30 )
+  {
+    configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
+  }
+  else
+  {
+    configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
+  }
 
-#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 );
@@ -326,15 +628,9 @@ void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
   configAttribs.PushBack( EGL_BLUE_SIZE );
   configAttribs.PushBack( 8 );
 
+//  For underlay video playback, we also need to set the alpha value of the 24/32bit window.
   configAttribs.PushBack( EGL_ALPHA_SIZE );
-#ifdef _ARCH_ARM_
-  // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
   configAttribs.PushBack( 8 );
-#else
-  // There is a bug in the desktop emulator
-  // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
-  configAttribs.PushBack( 0 );
-#endif // _ARCH_ARM_
 
   configAttribs.PushBack( EGL_DEPTH_SIZE );
   configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
@@ -352,8 +648,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. Retrying 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)
     {
@@ -383,20 +696,32 @@ 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( mIsKhrCreateContextSupported )
   {
-    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( mGlesVersion / 10 );
+  }
+  mContextAttribs.PushBack( EGL_NONE );
+
+  return true;
 }
 
-void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
+EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
 {
-  DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
-
   mEglNativeWindow = window;
   mColorDepth = depth;
   mIsWindow = true;
@@ -408,6 +733,8 @@ void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDe
   TEST_EGL_ERROR("eglCreateWindowSurface");
 
   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
+
+  return mCurrentEglSurface;
 }
 
 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
@@ -427,7 +754,7 @@ EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, C
   return mCurrentEglSurface;
 }
 
-bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
+bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
 {
   bool contextLost = false;
 
@@ -435,14 +762,17 @@ bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
   //  the surface is bound to the context, so set the context to null
   MakeContextNull();
 
-  // destroy the surface
-  DestroySurface();
+  if( eglSurface )
+  {
+    // destroy the surface
+    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();
+  MakeContextCurrent( newEglSurface, eglContext );
 
   return contextLost;
 }
@@ -461,16 +791,45 @@ bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSur
   return contextLost;
 }
 
+void EglImplementation::SetGlesVersion( const int32_t glesVersion )
+{
+  mGlesVersion = glesVersion;
+}
+
+void EglImplementation::SetFirstFrameAfterResume()
+{
+  mSwapBufferCountAfterResume = 0;
+}
+
 EGLDisplay EglImplementation::GetDisplay() const
 {
   return mEglDisplay;
 }
 
-EGLDisplay EglImplementation::GetContext() const
+EGLContext EglImplementation::GetContext() const
 {
   return mEglContext;
 }
 
+int32_t EglImplementation::GetGlesVersion() const
+{
+  return mGlesVersion;
+}
+
+bool EglImplementation::IsSurfacelessContextSupported() const
+{
+  return mIsSurfacelessContextSupported;
+}
+
+void EglImplementation::WaitClient()
+{
+  // Wait for EGL to finish executing all rendering calls for the current context
+  if ( eglWaitClient() != EGL_TRUE )
+  {
+    TEST_EGL_ERROR("eglWaitClient");
+  }
+}
+
 } // namespace Adaptor
 
 } // namespace Internal