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 )
92 if( mPartialUpdateAvailable )
94 mEglSetDamageRegionKHR = reinterpret_cast< PFNEGLSETDAMAGEREGIONKHRPROC >( eglGetProcAddress( "eglSetDamageRegionKHR" ) );
95 mSwapBuffersWithDamage = reinterpret_cast< PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC >( eglGetProcAddress( "eglSwapBuffersWithDamageEXT" ) );
96 if( !mEglSetDamageRegionKHR || !mSwapBuffersWithDamage )
98 mPartialUpdateAvailable = false;
99 DALI_LOG_ERROR("Initialization of partial update failed.\n");
103 DALI_LOG_RELEASE_INFO("Initialization of partial update success!\n");
108 EglImplementation::~EglImplementation()
113 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
115 if ( !mGlesInitialized )
117 mEglNativeDisplay = display;
119 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
120 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
121 EGLint error = eglGetError();
123 if( mEglDisplay == NULL && error != EGL_SUCCESS )
125 throw Dali::DaliException( "", "OpenGL ES is not supported");
128 EGLint majorVersion = 0;
129 EGLint minorVersion = 0;
130 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
134 eglBindAPI(EGL_OPENGL_ES_API);
136 mGlesInitialized = true;
137 mIsOwnSurface = isOwnSurface;
140 // Query EGL extensions to check whether surfaceless context is supported
141 const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
142 std::istringstream stream( extensionStr );
143 std::string currentExtension;
144 uint32_t extensionCheckCount = 0;
145 while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
147 if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
149 mIsSurfacelessContextSupported = true;
150 extensionCheckCount++;
152 if( currentExtension == EGL_KHR_CREATE_CONTEXT )
154 mIsKhrCreateContextSupported = true;
155 extensionCheckCount++;
157 if( currentExtension == EGL_KHR_PARTIAL_UPDATE )
159 mIsKhrPartialUpdateSupported = true;
160 extensionCheckCount++;
164 if( !mIsKhrPartialUpdateSupported )
166 mPartialUpdateAvailable = false;
169 // We want to display this information all the time, so use the LogMessage directly
170 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
175 eglQueryString( mEglDisplay, EGL_VENDOR ),
176 eglQueryString( mEglDisplay, EGL_VERSION ),
177 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
180 return mGlesInitialized;
183 bool EglImplementation::CreateContext()
185 // make sure a context isn't created twice
186 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
188 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
189 TEST_EGL_ERROR("eglCreateContext render thread");
191 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
193 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
194 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
195 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
196 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
197 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
202 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
204 // make sure a context isn't created twice
205 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
207 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
208 TEST_EGL_ERROR("eglCreateContext render thread");
210 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
212 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
213 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
214 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
215 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
216 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
218 mEglWindowContexts.push_back( eglContext );
223 void EglImplementation::DestroyContext( EGLContext& eglContext )
225 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
227 eglDestroyContext( mEglDisplay, eglContext );
231 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
233 if(mIsOwnSurface && eglSurface)
235 // Make context null to prevent crash in driver side
237 eglDestroySurface( mEglDisplay, eglSurface );
242 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
244 if (mCurrentEglContext == eglContext)
249 mCurrentEglSurface = eglSurface;
253 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
255 mCurrentEglContext = eglContext;
258 EGLint error = eglGetError();
260 if ( error != EGL_SUCCESS )
262 Egl::PrintError(error);
264 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
268 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
270 if (mCurrentEglContext == mEglContext)
275 mCurrentEglNativePixmap = pixmap;
276 mCurrentEglSurface = eglSurface;
280 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
282 mCurrentEglContext = mEglContext;
285 EGLint error = eglGetError();
287 if ( error != EGL_SUCCESS )
289 Egl::PrintError(error);
291 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
295 void EglImplementation::MakeContextNull()
297 // clear the current context
298 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
299 mCurrentEglContext = EGL_NO_CONTEXT;
302 void EglImplementation::TerminateGles()
304 if ( mGlesInitialized )
306 // Make context null to prevent crash in driver side
309 for ( auto eglSurface : mEglWindowSurfaces )
311 if(mIsOwnSurface && eglSurface)
313 eglDestroySurface(mEglDisplay, eglSurface);
316 eglDestroyContext(mEglDisplay, mEglContext);
317 for ( auto eglContext : mEglWindowContexts )
319 eglDestroyContext(mEglDisplay, eglContext);
322 eglTerminate(mEglDisplay);
327 mCurrentEglSurface = NULL;
328 mCurrentEglContext = EGL_NO_CONTEXT;
330 mGlesInitialized = false;
334 bool EglImplementation::IsGlesInitialized() const
336 return mGlesInitialized;
340 const char* GetEglErrorString(EGLint error)
344 case EGL_SUCCESS: return "No error";
345 case EGL_NOT_INITIALIZED: return "EGL not initialized or failed to initialize";
346 case EGL_BAD_ACCESS: return "Resource inaccessible";
347 case EGL_BAD_ALLOC: return "Cannot allocate resources";
348 case EGL_BAD_ATTRIBUTE: return "Unrecognized attribute or attribute value";
349 case EGL_BAD_CONTEXT: return "Invalid EGL context";
350 case EGL_BAD_CONFIG: return "Invalid EGL frame buffer configuration";
351 case EGL_BAD_CURRENT_SURFACE: return "Current surface is no longer valid";
352 case EGL_BAD_DISPLAY: return "Invalid EGL display";
353 case EGL_BAD_SURFACE: return "Invalid surface";
354 case EGL_BAD_MATCH: return "Inconsistent arguments";
355 case EGL_BAD_PARAMETER: return "Invalid argument";
356 case EGL_BAD_NATIVE_PIXMAP: return "Invalid native pixmap";
357 case EGL_BAD_NATIVE_WINDOW: return "Invalid native window";
358 case EGL_CONTEXT_LOST: return "Context lost";
360 return "Unknown error ";
364 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
366 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
368 if( mIsFirstFrameAfterResume )
370 DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers: First SwapBuffers call.\n" );
371 mIsFirstFrameAfterResume = false;
373 if( mPartialUpdateAvailable && mSwapBuffersWithDamage )
375 mSwapBuffersWithDamage( mEglDisplay, eglSurface, &mDamagedRectArray[0], mDamagedRectArray.size()/4 );
379 eglSwapBuffers( mEglDisplay, eglSurface );
385 int EglImplementation::GetBufferAge( EGLSurface& eglSurface )
388 if ( eglSurface != EGL_NO_SURFACE && mIsKhrPartialUpdateSupported )
390 if( !eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_KHR, &bufferAge) )
392 DALI_LOG_ERROR("EglImplementation::GetBufferAge() eglQuerySurface %s ", GetEglErrorString(eglGetError()) );
398 void EglImplementation::SetDamagedRect(std::vector<int> damagedRectArray, EGLSurface& eglSurface)
400 mDamagedRectArray = damagedRectArray;
401 if ( eglSurface != EGL_NO_SURFACE && mPartialUpdateAvailable && mEglSetDamageRegionKHR )
403 if( !mEglSetDamageRegionKHR( mEglDisplay, eglSurface, &damagedRectArray[0], damagedRectArray.size() / 4 ) )
405 DALI_LOG_ERROR("EglImplementation::mEglSetDamageRegionKHR() error %s ", GetEglErrorString(eglGetError()) );
410 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
412 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
415 void EglImplementation::WaitGL()
420 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
422 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
428 mIsWindow = isWindowType;
431 Vector<EGLint> configAttribs;
432 configAttribs.Reserve(31);
436 configAttribs.PushBack( EGL_SURFACE_TYPE );
437 configAttribs.PushBack( EGL_WINDOW_BIT );
441 configAttribs.PushBack( EGL_SURFACE_TYPE );
442 configAttribs.PushBack( EGL_PIXMAP_BIT );
445 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
447 if( mGlesVersion >= 30 )
449 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
453 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
456 // TODO: enable this flag when it becomes supported
457 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
458 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
460 configAttribs.PushBack( EGL_RED_SIZE );
461 configAttribs.PushBack( 8 );
462 configAttribs.PushBack( EGL_GREEN_SIZE );
463 configAttribs.PushBack( 8 );
464 configAttribs.PushBack( EGL_BLUE_SIZE );
465 configAttribs.PushBack( 8 );
467 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
468 configAttribs.PushBack( EGL_ALPHA_SIZE );
469 configAttribs.PushBack( 8 );
471 configAttribs.PushBack( EGL_DEPTH_SIZE );
472 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
473 configAttribs.PushBack( EGL_STENCIL_SIZE );
474 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
476 #ifndef DALI_PROFILE_UBUNTU
477 if( mMultiSamplingLevel != EGL_DONT_CARE )
479 configAttribs.PushBack( EGL_SAMPLES );
480 configAttribs.PushBack( mMultiSamplingLevel );
481 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
482 configAttribs.PushBack( 1 );
484 #endif // DALI_PROFILE_UBUNTU
485 configAttribs.PushBack( EGL_NONE );
487 // Ensure number of configs is set to 1 as on some drivers,
488 // eglChooseConfig succeeds but does not actually create a proper configuration.
489 if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
490 ( numConfigs != 1 ) )
492 if( mGlesVersion >= 30 )
495 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
499 if ( numConfigs != 1 )
501 DALI_LOG_ERROR("No configurations found.\n");
503 TEST_EGL_ERROR("eglChooseConfig");
506 EGLint error = eglGetError();
509 case EGL_BAD_DISPLAY:
511 DALI_LOG_ERROR("Display is not an EGL display connection\n");
514 case EGL_BAD_ATTRIBUTE:
516 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");
519 case EGL_NOT_INITIALIZED:
521 DALI_LOG_ERROR("Display has not been initialized\n");
524 case EGL_BAD_PARAMETER:
526 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
531 DALI_LOG_ERROR("Unknown error.\n");
534 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
537 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
539 mContextAttribs.Clear();
540 if( mIsKhrCreateContextSupported )
542 mContextAttribs.Reserve(5);
543 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
544 mContextAttribs.PushBack( mGlesVersion / 10 );
545 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
546 mContextAttribs.PushBack( mGlesVersion % 10 );
550 mContextAttribs.Reserve(3);
551 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
552 mContextAttribs.PushBack( mGlesVersion / 10 );
554 mContextAttribs.PushBack( EGL_NONE );
559 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
561 mEglNativeWindow = window;
566 ChooseConfig(mIsWindow, mColorDepth);
568 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
569 TEST_EGL_ERROR("eglCreateWindowSurface");
571 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
573 return mCurrentEglSurface;
576 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
578 mCurrentEglNativePixmap = pixmap;
583 ChooseConfig(mIsWindow, mColorDepth);
585 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
586 TEST_EGL_ERROR("eglCreatePixmapSurface");
588 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
590 return mCurrentEglSurface;
593 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
595 bool contextLost = false;
597 // display connection has not changed, then we can just create a new surface
598 // the surface is bound to the context, so set the context to null
601 // destroy the surface
602 DestroySurface( eglSurface );
604 // create the EGL surface
605 EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
607 // set the context to be current with the new surface
608 MakeContextCurrent( newEglSurface, eglContext );
613 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
615 bool contextLost = false;
617 // display connection has not changed, then we can just create a new surface
618 // create the EGL surface
619 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
621 // set the eglSurface to be current
622 MakeCurrent( pixmap, eglSurface );
627 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
629 mGlesVersion = glesVersion;
632 void EglImplementation::SetFirstFrameAfterResume()
634 mIsFirstFrameAfterResume = true;
637 EGLDisplay EglImplementation::GetDisplay() const
642 EGLContext EglImplementation::GetContext() const
647 int32_t EglImplementation::GetGlesVersion() const
652 bool EglImplementation::IsSurfacelessContextSupported() const
654 return mIsSurfacelessContextSupported;
657 void EglImplementation::WaitClient()
659 // Wait for EGL to finish executing all rendering calls for the current context
660 if ( eglWaitClient() != EGL_TRUE )
662 TEST_EGL_ERROR("eglWaitClient");
666 } // namespace Adaptor
668 } // namespace Internal
672 #pragma GCC diagnostic pop