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 ),
85 mIsFirstFrameAfterResume( false )
89 EglImplementation::~EglImplementation()
94 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
96 if ( !mGlesInitialized )
98 mEglNativeDisplay = display;
100 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
101 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
102 EGLint error = eglGetError();
104 if( mEglDisplay == NULL && error != EGL_SUCCESS )
106 throw Dali::DaliException( "", "OpenGL ES is not supported");
109 EGLint majorVersion = 0;
110 EGLint minorVersion = 0;
111 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
115 eglBindAPI(EGL_OPENGL_ES_API);
117 mGlesInitialized = true;
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 // We want to display this information all the time, so use the LogMessage directly
141 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
146 eglQueryString( mEglDisplay, EGL_VENDOR ),
147 eglQueryString( mEglDisplay, EGL_VERSION ),
148 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
151 return mGlesInitialized;
154 bool EglImplementation::CreateContext()
156 // make sure a context isn't created twice
157 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
159 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
160 TEST_EGL_ERROR("eglCreateContext render thread");
162 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
164 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
165 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
166 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
167 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
168 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
173 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
175 // make sure a context isn't created twice
176 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
178 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
179 TEST_EGL_ERROR("eglCreateContext render thread");
181 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
183 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
184 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
185 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
186 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
187 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
189 mEglWindowContexts.push_back( eglContext );
194 void EglImplementation::DestroyContext( EGLContext& eglContext )
196 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
198 eglDestroyContext( mEglDisplay, eglContext );
202 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
204 if(mIsOwnSurface && eglSurface)
206 // Make context null to prevent crash in driver side
208 eglDestroySurface( mEglDisplay, eglSurface );
213 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
215 if (mCurrentEglContext == eglContext)
220 mCurrentEglSurface = eglSurface;
224 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
226 mCurrentEglContext = eglContext;
229 EGLint error = eglGetError();
231 if ( error != EGL_SUCCESS )
233 Egl::PrintError(error);
235 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
239 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
241 if (mCurrentEglContext == mEglContext)
246 mCurrentEglNativePixmap = pixmap;
247 mCurrentEglSurface = eglSurface;
251 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
253 mCurrentEglContext = mEglContext;
256 EGLint error = eglGetError();
258 if ( error != EGL_SUCCESS )
260 Egl::PrintError(error);
262 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
266 void EglImplementation::MakeContextNull()
268 // clear the current context
269 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
270 mCurrentEglContext = EGL_NO_CONTEXT;
273 void EglImplementation::TerminateGles()
275 if ( mGlesInitialized )
277 // Make context null to prevent crash in driver side
280 for ( auto eglSurface : mEglWindowSurfaces )
282 if(mIsOwnSurface && eglSurface)
284 eglDestroySurface(mEglDisplay, eglSurface);
287 eglDestroyContext(mEglDisplay, mEglContext);
288 for ( auto eglContext : mEglWindowContexts )
290 eglDestroyContext(mEglDisplay, eglContext);
293 eglTerminate(mEglDisplay);
298 mCurrentEglSurface = NULL;
299 mCurrentEglContext = EGL_NO_CONTEXT;
301 mGlesInitialized = false;
305 bool EglImplementation::IsGlesInitialized() const
307 return mGlesInitialized;
310 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
312 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
314 if( mIsFirstFrameAfterResume )
316 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers: First SwapBuffers call.\n" );
317 mIsFirstFrameAfterResume = false;
319 eglSwapBuffers( mEglDisplay, eglSurface );
323 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
325 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
328 void EglImplementation::WaitGL()
333 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
335 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
341 mIsWindow = isWindowType;
344 Vector<EGLint> configAttribs;
345 configAttribs.Reserve(31);
349 configAttribs.PushBack( EGL_SURFACE_TYPE );
350 configAttribs.PushBack( EGL_WINDOW_BIT );
354 configAttribs.PushBack( EGL_SURFACE_TYPE );
355 configAttribs.PushBack( EGL_PIXMAP_BIT );
358 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
360 if( mGlesVersion >= 30 )
362 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
366 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
369 // TODO: enable this flag when it becomes supported
370 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
371 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
373 configAttribs.PushBack( EGL_RED_SIZE );
374 configAttribs.PushBack( 8 );
375 configAttribs.PushBack( EGL_GREEN_SIZE );
376 configAttribs.PushBack( 8 );
377 configAttribs.PushBack( EGL_BLUE_SIZE );
378 configAttribs.PushBack( 8 );
380 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
381 configAttribs.PushBack( EGL_ALPHA_SIZE );
382 configAttribs.PushBack( 8 );
384 configAttribs.PushBack( EGL_DEPTH_SIZE );
385 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
386 configAttribs.PushBack( EGL_STENCIL_SIZE );
387 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
389 #ifndef DALI_PROFILE_UBUNTU
390 if( mMultiSamplingLevel != EGL_DONT_CARE )
392 configAttribs.PushBack( EGL_SAMPLES );
393 configAttribs.PushBack( mMultiSamplingLevel );
394 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
395 configAttribs.PushBack( 1 );
397 #endif // DALI_PROFILE_UBUNTU
398 configAttribs.PushBack( EGL_NONE );
400 // Ensure number of configs is set to 1 as on some drivers,
401 // eglChooseConfig succeeds but does not actually create a proper configuration.
402 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
403 ( numConfigs != 1 ) )
405 if( mGlesVersion >= 30 )
408 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
412 if ( numConfigs != 1 )
414 DALI_LOG_ERROR("No configurations found.\n");
416 TEST_EGL_ERROR("eglChooseConfig");
419 EGLint error = eglGetError();
422 case EGL_BAD_DISPLAY:
424 DALI_LOG_ERROR("Display is not an EGL display connection\n");
427 case EGL_BAD_ATTRIBUTE:
429 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");
432 case EGL_NOT_INITIALIZED:
434 DALI_LOG_ERROR("Display has not been initialized\n");
437 case EGL_BAD_PARAMETER:
439 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
444 DALI_LOG_ERROR("Unknown error.\n");
447 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
450 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
452 mContextAttribs.Clear();
453 if( mIsKhrCreateContextSupported )
455 mContextAttribs.Reserve(5);
456 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
457 mContextAttribs.PushBack( mGlesVersion / 10 );
458 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
459 mContextAttribs.PushBack( mGlesVersion % 10 );
463 mContextAttribs.Reserve(3);
464 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
465 mContextAttribs.PushBack( mGlesVersion / 10 );
467 mContextAttribs.PushBack( EGL_NONE );
472 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
474 mEglNativeWindow = window;
479 ChooseConfig(mIsWindow, mColorDepth);
481 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
482 TEST_EGL_ERROR("eglCreateWindowSurface");
484 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
486 return mCurrentEglSurface;
489 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
491 mCurrentEglNativePixmap = pixmap;
496 ChooseConfig(mIsWindow, mColorDepth);
498 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
499 TEST_EGL_ERROR("eglCreatePixmapSurface");
501 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
503 return mCurrentEglSurface;
506 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
508 bool contextLost = false;
510 // display connection has not changed, then we can just create a new surface
511 // the surface is bound to the context, so set the context to null
514 // destroy the surface
515 DestroySurface( eglSurface );
517 // create the EGL surface
518 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
520 // set the context to be current with the new surface
521 MakeContextCurrent( newEglSurface, eglContext );
526 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
528 bool contextLost = false;
530 // display connection has not changed, then we can just create a new surface
531 // create the EGL surface
532 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
534 // set the eglSurface to be current
535 MakeCurrent( pixmap, eglSurface );
540 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
542 mGlesVersion = glesVersion;
545 void EglImplementation::SetFirstFrameAfterResume()
547 mIsFirstFrameAfterResume = true;
550 EGLDisplay EglImplementation::GetDisplay() const
555 EGLContext EglImplementation::GetContext() const
560 int32_t EglImplementation::GetGlesVersion() const
565 bool EglImplementation::IsSurfacelessContextSupported() const
567 return mIsSurfacelessContextSupported;
570 void EglImplementation::WaitClient()
572 // Wait for EGL to finish executing all rendering calls for the current context
573 if ( eglWaitClient() != EGL_TRUE )
575 TEST_EGL_ERROR("eglWaitClient");
579 } // namespace Adaptor
581 } // namespace Internal
585 #pragma GCC diagnostic pop