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()
56 : mEglNativeDisplay(0),
58 mCurrentEglNativePixmap(0),
62 mCurrentEglSurface(0),
63 mGlesInitialized(false),
65 mContextCurrent(false),
67 mColorDepth(COLOR_DEPTH_24)
71 EglImplementation::~EglImplementation()
76 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
78 if ( !mGlesInitialized )
80 mEglNativeDisplay = display;
82 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
83 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
84 EGLint error = eglGetError();
86 if( mEglDisplay == NULL && error != EGL_SUCCESS )
88 throw Dali::DaliException( "", "OpenGL ES is not supported");
91 EGLint majorVersion = 0;
92 EGLint minorVersion = 0;
93 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
97 eglBindAPI(EGL_OPENGL_ES_API);
99 mContextAttribs.Clear();
101 #if DALI_GLES_VERSION >= 30
103 mContextAttribs.Reserve(5);
104 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
105 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
106 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
107 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
109 #else // DALI_GLES_VERSION >= 30
111 mContextAttribs.Reserve(3);
112 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
113 mContextAttribs.PushBack( 2 );
115 #endif // DALI_GLES_VERSION >= 30
117 mContextAttribs.PushBack( EGL_NONE );
119 mGlesInitialized = true;
120 mIsOwnSurface = isOwnSurface;
123 return mGlesInitialized;
126 bool EglImplementation::CreateContext()
128 // make sure a context isn't created twice
129 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
131 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
132 TEST_EGL_ERROR("eglCreateContext render thread");
134 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
136 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
137 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
138 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
139 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
140 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
145 void EglImplementation::DestroyContext()
147 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
149 eglDestroyContext( mEglDisplay, mEglContext );
153 void EglImplementation::DestroySurface()
155 if(mIsOwnSurface && mCurrentEglSurface)
157 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
158 mCurrentEglSurface = 0;
162 void EglImplementation::MakeContextCurrent()
164 mContextCurrent = true;
168 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
171 EGLint error = eglGetError();
173 if ( error != EGL_SUCCESS )
175 Egl::PrintError(error);
177 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
180 // We want to display this information all the time, so use the LogMessage directly
181 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
186 eglQueryString(mEglDisplay, EGL_VENDOR),
187 eglQueryString(mEglDisplay, EGL_VERSION),
188 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
189 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
192 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
194 mCurrentEglNativePixmap = pixmap;
195 mCurrentEglSurface = eglSurface;
199 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
202 EGLint error = eglGetError();
204 if ( error != EGL_SUCCESS )
206 Egl::PrintError(error);
208 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
212 void EglImplementation::MakeContextNull()
214 mContextCurrent = false;
215 // clear the current context
216 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
219 void EglImplementation::TerminateGles()
221 if ( mGlesInitialized )
223 // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
224 // MakeContextNull should be called before eglDestroy surface
225 // to prevent crash in _mali_surface_destroy_callback
228 if(mIsOwnSurface && mCurrentEglSurface)
230 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
232 eglDestroyContext(mEglDisplay, mEglContext);
234 eglTerminate(mEglDisplay);
239 mCurrentEglSurface = NULL;
241 mGlesInitialized = false;
245 bool EglImplementation::IsGlesInitialized() const
247 return mGlesInitialized;
250 void EglImplementation::SwapBuffers()
252 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
255 void EglImplementation::CopyBuffers()
257 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
260 void EglImplementation::WaitGL()
265 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
267 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
272 mIsWindow = isWindowType;
275 Vector<EGLint> configAttribs;
276 configAttribs.Reserve(31);
280 configAttribs.PushBack( EGL_SURFACE_TYPE );
281 configAttribs.PushBack( EGL_WINDOW_BIT );
285 configAttribs.PushBack( EGL_SURFACE_TYPE );
286 configAttribs.PushBack( EGL_PIXMAP_BIT );
289 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
291 #if DALI_GLES_VERSION >= 30
294 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
296 // There is a bug in the desktop emulator
297 // Requesting for ES3 causes eglCreateContext even though it allows to ask
298 // for a configuration that supports GLES 3.0
299 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
302 #else // DALI_GLES_VERSION >= 30
304 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
305 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
307 #endif //DALI_GLES_VERSION >= 30
309 #if DALI_GLES_VERSION >= 30
310 // TODO: enable this flag when it becomes supported
311 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
312 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
313 #endif //DALI_GLES_VERSION >= 30
315 configAttribs.PushBack( EGL_RED_SIZE );
316 configAttribs.PushBack( 8 );
317 configAttribs.PushBack( EGL_GREEN_SIZE );
318 configAttribs.PushBack( 8 );
319 configAttribs.PushBack( EGL_BLUE_SIZE );
320 configAttribs.PushBack( 8 );
322 configAttribs.PushBack( EGL_ALPHA_SIZE );
324 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
326 // There is a bug in the desktop emulator
327 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
328 configAttribs.PushBack( 0 );
331 configAttribs.PushBack( EGL_DEPTH_SIZE );
332 configAttribs.PushBack( 24 );
333 configAttribs.PushBack( EGL_STENCIL_SIZE );
334 configAttribs.PushBack( 8 );
335 #ifndef DALI_PROFILE_UBUNTU
336 configAttribs.PushBack( EGL_SAMPLES );
337 configAttribs.PushBack( 4 );
338 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
339 configAttribs.PushBack( 1 );
340 #endif // DALI_PROFILE_UBUNTU
341 configAttribs.PushBack( EGL_NONE );
343 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
345 EGLint error = eglGetError();
348 case EGL_BAD_DISPLAY:
350 DALI_LOG_ERROR("Display is not an EGL display connection\n");
353 case EGL_BAD_ATTRIBUTE:
355 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");
358 case EGL_NOT_INITIALIZED:
360 DALI_LOG_ERROR("Display has not been initialized\n");
363 case EGL_BAD_PARAMETER:
365 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
370 DALI_LOG_ERROR("Unknown error.\n");
373 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
376 if ( numConfigs != 1 )
378 DALI_LOG_ERROR("No configurations found.\n");
380 TEST_EGL_ERROR("eglChooseConfig");
384 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
386 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
388 mEglNativeWindow = window;
393 ChooseConfig(mIsWindow, mColorDepth);
395 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
396 TEST_EGL_ERROR("eglCreateWindowSurface");
398 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
401 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
403 mCurrentEglNativePixmap = pixmap;
408 ChooseConfig(mIsWindow, mColorDepth);
410 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
411 TEST_EGL_ERROR("eglCreatePixmapSurface");
413 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
415 return mCurrentEglSurface;
418 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
420 bool contextLost = false;
422 // display connection has not changed, then we can just create a new surface
423 // the surface is bound to the context, so set the context to null
426 // destroy the surface
429 // create the EGL surface
430 CreateSurfaceWindow( window, mColorDepth );
432 // set the context to be current with the new surface
433 MakeContextCurrent();
438 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
440 bool contextLost = false;
442 // display connection has not changed, then we can just create a new surface
443 // create the EGL surface
444 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
446 // set the eglSurface to be current
447 MakeCurrent( pixmap, eglSurface );
452 EGLDisplay EglImplementation::GetDisplay() const
457 EGLDisplay EglImplementation::GetContext() const
462 } // namespace Adaptor
464 } // namespace Internal
468 #pragma GCC diagnostic pop