2 * Copyright (c) 2018 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 <dali/internal/graphics/gles20/egl-implementation.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/dali-adaptor-common.h>
28 #include <dali/internal/graphics/gles20/gl-implementation.h>
29 #include <dali/internal/graphics/gles20/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( int multiSamplingLevel,
56 Integration::DepthBufferAvailable depthBufferRequired,
57 Integration::StencilBufferAvailable stencilBufferRequired )
59 mEglNativeDisplay( 0 ),
60 mEglNativeWindow( 0 ),
61 mCurrentEglNativePixmap( 0 ),
65 mCurrentEglSurface( 0 ),
66 mMultiSamplingLevel( multiSamplingLevel ),
67 mColorDepth( COLOR_DEPTH_24 ),
68 mGlesInitialized( false ),
69 mIsOwnSurface( true ),
70 mContextCurrent( false ),
72 mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
73 mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
77 EglImplementation::~EglImplementation()
82 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
84 if ( !mGlesInitialized )
86 mEglNativeDisplay = display;
88 //@todo see if we can just EGL_DEFAULT_DISPLAY instead
89 mEglDisplay = eglGetDisplay(mEglNativeDisplay);
90 EGLint error = eglGetError();
92 if( mEglDisplay == NULL && error != EGL_SUCCESS )
94 throw Dali::DaliException( "", "OpenGL ES is not supported");
97 EGLint majorVersion = 0;
98 EGLint minorVersion = 0;
99 if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
103 eglBindAPI(EGL_OPENGL_ES_API);
105 mContextAttribs.Clear();
107 #if DALI_GLES_VERSION >= 30
109 mContextAttribs.Reserve(5);
110 mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
111 mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
112 mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
113 mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
115 #else // DALI_GLES_VERSION >= 30
117 mContextAttribs.Reserve(3);
118 mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
119 mContextAttribs.PushBack( 2 );
121 #endif // DALI_GLES_VERSION >= 30
123 mContextAttribs.PushBack( EGL_NONE );
125 mGlesInitialized = true;
126 mIsOwnSurface = isOwnSurface;
129 return mGlesInitialized;
132 bool EglImplementation::CreateContext()
134 // make sure a context isn't created twice
135 DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
137 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
138 TEST_EGL_ERROR("eglCreateContext render thread");
140 DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
142 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
143 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
144 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
145 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
146 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
151 void EglImplementation::DestroyContext()
153 DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
155 eglDestroyContext( mEglDisplay, mEglContext );
159 void EglImplementation::DestroySurface()
161 if(mIsOwnSurface && mCurrentEglSurface)
163 // Make context null to prevent crash in driver side
165 eglDestroySurface( mEglDisplay, mCurrentEglSurface );
166 mCurrentEglSurface = 0;
170 void EglImplementation::MakeContextCurrent()
172 mContextCurrent = true;
176 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
179 EGLint error = eglGetError();
181 if ( error != EGL_SUCCESS )
183 Egl::PrintError(error);
185 DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
188 // We want to display this information all the time, so use the LogMessage directly
189 Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
194 eglQueryString(mEglDisplay, EGL_VENDOR),
195 eglQueryString(mEglDisplay, EGL_VERSION),
196 eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
197 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
200 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
202 mCurrentEglNativePixmap = pixmap;
203 mCurrentEglSurface = eglSurface;
207 eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
210 EGLint error = eglGetError();
212 if ( error != EGL_SUCCESS )
214 Egl::PrintError(error);
216 DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
220 void EglImplementation::MakeContextNull()
222 mContextCurrent = false;
223 // clear the current context
224 eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
227 void EglImplementation::TerminateGles()
229 if ( mGlesInitialized )
231 // Make context null to prevent crash in driver side
234 if(mIsOwnSurface && mCurrentEglSurface)
236 eglDestroySurface(mEglDisplay, mCurrentEglSurface);
238 eglDestroyContext(mEglDisplay, mEglContext);
240 eglTerminate(mEglDisplay);
245 mCurrentEglSurface = NULL;
247 mGlesInitialized = false;
251 bool EglImplementation::IsGlesInitialized() const
253 return mGlesInitialized;
256 void EglImplementation::SwapBuffers()
258 eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
261 void EglImplementation::CopyBuffers()
263 eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
266 void EglImplementation::WaitGL()
271 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
273 if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
278 mIsWindow = isWindowType;
281 Vector<EGLint> configAttribs;
282 configAttribs.Reserve(31);
286 configAttribs.PushBack( EGL_SURFACE_TYPE );
287 configAttribs.PushBack( EGL_WINDOW_BIT );
291 configAttribs.PushBack( EGL_SURFACE_TYPE );
292 configAttribs.PushBack( EGL_PIXMAP_BIT );
295 configAttribs.PushBack( EGL_RENDERABLE_TYPE );
297 #if DALI_GLES_VERSION >= 30
300 configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
302 // There is a bug in the desktop emulator
303 // Requesting for ES3 causes eglCreateContext even though it allows to ask
304 // for a configuration that supports GLES 3.0
305 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
308 #else // DALI_GLES_VERSION >= 30
310 Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
311 configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
313 #endif //DALI_GLES_VERSION >= 30
315 #if DALI_GLES_VERSION >= 30
316 // TODO: enable this flag when it becomes supported
317 // configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
318 // configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
319 #endif //DALI_GLES_VERSION >= 30
321 configAttribs.PushBack( EGL_RED_SIZE );
322 configAttribs.PushBack( 8 );
323 configAttribs.PushBack( EGL_GREEN_SIZE );
324 configAttribs.PushBack( 8 );
325 configAttribs.PushBack( EGL_BLUE_SIZE );
326 configAttribs.PushBack( 8 );
328 configAttribs.PushBack( EGL_ALPHA_SIZE );
330 // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
331 configAttribs.PushBack( 8 );
333 // There is a bug in the desktop emulator
334 // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
335 configAttribs.PushBack( 0 );
338 configAttribs.PushBack( EGL_DEPTH_SIZE );
339 configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
340 configAttribs.PushBack( EGL_STENCIL_SIZE );
341 configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
343 #ifndef DALI_PROFILE_UBUNTU
344 if( mMultiSamplingLevel != EGL_DONT_CARE )
346 configAttribs.PushBack( EGL_SAMPLES );
347 configAttribs.PushBack( mMultiSamplingLevel );
348 configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
349 configAttribs.PushBack( 1 );
351 #endif // DALI_PROFILE_UBUNTU
352 configAttribs.PushBack( EGL_NONE );
354 if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
356 EGLint error = eglGetError();
359 case EGL_BAD_DISPLAY:
361 DALI_LOG_ERROR("Display is not an EGL display connection\n");
364 case EGL_BAD_ATTRIBUTE:
366 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");
369 case EGL_NOT_INITIALIZED:
371 DALI_LOG_ERROR("Display has not been initialized\n");
374 case EGL_BAD_PARAMETER:
376 DALI_LOG_ERROR("The parameter numConfig is NULL\n");
381 DALI_LOG_ERROR("Unknown error.\n");
384 DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
387 if ( numConfigs != 1 )
389 DALI_LOG_ERROR("No configurations found.\n");
391 TEST_EGL_ERROR("eglChooseConfig");
395 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
397 DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
399 mEglNativeWindow = window;
404 ChooseConfig(mIsWindow, mColorDepth);
406 mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
407 TEST_EGL_ERROR("eglCreateWindowSurface");
409 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
412 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
414 mCurrentEglNativePixmap = pixmap;
419 ChooseConfig(mIsWindow, mColorDepth);
421 mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
422 TEST_EGL_ERROR("eglCreatePixmapSurface");
424 DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
426 return mCurrentEglSurface;
429 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
431 bool contextLost = false;
433 // display connection has not changed, then we can just create a new surface
434 // the surface is bound to the context, so set the context to null
437 // destroy the surface
440 // create the EGL surface
441 CreateSurfaceWindow( window, mColorDepth );
443 // set the context to be current with the new surface
444 MakeContextCurrent();
449 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
451 bool contextLost = false;
453 // display connection has not changed, then we can just create a new surface
454 // create the EGL surface
455 eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
457 // set the eglSurface to be current
458 MakeCurrent( pixmap, eglSurface );
463 EGLDisplay EglImplementation::GetDisplay() const
468 EGLDisplay EglImplementation::GetContext() const
473 } // namespace Adaptor
475 } // namespace Internal
479 #pragma GCC diagnostic pop