Select OpenGL es version automatically
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-implementation.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18
19 // CLASS HEADER
20 #include <dali/internal/graphics/gles/egl-implementation.h>
21
22 // EXTERNAL INCLUDES
23 #include <dali/integration-api/debug.h>
24
25 #include <dali/public-api/common/dali-vector.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/public-api/dali-adaptor-common.h>
29 #include <dali/internal/graphics/gles/gl-implementation.h>
30 #include <dali/internal/graphics/gles/egl-debug.h>
31
32 // EGL constants use C style casts
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wold-style-cast"
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace Adaptor
43 {
44
45 #define TEST_EGL_ERROR(lastCommand) \
46 { \
47   EGLint err = eglGetError(); \
48   if (err != EGL_SUCCESS) \
49   { \
50     DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
51     Egl::PrintError(err); \
52     DALI_ASSERT_ALWAYS(0 && "EGL error"); \
53   } \
54 }
55
56 EglImplementation::EglImplementation( int multiSamplingLevel,
57                                       Integration::DepthBufferAvailable depthBufferRequired,
58                                       Integration::StencilBufferAvailable stencilBufferRequired )
59 : mContextAttribs(),
60   mEglNativeDisplay( 0 ),
61   mEglNativeWindow( 0 ),
62   mCurrentEglNativePixmap( 0 ),
63   mEglDisplay( 0 ),
64   mEglConfig( 0 ),
65   mEglContext( 0 ),
66   mCurrentEglSurface( 0 ),
67   mCurrentEglContext( EGL_NO_CONTEXT ),
68   mMultiSamplingLevel( multiSamplingLevel ),
69   mGlesVersion( 30 ),
70   mColorDepth( COLOR_DEPTH_24 ),
71   mGlesInitialized( false ),
72   mIsOwnSurface( true ),
73   mIsWindow( true ),
74   mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
75   mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
76 {
77 }
78
79 EglImplementation::~EglImplementation()
80 {
81   TerminateGles();
82 }
83
84 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
85 {
86   if ( !mGlesInitialized )
87   {
88     mEglNativeDisplay = display;
89
90     //@todo see if we can just EGL_DEFAULT_DISPLAY instead
91     mEglDisplay = eglGetDisplay(mEglNativeDisplay);
92     EGLint error = eglGetError();
93
94     if( mEglDisplay == NULL && error != EGL_SUCCESS )
95     {
96       throw Dali::DaliException( "", "OpenGL ES is not supported");
97     }
98
99     EGLint majorVersion = 0;
100     EGLint minorVersion = 0;
101     if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
102     {
103       return false;
104     }
105     eglBindAPI(EGL_OPENGL_ES_API);
106
107     mGlesInitialized = true;
108     mIsOwnSurface = isOwnSurface;
109   }
110
111   // We want to display this information all the time, so use the LogMessage directly
112   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
113       "            Vendor:        %s\n"
114       "            Version:       %s\n"
115       "            Client APIs:   %s\n"
116       "            Extensions:    %s\n",
117       eglQueryString( mEglDisplay, EGL_VENDOR ),
118       eglQueryString( mEglDisplay, EGL_VERSION ),
119       eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
120       eglQueryString( mEglDisplay, EGL_EXTENSIONS ));
121
122   return mGlesInitialized;
123 }
124
125 bool EglImplementation::CreateContext()
126 {
127   // make sure a context isn't created twice
128   DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
129
130   mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
131   TEST_EGL_ERROR("eglCreateContext render thread");
132
133   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
134
135   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
136   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
137   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
138   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
139   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
140
141   return true;
142 }
143
144 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
145 {
146   // make sure a context isn't created twice
147   DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
148
149   eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
150   TEST_EGL_ERROR("eglCreateContext render thread");
151
152   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
153
154   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
155   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
156   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
157   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
158   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
159
160   mEglWindowContexts.push_back( eglContext );
161
162   return true;
163 }
164
165 void EglImplementation::DestroyContext( EGLContext& eglContext )
166 {
167   DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
168
169   eglDestroyContext( mEglDisplay, eglContext );
170   eglContext = 0;
171 }
172
173 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
174 {
175   if(mIsOwnSurface && eglSurface)
176   {
177     // Make context null to prevent crash in driver side
178     MakeContextNull();
179     eglDestroySurface( mEglDisplay, eglSurface );
180     eglSurface = 0;
181   }
182 }
183
184 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
185 {
186   if (mCurrentEglContext == eglContext)
187   {
188     return;
189   }
190
191   mCurrentEglSurface = eglSurface;
192
193   if(mIsOwnSurface)
194   {
195     glFinish();
196
197     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
198
199     mCurrentEglContext = eglContext;
200   }
201
202   EGLint error = eglGetError();
203
204   if ( error != EGL_SUCCESS )
205   {
206     Egl::PrintError(error);
207
208     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
209   }
210 }
211
212 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
213 {
214   if (mCurrentEglContext == mEglContext)
215   {
216     return;
217   }
218
219   mCurrentEglNativePixmap = pixmap;
220   mCurrentEglSurface = eglSurface;
221
222   if(mIsOwnSurface)
223   {
224     glFinish();
225
226     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
227
228     mCurrentEglContext = mEglContext;
229   }
230
231   EGLint error = eglGetError();
232
233   if ( error != EGL_SUCCESS )
234   {
235     Egl::PrintError(error);
236
237     DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
238   }
239 }
240
241 void EglImplementation::MakeContextNull()
242 {
243   // clear the current context
244   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
245   mCurrentEglContext = EGL_NO_CONTEXT;
246 }
247
248 void EglImplementation::TerminateGles()
249 {
250   if ( mGlesInitialized )
251   {
252     // Make context null to prevent crash in driver side
253     MakeContextNull();
254
255     for ( auto eglSurface : mEglWindowSurfaces )
256     {
257       if(mIsOwnSurface && eglSurface)
258       {
259         eglDestroySurface(mEglDisplay, eglSurface);
260       }
261     }
262     eglDestroyContext(mEglDisplay, mEglContext);
263     for ( auto eglContext : mEglWindowContexts )
264     {
265       eglDestroyContext(mEglDisplay, eglContext);
266     }
267
268     eglTerminate(mEglDisplay);
269
270     mEglDisplay = NULL;
271     mEglConfig  = NULL;
272     mEglContext = NULL;
273     mCurrentEglSurface = NULL;
274     mCurrentEglContext = EGL_NO_CONTEXT;
275
276     mGlesInitialized = false;
277   }
278 }
279
280 bool EglImplementation::IsGlesInitialized() const
281 {
282   return mGlesInitialized;
283 }
284
285 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
286 {
287   if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
288   {
289     eglSwapBuffers( mEglDisplay, eglSurface );
290   }
291 }
292
293 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
294 {
295   eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
296 }
297
298 void EglImplementation::WaitGL()
299 {
300   eglWaitGL();
301 }
302
303 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
304 {
305   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
306   {
307     return true;
308   }
309
310   bool isTransparent = ( depth == COLOR_DEPTH_32 );
311
312   mColorDepth = depth;
313   mIsWindow = isWindowType;
314
315   EGLint numConfigs;
316   Vector<EGLint> configAttribs;
317   configAttribs.Reserve(31);
318
319   if(isWindowType)
320   {
321     configAttribs.PushBack( EGL_SURFACE_TYPE );
322     configAttribs.PushBack( EGL_WINDOW_BIT );
323   }
324   else
325   {
326     configAttribs.PushBack( EGL_SURFACE_TYPE );
327     configAttribs.PushBack( EGL_PIXMAP_BIT );
328   }
329
330   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
331
332   if( mGlesVersion >= 30 )
333   {
334 #ifdef _ARCH_ARM_
335     configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
336 #else
337     configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
338 #endif // _ARCH_ARM_
339   }
340   else
341   {
342     configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
343   }
344
345 // TODO: enable this flag when it becomes supported
346 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
347 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
348
349   configAttribs.PushBack( EGL_RED_SIZE );
350   configAttribs.PushBack( 8 );
351   configAttribs.PushBack( EGL_GREEN_SIZE );
352   configAttribs.PushBack( 8 );
353   configAttribs.PushBack( EGL_BLUE_SIZE );
354   configAttribs.PushBack( 8 );
355
356   if ( isTransparent )
357   {
358     configAttribs.PushBack( EGL_ALPHA_SIZE );
359 #ifdef _ARCH_ARM_
360     // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
361     configAttribs.PushBack( 8 );
362 #else
363     // There is a bug in the desktop emulator
364     // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
365     configAttribs.PushBack( 8 );
366 #endif // _ARCH_ARM_
367   }
368
369   configAttribs.PushBack( EGL_DEPTH_SIZE );
370   configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
371   configAttribs.PushBack( EGL_STENCIL_SIZE );
372   configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
373
374 #ifndef DALI_PROFILE_UBUNTU
375   if( mMultiSamplingLevel != EGL_DONT_CARE )
376   {
377     configAttribs.PushBack( EGL_SAMPLES );
378     configAttribs.PushBack( mMultiSamplingLevel );
379     configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
380     configAttribs.PushBack( 1 );
381   }
382 #endif // DALI_PROFILE_UBUNTU
383   configAttribs.PushBack( EGL_NONE );
384
385   if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
386   {
387     if( mGlesVersion >= 30 )
388     {
389       mEglConfig = NULL;
390       DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retring to use OpenGL es 2.0.");
391       return false;
392     }
393
394     EGLint error = eglGetError();
395     switch (error)
396     {
397       case EGL_BAD_DISPLAY:
398       {
399         DALI_LOG_ERROR("Display is not an EGL display connection\n");
400         break;
401       }
402       case EGL_BAD_ATTRIBUTE:
403       {
404         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");
405         break;
406       }
407       case EGL_NOT_INITIALIZED:
408       {
409         DALI_LOG_ERROR("Display has not been initialized\n");
410         break;
411       }
412       case EGL_BAD_PARAMETER:
413       {
414         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
415         break;
416       }
417       default:
418       {
419         DALI_LOG_ERROR("Unknown error.\n");
420       }
421     }
422     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
423     return false;
424   }
425   Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
426
427   mContextAttribs.Clear();
428   if( mGlesVersion >= 30 )
429   {
430     mContextAttribs.Reserve(5);
431     mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
432     mContextAttribs.PushBack( mGlesVersion / 10 );
433     mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
434     mContextAttribs.PushBack( mGlesVersion % 10 );
435   }
436   else
437   {
438     mContextAttribs.Reserve(3);
439     mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
440     mContextAttribs.PushBack( 2 );
441   }
442   mContextAttribs.PushBack( EGL_NONE );
443
444   if ( numConfigs != 1 )
445   {
446     DALI_LOG_ERROR("No configurations found.\n");
447
448     TEST_EGL_ERROR("eglChooseConfig");
449   }
450
451   return true;
452 }
453
454 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
455 {
456   mEglNativeWindow = window;
457   mColorDepth = depth;
458   mIsWindow = true;
459
460   // egl choose config
461   ChooseConfig(mIsWindow, mColorDepth);
462
463   mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
464   TEST_EGL_ERROR("eglCreateWindowSurface");
465
466   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
467
468   return mCurrentEglSurface;
469 }
470
471 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
472 {
473   mCurrentEglNativePixmap = pixmap;
474   mColorDepth = depth;
475   mIsWindow = false;
476
477   // egl choose config
478   ChooseConfig(mIsWindow, mColorDepth);
479
480   mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
481   TEST_EGL_ERROR("eglCreatePixmapSurface");
482
483   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
484
485   return mCurrentEglSurface;
486 }
487
488 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
489 {
490   bool contextLost = false;
491
492   // display connection has not changed, then we can just create a new surface
493   //  the surface is bound to the context, so set the context to null
494   MakeContextNull();
495
496   // destroy the surface
497   DestroySurface( eglSurface );
498
499   // create the EGL surface
500   CreateSurfaceWindow( window, mColorDepth );
501
502   // set the context to be current with the new surface
503   MakeContextCurrent( eglSurface, eglContext );
504
505   return contextLost;
506 }
507
508 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
509 {
510   bool contextLost = false;
511
512   // display connection has not changed, then we can just create a new surface
513   // create the EGL surface
514   eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
515
516   // set the eglSurface to be current
517   MakeCurrent( pixmap, eglSurface );
518
519   return contextLost;
520 }
521
522 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
523 {
524   mGlesVersion = glesVersion;
525 }
526
527 EGLDisplay EglImplementation::GetDisplay() const
528 {
529   return mEglDisplay;
530 }
531
532 EGLContext EglImplementation::GetContext() const
533 {
534   return mEglContext;
535 }
536
537 int32_t EglImplementation::GetGlesVersion() const
538 {
539   return mGlesVersion;
540 }
541
542 } // namespace Adaptor
543
544 } // namespace Internal
545
546 } // namespace Dali
547
548 #pragma GCC diagnostic pop