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>
40 #define TEST_EGL_ERROR(lastCommand) \
42 EGLint err = eglGetError(); \
43 if (err != EGL_SUCCESS) \
45 DALI_LOG_ERROR("EGL error after %s code=%d\n", lastCommand,err); \
46 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
50 EglImplementation::EglImplementation()
51 : mEglNativeDisplay(0),
58 mGlesInitialized(false),
60 mContextCurrent(false),
62 mColorDepth(COLOR_DEPTH_24)
66 EglImplementation::~EglImplementation()
71 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
73 if ( !mGlesInitialized )
75 mEglNativeDisplay = display;
77 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
78 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
79 EGLint error = eglGetError();
81 if( mEglDisplay == NULL && error != EGL_SUCCESS )
83 throw Dali::DaliException( "", "OpenGL ES is not supported");
86 EGLint majorVersion = 0;
87 EGLint minorVersion = 0;
88 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
92 eglBindAPI(EGL_OPENGL_ES_API);
94 mContextAttribs.Clear();
96 #if DALI_GLES_VERSION >= 30
98 mContextAttribs.Reserve(5);
99 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
100 mContextAttribs.PushBack( 3 );
101 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
102 mContextAttribs.PushBack( 0 );
104 #else // DALI_GLES_VERSION >= 30
106 mContextAttribs.Reserve(3);
107 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
108 mContextAttribs.PushBack( 2 );
110 #endif // DALI_GLES_VERSION >= 30
112 mContextAttribs.PushBack( EGL_NONE );
114 mGlesInitialized = true;
115 mIsOwnSurface = isOwnSurface;
118 return mGlesInitialized;
121 bool EglImplementation::CreateContext()
123 // make sure a context isn't created twice
124 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
126 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
127 TEST_EGL_ERROR("eglCreateContext render thread");
129 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
131 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
132 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
133 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
134 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
135 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
140 void EglImplementation::DestroyContext()
142 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
144 eglDestroyContext( mEglDisplay, mEglContext );
148 void EglImplementation::DestroySurface()
150 if(mIsOwnSurface && mEglSurface)
152 eglDestroySurface( mEglDisplay, mEglSurface );
157 void EglImplementation::MakeContextCurrent()
159 mContextCurrent = true;
163 eglMakeCurrent( mEglDisplay, mEglSurface, mEglSurface, mEglContext );
166 EGLint error = eglGetError();
168 if ( error != EGL_SUCCESS )
172 case EGL_BAD_DISPLAY:
174 DALI_LOG_ERROR("EGL_BAD_DISPLAY : Display is not an EGL display connection");
177 case EGL_NOT_INITIALIZED:
179 DALI_LOG_ERROR("EGL_NOT_INITIALIZED : Display has not been initialized");
182 case EGL_BAD_SURFACE:
184 DALI_LOG_ERROR("EGL_BAD_SURFACE : Draw or read is not an EGL surface");
187 case EGL_BAD_CONTEXT:
189 DALI_LOG_ERROR("EGL_BAD_CONTEXT : Context is not an EGL rendering context");
194 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");
199 DALI_LOG_ERROR("EGL_BAD_ACCESS : Context is current to some other thread");
202 case EGL_BAD_NATIVE_PIXMAP:
204 DALI_LOG_ERROR("EGL_BAD_NATIVE_PIXMAP : A native pixmap underlying either draw or read is no longer valid.");
207 case EGL_BAD_NATIVE_WINDOW:
209 DALI_LOG_ERROR("EGL_BAD_NATIVE_WINDOW : A native window underlying either draw or read is no longer valid.");
212 case EGL_BAD_CURRENT_SURFACE:
214 DALI_LOG_ERROR("EGL_BAD_CURRENT_SURFACE : The previous context has unflushed commands and the previous surface is no longer valid.");
219 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");
222 case EGL_CONTEXT_LOST:
224 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");
229 DALI_LOG_ERROR("Unknown error");
233 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
236 // We want to display this information all the time, so use the LogMessage directly
237 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
242 eglQueryString(mEglDisplay, EGL_VENDOR),
243 eglQueryString(mEglDisplay, EGL_VERSION),
244 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
245 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
248 void EglImplementation::MakeContextNull()
250 mContextCurrent = false;
251 // clear the current context
252 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
255 void EglImplementation::TerminateGles()
257 if ( mGlesInitialized )
259 // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
260 // MakeContextNull should be called before eglDestroy surface
261 // to prevent crash in _mali_surface_destroy_callback
264 if(mIsOwnSurface && mEglSurface)
266 eglDestroySurface(mEglDisplay, mEglSurface);
268 eglDestroyContext(mEglDisplay, mEglContext);
270 eglTerminate(mEglDisplay);
277 mGlesInitialized = false;
281 bool EglImplementation::IsGlesInitialized() const
283 return mGlesInitialized;
286 void EglImplementation::SwapBuffers()
288 eglSwapBuffers( mEglDisplay, mEglSurface );
291 void EglImplementation::CopyBuffers()
293 eglCopyBuffers( mEglDisplay, mEglSurface, mEglNativePixmap );
296 void EglImplementation::WaitGL()
301 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
303 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
308 mIsWindow = isWindowType;
311 Vector<EGLint> configAttribs;
312 configAttribs.Reserve(31);
316 configAttribs.PushBack( EGL_SURFACE_TYPE );
317 configAttribs.PushBack( EGL_WINDOW_BIT );
321 configAttribs.PushBack( EGL_SURFACE_TYPE );
322 configAttribs.PushBack( EGL_PIXMAP_BIT );
325 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
327 #if DALI_GLES_VERSION >= 30
330 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
332 // There is a bug in the desktop emulator
333 // Requesting for ES3 causes eglCreateContext even though it allows to ask
334 // for a configuration that supports GLES 3.0
335 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
338 #else // DALI_GLES_VERSION >= 30
340 DALI_LOG_WARNING( "Using OpenGL ES 2 \n" );
341 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
343 #endif //DALI_GLES_VERSION >= 30
345 #if DALI_GLES_VERSION >= 30
346 // TODO: enable this flag when it becomes supported
347 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
348 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
349 #endif //DALI_GLES_VERSION >= 30
351 configAttribs.PushBack( EGL_RED_SIZE );
352 configAttribs.PushBack( 8 );
353 configAttribs.PushBack( EGL_GREEN_SIZE );
354 configAttribs.PushBack( 8 );
355 configAttribs.PushBack( EGL_BLUE_SIZE );
356 configAttribs.PushBack( 8 );
358 configAttribs.PushBack( EGL_ALPHA_SIZE );
360 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
362 // There is a bug in the desktop emulator
363 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
364 configAttribs.PushBack( 0 );
367 configAttribs.PushBack( EGL_DEPTH_SIZE );
368 configAttribs.PushBack( 24 );
369 configAttribs.PushBack( EGL_STENCIL_SIZE );
370 configAttribs.PushBack( 8 );
371 #ifndef DALI_PROFILE_UBUNTU
372 configAttribs.PushBack( EGL_SAMPLES );
373 configAttribs.PushBack( 4 );
374 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
375 configAttribs.PushBack( 1 );
376 #endif // DALI_PROFILE_UBUNTU
377 configAttribs.PushBack( EGL_NONE );
379 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
381 EGLint error = eglGetError();
384 case EGL_BAD_DISPLAY:
386 DALI_LOG_ERROR("Display is not an EGL display connection\n");
389 case EGL_BAD_ATTRIBUTE:
391 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");
394 case EGL_NOT_INITIALIZED:
396 DALI_LOG_ERROR("Display has not been initialized\n");
399 case EGL_BAD_PARAMETER:
401 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
406 DALI_LOG_ERROR("Unknown error.\n");
409 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
412 if ( numConfigs != 1 )
414 DALI_LOG_ERROR("No configurations found.\n");
416 TEST_EGL_ERROR("eglChooseConfig");
420 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
422 DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
424 mEglNativeWindow = window;
429 ChooseConfig(mIsWindow, mColorDepth);
431 mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
432 TEST_EGL_ERROR("eglCreateWindowSurface");
434 DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
437 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
439 DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
441 mEglNativePixmap = pixmap;
446 ChooseConfig(mIsWindow, mColorDepth);
448 mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
449 TEST_EGL_ERROR("eglCreatePixmapSurface");
451 DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
454 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
456 bool contextLost = false;
458 // display connection has not changed, then we can just create a new surface
459 // the surface is bound to the context, so set the context to null
462 // destroy the surface
465 // create the EGL surface
466 CreateSurfaceWindow( window, mColorDepth );
468 // set the context to be current with the new surface
469 MakeContextCurrent();
474 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap )
476 bool contextLost = false;
478 // the surface is bound to the context, so set the context to null
481 // destroy the surface
484 // display connection has not changed, then we can just create a new surface
485 // create the EGL surface
486 CreateSurfacePixmap( pixmap, mColorDepth );
488 // set the context to be current with the new surface
489 MakeContextCurrent();
494 EGLDisplay EglImplementation::GetDisplay() const
499 EGLDisplay EglImplementation::GetContext() const
504 } // namespace Adaptor
506 } // namespace Internal