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 = 3;
40 const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
41 const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
42 const std::string EGL_KHR_PARTIAL_UPDATE = "EGL_KHR_partial_update";
54 #define TEST_EGL_ERROR(lastCommand) \
56 EGLint err = eglGetError(); \
57 if (err != EGL_SUCCESS) \
59 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
60 Egl::PrintError(err); \
61 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
65 EglImplementation::EglImplementation( int multiSamplingLevel,
66 Integration::DepthBufferAvailable depthBufferRequired,
67 Integration::StencilBufferAvailable stencilBufferRequired,
68 Integration::PartialUpdateAvailable partialUpdateAvailable )
70 mEglNativeDisplay( 0 ),
71 mEglNativeWindow( 0 ),
72 mCurrentEglNativePixmap( 0 ),
76 mCurrentEglSurface( 0 ),
77 mCurrentEglContext( EGL_NO_CONTEXT ),
78 mMultiSamplingLevel( multiSamplingLevel ),
80 mDamagedRectArray( 0 ),
81 mColorDepth( COLOR_DEPTH_24 ),
82 mGlesInitialized( false ),
83 mIsOwnSurface( true ),
85 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
86 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
87 mIsSurfacelessContextSupported( false ),
88 mIsKhrCreateContextSupported( false ),
89 mSwapBufferCountAfterResume( 0 ),
90 mIsKhrPartialUpdateSupported( false ),
91 mPartialUpdateAvailable( partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE ),
92 mEglSetDamageRegionKHR( nullptr ),
93 mSwapBuffersWithDamage( nullptr )
95 if( mPartialUpdateAvailable )
97 mEglSetDamageRegionKHR = reinterpret_cast< PFNEGLSETDAMAGEREGIONKHRPROC >( eglGetProcAddress( "eglSetDamageRegionKHR" ) );
98 mSwapBuffersWithDamage = reinterpret_cast< PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC >( eglGetProcAddress( "eglSwapBuffersWithDamageEXT" ) );
99 if( !mEglSetDamageRegionKHR || !mSwapBuffersWithDamage )
101 mPartialUpdateAvailable = false;
102 DALI_LOG_ERROR("Initialization of partial update failed.\n");
106 DALI_LOG_RELEASE_INFO("Initialization of partial update success!\n");
111 EglImplementation::~EglImplementation()
116 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
118 if ( !mGlesInitialized )
120 mEglNativeDisplay = display;
122 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
123 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
124 EGLint error = eglGetError();
126 if( mEglDisplay == NULL && error != EGL_SUCCESS )
128 throw Dali::DaliException( "", "OpenGL ES is not supported");
131 EGLint majorVersion = 0;
132 EGLint minorVersion = 0;
133 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
137 eglBindAPI(EGL_OPENGL_ES_API);
139 mGlesInitialized = true;
140 mIsOwnSurface = isOwnSurface;
143 // Query EGL extensions to check whether surfaceless context is supported
144 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
145 std::istringstream stream( extensionStr );
146 std::string currentExtension;
147 uint32_t extensionCheckCount = 0;
148 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
150 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
152 mIsSurfacelessContextSupported = true;
153 extensionCheckCount++;
155 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
157 mIsKhrCreateContextSupported = true;
158 extensionCheckCount++;
160 if( currentExtension == EGL_KHR_PARTIAL_UPDATE )
162 mIsKhrPartialUpdateSupported = true;
163 extensionCheckCount++;
167 if( !mIsKhrPartialUpdateSupported )
169 mPartialUpdateAvailable = false;
172 // We want to display this information all the time, so use the LogMessage directly
173 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
178 eglQueryString( mEglDisplay, EGL_VENDOR ),
179 eglQueryString( mEglDisplay, EGL_VERSION ),
180 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
183 return mGlesInitialized;
186 bool EglImplementation::CreateContext()
188 // make sure a context isn't created twice
189 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
191 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
192 TEST_EGL_ERROR("eglCreateContext render thread");
194 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
196 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
197 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
198 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
199 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
200 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
205 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
207 // make sure a context isn't created twice
208 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
210 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
211 TEST_EGL_ERROR("eglCreateContext render thread");
213 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
215 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
216 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
217 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
218 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
219 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
221 mEglWindowContexts.push_back( eglContext );
226 void EglImplementation::DestroyContext( EGLContext& eglContext )
228 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
230 eglDestroyContext( mEglDisplay, eglContext );
234 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
236 if(mIsOwnSurface && eglSurface)
238 // Make context null to prevent crash in driver side
240 eglDestroySurface( mEglDisplay, eglSurface );
245 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
247 if (mCurrentEglContext == eglContext)
252 mCurrentEglSurface = eglSurface;
256 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
258 mCurrentEglContext = eglContext;
261 EGLint error = eglGetError();
263 if ( error != EGL_SUCCESS )
265 Egl::PrintError(error);
267 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
271 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
273 if (mCurrentEglContext == mEglContext)
278 mCurrentEglNativePixmap = pixmap;
279 mCurrentEglSurface = eglSurface;
283 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
285 mCurrentEglContext = mEglContext;
288 EGLint error = eglGetError();
290 if ( error != EGL_SUCCESS )
292 Egl::PrintError(error);
294 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
298 void EglImplementation::MakeContextNull()
300 // clear the current context
301 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
302 mCurrentEglContext = EGL_NO_CONTEXT;
305 void EglImplementation::TerminateGles()
307 if ( mGlesInitialized )
309 // Make context null to prevent crash in driver side
312 for ( auto eglSurface : mEglWindowSurfaces )
314 if(mIsOwnSurface && eglSurface)
316 eglDestroySurface(mEglDisplay, eglSurface);
319 eglDestroyContext(mEglDisplay, mEglContext);
320 for ( auto eglContext : mEglWindowContexts )
322 eglDestroyContext(mEglDisplay, eglContext);
325 eglTerminate(mEglDisplay);
330 mCurrentEglSurface = NULL;
331 mCurrentEglContext = EGL_NO_CONTEXT;
333 mGlesInitialized = false;
337 bool EglImplementation::IsGlesInitialized() const
339 return mGlesInitialized;
343 const char* GetEglErrorString(EGLint error)
347 case EGL_SUCCESS: return "No error";
348 case EGL_NOT_INITIALIZED: return "EGL not initialized or failed to initialize";
349 case EGL_BAD_ACCESS: return "Resource inaccessible";
350 case EGL_BAD_ALLOC: return "Cannot allocate resources";
351 case EGL_BAD_ATTRIBUTE: return "Unrecognized attribute or attribute value";
352 case EGL_BAD_CONTEXT: return "Invalid EGL context";
353 case EGL_BAD_CONFIG: return "Invalid EGL frame buffer configuration";
354 case EGL_BAD_CURRENT_SURFACE: return "Current surface is no longer valid";
355 case EGL_BAD_DISPLAY: return "Invalid EGL display";
356 case EGL_BAD_SURFACE: return "Invalid surface";
357 case EGL_BAD_MATCH: return "Inconsistent arguments";
358 case EGL_BAD_PARAMETER: return "Invalid argument";
359 case EGL_BAD_NATIVE_PIXMAP: return "Invalid native pixmap";
360 case EGL_BAD_NATIVE_WINDOW: return "Invalid native window";
361 case EGL_CONTEXT_LOST: return "Context lost";
363 return "Unknown error ";
366 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
368 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
370 #ifndef DALI_PROFILE_UBUNTU
371 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
373 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
375 #endif //DALI_PROFILE_UBUNTU+
377 if( mPartialUpdateAvailable && mSwapBuffersWithDamage )
379 mSwapBuffersWithDamage( mEglDisplay, eglSurface, &mDamagedRectArray[0], mDamagedRectArray.size()/4 );
383 eglSwapBuffers( mEglDisplay, eglSurface );
386 #ifndef DALI_PROFILE_UBUNTU
387 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
389 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
390 mSwapBufferCountAfterResume++;
392 #endif //DALI_PROFILE_UBUNTU
397 int EglImplementation::GetBufferAge( EGLSurface& eglSurface )
400 if ( eglSurface != EGL_NO_SURFACE && mIsKhrPartialUpdateSupported )
402 if( !eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_KHR, &bufferAge) )
404 DALI_LOG_ERROR("EglImplementation::GetBufferAge() eglQuerySurface %s ", GetEglErrorString(eglGetError()) );
410 void EglImplementation::SetDamagedRect( std::vector<int> damagedRectArray, EGLSurface& eglSurface )
412 mDamagedRectArray = damagedRectArray;
413 if ( eglSurface != EGL_NO_SURFACE && mPartialUpdateAvailable && mEglSetDamageRegionKHR )
415 if( !mEglSetDamageRegionKHR( mEglDisplay, eglSurface, &damagedRectArray[0], damagedRectArray.size() / 4 ) )
417 DALI_LOG_ERROR("EglImplementation::mEglSetDamageRegionKHR() error %s ", GetEglErrorString(eglGetError()) );
422 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
424 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
427 void EglImplementation::WaitGL()
432 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
434 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
440 mIsWindow = isWindowType;
443 Vector<EGLint> configAttribs;
444 configAttribs.Reserve(31);
448 configAttribs.PushBack( EGL_SURFACE_TYPE );
449 configAttribs.PushBack( EGL_WINDOW_BIT );
453 configAttribs.PushBack( EGL_SURFACE_TYPE );
454 configAttribs.PushBack( EGL_PIXMAP_BIT );
457 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
459 if( mGlesVersion >= 30 )
461 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
465 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
468 // TODO: enable this flag when it becomes supported
469 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
470 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
472 configAttribs.PushBack( EGL_RED_SIZE );
473 configAttribs.PushBack( 8 );
474 configAttribs.PushBack( EGL_GREEN_SIZE );
475 configAttribs.PushBack( 8 );
476 configAttribs.PushBack( EGL_BLUE_SIZE );
477 configAttribs.PushBack( 8 );
479 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
480 configAttribs.PushBack( EGL_ALPHA_SIZE );
481 configAttribs.PushBack( 8 );
483 configAttribs.PushBack( EGL_DEPTH_SIZE );
484 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
485 configAttribs.PushBack( EGL_STENCIL_SIZE );
486 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
488 #ifndef DALI_PROFILE_UBUNTU
489 if( mMultiSamplingLevel != EGL_DONT_CARE )
491 configAttribs.PushBack( EGL_SAMPLES );
492 configAttribs.PushBack( mMultiSamplingLevel );
493 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
494 configAttribs.PushBack( 1 );
496 #endif // DALI_PROFILE_UBUNTU
497 configAttribs.PushBack( EGL_NONE );
499 // Ensure number of configs is set to 1 as on some drivers,
500 // eglChooseConfig succeeds but does not actually create a proper configuration.
501 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
502 ( numConfigs != 1 ) )
504 if( mGlesVersion >= 30 )
507 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
511 if ( numConfigs != 1 )
513 DALI_LOG_ERROR("No configurations found.\n");
515 TEST_EGL_ERROR("eglChooseConfig");
518 EGLint error = eglGetError();
521 case EGL_BAD_DISPLAY:
523 DALI_LOG_ERROR("Display is not an EGL display connection\n");
526 case EGL_BAD_ATTRIBUTE:
528 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");
531 case EGL_NOT_INITIALIZED:
533 DALI_LOG_ERROR("Display has not been initialized\n");
536 case EGL_BAD_PARAMETER:
538 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
543 DALI_LOG_ERROR("Unknown error.\n");
546 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
549 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
551 mContextAttribs.Clear();
552 if( mIsKhrCreateContextSupported )
554 mContextAttribs.Reserve(5);
555 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
556 mContextAttribs.PushBack( mGlesVersion / 10 );
557 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
558 mContextAttribs.PushBack( mGlesVersion % 10 );
562 mContextAttribs.Reserve(3);
563 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
564 mContextAttribs.PushBack( mGlesVersion / 10 );
566 mContextAttribs.PushBack( EGL_NONE );
571 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
573 mEglNativeWindow = window;
578 ChooseConfig(mIsWindow, mColorDepth);
580 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
581 TEST_EGL_ERROR("eglCreateWindowSurface");
583 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
585 return mCurrentEglSurface;
588 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
590 mCurrentEglNativePixmap = pixmap;
595 ChooseConfig(mIsWindow, mColorDepth);
597 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
598 TEST_EGL_ERROR("eglCreatePixmapSurface");
600 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
602 return mCurrentEglSurface;
605 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
607 bool contextLost = false;
609 // display connection has not changed, then we can just create a new surface
610 // the surface is bound to the context, so set the context to null
613 // destroy the surface
614 DestroySurface( eglSurface );
616 // create the EGL surface
617 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
619 // set the context to be current with the new surface
620 MakeContextCurrent( newEglSurface, eglContext );
625 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
627 bool contextLost = false;
629 // display connection has not changed, then we can just create a new surface
630 // create the EGL surface
631 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
633 // set the eglSurface to be current
634 MakeCurrent( pixmap, eglSurface );
639 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
641 mGlesVersion = glesVersion;
644 void EglImplementation::SetFirstFrameAfterResume()
646 mSwapBufferCountAfterResume = 0;
649 EGLDisplay EglImplementation::GetDisplay() const
654 EGLContext EglImplementation::GetContext() const
659 int32_t EglImplementation::GetGlesVersion() const
664 bool EglImplementation::IsSurfacelessContextSupported() const
666 return mIsSurfacelessContextSupported;
669 void EglImplementation::WaitClient()
671 // Wait for EGL to finish executing all rendering calls for the current context
672 if ( eglWaitClient() != EGL_TRUE )
674 TEST_EGL_ERROR("eglWaitClient");
678 } // namespace Adaptor
680 } // namespace Internal
684 #pragma GCC diagnostic pop