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 mGlesInitialized = true;
119 mIsOwnSurface = isOwnSurface;
122 // Query EGL extensions to check whether surfaceless context is supported
123 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
124 std::istringstream stream( extensionStr );
125 std::string currentExtension;
126 uint32_t extensionCheckCount = 0;
127 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
129 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
131 mIsSurfacelessContextSupported = true;
132 extensionCheckCount++;
134 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
136 mIsKhrCreateContextSupported = true;
137 extensionCheckCount++;
141 // We want to display this information all the time, so use the LogMessage directly
142 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
147 eglQueryString( mEglDisplay, EGL_VENDOR ),
148 eglQueryString( mEglDisplay, EGL_VERSION ),
149 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
152 return mGlesInitialized;
155 bool EglImplementation::CreateContext()
157 // make sure a context isn't created twice
158 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
160 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
161 TEST_EGL_ERROR("eglCreateContext render thread");
163 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
165 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
166 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
167 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
168 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
169 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
174 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
176 // make sure a context isn't created twice
177 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
179 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
180 TEST_EGL_ERROR("eglCreateContext render thread");
182 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
184 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
185 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
186 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
187 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
188 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
190 mEglWindowContexts.push_back( eglContext );
195 void EglImplementation::DestroyContext( EGLContext& eglContext )
197 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
199 eglDestroyContext( mEglDisplay, eglContext );
203 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
205 if(mIsOwnSurface && eglSurface)
207 // Make context null to prevent crash in driver side
209 eglDestroySurface( mEglDisplay, eglSurface );
214 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
216 if (mCurrentEglContext == eglContext)
221 mCurrentEglSurface = eglSurface;
225 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
227 mCurrentEglContext = eglContext;
230 EGLint error = eglGetError();
232 if ( error != EGL_SUCCESS )
234 Egl::PrintError(error);
236 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
240 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
242 if (mCurrentEglContext == mEglContext)
247 mCurrentEglNativePixmap = pixmap;
248 mCurrentEglSurface = eglSurface;
252 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
254 mCurrentEglContext = mEglContext;
257 EGLint error = eglGetError();
259 if ( error != EGL_SUCCESS )
261 Egl::PrintError(error);
263 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
267 void EglImplementation::MakeContextNull()
269 // clear the current context
270 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
271 mCurrentEglContext = EGL_NO_CONTEXT;
274 void EglImplementation::TerminateGles()
276 if ( mGlesInitialized )
278 // Make context null to prevent crash in driver side
281 for ( auto eglSurface : mEglWindowSurfaces )
283 if(mIsOwnSurface && eglSurface)
285 eglDestroySurface(mEglDisplay, eglSurface);
288 eglDestroyContext(mEglDisplay, mEglContext);
289 for ( auto eglContext : mEglWindowContexts )
291 eglDestroyContext(mEglDisplay, eglContext);
294 eglTerminate(mEglDisplay);
299 mCurrentEglSurface = NULL;
300 mCurrentEglContext = EGL_NO_CONTEXT;
302 mGlesInitialized = false;
306 bool EglImplementation::IsGlesInitialized() const
308 return mGlesInitialized;
311 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
313 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
315 #ifndef DALI_PROFILE_UBUNTU
316 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
318 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
320 #endif //DALI_PROFILE_UBUNTU
322 eglSwapBuffers( mEglDisplay, eglSurface );
324 #ifndef DALI_PROFILE_UBUNTU
325 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
327 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
328 mSwapBufferCountAfterResume++;
330 #endif //DALI_PROFILE_UBUNTU
334 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
336 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
339 void EglImplementation::WaitGL()
344 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
346 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
352 mIsWindow = isWindowType;
355 Vector<EGLint> configAttribs;
356 configAttribs.Reserve(31);
360 configAttribs.PushBack( EGL_SURFACE_TYPE );
361 configAttribs.PushBack( EGL_WINDOW_BIT );
365 configAttribs.PushBack( EGL_SURFACE_TYPE );
366 configAttribs.PushBack( EGL_PIXMAP_BIT );
369 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
371 if( mGlesVersion >= 30 )
373 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
377 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
380 // TODO: enable this flag when it becomes supported
381 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
382 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
384 configAttribs.PushBack( EGL_RED_SIZE );
385 configAttribs.PushBack( 8 );
386 configAttribs.PushBack( EGL_GREEN_SIZE );
387 configAttribs.PushBack( 8 );
388 configAttribs.PushBack( EGL_BLUE_SIZE );
389 configAttribs.PushBack( 8 );
391 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
392 configAttribs.PushBack( EGL_ALPHA_SIZE );
393 configAttribs.PushBack( 8 );
395 configAttribs.PushBack( EGL_DEPTH_SIZE );
396 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
397 configAttribs.PushBack( EGL_STENCIL_SIZE );
398 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
400 #ifndef DALI_PROFILE_UBUNTU
401 if( mMultiSamplingLevel != EGL_DONT_CARE )
403 configAttribs.PushBack( EGL_SAMPLES );
404 configAttribs.PushBack( mMultiSamplingLevel );
405 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
406 configAttribs.PushBack( 1 );
408 #endif // DALI_PROFILE_UBUNTU
409 configAttribs.PushBack( EGL_NONE );
411 // Ensure number of configs is set to 1 as on some drivers,
412 // eglChooseConfig succeeds but does not actually create a proper configuration.
413 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
414 ( numConfigs != 1 ) )
416 if( mGlesVersion >= 30 )
419 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
423 if ( numConfigs != 1 )
425 DALI_LOG_ERROR("No configurations found.\n");
427 TEST_EGL_ERROR("eglChooseConfig");
430 EGLint error = eglGetError();
433 case EGL_BAD_DISPLAY:
435 DALI_LOG_ERROR("Display is not an EGL display connection\n");
438 case EGL_BAD_ATTRIBUTE:
440 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");
443 case EGL_NOT_INITIALIZED:
445 DALI_LOG_ERROR("Display has not been initialized\n");
448 case EGL_BAD_PARAMETER:
450 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
455 DALI_LOG_ERROR("Unknown error.\n");
458 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
461 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
463 mContextAttribs.Clear();
464 if( mIsKhrCreateContextSupported )
466 mContextAttribs.Reserve(5);
467 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
468 mContextAttribs.PushBack( mGlesVersion / 10 );
469 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
470 mContextAttribs.PushBack( mGlesVersion % 10 );
474 mContextAttribs.Reserve(3);
475 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
476 mContextAttribs.PushBack( mGlesVersion / 10 );
478 mContextAttribs.PushBack( EGL_NONE );
483 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
485 mEglNativeWindow = window;
490 ChooseConfig(mIsWindow, mColorDepth);
492 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
493 TEST_EGL_ERROR("eglCreateWindowSurface");
495 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
497 return mCurrentEglSurface;
500 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
502 mCurrentEglNativePixmap = pixmap;
507 ChooseConfig(mIsWindow, mColorDepth);
509 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
510 TEST_EGL_ERROR("eglCreatePixmapSurface");
512 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
514 return mCurrentEglSurface;
517 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
519 bool contextLost = false;
521 // display connection has not changed, then we can just create a new surface
522 // the surface is bound to the context, so set the context to null
525 // destroy the surface
526 DestroySurface( eglSurface );
528 // create the EGL surface
529 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
531 // set the context to be current with the new surface
532 MakeContextCurrent( newEglSurface, eglContext );
537 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
539 bool contextLost = false;
541 // display connection has not changed, then we can just create a new surface
542 // create the EGL surface
543 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
545 // set the eglSurface to be current
546 MakeCurrent( pixmap, eglSurface );
551 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
553 mGlesVersion = glesVersion;
556 void EglImplementation::SetFirstFrameAfterResume()
558 mSwapBufferCountAfterResume = 0;
561 EGLDisplay EglImplementation::GetDisplay() const
566 EGLContext EglImplementation::GetContext() const
571 int32_t EglImplementation::GetGlesVersion() const
576 bool EglImplementation::IsSurfacelessContextSupported() const
578 return mIsSurfacelessContextSupported;
581 void EglImplementation::WaitClient()
583 // Wait for EGL to finish executing all rendering calls for the current context
584 if ( eglWaitClient() != EGL_TRUE )
586 TEST_EGL_ERROR("eglWaitClient");
590 } // namespace Adaptor
592 } // namespace Internal
596 #pragma GCC diagnostic pop