2 * Copyright (c) 2014 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 <ecore-x-render-surface.h>
29 #include <gl/gl-implementation.h>
34 void PrintEglError( EGLint error )
40 DALI_LOG_ERROR("EGL_BAD_DISPLAY : Display is not an EGL display connection\n");
43 case EGL_NOT_INITIALIZED:
45 DALI_LOG_ERROR("EGL_NOT_INITIALIZED : Display has not been initialized\n");
50 DALI_LOG_ERROR("EGL_BAD_SURFACE : Draw or read is not an EGL surface\n");
55 DALI_LOG_ERROR("EGL_BAD_CONTEXT : Context is not an EGL rendering context\n");
60 DALI_LOG_ERROR("EGL_BAD_MATCH : Draw or read are not compatible with context, or if context is set to EGL_NO_CONTEXT and draw or read are not set to EGL_NO_SURFACE, or if draw or read are set to EGL_NO_SURFACE and context is not set to EGL_NO_CONTEXT\n");
65 DALI_LOG_ERROR("EGL_BAD_ACCESS : Context is current to some other thread\n");
68 case EGL_BAD_NATIVE_PIXMAP:
70 DALI_LOG_ERROR("EGL_BAD_NATIVE_PIXMAP : A native pixmap underlying either draw or read is no longer valid\n");
73 case EGL_BAD_NATIVE_WINDOW:
75 DALI_LOG_ERROR("EGL_BAD_NATIVE_WINDOW : A native window underlying either draw or read is no longer valid\n");
78 case EGL_BAD_CURRENT_SURFACE:
80 DALI_LOG_ERROR("EGL_BAD_CURRENT_SURFACE : The previous context has unflushed commands and the previous surface is no longer valid\n");
85 DALI_LOG_ERROR("EGL_BAD_ALLOC : Allocation of ancillary buffers for draw or read were delayed until eglMakeCurrent is called, and there are not enough resources to allocate them\n");
88 case EGL_CONTEXT_LOST:
90 DALI_LOG_ERROR("EGL_CONTEXT_LOST : If a power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering\n");
95 DALI_LOG_ERROR("Unknown error with code: %d\n", error);
101 } // unnamed namespace
112 #define TEST_EGL_ERROR(lastCommand) \
114 EGLint err = eglGetError(); \
115 if (err != EGL_SUCCESS) \
117 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
118 PrintEglError(err); \
119 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
123 EglImplementation::EglImplementation()
124 : mEglNativeDisplay(0),
131 mGlesInitialized(false),
133 mContextCurrent(false),
135 mColorDepth(COLOR_DEPTH_24)
139 EglImplementation::~EglImplementation()
144 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
146 if ( !mGlesInitialized )
148 mEglNativeDisplay = display;
150 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
151 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
152 EGLint error = eglGetError();
154 if( mEglDisplay == NULL && error != EGL_SUCCESS )
156 throw Dali::DaliException( "", "OpenGL ES is not supported");
159 EGLint majorVersion = 0;
160 EGLint minorVersion = 0;
161 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
165 eglBindAPI(EGL_OPENGL_ES_API);
167 mContextAttribs.Clear();
169 #if DALI_GLES_VERSION >= 30
171 mContextAttribs.Reserve(5);
172 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
173 mContextAttribs.PushBack( 3 );
174 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
175 mContextAttribs.PushBack( 0 );
177 #else // DALI_GLES_VERSION >= 30
179 mContextAttribs.Reserve(3);
180 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
181 mContextAttribs.PushBack( 2 );
183 #endif // DALI_GLES_VERSION >= 30
185 mContextAttribs.PushBack( EGL_NONE );
187 mGlesInitialized = true;
188 mIsOwnSurface = isOwnSurface;
191 return mGlesInitialized;
194 bool EglImplementation::CreateContext()
196 // make sure a context isn't created twice
197 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
199 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
200 TEST_EGL_ERROR("eglCreateContext render thread");
202 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
204 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
205 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
206 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
207 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
208 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
213 void EglImplementation::DestroyContext()
215 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
217 eglDestroyContext( mEglDisplay, mEglContext );
221 void EglImplementation::DestroySurface()
223 if(mIsOwnSurface && mEglSurface)
225 eglDestroySurface( mEglDisplay, mEglSurface );
230 void EglImplementation::MakeContextCurrent()
232 mContextCurrent = true;
236 eglMakeCurrent( mEglDisplay, mEglSurface, mEglSurface, mEglContext );
239 EGLint error = eglGetError();
241 if ( error != EGL_SUCCESS )
243 PrintEglError(error);
245 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
248 // We want to display this information all the time, so use the LogMessage directly
249 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
254 eglQueryString(mEglDisplay, EGL_VENDOR),
255 eglQueryString(mEglDisplay, EGL_VERSION),
256 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
257 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
260 void EglImplementation::MakeContextNull()
262 mContextCurrent = false;
263 // clear the current context
264 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
267 void EglImplementation::TerminateGles()
269 if ( mGlesInitialized )
271 // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
272 // MakeContextNull should be called before eglDestroy surface
273 // to prevent crash in _mali_surface_destroy_callback
276 if(mIsOwnSurface && mEglSurface)
278 eglDestroySurface(mEglDisplay, mEglSurface);
280 eglDestroyContext(mEglDisplay, mEglContext);
282 eglTerminate(mEglDisplay);
289 mGlesInitialized = false;
293 bool EglImplementation::IsGlesInitialized() const
295 return mGlesInitialized;
298 void EglImplementation::SwapBuffers()
300 eglSwapBuffers( mEglDisplay, mEglSurface );
303 void EglImplementation::CopyBuffers()
305 eglCopyBuffers( mEglDisplay, mEglSurface, mEglNativePixmap );
308 void EglImplementation::WaitGL()
313 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
315 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
320 mIsWindow = isWindowType;
323 Vector<EGLint> configAttribs;
324 configAttribs.Reserve(31);
328 configAttribs.PushBack( EGL_SURFACE_TYPE );
329 configAttribs.PushBack( EGL_WINDOW_BIT );
333 configAttribs.PushBack( EGL_SURFACE_TYPE );
334 configAttribs.PushBack( EGL_PIXMAP_BIT );
337 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
339 #if DALI_GLES_VERSION >= 30
342 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
344 // There is a bug in the desktop emulator
345 // Requesting for ES3 causes eglCreateContext even though it allows to ask
346 // for a configuration that supports GLES 3.0
347 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
350 #else // DALI_GLES_VERSION >= 30
352 DALI_LOG_WARNING( "Using OpenGL ES 2 \n" );
353 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
355 #endif //DALI_GLES_VERSION >= 30
357 #if DALI_GLES_VERSION >= 30
358 // TODO: enable this flag when it becomes supported
359 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
360 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
361 #endif //DALI_GLES_VERSION >= 30
363 configAttribs.PushBack( EGL_RED_SIZE );
364 configAttribs.PushBack( 8 );
365 configAttribs.PushBack( EGL_GREEN_SIZE );
366 configAttribs.PushBack( 8 );
367 configAttribs.PushBack( EGL_BLUE_SIZE );
368 configAttribs.PushBack( 8 );
370 configAttribs.PushBack( EGL_ALPHA_SIZE );
372 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
374 // There is a bug in the desktop emulator
375 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
376 configAttribs.PushBack( 0 );
379 configAttribs.PushBack( EGL_DEPTH_SIZE );
380 configAttribs.PushBack( 24 );
381 configAttribs.PushBack( EGL_STENCIL_SIZE );
382 configAttribs.PushBack( 8 );
383 #ifndef DALI_PROFILE_UBUNTU
384 configAttribs.PushBack( EGL_SAMPLES );
385 configAttribs.PushBack( 4 );
386 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
387 configAttribs.PushBack( 1 );
388 #endif // DALI_PROFILE_UBUNTU
389 configAttribs.PushBack( EGL_NONE );
391 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
393 EGLint error = eglGetError();
396 case EGL_BAD_DISPLAY:
398 DALI_LOG_ERROR("Display is not an EGL display connection\n");
401 case EGL_BAD_ATTRIBUTE:
403 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");
406 case EGL_NOT_INITIALIZED:
408 DALI_LOG_ERROR("Display has not been initialized\n");
411 case EGL_BAD_PARAMETER:
413 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
418 DALI_LOG_ERROR("Unknown error.\n");
421 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
424 if ( numConfigs != 1 )
426 DALI_LOG_ERROR("No configurations found.\n");
428 TEST_EGL_ERROR("eglChooseConfig");
432 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
434 DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
436 mEglNativeWindow = window;
441 ChooseConfig(mIsWindow, mColorDepth);
443 mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
444 TEST_EGL_ERROR("eglCreateWindowSurface");
446 DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
449 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
451 DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
453 mEglNativePixmap = pixmap;
458 ChooseConfig(mIsWindow, mColorDepth);
460 mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
461 TEST_EGL_ERROR("eglCreatePixmapSurface");
463 DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
466 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
468 bool contextLost = false;
470 // display connection has not changed, then we can just create a new surface
471 // the surface is bound to the context, so set the context to null
474 // destroy the surface
477 // create the EGL surface
478 CreateSurfaceWindow( window, mColorDepth );
480 // set the context to be current with the new surface
481 MakeContextCurrent();
486 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap )
488 bool contextLost = false;
490 // the surface is bound to the context, so set the context to null
493 // destroy the surface
496 // display connection has not changed, then we can just create a new surface
497 // create the EGL surface
498 CreateSurfacePixmap( pixmap, mColorDepth );
500 // set the context to be current with the new surface
501 MakeContextCurrent();
506 EGLDisplay EglImplementation::GetDisplay() const
511 EGLDisplay EglImplementation::GetContext() const
516 } // namespace Adaptor
518 } // namespace Internal