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