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