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>
23 #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"
45 #define TEST_EGL_ERROR(lastCommand) \
47 EGLint err = eglGetError(); \
48 if (err != EGL_SUCCESS) \
50 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
51 Egl::PrintError(err); \
52 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
56 EglImplementation::EglImplementation( int multiSamplingLevel,
57 Integration::DepthBufferAvailable depthBufferRequired,
58 Integration::StencilBufferAvailable stencilBufferRequired )
60 mEglNativeDisplay( 0 ),
61 mEglNativeWindow( 0 ),
62 mCurrentEglNativePixmap( 0 ),
66 mCurrentEglSurface( 0 ),
67 mCurrentEglContext( EGL_NO_CONTEXT ),
68 mMultiSamplingLevel( multiSamplingLevel ),
70 mColorDepth( COLOR_DEPTH_24 ),
71 mGlesInitialized( false ),
72 mIsOwnSurface( true ),
74 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
75 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
79 EglImplementation::~EglImplementation()
84 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
86 if ( !mGlesInitialized )
88 mEglNativeDisplay = display;
90 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
91 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
92 EGLint error = eglGetError();
94 if( mEglDisplay == NULL && error != EGL_SUCCESS )
96 throw Dali::DaliException( "", "OpenGL ES is not supported");
99 EGLint majorVersion = 0;
100 EGLint minorVersion = 0;
101 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
105 eglBindAPI(EGL_OPENGL_ES_API);
107 mGlesInitialized = true;
108 mIsOwnSurface = isOwnSurface;
111 // We want to display this information all the time, so use the LogMessage directly
112 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
117 eglQueryString( mEglDisplay, EGL_VENDOR ),
118 eglQueryString( mEglDisplay, EGL_VERSION ),
119 eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
120 eglQueryString( mEglDisplay, EGL_EXTENSIONS ));
122 return mGlesInitialized;
125 bool EglImplementation::CreateContext()
127 // make sure a context isn't created twice
128 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
130 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
131 TEST_EGL_ERROR("eglCreateContext render thread");
133 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
135 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
136 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
137 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
138 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
139 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
144 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
146 // make sure a context isn't created twice
147 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
149 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
150 TEST_EGL_ERROR("eglCreateContext render thread");
152 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
154 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
155 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
156 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
157 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
158 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
160 mEglWindowContexts.push_back( eglContext );
165 void EglImplementation::DestroyContext( EGLContext& eglContext )
167 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
169 eglDestroyContext( mEglDisplay, eglContext );
173 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
175 if(mIsOwnSurface && eglSurface)
177 // Make context null to prevent crash in driver side
179 eglDestroySurface( mEglDisplay, eglSurface );
184 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
186 if (mCurrentEglContext == eglContext)
191 mCurrentEglSurface = eglSurface;
197 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
199 mCurrentEglContext = eglContext;
202 EGLint error = eglGetError();
204 if ( error != EGL_SUCCESS )
206 Egl::PrintError(error);
208 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
212 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
214 if (mCurrentEglContext == mEglContext)
219 mCurrentEglNativePixmap = pixmap;
220 mCurrentEglSurface = eglSurface;
226 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
228 mCurrentEglContext = mEglContext;
231 EGLint error = eglGetError();
233 if ( error != EGL_SUCCESS )
235 Egl::PrintError(error);
237 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
241 void EglImplementation::MakeContextNull()
243 // clear the current context
244 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
245 mCurrentEglContext = EGL_NO_CONTEXT;
248 void EglImplementation::TerminateGles()
250 if ( mGlesInitialized )
252 // Make context null to prevent crash in driver side
255 for ( auto eglSurface : mEglWindowSurfaces )
257 if(mIsOwnSurface && eglSurface)
259 eglDestroySurface(mEglDisplay, eglSurface);
262 eglDestroyContext(mEglDisplay, mEglContext);
263 for ( auto eglContext : mEglWindowContexts )
265 eglDestroyContext(mEglDisplay, eglContext);
268 eglTerminate(mEglDisplay);
273 mCurrentEglSurface = NULL;
274 mCurrentEglContext = EGL_NO_CONTEXT;
276 mGlesInitialized = false;
280 bool EglImplementation::IsGlesInitialized() const
282 return mGlesInitialized;
285 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
287 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
289 eglSwapBuffers( mEglDisplay, eglSurface );
293 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
295 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
298 void EglImplementation::WaitGL()
303 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
305 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
310 bool isTransparent = ( depth == COLOR_DEPTH_32 );
313 mIsWindow = isWindowType;
316 Vector<EGLint> configAttribs;
317 configAttribs.Reserve(31);
321 configAttribs.PushBack( EGL_SURFACE_TYPE );
322 configAttribs.PushBack( EGL_WINDOW_BIT );
326 configAttribs.PushBack( EGL_SURFACE_TYPE );
327 configAttribs.PushBack( EGL_PIXMAP_BIT );
330 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
332 if( mGlesVersion >= 30 )
335 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
337 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
342 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
345 // TODO: enable this flag when it becomes supported
346 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
347 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
349 configAttribs.PushBack( EGL_RED_SIZE );
350 configAttribs.PushBack( 8 );
351 configAttribs.PushBack( EGL_GREEN_SIZE );
352 configAttribs.PushBack( 8 );
353 configAttribs.PushBack( EGL_BLUE_SIZE );
354 configAttribs.PushBack( 8 );
358 configAttribs.PushBack( EGL_ALPHA_SIZE );
360 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
361 configAttribs.PushBack( 8 );
363 // There is a bug in the desktop emulator
364 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
365 configAttribs.PushBack( 8 );
369 configAttribs.PushBack( EGL_DEPTH_SIZE );
370 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
371 configAttribs.PushBack( EGL_STENCIL_SIZE );
372 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
374 #ifndef DALI_PROFILE_UBUNTU
375 if( mMultiSamplingLevel != EGL_DONT_CARE )
377 configAttribs.PushBack( EGL_SAMPLES );
378 configAttribs.PushBack( mMultiSamplingLevel );
379 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
380 configAttribs.PushBack( 1 );
382 #endif // DALI_PROFILE_UBUNTU
383 configAttribs.PushBack( EGL_NONE );
385 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
387 if( mGlesVersion >= 30 )
390 DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retring to use OpenGL es 2.0.");
394 EGLint error = eglGetError();
397 case EGL_BAD_DISPLAY:
399 DALI_LOG_ERROR("Display is not an EGL display connection\n");
402 case EGL_BAD_ATTRIBUTE:
404 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");
407 case EGL_NOT_INITIALIZED:
409 DALI_LOG_ERROR("Display has not been initialized\n");
412 case EGL_BAD_PARAMETER:
414 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
419 DALI_LOG_ERROR("Unknown error.\n");
422 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
425 Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
427 mContextAttribs.Clear();
428 if( mGlesVersion >= 30 )
430 mContextAttribs.Reserve(5);
431 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
432 mContextAttribs.PushBack( mGlesVersion / 10 );
433 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
434 mContextAttribs.PushBack( mGlesVersion % 10 );
438 mContextAttribs.Reserve(3);
439 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
440 mContextAttribs.PushBack( 2 );
442 mContextAttribs.PushBack( EGL_NONE );
444 if ( numConfigs != 1 )
446 DALI_LOG_ERROR("No configurations found.\n");
448 TEST_EGL_ERROR("eglChooseConfig");
454 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
456 mEglNativeWindow = window;
461 ChooseConfig(mIsWindow, mColorDepth);
463 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
464 TEST_EGL_ERROR("eglCreateWindowSurface");
466 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
468 return mCurrentEglSurface;
471 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
473 mCurrentEglNativePixmap = pixmap;
478 ChooseConfig(mIsWindow, mColorDepth);
480 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
481 TEST_EGL_ERROR("eglCreatePixmapSurface");
483 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
485 return mCurrentEglSurface;
488 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
490 bool contextLost = false;
492 // display connection has not changed, then we can just create a new surface
493 // the surface is bound to the context, so set the context to null
496 // destroy the surface
497 DestroySurface( eglSurface );
499 // create the EGL surface
500 CreateSurfaceWindow( window, mColorDepth );
502 // set the context to be current with the new surface
503 MakeContextCurrent( eglSurface, eglContext );
508 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
510 bool contextLost = false;
512 // display connection has not changed, then we can just create a new surface
513 // create the EGL surface
514 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
516 // set the eglSurface to be current
517 MakeCurrent( pixmap, eglSurface );
522 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
524 mGlesVersion = glesVersion;
527 EGLDisplay EglImplementation::GetDisplay() const
532 EGLContext EglImplementation::GetContext() const
537 int32_t EglImplementation::GetGlesVersion() const
542 } // namespace Adaptor
544 } // namespace Internal
548 #pragma GCC diagnostic pop