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>
40 #define TEST_EGL_ERROR(lastCommand) \
42 EGLint err = eglGetError(); \
43 if (err != EGL_SUCCESS) \
45 DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
46 Egl::PrintError(err); \
47 DALI_ASSERT_ALWAYS(0 && "EGL error"); \
51 EglImplementation::EglImplementation()
52 : mEglNativeDisplay(0),
54 mCurrentEglNativePixmap(0),
58 mCurrentEglSurface(0),
59 mGlesInitialized(false),
61 mContextCurrent(false),
63 mColorDepth(COLOR_DEPTH_24)
67 EglImplementation::~EglImplementation()
72 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
74 if ( !mGlesInitialized )
76 mEglNativeDisplay = display;
78 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
79 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
80 EGLint error = eglGetError();
82 if( mEglDisplay == NULL && error != EGL_SUCCESS )
84 throw Dali::DaliException( "", "OpenGL ES is not supported");
87 EGLint majorVersion = 0;
88 EGLint minorVersion = 0;
89 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
93 eglBindAPI(EGL_OPENGL_ES_API);
95 mContextAttribs.Clear();
97 #if DALI_GLES_VERSION >= 30
99 mContextAttribs.Reserve(5);
100 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
101 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
102 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
103 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
105 #else // DALI_GLES_VERSION >= 30
107 mContextAttribs.Reserve(3);
108 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
109 mContextAttribs.PushBack( 2 );
111 #endif // DALI_GLES_VERSION >= 30
113 mContextAttribs.PushBack( EGL_NONE );
115 mGlesInitialized = true;
116 mIsOwnSurface = isOwnSurface;
119 return mGlesInitialized;
122 bool EglImplementation::CreateContext()
124 // make sure a context isn't created twice
125 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
127 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
128 TEST_EGL_ERROR("eglCreateContext render thread");
130 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
132 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
133 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
134 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
135 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
136 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
141 void EglImplementation::DestroyContext()
143 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
145 eglDestroyContext( mEglDisplay, mEglContext );
149 void EglImplementation::DestroySurface()
151 if(mIsOwnSurface && mCurrentEglSurface)
153 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
154 mCurrentEglSurface = 0;
158 void EglImplementation::MakeContextCurrent()
160 mContextCurrent = true;
164 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
167 EGLint error = eglGetError();
169 if ( error != EGL_SUCCESS )
171 Egl::PrintError(error);
173 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
176 // We want to display this information all the time, so use the LogMessage directly
177 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
182 eglQueryString(mEglDisplay, EGL_VENDOR),
183 eglQueryString(mEglDisplay, EGL_VERSION),
184 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
185 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
188 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
190 mCurrentEglNativePixmap = pixmap;
191 mCurrentEglSurface = eglSurface;
195 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
198 EGLint error = eglGetError();
200 if ( error != EGL_SUCCESS )
202 Egl::PrintError(error);
204 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
208 void EglImplementation::MakeContextNull()
210 mContextCurrent = false;
211 // clear the current context
212 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
215 void EglImplementation::TerminateGles()
217 if ( mGlesInitialized )
219 // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
220 // MakeContextNull should be called before eglDestroy surface
221 // to prevent crash in _mali_surface_destroy_callback
224 if(mIsOwnSurface && mCurrentEglSurface)
226 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
228 eglDestroyContext(mEglDisplay, mEglContext);
230 eglTerminate(mEglDisplay);
235 mCurrentEglSurface = NULL;
237 mGlesInitialized = false;
241 bool EglImplementation::IsGlesInitialized() const
243 return mGlesInitialized;
246 void EglImplementation::SwapBuffers()
248 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
251 void EglImplementation::CopyBuffers()
253 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
256 void EglImplementation::WaitGL()
261 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
263 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
268 mIsWindow = isWindowType;
271 Vector<EGLint> configAttribs;
272 configAttribs.Reserve(31);
276 configAttribs.PushBack( EGL_SURFACE_TYPE );
277 configAttribs.PushBack( EGL_WINDOW_BIT );
281 configAttribs.PushBack( EGL_SURFACE_TYPE );
282 configAttribs.PushBack( EGL_PIXMAP_BIT );
285 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
287 #if DALI_GLES_VERSION >= 30
290 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
292 // There is a bug in the desktop emulator
293 // Requesting for ES3 causes eglCreateContext even though it allows to ask
294 // for a configuration that supports GLES 3.0
295 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
298 #else // DALI_GLES_VERSION >= 30
300 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
301 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
303 #endif //DALI_GLES_VERSION >= 30
305 #if DALI_GLES_VERSION >= 30
306 // TODO: enable this flag when it becomes supported
307 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
308 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
309 #endif //DALI_GLES_VERSION >= 30
311 configAttribs.PushBack( EGL_RED_SIZE );
312 configAttribs.PushBack( 8 );
313 configAttribs.PushBack( EGL_GREEN_SIZE );
314 configAttribs.PushBack( 8 );
315 configAttribs.PushBack( EGL_BLUE_SIZE );
316 configAttribs.PushBack( 8 );
318 configAttribs.PushBack( EGL_ALPHA_SIZE );
320 configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
322 // There is a bug in the desktop emulator
323 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
324 configAttribs.PushBack( 0 );
327 configAttribs.PushBack( EGL_DEPTH_SIZE );
328 configAttribs.PushBack( 24 );
329 configAttribs.PushBack( EGL_STENCIL_SIZE );
330 configAttribs.PushBack( 8 );
331 #ifndef DALI_PROFILE_UBUNTU
332 configAttribs.PushBack( EGL_SAMPLES );
333 configAttribs.PushBack( 4 );
334 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
335 configAttribs.PushBack( 1 );
336 #endif // DALI_PROFILE_UBUNTU
337 configAttribs.PushBack( EGL_NONE );
339 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
341 EGLint error = eglGetError();
344 case EGL_BAD_DISPLAY:
346 DALI_LOG_ERROR("Display is not an EGL display connection\n");
349 case EGL_BAD_ATTRIBUTE:
351 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");
354 case EGL_NOT_INITIALIZED:
356 DALI_LOG_ERROR("Display has not been initialized\n");
359 case EGL_BAD_PARAMETER:
361 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
366 DALI_LOG_ERROR("Unknown error.\n");
369 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
372 if ( numConfigs != 1 )
374 DALI_LOG_ERROR("No configurations found.\n");
376 TEST_EGL_ERROR("eglChooseConfig");
380 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
382 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
384 mEglNativeWindow = window;
389 ChooseConfig(mIsWindow, mColorDepth);
391 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
392 TEST_EGL_ERROR("eglCreateWindowSurface");
394 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
397 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
399 mCurrentEglNativePixmap = pixmap;
404 ChooseConfig(mIsWindow, mColorDepth);
406 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
407 TEST_EGL_ERROR("eglCreatePixmapSurface");
409 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
411 return mCurrentEglSurface;
414 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
416 bool contextLost = false;
418 // display connection has not changed, then we can just create a new surface
419 // the surface is bound to the context, so set the context to null
422 // destroy the surface
425 // create the EGL surface
426 CreateSurfaceWindow( window, mColorDepth );
428 // set the context to be current with the new surface
429 MakeContextCurrent();
434 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
436 bool contextLost = false;
438 // display connection has not changed, then we can just create a new surface
439 // create the EGL surface
440 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
442 // set the eglSurface to be current
443 MakeCurrent( pixmap, eglSurface );
448 EGLDisplay EglImplementation::GetDisplay() const
453 EGLDisplay EglImplementation::GetContext() const
458 } // namespace Adaptor
460 } // namespace Internal