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)
158 // Make context null to prevent crash in driver side
160 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
161 mCurrentEglSurface = 0;
165 void EglImplementation::MakeContextCurrent()
167 mContextCurrent = true;
171 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
174 EGLint error = eglGetError();
176 if ( error != EGL_SUCCESS )
178 Egl::PrintError(error);
180 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
183 // We want to display this information all the time, so use the LogMessage directly
184 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
189 eglQueryString(mEglDisplay, EGL_VENDOR),
190 eglQueryString(mEglDisplay, EGL_VERSION),
191 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
192 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
195 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
197 mCurrentEglNativePixmap = pixmap;
198 mCurrentEglSurface = eglSurface;
202 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
205 EGLint error = eglGetError();
207 if ( error != EGL_SUCCESS )
209 Egl::PrintError(error);
211 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
215 void EglImplementation::MakeContextNull()
217 mContextCurrent = false;
218 // clear the current context
219 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
222 void EglImplementation::TerminateGles()
224 if ( mGlesInitialized )
226 // Make context null to prevent crash in driver side
229 if(mIsOwnSurface && mCurrentEglSurface)
231 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
233 eglDestroyContext(mEglDisplay, mEglContext);
235 eglTerminate(mEglDisplay);
240 mCurrentEglSurface = NULL;
242 mGlesInitialized = false;
246 bool EglImplementation::IsGlesInitialized() const
248 return mGlesInitialized;
251 void EglImplementation::SwapBuffers()
253 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
256 void EglImplementation::CopyBuffers()
258 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
261 void EglImplementation::WaitGL()
266 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
268 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
273 mIsWindow = isWindowType;
276 Vector<EGLint> configAttribs;
277 configAttribs.Reserve(31);
281 configAttribs.PushBack( EGL_SURFACE_TYPE );
282 configAttribs.PushBack( EGL_WINDOW_BIT );
286 configAttribs.PushBack( EGL_SURFACE_TYPE );
287 configAttribs.PushBack( EGL_PIXMAP_BIT );
290 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
292 #if DALI_GLES_VERSION >= 30
295 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
297 // There is a bug in the desktop emulator
298 // Requesting for ES3 causes eglCreateContext even though it allows to ask
299 // for a configuration that supports GLES 3.0
300 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
303 #else // DALI_GLES_VERSION >= 30
305 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
306 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
308 #endif //DALI_GLES_VERSION >= 30
310 #if DALI_GLES_VERSION >= 30
311 // TODO: enable this flag when it becomes supported
312 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
313 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
314 #endif //DALI_GLES_VERSION >= 30
316 configAttribs.PushBack( EGL_RED_SIZE );
317 configAttribs.PushBack( 8 );
318 configAttribs.PushBack( EGL_GREEN_SIZE );
319 configAttribs.PushBack( 8 );
320 configAttribs.PushBack( EGL_BLUE_SIZE );
321 configAttribs.PushBack( 8 );
323 configAttribs.PushBack( EGL_ALPHA_SIZE );
325 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
327 // There is a bug in the desktop emulator
328 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
329 configAttribs.PushBack( 0 );
332 configAttribs.PushBack( EGL_DEPTH_SIZE );
333 configAttribs.PushBack( 24 );
334 configAttribs.PushBack( EGL_STENCIL_SIZE );
335 configAttribs.PushBack( 8 );
336 #ifndef DALI_PROFILE_UBUNTU
337 configAttribs.PushBack( EGL_SAMPLES );
338 configAttribs.PushBack( mMultiSamplingLevel );
339 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
340 configAttribs.PushBack( 1 );
341 #endif // DALI_PROFILE_UBUNTU
342 configAttribs.PushBack( EGL_NONE );
344 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
346 EGLint error = eglGetError();
349 case EGL_BAD_DISPLAY:
351 DALI_LOG_ERROR("Display is not an EGL display connection\n");
354 case EGL_BAD_ATTRIBUTE:
356 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");
359 case EGL_NOT_INITIALIZED:
361 DALI_LOG_ERROR("Display has not been initialized\n");
364 case EGL_BAD_PARAMETER:
366 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
371 DALI_LOG_ERROR("Unknown error.\n");
374 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
377 if ( numConfigs != 1 )
379 DALI_LOG_ERROR("No configurations found.\n");
381 TEST_EGL_ERROR("eglChooseConfig");
385 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
387 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
389 mEglNativeWindow = window;
394 ChooseConfig(mIsWindow, mColorDepth);
396 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
397 TEST_EGL_ERROR("eglCreateWindowSurface");
399 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
402 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
404 mCurrentEglNativePixmap = pixmap;
409 ChooseConfig(mIsWindow, mColorDepth);
411 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
412 TEST_EGL_ERROR("eglCreatePixmapSurface");
414 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
416 return mCurrentEglSurface;
419 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
421 bool contextLost = false;
423 // display connection has not changed, then we can just create a new surface
424 // the surface is bound to the context, so set the context to null
427 // destroy the surface
430 // create the EGL surface
431 CreateSurfaceWindow( window, mColorDepth );
433 // set the context to be current with the new surface
434 MakeContextCurrent();
439 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
441 bool contextLost = false;
443 // display connection has not changed, then we can just create a new surface
444 // create the EGL surface
445 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
447 // set the eglSurface to be current
448 MakeCurrent( pixmap, eglSurface );
453 EGLDisplay EglImplementation::GetDisplay() const
458 EGLDisplay EglImplementation::GetContext() const
463 } // namespace Adaptor
465 } // namespace Internal
469 #pragma GCC diagnostic pop