2 * Copyright (c) 2017 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 <gl/egl-implementation.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/common/dali-vector.h>
28 #include <gl/gl-implementation.h>
29 #include <gl/egl-debug.h>
31 // EGL constants use C style casts
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wold-style-cast"
44 #define TEST_EGL_ERROR(lastCommand) \
46 EGLint err = eglGetError(); \
47 if (err != EGL_SUCCESS) \
49 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
50 Egl::PrintError(err); \
51 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
55 EglImplementation::EglImplementation( unsigned int multiSamplingLevel )
56 : mEglNativeDisplay(0),
58 mCurrentEglNativePixmap(0),
62 mCurrentEglSurface(0),
63 mGlesInitialized(false),
65 mContextCurrent(false),
67 mColorDepth(COLOR_DEPTH_24),
68 mMultiSamplingLevel( multiSamplingLevel )
72 EglImplementation::~EglImplementation()
77 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
79 if ( !mGlesInitialized )
81 mEglNativeDisplay = display;
83 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
84 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
85 EGLint error = eglGetError();
87 if( mEglDisplay == NULL && error != EGL_SUCCESS )
89 throw Dali::DaliException( "", "OpenGL ES is not supported");
92 EGLint majorVersion = 0;
93 EGLint minorVersion = 0;
94 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
98 eglBindAPI(EGL_OPENGL_ES_API);
100 mContextAttribs.Clear();
102 #if DALI_GLES_VERSION >= 30
104 mContextAttribs.Reserve(5);
105 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
106 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
107 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
108 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
110 #else // DALI_GLES_VERSION >= 30
112 mContextAttribs.Reserve(3);
113 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
114 mContextAttribs.PushBack( 2 );
116 #endif // DALI_GLES_VERSION >= 30
118 mContextAttribs.PushBack( EGL_NONE );
120 mGlesInitialized = true;
121 mIsOwnSurface = isOwnSurface;
124 return mGlesInitialized;
127 bool EglImplementation::CreateContext()
129 // make sure a context isn't created twice
130 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
132 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
133 TEST_EGL_ERROR("eglCreateContext render thread");
135 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
137 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
138 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
139 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
140 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
141 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
146 void EglImplementation::DestroyContext()
148 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
150 eglDestroyContext( mEglDisplay, mEglContext );
154 void EglImplementation::DestroySurface()
156 if(mIsOwnSurface && mCurrentEglSurface)
159 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
160 mCurrentEglSurface = 0;
164 void EglImplementation::MakeContextCurrent()
166 mContextCurrent = true;
170 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
173 EGLint error = eglGetError();
175 if ( error != EGL_SUCCESS )
177 Egl::PrintError(error);
179 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
182 // We want to display this information all the time, so use the LogMessage directly
183 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
188 eglQueryString(mEglDisplay, EGL_VENDOR),
189 eglQueryString(mEglDisplay, EGL_VERSION),
190 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
191 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
194 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
196 mCurrentEglNativePixmap = pixmap;
197 mCurrentEglSurface = eglSurface;
201 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
204 EGLint error = eglGetError();
206 if ( error != EGL_SUCCESS )
208 Egl::PrintError(error);
210 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
214 void EglImplementation::MakeContextNull()
216 mContextCurrent = false;
217 // clear the current context
218 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
221 void EglImplementation::TerminateGles()
223 if ( mGlesInitialized )
225 // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
226 // MakeContextNull should be called before eglDestroy surface
227 // to prevent crash in _mali_surface_destroy_callback
230 if(mIsOwnSurface && mCurrentEglSurface)
232 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
234 eglDestroyContext(mEglDisplay, mEglContext);
236 eglTerminate(mEglDisplay);
241 mCurrentEglSurface = NULL;
243 mGlesInitialized = false;
247 bool EglImplementation::IsGlesInitialized() const
249 return mGlesInitialized;
252 void EglImplementation::SwapBuffers()
254 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
257 void EglImplementation::CopyBuffers()
259 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
262 void EglImplementation::WaitGL()
267 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
269 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
274 mIsWindow = isWindowType;
277 Vector<EGLint> configAttribs;
278 configAttribs.Reserve(31);
282 configAttribs.PushBack( EGL_SURFACE_TYPE );
283 configAttribs.PushBack( EGL_WINDOW_BIT );
287 configAttribs.PushBack( EGL_SURFACE_TYPE );
288 configAttribs.PushBack( EGL_PIXMAP_BIT );
291 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
293 #if DALI_GLES_VERSION >= 30
296 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
298 // There is a bug in the desktop emulator
299 // Requesting for ES3 causes eglCreateContext even though it allows to ask
300 // for a configuration that supports GLES 3.0
301 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
304 #else // DALI_GLES_VERSION >= 30
306 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
307 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
309 #endif //DALI_GLES_VERSION >= 30
311 #if DALI_GLES_VERSION >= 30
312 // TODO: enable this flag when it becomes supported
313 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
314 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
315 #endif //DALI_GLES_VERSION >= 30
317 configAttribs.PushBack( EGL_RED_SIZE );
318 configAttribs.PushBack( 8 );
319 configAttribs.PushBack( EGL_GREEN_SIZE );
320 configAttribs.PushBack( 8 );
321 configAttribs.PushBack( EGL_BLUE_SIZE );
322 configAttribs.PushBack( 8 );
324 configAttribs.PushBack( EGL_ALPHA_SIZE );
326 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
328 // There is a bug in the desktop emulator
329 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
330 configAttribs.PushBack( 0 );
333 configAttribs.PushBack( EGL_DEPTH_SIZE );
334 configAttribs.PushBack( 24 );
335 configAttribs.PushBack( EGL_STENCIL_SIZE );
336 configAttribs.PushBack( 8 );
337 #ifndef DALI_PROFILE_UBUNTU
338 configAttribs.PushBack( EGL_SAMPLES );
339 configAttribs.PushBack( mMultiSamplingLevel );
340 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
341 configAttribs.PushBack( 1 );
342 #endif // DALI_PROFILE_UBUNTU
343 configAttribs.PushBack( EGL_NONE );
345 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
347 EGLint error = eglGetError();
350 case EGL_BAD_DISPLAY:
352 DALI_LOG_ERROR("Display is not an EGL display connection\n");
355 case EGL_BAD_ATTRIBUTE:
357 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");
360 case EGL_NOT_INITIALIZED:
362 DALI_LOG_ERROR("Display has not been initialized\n");
365 case EGL_BAD_PARAMETER:
367 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
372 DALI_LOG_ERROR("Unknown error.\n");
375 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
378 if ( numConfigs != 1 )
380 DALI_LOG_ERROR("No configurations found.\n");
382 TEST_EGL_ERROR("eglChooseConfig");
386 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
388 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
390 mEglNativeWindow = window;
395 ChooseConfig(mIsWindow, mColorDepth);
397 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
398 TEST_EGL_ERROR("eglCreateWindowSurface");
400 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
403 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
405 mCurrentEglNativePixmap = pixmap;
410 ChooseConfig(mIsWindow, mColorDepth);
412 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
413 TEST_EGL_ERROR("eglCreatePixmapSurface");
415 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
417 return mCurrentEglSurface;
420 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
422 bool contextLost = false;
424 // display connection has not changed, then we can just create a new surface
425 // the surface is bound to the context, so set the context to null
428 // destroy the surface
431 // create the EGL surface
432 CreateSurfaceWindow( window, mColorDepth );
434 // set the context to be current with the new surface
435 MakeContextCurrent();
440 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
442 bool contextLost = false;
444 // display connection has not changed, then we can just create a new surface
445 // create the EGL surface
446 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
448 // set the eglSurface to be current
449 MakeCurrent( pixmap, eglSurface );
454 EGLDisplay EglImplementation::GetDisplay() const
459 EGLDisplay EglImplementation::GetContext() const
464 } // namespace Adaptor
466 } // namespace Internal
470 #pragma GCC diagnostic pop