2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <dali/internal/graphics/gles/egl-implementation.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/dali-vector.h>
28 #include <dali/public-api/dali-adaptor-common.h>
29 #include <dali/internal/graphics/gles/gl-implementation.h>
30 #include <dali/internal/graphics/gles/egl-debug.h>
32 // EGL constants use C style casts
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wold-style-cast"
38 const uint32_t THRESHOLD_SWAPBUFFER_COUNT = 5;
39 const uint32_t CHECK_EXTENSION_NUMBER = 2;
40 const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
41 const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
53 #define TEST_EGL_ERROR(lastCommand) \
55 EGLint err = eglGetError(); \
56 if (err != EGL_SUCCESS) \
58 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
59 Egl::PrintError(err); \
60 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
64 EglImplementation::EglImplementation( int multiSamplingLevel,
65 Integration::DepthBufferAvailable depthBufferRequired,
66 Integration::StencilBufferAvailable stencilBufferRequired )
68 mEglNativeDisplay( 0 ),
69 mEglNativeWindow( 0 ),
70 mCurrentEglNativePixmap( 0 ),
74 mCurrentEglSurface( 0 ),
75 mCurrentEglContext( EGL_NO_CONTEXT ),
76 mMultiSamplingLevel( multiSamplingLevel ),
78 mColorDepth( COLOR_DEPTH_24 ),
79 mGlesInitialized( false ),
80 mIsOwnSurface( true ),
82 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
83 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
84 mIsSurfacelessContextSupported( false ),
85 mIsKhrCreateContextSupported( false ),
86 mSwapBufferCountAfterResume( 0 )
90 EglImplementation::~EglImplementation()
95 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
97 if ( !mGlesInitialized )
99 mEglNativeDisplay = display;
101 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
102 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
103 EGLint error = eglGetError();
105 if( mEglDisplay == NULL && error != EGL_SUCCESS )
107 throw Dali::DaliException( "", "OpenGL ES is not supported");
110 EGLint majorVersion = 0;
111 EGLint minorVersion = 0;
112 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
116 eglBindAPI(EGL_OPENGL_ES_API);
118 mIsOwnSurface = isOwnSurface;
121 // Query EGL extensions to check whether surfaceless context is supported
122 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
123 std::istringstream stream( extensionStr );
124 std::string currentExtension;
125 uint32_t extensionCheckCount = 0;
126 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
128 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
130 mIsSurfacelessContextSupported = true;
131 extensionCheckCount++;
133 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
135 mIsKhrCreateContextSupported = true;
136 extensionCheckCount++;
140 mGlesInitialized = true;
142 // We want to display this information all the time, so use the LogMessage directly
143 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
148 eglQueryString( mEglDisplay, EGL_VENDOR ),
149 eglQueryString( mEglDisplay, EGL_VERSION ),
150 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
153 return mGlesInitialized;
156 bool EglImplementation::CreateContext()
158 // make sure a context isn't created twice
159 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
161 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
162 TEST_EGL_ERROR("eglCreateContext render thread");
164 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
166 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
167 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
168 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
169 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
170 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
175 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
177 // make sure a context isn't created twice
178 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
180 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
181 TEST_EGL_ERROR("eglCreateContext render thread");
183 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
185 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
186 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
187 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
188 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
189 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
191 mEglWindowContexts.push_back( eglContext );
196 void EglImplementation::DestroyContext( EGLContext& eglContext )
200 eglDestroyContext( mEglDisplay, eglContext );
205 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
207 if(mIsOwnSurface && eglSurface)
209 // Make context null to prevent crash in driver side
211 eglDestroySurface( mEglDisplay, eglSurface );
216 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
218 if (mCurrentEglContext == eglContext)
223 mCurrentEglSurface = eglSurface;
227 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
229 mCurrentEglContext = eglContext;
232 EGLint error = eglGetError();
234 if ( error != EGL_SUCCESS )
236 Egl::PrintError(error);
238 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
242 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
244 if (mCurrentEglContext == mEglContext)
249 mCurrentEglNativePixmap = pixmap;
250 mCurrentEglSurface = eglSurface;
254 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
256 mCurrentEglContext = mEglContext;
259 EGLint error = eglGetError();
261 if ( error != EGL_SUCCESS )
263 Egl::PrintError(error);
265 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
269 void EglImplementation::MakeContextNull()
271 // clear the current context
272 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
273 mCurrentEglContext = EGL_NO_CONTEXT;
276 void EglImplementation::TerminateGles()
278 if ( mGlesInitialized )
280 // Make context null to prevent crash in driver side
283 for ( auto eglSurface : mEglWindowSurfaces )
285 if(mIsOwnSurface && eglSurface)
287 eglDestroySurface(mEglDisplay, eglSurface);
290 eglDestroyContext(mEglDisplay, mEglContext);
291 for ( auto eglContext : mEglWindowContexts )
293 eglDestroyContext(mEglDisplay, eglContext);
296 eglTerminate(mEglDisplay);
301 mCurrentEglSurface = NULL;
302 mCurrentEglContext = EGL_NO_CONTEXT;
304 mGlesInitialized = false;
308 bool EglImplementation::IsGlesInitialized() const
310 return mGlesInitialized;
313 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
315 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
317 #ifndef DALI_PROFILE_UBUNTU
318 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
320 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
322 #endif //DALI_PROFILE_UBUNTU
324 eglSwapBuffers( mEglDisplay, eglSurface );
326 #ifndef DALI_PROFILE_UBUNTU
327 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
329 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
330 mSwapBufferCountAfterResume++;
332 #endif //DALI_PROFILE_UBUNTU
336 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
338 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
341 void EglImplementation::WaitGL()
346 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
348 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
354 mIsWindow = isWindowType;
357 Vector<EGLint> configAttribs;
358 configAttribs.Reserve(31);
362 configAttribs.PushBack( EGL_SURFACE_TYPE );
363 configAttribs.PushBack( EGL_WINDOW_BIT );
367 configAttribs.PushBack( EGL_SURFACE_TYPE );
368 configAttribs.PushBack( EGL_PIXMAP_BIT );
371 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
373 if( mGlesVersion >= 30 )
375 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
379 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
382 // TODO: enable this flag when it becomes supported
383 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
384 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
386 configAttribs.PushBack( EGL_RED_SIZE );
387 configAttribs.PushBack( 8 );
388 configAttribs.PushBack( EGL_GREEN_SIZE );
389 configAttribs.PushBack( 8 );
390 configAttribs.PushBack( EGL_BLUE_SIZE );
391 configAttribs.PushBack( 8 );
393 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
394 configAttribs.PushBack( EGL_ALPHA_SIZE );
395 configAttribs.PushBack( 8 );
397 configAttribs.PushBack( EGL_DEPTH_SIZE );
398 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
399 configAttribs.PushBack( EGL_STENCIL_SIZE );
400 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
402 #ifndef DALI_PROFILE_UBUNTU
403 if( mMultiSamplingLevel != EGL_DONT_CARE )
405 configAttribs.PushBack( EGL_SAMPLES );
406 configAttribs.PushBack( mMultiSamplingLevel );
407 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
408 configAttribs.PushBack( 1 );
410 #endif // DALI_PROFILE_UBUNTU
411 configAttribs.PushBack( EGL_NONE );
413 // Ensure number of configs is set to 1 as on some drivers,
414 // eglChooseConfig succeeds but does not actually create a proper configuration.
415 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
416 ( numConfigs != 1 ) )
418 if( mGlesVersion >= 30 )
421 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
425 if ( numConfigs != 1 )
427 DALI_LOG_ERROR("No configurations found.\n");
429 TEST_EGL_ERROR("eglChooseConfig");
432 EGLint error = eglGetError();
435 case EGL_BAD_DISPLAY:
437 DALI_LOG_ERROR("Display is not an EGL display connection\n");
440 case EGL_BAD_ATTRIBUTE:
442 DALI_LOG_ERROR("The parameter configAttribs contains an invalid frame buffer configuration attribute or an attribute value that is unrecognized or out of range\n");
445 case EGL_NOT_INITIALIZED:
447 DALI_LOG_ERROR("Display has not been initialized\n");
450 case EGL_BAD_PARAMETER:
452 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
457 DALI_LOG_ERROR("Unknown error.\n");
460 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
463 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
465 mContextAttribs.Clear();
466 if( mIsKhrCreateContextSupported )
468 mContextAttribs.Reserve(5);
469 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
470 mContextAttribs.PushBack( mGlesVersion / 10 );
471 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
472 mContextAttribs.PushBack( mGlesVersion % 10 );
476 mContextAttribs.Reserve(3);
477 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
478 mContextAttribs.PushBack( mGlesVersion / 10 );
480 mContextAttribs.PushBack( EGL_NONE );
485 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
487 mEglNativeWindow = window;
492 ChooseConfig(mIsWindow, mColorDepth);
494 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
495 TEST_EGL_ERROR("eglCreateWindowSurface");
497 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
499 return mCurrentEglSurface;
502 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
504 mCurrentEglNativePixmap = pixmap;
509 ChooseConfig(mIsWindow, mColorDepth);
511 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
512 TEST_EGL_ERROR("eglCreatePixmapSurface");
514 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
516 return mCurrentEglSurface;
519 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
521 bool contextLost = false;
523 // display connection has not changed, then we can just create a new surface
524 // the surface is bound to the context, so set the context to null
529 // destroy the surface
530 DestroySurface( eglSurface );
533 // create the EGL surface
534 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
536 // set the context to be current with the new surface
537 MakeContextCurrent( newEglSurface, eglContext );
542 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
544 bool contextLost = false;
546 // display connection has not changed, then we can just create a new surface
547 // create the EGL surface
548 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
550 // set the eglSurface to be current
551 MakeCurrent( pixmap, eglSurface );
556 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
558 mGlesVersion = glesVersion;
561 void EglImplementation::SetFirstFrameAfterResume()
563 mSwapBufferCountAfterResume = 0;
566 EGLDisplay EglImplementation::GetDisplay() const
571 EGLContext EglImplementation::GetContext() const
576 int32_t EglImplementation::GetGlesVersion() const
581 bool EglImplementation::IsSurfacelessContextSupported() const
583 return mIsSurfacelessContextSupported;
586 void EglImplementation::WaitClient()
588 // Wait for EGL to finish executing all rendering calls for the current context
589 if ( eglWaitClient() != EGL_TRUE )
591 TEST_EGL_ERROR("eglWaitClient");
595 } // namespace Adaptor
597 } // namespace Internal
601 #pragma GCC diagnostic pop