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 CHECK_EXTENSION_NUMBER = 2;
39 const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
40 const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
52 #define TEST_EGL_ERROR(lastCommand) \
54 EGLint err = eglGetError(); \
55 if (err != EGL_SUCCESS) \
57 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
58 Egl::PrintError(err); \
59 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
63 EglImplementation::EglImplementation( int multiSamplingLevel,
64 Integration::DepthBufferAvailable depthBufferRequired,
65 Integration::StencilBufferAvailable stencilBufferRequired )
67 mEglNativeDisplay( 0 ),
68 mEglNativeWindow( 0 ),
69 mCurrentEglNativePixmap( 0 ),
73 mCurrentEglSurface( 0 ),
74 mCurrentEglContext( EGL_NO_CONTEXT ),
75 mMultiSamplingLevel( multiSamplingLevel ),
77 mColorDepth( COLOR_DEPTH_24 ),
78 mGlesInitialized( false ),
79 mIsOwnSurface( true ),
81 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
82 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
83 mIsSurfacelessContextSupported( false ),
84 mIsKhrCreateContextSupported( false )
88 EglImplementation::~EglImplementation()
93 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
95 if ( !mGlesInitialized )
97 mEglNativeDisplay = display;
99 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
100 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
101 EGLint error = eglGetError();
103 if( mEglDisplay == NULL && error != EGL_SUCCESS )
105 throw Dali::DaliException( "", "OpenGL ES is not supported");
108 EGLint majorVersion = 0;
109 EGLint minorVersion = 0;
110 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
114 eglBindAPI(EGL_OPENGL_ES_API);
116 mGlesInitialized = true;
117 mIsOwnSurface = isOwnSurface;
120 // Query EGL extensions to check whether surfaceless context is supported
121 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
122 std::istringstream stream( extensionStr );
123 std::string currentExtension;
124 uint32_t extensionCheckCount = 0;
125 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
127 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
129 mIsSurfacelessContextSupported = true;
130 extensionCheckCount++;
132 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
134 mIsKhrCreateContextSupported = true;
135 extensionCheckCount++;
139 // We want to display this information all the time, so use the LogMessage directly
140 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
145 eglQueryString( mEglDisplay, EGL_VENDOR ),
146 eglQueryString( mEglDisplay, EGL_VERSION ),
147 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
150 return mGlesInitialized;
153 bool EglImplementation::CreateContext()
155 // make sure a context isn't created twice
156 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
158 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
159 TEST_EGL_ERROR("eglCreateContext render thread");
161 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
163 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
164 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
165 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
166 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
167 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
172 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
174 // make sure a context isn't created twice
175 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
177 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
178 TEST_EGL_ERROR("eglCreateContext render thread");
180 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
182 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
183 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
184 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
185 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
186 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
188 mEglWindowContexts.push_back( eglContext );
193 void EglImplementation::DestroyContext( EGLContext& eglContext )
195 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
197 eglDestroyContext( mEglDisplay, eglContext );
201 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
203 if(mIsOwnSurface && eglSurface)
205 // Make context null to prevent crash in driver side
207 eglDestroySurface( mEglDisplay, eglSurface );
212 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
214 if (mCurrentEglContext == eglContext)
219 mCurrentEglSurface = eglSurface;
223 if( mCurrentEglContext != EGL_NO_CONTEXT )
228 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
230 mCurrentEglContext = eglContext;
233 EGLint error = eglGetError();
235 if ( error != EGL_SUCCESS )
237 Egl::PrintError(error);
239 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
243 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
245 if (mCurrentEglContext == mEglContext)
250 mCurrentEglNativePixmap = pixmap;
251 mCurrentEglSurface = eglSurface;
255 if( mCurrentEglContext != EGL_NO_CONTEXT )
260 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
262 mCurrentEglContext = mEglContext;
265 EGLint error = eglGetError();
267 if ( error != EGL_SUCCESS )
269 Egl::PrintError(error);
271 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
275 void EglImplementation::MakeContextNull()
277 // clear the current context
278 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
279 mCurrentEglContext = EGL_NO_CONTEXT;
282 void EglImplementation::TerminateGles()
284 if ( mGlesInitialized )
286 // Make context null to prevent crash in driver side
289 for ( auto eglSurface : mEglWindowSurfaces )
291 if(mIsOwnSurface && eglSurface)
293 eglDestroySurface(mEglDisplay, eglSurface);
296 eglDestroyContext(mEglDisplay, mEglContext);
297 for ( auto eglContext : mEglWindowContexts )
299 eglDestroyContext(mEglDisplay, eglContext);
302 eglTerminate(mEglDisplay);
307 mCurrentEglSurface = NULL;
308 mCurrentEglContext = EGL_NO_CONTEXT;
310 mGlesInitialized = false;
314 bool EglImplementation::IsGlesInitialized() const
316 return mGlesInitialized;
319 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
321 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
323 eglSwapBuffers( mEglDisplay, eglSurface );
327 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
329 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
332 void EglImplementation::WaitGL()
337 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
339 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
344 bool isTransparent = ( depth == COLOR_DEPTH_32 );
347 mIsWindow = isWindowType;
350 Vector<EGLint> configAttribs;
351 configAttribs.Reserve(31);
355 configAttribs.PushBack( EGL_SURFACE_TYPE );
356 configAttribs.PushBack( EGL_WINDOW_BIT );
360 configAttribs.PushBack( EGL_SURFACE_TYPE );
361 configAttribs.PushBack( EGL_PIXMAP_BIT );
364 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
366 if( mGlesVersion >= 30 )
368 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
372 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
375 // TODO: enable this flag when it becomes supported
376 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
377 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
379 configAttribs.PushBack( EGL_RED_SIZE );
380 configAttribs.PushBack( 8 );
381 configAttribs.PushBack( EGL_GREEN_SIZE );
382 configAttribs.PushBack( 8 );
383 configAttribs.PushBack( EGL_BLUE_SIZE );
384 configAttribs.PushBack( 8 );
388 configAttribs.PushBack( EGL_ALPHA_SIZE );
390 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
391 configAttribs.PushBack( 8 );
393 // There is a bug in the desktop emulator
394 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
395 configAttribs.PushBack( 8 );
399 configAttribs.PushBack( EGL_DEPTH_SIZE );
400 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
401 configAttribs.PushBack( EGL_STENCIL_SIZE );
402 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
404 #ifndef DALI_PROFILE_UBUNTU
405 if( mMultiSamplingLevel != EGL_DONT_CARE )
407 configAttribs.PushBack( EGL_SAMPLES );
408 configAttribs.PushBack( mMultiSamplingLevel );
409 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
410 configAttribs.PushBack( 1 );
412 #endif // DALI_PROFILE_UBUNTU
413 configAttribs.PushBack( EGL_NONE );
415 // Ensure number of configs is set to 1 as on some drivers,
416 // eglChooseConfig succeeds but does not actually create a proper configuration.
417 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
418 ( numConfigs != 1 ) )
420 if( mGlesVersion >= 30 )
423 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
427 if ( numConfigs != 1 )
429 DALI_LOG_ERROR("No configurations found.\n");
431 TEST_EGL_ERROR("eglChooseConfig");
434 EGLint error = eglGetError();
437 case EGL_BAD_DISPLAY:
439 DALI_LOG_ERROR("Display is not an EGL display connection\n");
442 case EGL_BAD_ATTRIBUTE:
444 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");
447 case EGL_NOT_INITIALIZED:
449 DALI_LOG_ERROR("Display has not been initialized\n");
452 case EGL_BAD_PARAMETER:
454 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
459 DALI_LOG_ERROR("Unknown error.\n");
462 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
465 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
467 mContextAttribs.Clear();
468 if( mIsKhrCreateContextSupported )
470 mContextAttribs.Reserve(5);
471 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
472 mContextAttribs.PushBack( mGlesVersion / 10 );
473 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
474 mContextAttribs.PushBack( mGlesVersion % 10 );
478 mContextAttribs.Reserve(3);
479 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
480 mContextAttribs.PushBack( mGlesVersion / 10 );
482 mContextAttribs.PushBack( EGL_NONE );
487 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
489 mEglNativeWindow = window;
494 ChooseConfig(mIsWindow, mColorDepth);
496 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
497 TEST_EGL_ERROR("eglCreateWindowSurface");
499 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
501 return mCurrentEglSurface;
504 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
506 mCurrentEglNativePixmap = pixmap;
511 ChooseConfig(mIsWindow, mColorDepth);
513 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
514 TEST_EGL_ERROR("eglCreatePixmapSurface");
516 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
518 return mCurrentEglSurface;
521 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
523 bool contextLost = false;
525 // display connection has not changed, then we can just create a new surface
526 // the surface is bound to the context, so set the context to null
529 // destroy the surface
530 DestroySurface( eglSurface );
532 // create the EGL surface
533 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
535 // set the context to be current with the new surface
536 MakeContextCurrent( newEglSurface, eglContext );
541 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
543 bool contextLost = false;
545 // display connection has not changed, then we can just create a new surface
546 // create the EGL surface
547 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
549 // set the eglSurface to be current
550 MakeCurrent( pixmap, eglSurface );
555 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
557 mGlesVersion = glesVersion;
560 EGLDisplay EglImplementation::GetDisplay() const
565 EGLContext EglImplementation::GetContext() const
570 int32_t EglImplementation::GetGlesVersion() const
575 bool EglImplementation::IsSurfacelessContextSupported() const
577 return mIsSurfacelessContextSupported;
580 } // namespace Adaptor
582 } // namespace Internal
586 #pragma GCC diagnostic pop