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 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 mGlesInitialized = true;
142 // We want to display this information all the time, so use the LogMessage directly
143 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
148 eglQueryString( mEglDisplay, EGL_VENDOR ),
149 eglQueryString( mEglDisplay, EGL_VERSION ),
150 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
153 return mGlesInitialized;
156 bool EglImplementation::CreateContext()
158 // make sure a context isn't created twice
159 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
161 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
162 TEST_EGL_ERROR("eglCreateContext render thread");
164 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
166 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
167 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
168 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
169 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
170 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
175 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
177 // make sure a context isn't created twice
178 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
180 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
181 TEST_EGL_ERROR("eglCreateContext render thread");
183 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
185 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
186 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
187 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
188 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
189 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
191 mEglWindowContexts.push_back( eglContext );
196 void EglImplementation::DestroyContext( EGLContext& eglContext )
198 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
200 eglDestroyContext( mEglDisplay, eglContext );
204 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
206 if(mIsOwnSurface && eglSurface)
208 // Make context null to prevent crash in driver side
210 eglDestroySurface( mEglDisplay, eglSurface );
215 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
217 if (mCurrentEglContext == eglContext)
222 mCurrentEglSurface = eglSurface;
226 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
228 mCurrentEglContext = eglContext;
231 EGLint error = eglGetError();
233 if ( error != EGL_SUCCESS )
235 Egl::PrintError(error);
237 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
241 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
243 if (mCurrentEglContext == mEglContext)
248 mCurrentEglNativePixmap = pixmap;
249 mCurrentEglSurface = eglSurface;
253 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
255 mCurrentEglContext = mEglContext;
258 EGLint error = eglGetError();
260 if ( error != EGL_SUCCESS )
262 Egl::PrintError(error);
264 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
268 void EglImplementation::MakeContextNull()
270 // clear the current context
271 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
272 mCurrentEglContext = EGL_NO_CONTEXT;
275 void EglImplementation::TerminateGles()
277 if ( mGlesInitialized )
279 // Make context null to prevent crash in driver side
282 for ( auto eglSurface : mEglWindowSurfaces )
284 if(mIsOwnSurface && eglSurface)
286 eglDestroySurface(mEglDisplay, eglSurface);
289 eglDestroyContext(mEglDisplay, mEglContext);
290 for ( auto eglContext : mEglWindowContexts )
292 eglDestroyContext(mEglDisplay, eglContext);
295 eglTerminate(mEglDisplay);
300 mCurrentEglSurface = NULL;
301 mCurrentEglContext = EGL_NO_CONTEXT;
303 mGlesInitialized = false;
307 bool EglImplementation::IsGlesInitialized() const
309 return mGlesInitialized;
312 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
314 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
316 #ifndef DALI_PROFILE_UBUNTU
317 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
319 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
321 #endif //DALI_PROFILE_UBUNTU
323 eglSwapBuffers( mEglDisplay, eglSurface );
325 #ifndef DALI_PROFILE_UBUNTU
326 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
328 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
329 mSwapBufferCountAfterResume++;
331 #endif //DALI_PROFILE_UBUNTU
335 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
337 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
340 void EglImplementation::WaitGL()
345 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
347 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
353 mIsWindow = isWindowType;
356 Vector<EGLint> configAttribs;
357 configAttribs.Reserve(31);
361 configAttribs.PushBack( EGL_SURFACE_TYPE );
362 configAttribs.PushBack( EGL_WINDOW_BIT );
366 configAttribs.PushBack( EGL_SURFACE_TYPE );
367 configAttribs.PushBack( EGL_PIXMAP_BIT );
370 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
372 if( mGlesVersion >= 30 )
374 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
378 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
381 // TODO: enable this flag when it becomes supported
382 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
383 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
385 configAttribs.PushBack( EGL_RED_SIZE );
386 configAttribs.PushBack( 8 );
387 configAttribs.PushBack( EGL_GREEN_SIZE );
388 configAttribs.PushBack( 8 );
389 configAttribs.PushBack( EGL_BLUE_SIZE );
390 configAttribs.PushBack( 8 );
392 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
393 configAttribs.PushBack( EGL_ALPHA_SIZE );
394 configAttribs.PushBack( 8 );
396 configAttribs.PushBack( EGL_DEPTH_SIZE );
397 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
398 configAttribs.PushBack( EGL_STENCIL_SIZE );
399 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
401 #ifndef DALI_PROFILE_UBUNTU
402 if( mMultiSamplingLevel != EGL_DONT_CARE )
404 configAttribs.PushBack( EGL_SAMPLES );
405 configAttribs.PushBack( mMultiSamplingLevel );
406 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
407 configAttribs.PushBack( 1 );
409 #endif // DALI_PROFILE_UBUNTU
410 configAttribs.PushBack( EGL_NONE );
412 // Ensure number of configs is set to 1 as on some drivers,
413 // eglChooseConfig succeeds but does not actually create a proper configuration.
414 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
415 ( numConfigs != 1 ) )
417 if( mGlesVersion >= 30 )
420 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
424 if ( numConfigs != 1 )
426 DALI_LOG_ERROR("No configurations found.\n");
428 TEST_EGL_ERROR("eglChooseConfig");
431 EGLint error = eglGetError();
434 case EGL_BAD_DISPLAY:
436 DALI_LOG_ERROR("Display is not an EGL display connection\n");
439 case EGL_BAD_ATTRIBUTE:
441 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");
444 case EGL_NOT_INITIALIZED:
446 DALI_LOG_ERROR("Display has not been initialized\n");
449 case EGL_BAD_PARAMETER:
451 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
456 DALI_LOG_ERROR("Unknown error.\n");
459 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
462 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
464 mContextAttribs.Clear();
465 if( mIsKhrCreateContextSupported )
467 mContextAttribs.Reserve(5);
468 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
469 mContextAttribs.PushBack( mGlesVersion / 10 );
470 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
471 mContextAttribs.PushBack( mGlesVersion % 10 );
475 mContextAttribs.Reserve(3);
476 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
477 mContextAttribs.PushBack( mGlesVersion / 10 );
479 mContextAttribs.PushBack( EGL_NONE );
484 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
486 mEglNativeWindow = window;
491 ChooseConfig(mIsWindow, mColorDepth);
493 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
494 TEST_EGL_ERROR("eglCreateWindowSurface");
496 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
498 return mCurrentEglSurface;
501 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
503 mCurrentEglNativePixmap = pixmap;
508 ChooseConfig(mIsWindow, mColorDepth);
510 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
511 TEST_EGL_ERROR("eglCreatePixmapSurface");
513 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
515 return mCurrentEglSurface;
518 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
520 bool contextLost = false;
522 // display connection has not changed, then we can just create a new surface
523 // the surface is bound to the context, so set the context to null
528 // destroy the surface
529 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 void EglImplementation::SetFirstFrameAfterResume()
562 mSwapBufferCountAfterResume = 0;
565 EGLDisplay EglImplementation::GetDisplay() const
570 EGLContext EglImplementation::GetContext() const
575 int32_t EglImplementation::GetGlesVersion() const
580 bool EglImplementation::IsSurfacelessContextSupported() const
582 return mIsSurfacelessContextSupported;
585 void EglImplementation::WaitClient()
587 // Wait for EGL to finish executing all rendering calls for the current context
588 if ( eglWaitClient() != EGL_TRUE )
590 TEST_EGL_ERROR("eglWaitClient");
594 } // namespace Adaptor
596 } // namespace Internal
600 #pragma GCC diagnostic pop