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 = 3;
39 const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
40 const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
41 const std::string EGL_KHR_PARTIAL_UPDATE = "EGL_KHR_partial_update";
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,
67 Integration::PartialUpdateAvailable partialUpdateAvailable )
69 mEglNativeDisplay( 0 ),
70 mEglNativeWindow( 0 ),
71 mCurrentEglNativePixmap( 0 ),
75 mCurrentEglSurface( 0 ),
76 mCurrentEglContext( EGL_NO_CONTEXT ),
77 mMultiSamplingLevel( multiSamplingLevel ),
79 mDamagedRectArray( 0 ),
80 mColorDepth( COLOR_DEPTH_24 ),
81 mGlesInitialized( false ),
82 mIsOwnSurface( true ),
84 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
85 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
86 mIsSurfacelessContextSupported( false ),
87 mIsKhrCreateContextSupported( false ),
88 mIsFirstFrameAfterResume( false ),
89 mIsKhrPartialUpdateSupported( false ),
90 mPartialUpdateAvailable( partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE ),
91 mEglSetDamageRegionKHR( nullptr ),
92 mSwapBuffersWithDamage( nullptr )
94 if( mPartialUpdateAvailable )
96 mEglSetDamageRegionKHR = reinterpret_cast< PFNEGLSETDAMAGEREGIONKHRPROC >( eglGetProcAddress( "eglSetDamageRegionKHR" ) );
97 mSwapBuffersWithDamage = reinterpret_cast< PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC >( eglGetProcAddress( "eglSwapBuffersWithDamageEXT" ) );
98 if( !mEglSetDamageRegionKHR || !mSwapBuffersWithDamage )
100 mPartialUpdateAvailable = false;
101 DALI_LOG_ERROR("Initialization of partial update failed.\n");
105 DALI_LOG_RELEASE_INFO("Initialization of partial update success!\n");
110 EglImplementation::~EglImplementation()
115 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
117 if ( !mGlesInitialized )
119 mEglNativeDisplay = display;
121 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
122 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
123 EGLint error = eglGetError();
125 if( mEglDisplay == NULL && error != EGL_SUCCESS )
127 throw Dali::DaliException( "", "OpenGL ES is not supported");
130 EGLint majorVersion = 0;
131 EGLint minorVersion = 0;
132 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
136 eglBindAPI(EGL_OPENGL_ES_API);
138 mGlesInitialized = true;
139 mIsOwnSurface = isOwnSurface;
142 // Query EGL extensions to check whether surfaceless context is supported
143 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
144 std::istringstream stream( extensionStr );
145 std::string currentExtension;
146 uint32_t extensionCheckCount = 0;
147 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
149 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
151 mIsSurfacelessContextSupported = true;
152 extensionCheckCount++;
154 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
156 mIsKhrCreateContextSupported = true;
157 extensionCheckCount++;
159 if( currentExtension == EGL_KHR_PARTIAL_UPDATE )
161 mIsKhrPartialUpdateSupported = true;
162 extensionCheckCount++;
166 if( !mIsKhrPartialUpdateSupported )
168 mPartialUpdateAvailable = false;
171 // We want to display this information all the time, so use the LogMessage directly
172 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
177 eglQueryString( mEglDisplay, EGL_VENDOR ),
178 eglQueryString( mEglDisplay, EGL_VERSION ),
179 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
182 return mGlesInitialized;
185 bool EglImplementation::CreateContext()
187 // make sure a context isn't created twice
188 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
190 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
191 TEST_EGL_ERROR("eglCreateContext render thread");
193 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
195 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
196 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
197 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
198 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
199 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
204 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
206 // make sure a context isn't created twice
207 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
209 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
210 TEST_EGL_ERROR("eglCreateContext render thread");
212 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
214 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
215 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
216 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
217 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
218 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
220 mEglWindowContexts.push_back( eglContext );
225 void EglImplementation::DestroyContext( EGLContext& eglContext )
227 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
229 eglDestroyContext( mEglDisplay, eglContext );
233 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
235 if(mIsOwnSurface && eglSurface)
237 // Make context null to prevent crash in driver side
239 eglDestroySurface( mEglDisplay, eglSurface );
244 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
246 if (mCurrentEglContext == eglContext)
251 mCurrentEglSurface = eglSurface;
255 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
257 mCurrentEglContext = eglContext;
260 EGLint error = eglGetError();
262 if ( error != EGL_SUCCESS )
264 Egl::PrintError(error);
266 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
270 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
272 if (mCurrentEglContext == mEglContext)
277 mCurrentEglNativePixmap = pixmap;
278 mCurrentEglSurface = eglSurface;
282 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
284 mCurrentEglContext = mEglContext;
287 EGLint error = eglGetError();
289 if ( error != EGL_SUCCESS )
291 Egl::PrintError(error);
293 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
297 void EglImplementation::MakeContextNull()
299 // clear the current context
300 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
301 mCurrentEglContext = EGL_NO_CONTEXT;
304 void EglImplementation::TerminateGles()
306 if ( mGlesInitialized )
308 // Make context null to prevent crash in driver side
311 for ( auto eglSurface : mEglWindowSurfaces )
313 if(mIsOwnSurface && eglSurface)
315 eglDestroySurface(mEglDisplay, eglSurface);
318 eglDestroyContext(mEglDisplay, mEglContext);
319 for ( auto eglContext : mEglWindowContexts )
321 eglDestroyContext(mEglDisplay, eglContext);
324 eglTerminate(mEglDisplay);
329 mCurrentEglSurface = NULL;
330 mCurrentEglContext = EGL_NO_CONTEXT;
332 mGlesInitialized = false;
336 bool EglImplementation::IsGlesInitialized() const
338 return mGlesInitialized;
342 const char* GetEglErrorString(EGLint error)
346 case EGL_SUCCESS: return "No error";
347 case EGL_NOT_INITIALIZED: return "EGL not initialized or failed to initialize";
348 case EGL_BAD_ACCESS: return "Resource inaccessible";
349 case EGL_BAD_ALLOC: return "Cannot allocate resources";
350 case EGL_BAD_ATTRIBUTE: return "Unrecognized attribute or attribute value";
351 case EGL_BAD_CONTEXT: return "Invalid EGL context";
352 case EGL_BAD_CONFIG: return "Invalid EGL frame buffer configuration";
353 case EGL_BAD_CURRENT_SURFACE: return "Current surface is no longer valid";
354 case EGL_BAD_DISPLAY: return "Invalid EGL display";
355 case EGL_BAD_SURFACE: return "Invalid surface";
356 case EGL_BAD_MATCH: return "Inconsistent arguments";
357 case EGL_BAD_PARAMETER: return "Invalid argument";
358 case EGL_BAD_NATIVE_PIXMAP: return "Invalid native pixmap";
359 case EGL_BAD_NATIVE_WINDOW: return "Invalid native window";
360 case EGL_CONTEXT_LOST: return "Context lost";
362 return "Unknown error ";
366 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
368 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
370 if( mIsFirstFrameAfterResume )
372 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers: First SwapBuffers call.\n" );
373 mIsFirstFrameAfterResume = false;
375 if( mPartialUpdateAvailable && mSwapBuffersWithDamage )
377 mSwapBuffersWithDamage( mEglDisplay, eglSurface, &mDamagedRectArray[0], mDamagedRectArray.size()/4 );
381 eglSwapBuffers( mEglDisplay, eglSurface );
387 int EglImplementation::GetBufferAge( EGLSurface& eglSurface )
390 if ( eglSurface != EGL_NO_SURFACE && mIsKhrPartialUpdateSupported )
392 if( !eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_KHR, &bufferAge) )
394 DALI_LOG_ERROR("EglImplementation::GetBufferAge() eglQuerySurface %s ", GetEglErrorString(eglGetError()) );
400 void EglImplementation::SetDamagedRect( std::vector<int> damagedRectArray, EGLSurface& eglSurface )
402 mDamagedRectArray = damagedRectArray;
403 if ( eglSurface != EGL_NO_SURFACE && mPartialUpdateAvailable && mEglSetDamageRegionKHR )
405 if( !mEglSetDamageRegionKHR( mEglDisplay, eglSurface, &damagedRectArray[0], damagedRectArray.size() / 4 ) )
407 DALI_LOG_ERROR("EglImplementation::mEglSetDamageRegionKHR() error %s ", GetEglErrorString(eglGetError()) );
412 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
414 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
417 void EglImplementation::WaitGL()
422 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
424 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
430 mIsWindow = isWindowType;
433 Vector<EGLint> configAttribs;
434 configAttribs.Reserve(31);
438 configAttribs.PushBack( EGL_SURFACE_TYPE );
439 configAttribs.PushBack( EGL_WINDOW_BIT );
443 configAttribs.PushBack( EGL_SURFACE_TYPE );
444 configAttribs.PushBack( EGL_PIXMAP_BIT );
447 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
449 if( mGlesVersion >= 30 )
451 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
455 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
458 // TODO: enable this flag when it becomes supported
459 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
460 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
462 configAttribs.PushBack( EGL_RED_SIZE );
463 configAttribs.PushBack( 8 );
464 configAttribs.PushBack( EGL_GREEN_SIZE );
465 configAttribs.PushBack( 8 );
466 configAttribs.PushBack( EGL_BLUE_SIZE );
467 configAttribs.PushBack( 8 );
469 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
470 configAttribs.PushBack( EGL_ALPHA_SIZE );
471 configAttribs.PushBack( 8 );
473 configAttribs.PushBack( EGL_DEPTH_SIZE );
474 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
475 configAttribs.PushBack( EGL_STENCIL_SIZE );
476 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
478 #ifndef DALI_PROFILE_UBUNTU
479 if( mMultiSamplingLevel != EGL_DONT_CARE )
481 configAttribs.PushBack( EGL_SAMPLES );
482 configAttribs.PushBack( mMultiSamplingLevel );
483 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
484 configAttribs.PushBack( 1 );
486 #endif // DALI_PROFILE_UBUNTU
487 configAttribs.PushBack( EGL_NONE );
489 // Ensure number of configs is set to 1 as on some drivers,
490 // eglChooseConfig succeeds but does not actually create a proper configuration.
491 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
492 ( numConfigs != 1 ) )
494 if( mGlesVersion >= 30 )
497 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
501 if ( numConfigs != 1 )
503 DALI_LOG_ERROR("No configurations found.\n");
505 TEST_EGL_ERROR("eglChooseConfig");
508 EGLint error = eglGetError();
511 case EGL_BAD_DISPLAY:
513 DALI_LOG_ERROR("Display is not an EGL display connection\n");
516 case EGL_BAD_ATTRIBUTE:
518 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");
521 case EGL_NOT_INITIALIZED:
523 DALI_LOG_ERROR("Display has not been initialized\n");
526 case EGL_BAD_PARAMETER:
528 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
533 DALI_LOG_ERROR("Unknown error.\n");
536 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
539 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
541 mContextAttribs.Clear();
542 if( mIsKhrCreateContextSupported )
544 mContextAttribs.Reserve(5);
545 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
546 mContextAttribs.PushBack( mGlesVersion / 10 );
547 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
548 mContextAttribs.PushBack( mGlesVersion % 10 );
552 mContextAttribs.Reserve(3);
553 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
554 mContextAttribs.PushBack( mGlesVersion / 10 );
556 mContextAttribs.PushBack( EGL_NONE );
561 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
563 mEglNativeWindow = window;
568 ChooseConfig(mIsWindow, mColorDepth);
570 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
571 TEST_EGL_ERROR("eglCreateWindowSurface");
573 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
575 return mCurrentEglSurface;
578 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
580 mCurrentEglNativePixmap = pixmap;
585 ChooseConfig(mIsWindow, mColorDepth);
587 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
588 TEST_EGL_ERROR("eglCreatePixmapSurface");
590 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
592 return mCurrentEglSurface;
595 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
597 bool contextLost = false;
599 // display connection has not changed, then we can just create a new surface
600 // the surface is bound to the context, so set the context to null
603 // destroy the surface
604 DestroySurface( eglSurface );
606 // create the EGL surface
607 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
609 // set the context to be current with the new surface
610 MakeContextCurrent( newEglSurface, eglContext );
615 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
617 bool contextLost = false;
619 // display connection has not changed, then we can just create a new surface
620 // create the EGL surface
621 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
623 // set the eglSurface to be current
624 MakeCurrent( pixmap, eglSurface );
629 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
631 mGlesVersion = glesVersion;
634 void EglImplementation::SetFirstFrameAfterResume()
636 mIsFirstFrameAfterResume = true;
639 EGLDisplay EglImplementation::GetDisplay() const
644 EGLContext EglImplementation::GetContext() const
649 int32_t EglImplementation::GetGlesVersion() const
654 bool EglImplementation::IsSurfacelessContextSupported() const
656 return mIsSurfacelessContextSupported;
659 void EglImplementation::WaitClient()
661 // Wait for EGL to finish executing all rendering calls for the current context
662 if ( eglWaitClient() != EGL_TRUE )
664 TEST_EGL_ERROR("eglWaitClient");
668 } // namespace Adaptor
670 } // namespace Internal
674 #pragma GCC diagnostic pop