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>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/dali-adaptor-common.h>
28 #include <dali/internal/graphics/gles/gl-implementation.h>
29 #include <dali/internal/graphics/gles/egl-debug.h>
31 // EGL constants use C style casts
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wold-style-cast"
44 #define TEST_EGL_ERROR(lastCommand) \
46 EGLint err = eglGetError(); \
47 if (err != EGL_SUCCESS) \
49 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
50 Egl::PrintError(err); \
51 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
55 EglImplementation::EglImplementation( int multiSamplingLevel,
56 Integration::DepthBufferAvailable depthBufferRequired,
57 Integration::StencilBufferAvailable stencilBufferRequired )
59 mEglNativeDisplay( 0 ),
60 mEglNativeWindow( 0 ),
61 mCurrentEglNativePixmap( 0 ),
65 mCurrentEglSurface( 0 ),
66 mCurrentEglContext( EGL_NO_CONTEXT ),
67 mMultiSamplingLevel( multiSamplingLevel ),
69 mColorDepth( COLOR_DEPTH_24 ),
70 mGlesInitialized( false ),
71 mIsOwnSurface( true ),
73 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
74 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
78 EglImplementation::~EglImplementation()
83 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
85 if ( !mGlesInitialized )
87 mEglNativeDisplay = display;
89 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
90 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
91 EGLint error = eglGetError();
93 if( mEglDisplay == NULL && error != EGL_SUCCESS )
95 throw Dali::DaliException( "", "OpenGL ES is not supported");
98 EGLint majorVersion = 0;
99 EGLint minorVersion = 0;
100 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
104 eglBindAPI(EGL_OPENGL_ES_API);
106 mGlesInitialized = true;
107 mIsOwnSurface = isOwnSurface;
110 // We want to display this information all the time, so use the LogMessage directly
111 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
116 eglQueryString( mEglDisplay, EGL_VENDOR ),
117 eglQueryString( mEglDisplay, EGL_VERSION ),
118 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
119 eglQueryString( mEglDisplay, EGL_EXTENSIONS ));
121 return mGlesInitialized;
124 bool EglImplementation::CreateContext()
126 // make sure a context isn't created twice
127 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
129 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
130 TEST_EGL_ERROR("eglCreateContext render thread");
132 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
134 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
135 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
136 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
137 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
138 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
143 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
145 // make sure a context isn't created twice
146 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
148 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
149 TEST_EGL_ERROR("eglCreateContext render thread");
151 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
153 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
154 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
155 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
156 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
157 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
159 mEglWindowContexts.push_back( eglContext );
164 void EglImplementation::DestroyContext( EGLContext& eglContext )
166 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
168 eglDestroyContext( mEglDisplay, eglContext );
172 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
174 if(mIsOwnSurface && eglSurface)
176 // Make context null to prevent crash in driver side
178 eglDestroySurface( mEglDisplay, eglSurface );
183 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
185 if (mCurrentEglContext == eglContext)
190 mCurrentEglSurface = eglSurface;
196 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
198 mCurrentEglContext = eglContext;
201 EGLint error = eglGetError();
203 if ( error != EGL_SUCCESS )
205 Egl::PrintError(error);
207 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
211 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
213 if (mCurrentEglContext == mEglContext)
218 mCurrentEglNativePixmap = pixmap;
219 mCurrentEglSurface = eglSurface;
225 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
227 mCurrentEglContext = mEglContext;
230 EGLint error = eglGetError();
232 if ( error != EGL_SUCCESS )
234 Egl::PrintError(error);
236 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
240 void EglImplementation::MakeContextNull()
242 // clear the current context
243 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
244 mCurrentEglContext = EGL_NO_CONTEXT;
247 void EglImplementation::TerminateGles()
249 if ( mGlesInitialized )
251 // Make context null to prevent crash in driver side
254 for ( auto eglSurface : mEglWindowSurfaces )
256 if(mIsOwnSurface && eglSurface)
258 eglDestroySurface(mEglDisplay, eglSurface);
261 eglDestroyContext(mEglDisplay, mEglContext);
262 for ( auto eglContext : mEglWindowContexts )
264 eglDestroyContext(mEglDisplay, eglContext);
267 eglTerminate(mEglDisplay);
272 mCurrentEglSurface = NULL;
273 mCurrentEglContext = EGL_NO_CONTEXT;
275 mGlesInitialized = false;
279 bool EglImplementation::IsGlesInitialized() const
281 return mGlesInitialized;
284 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
286 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
288 eglSwapBuffers( mEglDisplay, eglSurface );
292 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
294 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
297 void EglImplementation::WaitGL()
302 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
304 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
309 bool isTransparent = ( depth == COLOR_DEPTH_32 );
312 mIsWindow = isWindowType;
315 Vector<EGLint> configAttribs;
316 configAttribs.Reserve(31);
320 configAttribs.PushBack( EGL_SURFACE_TYPE );
321 configAttribs.PushBack( EGL_WINDOW_BIT );
325 configAttribs.PushBack( EGL_SURFACE_TYPE );
326 configAttribs.PushBack( EGL_PIXMAP_BIT );
329 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
331 if( mGlesVersion >= 30 )
334 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
336 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
341 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
344 // TODO: enable this flag when it becomes supported
345 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
346 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
348 configAttribs.PushBack( EGL_RED_SIZE );
349 configAttribs.PushBack( 8 );
350 configAttribs.PushBack( EGL_GREEN_SIZE );
351 configAttribs.PushBack( 8 );
352 configAttribs.PushBack( EGL_BLUE_SIZE );
353 configAttribs.PushBack( 8 );
357 configAttribs.PushBack( EGL_ALPHA_SIZE );
359 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
360 configAttribs.PushBack( 8 );
362 // There is a bug in the desktop emulator
363 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
364 configAttribs.PushBack( 8 );
368 configAttribs.PushBack( EGL_DEPTH_SIZE );
369 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
370 configAttribs.PushBack( EGL_STENCIL_SIZE );
371 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
373 #ifndef DALI_PROFILE_UBUNTU
374 if( mMultiSamplingLevel != EGL_DONT_CARE )
376 configAttribs.PushBack( EGL_SAMPLES );
377 configAttribs.PushBack( mMultiSamplingLevel );
378 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
379 configAttribs.PushBack( 1 );
381 #endif // DALI_PROFILE_UBUNTU
382 configAttribs.PushBack( EGL_NONE );
384 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
386 if( mGlesVersion >= 30 )
389 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retring to use OpenGL es 2.0.");
393 EGLint error = eglGetError();
396 case EGL_BAD_DISPLAY:
398 DALI_LOG_ERROR("Display is not an EGL display connection\n");
401 case EGL_BAD_ATTRIBUTE:
403 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");
406 case EGL_NOT_INITIALIZED:
408 DALI_LOG_ERROR("Display has not been initialized\n");
411 case EGL_BAD_PARAMETER:
413 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
418 DALI_LOG_ERROR("Unknown error.\n");
421 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
424 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
426 mContextAttribs.Clear();
427 if( mGlesVersion >= 30 )
429 mContextAttribs.Reserve(5);
430 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
431 mContextAttribs.PushBack( mGlesVersion / 10 );
432 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
433 mContextAttribs.PushBack( mGlesVersion % 10 );
437 mContextAttribs.Reserve(3);
438 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
439 mContextAttribs.PushBack( 2 );
441 mContextAttribs.PushBack( EGL_NONE );
443 if ( numConfigs != 1 )
445 DALI_LOG_ERROR("No configurations found.\n");
447 TEST_EGL_ERROR("eglChooseConfig");
453 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
455 mEglNativeWindow = window;
460 ChooseConfig(mIsWindow, mColorDepth);
462 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
463 TEST_EGL_ERROR("eglCreateWindowSurface");
465 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
467 return mCurrentEglSurface;
470 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
472 mCurrentEglNativePixmap = pixmap;
477 ChooseConfig(mIsWindow, mColorDepth);
479 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
480 TEST_EGL_ERROR("eglCreatePixmapSurface");
482 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
484 return mCurrentEglSurface;
487 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
489 bool contextLost = false;
491 // display connection has not changed, then we can just create a new surface
492 // the surface is bound to the context, so set the context to null
495 // destroy the surface
496 DestroySurface( eglSurface );
498 // create the EGL surface
499 CreateSurfaceWindow( window, mColorDepth );
501 // set the context to be current with the new surface
502 MakeContextCurrent( eglSurface, eglContext );
507 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
509 bool contextLost = false;
511 // display connection has not changed, then we can just create a new surface
512 // create the EGL surface
513 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
515 // set the eglSurface to be current
516 MakeCurrent( pixmap, eglSurface );
521 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
523 mGlesVersion = glesVersion;
526 EGLDisplay EglImplementation::GetDisplay() const
531 EGLContext EglImplementation::GetContext() const
536 int32_t EglImplementation::GetGlesVersion() const
541 } // namespace Adaptor
543 } // namespace Internal
547 #pragma GCC diagnostic pop