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