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