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 ";
367 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
369 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
371 #ifndef DALI_PROFILE_UBUNTU
372 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
374 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
376 #endif //DALI_PROFILE_UBUNTU
378 if( mPartialUpdateAvailable && mSwapBuffersWithDamage )
380 mSwapBuffersWithDamage( mEglDisplay, eglSurface, &mDamagedRectArray[0], mDamagedRectArray.size()/4 );
384 eglSwapBuffers( mEglDisplay, eglSurface );
387 #ifndef DALI_PROFILE_UBUNTU
388 if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
390 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
391 mSwapBufferCountAfterResume++;
393 #endif //DALI_PROFILE_UBUNTU
399 int EglImplementation::GetBufferAge( EGLSurface& eglSurface )
402 if ( eglSurface != EGL_NO_SURFACE && mIsKhrPartialUpdateSupported )
404 if( !eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_KHR, &bufferAge) )
406 DALI_LOG_ERROR("EglImplementation::GetBufferAge() eglQuerySurface %s ", GetEglErrorString(eglGetError()) );
412 void EglImplementation::SetDamagedRect( std::vector<int> damagedRectArray, EGLSurface& eglSurface )
414 mDamagedRectArray = damagedRectArray;
415 if ( eglSurface != EGL_NO_SURFACE && mPartialUpdateAvailable && mEglSetDamageRegionKHR )
417 if( !mEglSetDamageRegionKHR( mEglDisplay, eglSurface, &damagedRectArray[0], damagedRectArray.size() / 4 ) )
419 DALI_LOG_ERROR("EglImplementation::mEglSetDamageRegionKHR() error %s ", GetEglErrorString(eglGetError()) );
424 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
426 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
429 void EglImplementation::WaitGL()
434 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
436 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
442 mIsWindow = isWindowType;
445 Vector<EGLint> configAttribs;
446 configAttribs.Reserve(31);
450 configAttribs.PushBack( EGL_SURFACE_TYPE );
451 configAttribs.PushBack( EGL_WINDOW_BIT );
455 configAttribs.PushBack( EGL_SURFACE_TYPE );
456 configAttribs.PushBack( EGL_PIXMAP_BIT );
459 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
461 if( mGlesVersion >= 30 )
463 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
467 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
470 // TODO: enable this flag when it becomes supported
471 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
472 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
474 configAttribs.PushBack( EGL_RED_SIZE );
475 configAttribs.PushBack( 8 );
476 configAttribs.PushBack( EGL_GREEN_SIZE );
477 configAttribs.PushBack( 8 );
478 configAttribs.PushBack( EGL_BLUE_SIZE );
479 configAttribs.PushBack( 8 );
481 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
482 configAttribs.PushBack( EGL_ALPHA_SIZE );
483 configAttribs.PushBack( 8 );
485 configAttribs.PushBack( EGL_DEPTH_SIZE );
486 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
487 configAttribs.PushBack( EGL_STENCIL_SIZE );
488 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
490 #ifndef DALI_PROFILE_UBUNTU
491 if( mMultiSamplingLevel != EGL_DONT_CARE )
493 configAttribs.PushBack( EGL_SAMPLES );
494 configAttribs.PushBack( mMultiSamplingLevel );
495 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
496 configAttribs.PushBack( 1 );
498 #endif // DALI_PROFILE_UBUNTU
499 configAttribs.PushBack( EGL_NONE );
501 // Ensure number of configs is set to 1 as on some drivers,
502 // eglChooseConfig succeeds but does not actually create a proper configuration.
503 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
504 ( numConfigs != 1 ) )
506 if( mGlesVersion >= 30 )
509 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
513 if ( numConfigs != 1 )
515 DALI_LOG_ERROR("No configurations found.\n");
517 TEST_EGL_ERROR("eglChooseConfig");
520 EGLint error = eglGetError();
523 case EGL_BAD_DISPLAY:
525 DALI_LOG_ERROR("Display is not an EGL display connection\n");
528 case EGL_BAD_ATTRIBUTE:
530 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");
533 case EGL_NOT_INITIALIZED:
535 DALI_LOG_ERROR("Display has not been initialized\n");
538 case EGL_BAD_PARAMETER:
540 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
545 DALI_LOG_ERROR("Unknown error.\n");
548 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
551 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
553 mContextAttribs.Clear();
554 if( mIsKhrCreateContextSupported )
556 mContextAttribs.Reserve(5);
557 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
558 mContextAttribs.PushBack( mGlesVersion / 10 );
559 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
560 mContextAttribs.PushBack( mGlesVersion % 10 );
564 mContextAttribs.Reserve(3);
565 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
566 mContextAttribs.PushBack( mGlesVersion / 10 );
568 mContextAttribs.PushBack( EGL_NONE );
573 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
575 mEglNativeWindow = window;
580 ChooseConfig(mIsWindow, mColorDepth);
582 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
583 TEST_EGL_ERROR("eglCreateWindowSurface");
585 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
587 return mCurrentEglSurface;
590 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
592 mCurrentEglNativePixmap = pixmap;
597 ChooseConfig(mIsWindow, mColorDepth);
599 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
600 TEST_EGL_ERROR("eglCreatePixmapSurface");
602 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
604 return mCurrentEglSurface;
607 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
609 bool contextLost = false;
611 // display connection has not changed, then we can just create a new surface
612 // the surface is bound to the context, so set the context to null
615 // destroy the surface
616 DestroySurface( eglSurface );
618 // create the EGL surface
619 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
621 // set the context to be current with the new surface
622 MakeContextCurrent( newEglSurface, eglContext );
627 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
629 bool contextLost = false;
631 // display connection has not changed, then we can just create a new surface
632 // create the EGL surface
633 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
635 // set the eglSurface to be current
636 MakeCurrent( pixmap, eglSurface );
641 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
643 mGlesVersion = glesVersion;
646 void EglImplementation::SetFirstFrameAfterResume()
648 mSwapBufferCountAfterResume = 0;
651 EGLDisplay EglImplementation::GetDisplay() const
656 EGLContext EglImplementation::GetContext() const
661 int32_t EglImplementation::GetGlesVersion() const
666 bool EglImplementation::IsSurfacelessContextSupported() const
668 return mIsSurfacelessContextSupported;
671 void EglImplementation::WaitClient()
673 // Wait for EGL to finish executing all rendering calls for the current context
674 if ( eglWaitClient() != EGL_TRUE )
676 TEST_EGL_ERROR("eglWaitClient");
680 } // namespace Adaptor
682 } // namespace Internal
686 #pragma GCC diagnostic pop