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/gles20/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/gles20/gl-implementation.h>
30 #include <dali/internal/graphics/gles20/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 ),
71 mContextCurrent( false ),
73 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
74 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
78 EglImplementation::~EglImplementation()
83 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
85 if ( !mGlesInitialized )
87 mEglNativeDisplay = display;
89 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
90 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
91 EGLint error = eglGetError();
93 if( mEglDisplay == NULL && error != EGL_SUCCESS )
95 throw Dali::DaliException( "", "OpenGL ES is not supported");
98 EGLint majorVersion = 0;
99 EGLint minorVersion = 0;
100 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
104 eglBindAPI(EGL_OPENGL_ES_API);
106 mContextAttribs.Clear();
108 #if DALI_GLES_VERSION >= 30
110 mContextAttribs.Reserve(5);
111 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
112 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
113 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
114 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
116 #else // DALI_GLES_VERSION >= 30
118 mContextAttribs.Reserve(3);
119 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
120 mContextAttribs.PushBack( 2 );
122 #endif // DALI_GLES_VERSION >= 30
124 mContextAttribs.PushBack( EGL_NONE );
126 mGlesInitialized = true;
127 mIsOwnSurface = isOwnSurface;
130 return mGlesInitialized;
133 bool EglImplementation::CreateContext()
135 // make sure a context isn't created twice
136 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
138 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
139 TEST_EGL_ERROR("eglCreateContext render thread");
141 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
143 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
144 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
145 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
146 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
147 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
152 void EglImplementation::DestroyContext()
154 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
156 eglDestroyContext( mEglDisplay, mEglContext );
160 void EglImplementation::DestroySurface()
162 if(mIsOwnSurface && mCurrentEglSurface)
164 // Make context null to prevent crash in driver side
166 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
167 mCurrentEglSurface = 0;
171 void EglImplementation::MakeContextCurrent()
173 mContextCurrent = true;
177 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
180 EGLint error = eglGetError();
182 if ( error != EGL_SUCCESS )
184 Egl::PrintError(error);
186 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
189 // We want to display this information all the time, so use the LogMessage directly
190 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
195 eglQueryString(mEglDisplay, EGL_VENDOR),
196 eglQueryString(mEglDisplay, EGL_VERSION),
197 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
198 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
201 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
203 mCurrentEglNativePixmap = pixmap;
204 mCurrentEglSurface = eglSurface;
208 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
211 EGLint error = eglGetError();
213 if ( error != EGL_SUCCESS )
215 Egl::PrintError(error);
217 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
221 void EglImplementation::MakeContextNull()
223 mContextCurrent = false;
224 // clear the current context
225 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
228 void EglImplementation::TerminateGles()
230 if ( mGlesInitialized )
232 // Make context null to prevent crash in driver side
235 if(mIsOwnSurface && mCurrentEglSurface)
237 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
239 eglDestroyContext(mEglDisplay, mEglContext);
241 eglTerminate(mEglDisplay);
246 mCurrentEglSurface = NULL;
248 mGlesInitialized = false;
252 bool EglImplementation::IsGlesInitialized() const
254 return mGlesInitialized;
257 void EglImplementation::SwapBuffers()
259 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
262 void EglImplementation::CopyBuffers()
264 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
267 void EglImplementation::WaitGL()
272 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
274 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
279 mIsWindow = isWindowType;
282 Vector<EGLint> configAttribs;
283 configAttribs.Reserve(31);
287 configAttribs.PushBack( EGL_SURFACE_TYPE );
288 configAttribs.PushBack( EGL_WINDOW_BIT );
292 configAttribs.PushBack( EGL_SURFACE_TYPE );
293 configAttribs.PushBack( EGL_PIXMAP_BIT );
296 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
298 #if DALI_GLES_VERSION >= 30
301 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
303 // There is a bug in the desktop emulator
304 // Requesting for ES3 causes eglCreateContext even though it allows to ask
305 // for a configuration that supports GLES 3.0
306 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
309 #else // DALI_GLES_VERSION >= 30
311 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
312 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
314 #endif //DALI_GLES_VERSION >= 30
316 #if DALI_GLES_VERSION >= 30
317 // TODO: enable this flag when it becomes supported
318 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
319 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
320 #endif //DALI_GLES_VERSION >= 30
322 configAttribs.PushBack( EGL_RED_SIZE );
323 configAttribs.PushBack( 8 );
324 configAttribs.PushBack( EGL_GREEN_SIZE );
325 configAttribs.PushBack( 8 );
326 configAttribs.PushBack( EGL_BLUE_SIZE );
327 configAttribs.PushBack( 8 );
329 configAttribs.PushBack( EGL_ALPHA_SIZE );
331 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
332 configAttribs.PushBack( 8 );
334 // There is a bug in the desktop emulator
335 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
336 configAttribs.PushBack( 0 );
339 configAttribs.PushBack( EGL_DEPTH_SIZE );
340 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
341 configAttribs.PushBack( EGL_STENCIL_SIZE );
342 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
344 #ifndef DALI_PROFILE_UBUNTU
345 if( mMultiSamplingLevel != EGL_DONT_CARE )
347 configAttribs.PushBack( EGL_SAMPLES );
348 configAttribs.PushBack( mMultiSamplingLevel );
349 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
350 configAttribs.PushBack( 1 );
352 #endif // DALI_PROFILE_UBUNTU
353 configAttribs.PushBack( EGL_NONE );
355 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
357 EGLint error = eglGetError();
360 case EGL_BAD_DISPLAY:
362 DALI_LOG_ERROR("Display is not an EGL display connection\n");
365 case EGL_BAD_ATTRIBUTE:
367 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");
370 case EGL_NOT_INITIALIZED:
372 DALI_LOG_ERROR("Display has not been initialized\n");
375 case EGL_BAD_PARAMETER:
377 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
382 DALI_LOG_ERROR("Unknown error.\n");
385 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
388 if ( numConfigs != 1 )
390 DALI_LOG_ERROR("No configurations found.\n");
392 TEST_EGL_ERROR("eglChooseConfig");
396 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
398 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
400 mEglNativeWindow = window;
405 ChooseConfig(mIsWindow, mColorDepth);
407 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
408 TEST_EGL_ERROR("eglCreateWindowSurface");
410 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
413 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
415 mCurrentEglNativePixmap = pixmap;
420 ChooseConfig(mIsWindow, mColorDepth);
422 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
423 TEST_EGL_ERROR("eglCreatePixmapSurface");
425 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
427 return mCurrentEglSurface;
430 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
432 bool contextLost = false;
434 // display connection has not changed, then we can just create a new surface
435 // the surface is bound to the context, so set the context to null
438 // destroy the surface
441 // create the EGL surface
442 CreateSurfaceWindow( window, mColorDepth );
444 // set the context to be current with the new surface
445 MakeContextCurrent();
450 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
452 bool contextLost = false;
454 // display connection has not changed, then we can just create a new surface
455 // create the EGL surface
456 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
458 // set the eglSurface to be current
459 MakeCurrent( pixmap, eglSurface );
464 EGLDisplay EglImplementation::GetDisplay() const
469 EGLDisplay EglImplementation::GetContext() const
474 } // namespace Adaptor
476 } // namespace Internal
480 #pragma GCC diagnostic pop