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 std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
50 #define TEST_EGL_ERROR(lastCommand) \
52 EGLint err = eglGetError(); \
53 if (err != EGL_SUCCESS) \
55 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
56 Egl::PrintError(err); \
57 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
61 EglImplementation::EglImplementation( int multiSamplingLevel,
62 Integration::DepthBufferAvailable depthBufferRequired,
63 Integration::StencilBufferAvailable stencilBufferRequired )
65 mEglNativeDisplay( 0 ),
66 mEglNativeWindow( 0 ),
67 mCurrentEglNativePixmap( 0 ),
71 mCurrentEglSurface( 0 ),
72 mCurrentEglContext( EGL_NO_CONTEXT ),
73 mMultiSamplingLevel( multiSamplingLevel ),
75 mColorDepth( COLOR_DEPTH_24 ),
76 mGlesInitialized( false ),
77 mIsOwnSurface( true ),
79 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
80 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
81 mIsSurfacelessContextSupported( false )
85 EglImplementation::~EglImplementation()
90 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
92 if ( !mGlesInitialized )
94 mEglNativeDisplay = display;
96 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
97 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
98 EGLint error = eglGetError();
100 if( mEglDisplay == NULL && error != EGL_SUCCESS )
102 throw Dali::DaliException( "", "OpenGL ES is not supported");
105 EGLint majorVersion = 0;
106 EGLint minorVersion = 0;
107 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
111 eglBindAPI(EGL_OPENGL_ES_API);
113 mGlesInitialized = true;
114 mIsOwnSurface = isOwnSurface;
117 // Query EGL extensions to check whether surfaceless context is supported
118 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
119 std::istringstream stream(extensionStr);
120 std::string currentExtension;
121 while ( std::getline( stream, currentExtension, ' ' ) )
123 if ( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
125 mIsSurfacelessContextSupported = true;
130 // We want to display this information all the time, so use the LogMessage directly
131 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
136 eglQueryString( mEglDisplay, EGL_VENDOR ),
137 eglQueryString( mEglDisplay, EGL_VERSION ),
138 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
141 return mGlesInitialized;
144 bool EglImplementation::CreateContext()
146 // make sure a context isn't created twice
147 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
149 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
150 TEST_EGL_ERROR("eglCreateContext render thread");
152 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
154 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
155 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
156 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
157 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
158 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
163 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
165 // make sure a context isn't created twice
166 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
168 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
169 TEST_EGL_ERROR("eglCreateContext render thread");
171 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
173 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
174 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
175 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
176 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
177 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
179 mEglWindowContexts.push_back( eglContext );
184 void EglImplementation::DestroyContext( EGLContext& eglContext )
186 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
188 eglDestroyContext( mEglDisplay, eglContext );
192 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
194 if(mIsOwnSurface && eglSurface)
196 // Make context null to prevent crash in driver side
198 eglDestroySurface( mEglDisplay, eglSurface );
203 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
205 if (mCurrentEglContext == eglContext)
210 mCurrentEglSurface = eglSurface;
216 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
218 mCurrentEglContext = eglContext;
221 EGLint error = eglGetError();
223 if ( error != EGL_SUCCESS )
225 Egl::PrintError(error);
227 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
231 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
233 if (mCurrentEglContext == mEglContext)
238 mCurrentEglNativePixmap = pixmap;
239 mCurrentEglSurface = eglSurface;
245 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
247 mCurrentEglContext = mEglContext;
250 EGLint error = eglGetError();
252 if ( error != EGL_SUCCESS )
254 Egl::PrintError(error);
256 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
260 void EglImplementation::MakeContextNull()
262 // clear the current context
263 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
264 mCurrentEglContext = EGL_NO_CONTEXT;
267 void EglImplementation::TerminateGles()
269 if ( mGlesInitialized )
271 // Make context null to prevent crash in driver side
274 for ( auto eglSurface : mEglWindowSurfaces )
276 if(mIsOwnSurface && eglSurface)
278 eglDestroySurface(mEglDisplay, eglSurface);
281 eglDestroyContext(mEglDisplay, mEglContext);
282 for ( auto eglContext : mEglWindowContexts )
284 eglDestroyContext(mEglDisplay, eglContext);
287 eglTerminate(mEglDisplay);
292 mCurrentEglSurface = NULL;
293 mCurrentEglContext = EGL_NO_CONTEXT;
295 mGlesInitialized = false;
299 bool EglImplementation::IsGlesInitialized() const
301 return mGlesInitialized;
304 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
306 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
308 eglSwapBuffers( mEglDisplay, eglSurface );
312 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
314 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
317 void EglImplementation::WaitGL()
322 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
324 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
330 mIsWindow = isWindowType;
333 Vector<EGLint> configAttribs;
334 configAttribs.Reserve(31);
338 configAttribs.PushBack( EGL_SURFACE_TYPE );
339 configAttribs.PushBack( EGL_WINDOW_BIT );
343 configAttribs.PushBack( EGL_SURFACE_TYPE );
344 configAttribs.PushBack( EGL_PIXMAP_BIT );
347 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
349 if( mGlesVersion >= 30 )
352 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
354 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
359 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
362 // TODO: enable this flag when it becomes supported
363 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
364 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
366 configAttribs.PushBack( EGL_RED_SIZE );
367 configAttribs.PushBack( 8 );
368 configAttribs.PushBack( EGL_GREEN_SIZE );
369 configAttribs.PushBack( 8 );
370 configAttribs.PushBack( EGL_BLUE_SIZE );
371 configAttribs.PushBack( 8 );
373 // In the previous code, there was a branch for ARM.
374 // If there is an issue in only ARM, we need to check here again.
375 configAttribs.PushBack( EGL_ALPHA_SIZE );
376 configAttribs.PushBack( 8 );
378 configAttribs.PushBack( EGL_DEPTH_SIZE );
379 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
380 configAttribs.PushBack( EGL_STENCIL_SIZE );
381 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
383 #ifndef DALI_PROFILE_UBUNTU
384 if( mMultiSamplingLevel != EGL_DONT_CARE )
386 configAttribs.PushBack( EGL_SAMPLES );
387 configAttribs.PushBack( mMultiSamplingLevel );
388 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
389 configAttribs.PushBack( 1 );
391 #endif // DALI_PROFILE_UBUNTU
392 configAttribs.PushBack( EGL_NONE );
393 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
394 ( numConfigs != 1 ) )
396 if( mGlesVersion >= 30 )
399 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retring to use OpenGL es 2.0.");
403 if ( numConfigs != 1 )
405 DALI_LOG_ERROR("No configurations found.\n");
407 TEST_EGL_ERROR("eglChooseConfig");
410 EGLint error = eglGetError();
413 case EGL_BAD_DISPLAY:
415 DALI_LOG_ERROR("Display is not an EGL display connection\n");
418 case EGL_BAD_ATTRIBUTE:
420 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");
423 case EGL_NOT_INITIALIZED:
425 DALI_LOG_ERROR("Display has not been initialized\n");
428 case EGL_BAD_PARAMETER:
430 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
435 DALI_LOG_ERROR("Unknown error.\n");
438 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
441 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
443 mContextAttribs.Clear();
444 if( mGlesVersion >= 30 )
446 mContextAttribs.Reserve(5);
447 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
448 mContextAttribs.PushBack( mGlesVersion / 10 );
449 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
450 mContextAttribs.PushBack( mGlesVersion % 10 );
454 mContextAttribs.Reserve(3);
455 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
456 mContextAttribs.PushBack( 2 );
458 mContextAttribs.PushBack( EGL_NONE );
463 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
465 mEglNativeWindow = window;
470 ChooseConfig(mIsWindow, mColorDepth);
472 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
473 TEST_EGL_ERROR("eglCreateWindowSurface");
475 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
477 return mCurrentEglSurface;
480 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
482 mCurrentEglNativePixmap = pixmap;
487 ChooseConfig(mIsWindow, mColorDepth);
489 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
490 TEST_EGL_ERROR("eglCreatePixmapSurface");
492 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
494 return mCurrentEglSurface;
497 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
499 bool contextLost = false;
501 // display connection has not changed, then we can just create a new surface
502 // the surface is bound to the context, so set the context to null
505 // destroy the surface
506 DestroySurface( eglSurface );
508 // create the EGL surface
509 CreateSurfaceWindow( window, mColorDepth );
511 // set the context to be current with the new surface
512 MakeContextCurrent( eglSurface, eglContext );
517 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
519 bool contextLost = false;
521 // display connection has not changed, then we can just create a new surface
522 // create the EGL surface
523 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
525 // set the eglSurface to be current
526 MakeCurrent( pixmap, eglSurface );
531 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
533 mGlesVersion = glesVersion;
536 EGLDisplay EglImplementation::GetDisplay() const
541 EGLContext EglImplementation::GetContext() const
546 int32_t EglImplementation::GetGlesVersion() const
551 bool EglImplementation::IsSurfacelessContextSupported() const
553 return mIsSurfacelessContextSupported;
556 } // namespace Adaptor
558 } // namespace Internal
562 #pragma GCC diagnostic pop