2 * Copyright (c) 2018 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 mMultiSamplingLevel( multiSamplingLevel ),
68 mColorDepth( COLOR_DEPTH_24 ),
69 mGlesInitialized( false ),
70 mIsOwnSurface( true ),
72 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
73 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
77 EglImplementation::~EglImplementation()
82 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
84 if ( !mGlesInitialized )
86 mEglNativeDisplay = display;
88 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
89 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
90 EGLint error = eglGetError();
92 if( mEglDisplay == NULL && error != EGL_SUCCESS )
94 throw Dali::DaliException( "", "OpenGL ES is not supported");
97 EGLint majorVersion = 0;
98 EGLint minorVersion = 0;
99 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
103 eglBindAPI(EGL_OPENGL_ES_API);
105 mContextAttribs.Clear();
107 #if DALI_GLES_VERSION >= 30
109 mContextAttribs.Reserve(5);
110 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
111 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
112 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
113 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
115 #else // DALI_GLES_VERSION >= 30
117 mContextAttribs.Reserve(3);
118 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
119 mContextAttribs.PushBack( 2 );
121 #endif // DALI_GLES_VERSION >= 30
123 mContextAttribs.PushBack( EGL_NONE );
125 mGlesInitialized = true;
126 mIsOwnSurface = isOwnSurface;
129 return mGlesInitialized;
132 bool EglImplementation::CreateContext()
134 // make sure a context isn't created twice
135 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
137 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
138 TEST_EGL_ERROR("eglCreateContext render thread");
140 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
142 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
143 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
144 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
145 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
146 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
151 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
153 // make sure a context isn't created twice
154 DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
156 eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
157 TEST_EGL_ERROR("eglCreateContext render thread");
159 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
161 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
162 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
163 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
164 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
165 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
167 mEglWindowContexts.push_back( eglContext );
172 void EglImplementation::DestroyContext( EGLContext& eglContext )
174 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
176 eglDestroyContext( mEglDisplay, eglContext );
180 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
182 if(mIsOwnSurface && eglSurface)
184 // Make context null to prevent crash in driver side
186 eglDestroySurface( mEglDisplay, eglSurface );
191 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
193 mCurrentEglSurface = eglSurface;
197 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
200 EGLint error = eglGetError();
202 if ( error != EGL_SUCCESS )
204 Egl::PrintError(error);
206 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
209 // We want to display this information all the time, so use the LogMessage directly
210 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
215 eglQueryString(mEglDisplay, EGL_VENDOR),
216 eglQueryString(mEglDisplay, EGL_VERSION),
217 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
218 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
221 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
223 mCurrentEglNativePixmap = pixmap;
224 mCurrentEglSurface = eglSurface;
228 eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, 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 );
247 void EglImplementation::TerminateGles()
249 if ( mGlesInitialized )
251 // Make context null to prevent crash in driver side
254 for ( auto eglSurface : mEglWindowSurfaces )
256 if(mIsOwnSurface && eglSurface)
258 eglDestroySurface(mEglDisplay, eglSurface);
261 eglDestroyContext(mEglDisplay, mEglContext);
262 for ( auto eglContext : mEglWindowContexts )
264 eglDestroyContext(mEglDisplay, eglContext);
267 eglTerminate(mEglDisplay);
272 mCurrentEglSurface = NULL;
274 mGlesInitialized = false;
278 bool EglImplementation::IsGlesInitialized() const
280 return mGlesInitialized;
283 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
285 if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
287 eglSwapBuffers( mEglDisplay, eglSurface );
291 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
293 eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
296 void EglImplementation::WaitGL()
301 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
303 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
308 bool isTransparent = ( depth == COLOR_DEPTH_32 );
310 mIsWindow = isWindowType;
313 Vector<EGLint> configAttribs;
314 configAttribs.Reserve(31);
318 configAttribs.PushBack( EGL_SURFACE_TYPE );
319 configAttribs.PushBack( EGL_WINDOW_BIT );
323 configAttribs.PushBack( EGL_SURFACE_TYPE );
324 configAttribs.PushBack( EGL_PIXMAP_BIT );
327 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
329 #if DALI_GLES_VERSION >= 30
332 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
334 // There is a bug in the desktop emulator
335 // Requesting for ES3 causes eglCreateContext even though it allows to ask
336 // for a configuration that supports GLES 3.0
337 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
340 #else // DALI_GLES_VERSION >= 30
342 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
343 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
345 #endif //DALI_GLES_VERSION >= 30
347 #if DALI_GLES_VERSION >= 30
348 // TODO: enable this flag when it becomes supported
349 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
350 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
351 #endif //DALI_GLES_VERSION >= 30
353 configAttribs.PushBack( EGL_RED_SIZE );
354 configAttribs.PushBack( 8 );
355 configAttribs.PushBack( EGL_GREEN_SIZE );
356 configAttribs.PushBack( 8 );
357 configAttribs.PushBack( EGL_BLUE_SIZE );
358 configAttribs.PushBack( 8 );
362 configAttribs.PushBack( EGL_ALPHA_SIZE );
364 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
365 configAttribs.PushBack( 8 );
367 // There is a bug in the desktop emulator
368 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
369 configAttribs.PushBack( 8 );
373 configAttribs.PushBack( EGL_DEPTH_SIZE );
374 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
375 configAttribs.PushBack( EGL_STENCIL_SIZE );
376 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
378 #ifndef DALI_PROFILE_UBUNTU
379 if( mMultiSamplingLevel != EGL_DONT_CARE )
381 configAttribs.PushBack( EGL_SAMPLES );
382 configAttribs.PushBack( mMultiSamplingLevel );
383 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
384 configAttribs.PushBack( 1 );
386 #endif // DALI_PROFILE_UBUNTU
387 configAttribs.PushBack( EGL_NONE );
389 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
391 EGLint error = eglGetError();
394 case EGL_BAD_DISPLAY:
396 DALI_LOG_ERROR("Display is not an EGL display connection\n");
399 case EGL_BAD_ATTRIBUTE:
401 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");
404 case EGL_NOT_INITIALIZED:
406 DALI_LOG_ERROR("Display has not been initialized\n");
409 case EGL_BAD_PARAMETER:
411 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
416 DALI_LOG_ERROR("Unknown error.\n");
419 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
422 if ( numConfigs != 1 )
424 DALI_LOG_ERROR("No configurations found.\n");
426 TEST_EGL_ERROR("eglChooseConfig");
430 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
432 mEglNativeWindow = window;
437 ChooseConfig(mIsWindow, mColorDepth);
439 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
440 TEST_EGL_ERROR("eglCreateWindowSurface");
442 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
444 return mCurrentEglSurface;
447 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
449 mCurrentEglNativePixmap = pixmap;
454 ChooseConfig(mIsWindow, mColorDepth);
456 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
457 TEST_EGL_ERROR("eglCreatePixmapSurface");
459 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
461 return mCurrentEglSurface;
464 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
466 bool contextLost = false;
468 // display connection has not changed, then we can just create a new surface
469 // the surface is bound to the context, so set the context to null
472 // destroy the surface
473 DestroySurface( eglSurface );
475 // create the EGL surface
476 CreateSurfaceWindow( window, mColorDepth );
478 // set the context to be current with the new surface
479 MakeContextCurrent( eglSurface, eglContext );
484 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
486 bool contextLost = false;
488 // display connection has not changed, then we can just create a new surface
489 // create the EGL surface
490 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
492 // set the eglSurface to be current
493 MakeCurrent( pixmap, eglSurface );
498 EGLDisplay EglImplementation::GetDisplay() const
503 EGLContext EglImplementation::GetContext() const
508 } // namespace Adaptor
510 } // namespace Internal
514 #pragma GCC diagnostic pop