783c06a01a6c5469a27f02c6c9bea3618bd21452
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-implementation.cpp
1 /*
2  * Copyright (c) 2018 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   mMultiSamplingLevel( multiSamplingLevel ),
68   mColorDepth( COLOR_DEPTH_24 ),
69   mGlesInitialized( false ),
70   mIsOwnSurface( true ),
71   mContextCurrent( false ),
72   mIsWindow( true ),
73   mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
74   mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE )
75 {
76 }
77
78 EglImplementation::~EglImplementation()
79 {
80   TerminateGles();
81 }
82
83 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
84 {
85   if ( !mGlesInitialized )
86   {
87     mEglNativeDisplay = display;
88
89     //@todo see if we can just EGL_DEFAULT_DISPLAY instead
90     mEglDisplay = eglGetDisplay(mEglNativeDisplay);
91     EGLint error = eglGetError();
92
93     if( mEglDisplay == NULL && error != EGL_SUCCESS )
94     {
95       throw Dali::DaliException( "", "OpenGL ES is not supported");
96     }
97
98     EGLint majorVersion = 0;
99     EGLint minorVersion = 0;
100     if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
101     {
102       return false;
103     }
104     eglBindAPI(EGL_OPENGL_ES_API);
105
106     mContextAttribs.Clear();
107
108 #if DALI_GLES_VERSION >= 30
109
110     mContextAttribs.Reserve(5);
111     mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
112     mContextAttribs.PushBack( DALI_GLES_VERSION / 10 );
113     mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
114     mContextAttribs.PushBack( DALI_GLES_VERSION % 10 );
115
116 #else // DALI_GLES_VERSION >= 30
117
118     mContextAttribs.Reserve(3);
119     mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
120     mContextAttribs.PushBack( 2 );
121
122 #endif // DALI_GLES_VERSION >= 30
123
124     mContextAttribs.PushBack( EGL_NONE );
125
126     mGlesInitialized = true;
127     mIsOwnSurface = isOwnSurface;
128   }
129
130   return mGlesInitialized;
131 }
132
133 bool EglImplementation::CreateContext()
134 {
135   // make sure a context isn't created twice
136   DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
137
138   mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
139   TEST_EGL_ERROR("eglCreateContext render thread");
140
141   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
142
143   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
144   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
145   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
146   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
147   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
148
149   return true;
150 }
151
152 void EglImplementation::DestroyContext()
153 {
154   DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
155
156   eglDestroyContext( mEglDisplay, mEglContext );
157   mEglContext = 0;
158 }
159
160 void EglImplementation::DestroySurface()
161 {
162   if(mIsOwnSurface && mCurrentEglSurface)
163   {
164     // Make context null to prevent crash in driver side
165     MakeContextNull();
166     eglDestroySurface( mEglDisplay, mCurrentEglSurface );
167     mCurrentEglSurface = 0;
168   }
169 }
170
171 void EglImplementation::MakeContextCurrent()
172 {
173   mContextCurrent = true;
174
175   if(mIsOwnSurface)
176   {
177     eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
178   }
179
180   EGLint error = eglGetError();
181
182   if ( error != EGL_SUCCESS )
183   {
184     Egl::PrintError(error);
185
186     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
187   }
188
189   // We want to display this information all the time, so use the LogMessage directly
190   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
191       "            Vendor:        %s\n"
192       "            Version:       %s\n"
193       "            Client APIs:   %s\n"
194       "            Extensions:    %s\n",
195       eglQueryString(mEglDisplay, EGL_VENDOR),
196       eglQueryString(mEglDisplay, EGL_VERSION),
197       eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
198       eglQueryString(mEglDisplay, EGL_EXTENSIONS));
199 }
200
201 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
202 {
203   mCurrentEglNativePixmap = pixmap;
204   mCurrentEglSurface = eglSurface;
205
206   if(mIsOwnSurface)
207   {
208     eglMakeCurrent( mEglDisplay, mCurrentEglSurface, mCurrentEglSurface, mEglContext );
209   }
210
211   EGLint error = eglGetError();
212
213   if ( error != EGL_SUCCESS )
214   {
215     Egl::PrintError(error);
216
217     DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
218   }
219 }
220
221 void EglImplementation::MakeContextNull()
222 {
223   mContextCurrent = false;
224   // clear the current context
225   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
226 }
227
228 void EglImplementation::TerminateGles()
229 {
230   if ( mGlesInitialized )
231   {
232     // Make context null to prevent crash in driver side
233     MakeContextNull();
234
235     if(mIsOwnSurface && mCurrentEglSurface)
236     {
237       eglDestroySurface(mEglDisplay, mCurrentEglSurface);
238     }
239     eglDestroyContext(mEglDisplay, mEglContext);
240
241     eglTerminate(mEglDisplay);
242
243     mEglDisplay = NULL;
244     mEglConfig  = NULL;
245     mEglContext = NULL;
246     mCurrentEglSurface = NULL;
247
248     mGlesInitialized = false;
249   }
250 }
251
252 bool EglImplementation::IsGlesInitialized() const
253 {
254   return mGlesInitialized;
255 }
256
257 void EglImplementation::SwapBuffers()
258 {
259   eglSwapBuffers( mEglDisplay, mCurrentEglSurface );
260 }
261
262 void EglImplementation::CopyBuffers()
263 {
264   eglCopyBuffers( mEglDisplay, mCurrentEglSurface, mCurrentEglNativePixmap );
265 }
266
267 void EglImplementation::WaitGL()
268 {
269   eglWaitGL();
270 }
271
272 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
273 {
274   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
275   {
276     return;
277   }
278
279   mIsWindow = isWindowType;
280
281   EGLint numConfigs;
282   Vector<EGLint> configAttribs;
283   configAttribs.Reserve(31);
284
285   if(isWindowType)
286   {
287     configAttribs.PushBack( EGL_SURFACE_TYPE );
288     configAttribs.PushBack( EGL_WINDOW_BIT );
289   }
290   else
291   {
292     configAttribs.PushBack( EGL_SURFACE_TYPE );
293     configAttribs.PushBack( EGL_PIXMAP_BIT );
294   }
295
296   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
297
298 #if DALI_GLES_VERSION >= 30
299
300 #ifdef _ARCH_ARM_
301   configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
302 #else
303   // There is a bug in the desktop emulator
304   // Requesting for ES3 causes eglCreateContext even though it allows to ask
305   // for a configuration that supports GLES 3.0
306   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
307 #endif // _ARCH_ARM_
308
309 #else // DALI_GLES_VERSION >= 30
310
311   Integration::Log::LogMessage( Integration::Log::DebugInfo, "Using OpenGL ES 2 \n" );
312   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
313
314 #endif //DALI_GLES_VERSION >= 30
315
316 #if DALI_GLES_VERSION >= 30
317 // TODO: enable this flag when it becomes supported
318 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
319 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
320 #endif //DALI_GLES_VERSION >= 30
321
322   configAttribs.PushBack( EGL_RED_SIZE );
323   configAttribs.PushBack( 8 );
324   configAttribs.PushBack( EGL_GREEN_SIZE );
325   configAttribs.PushBack( 8 );
326   configAttribs.PushBack( EGL_BLUE_SIZE );
327   configAttribs.PushBack( 8 );
328
329   configAttribs.PushBack( EGL_ALPHA_SIZE );
330 #ifdef _ARCH_ARM_
331   // For underlay video playback, we also need to set the alpha value of the 24/32bit window.
332   configAttribs.PushBack( 8 );
333 #else
334   // There is a bug in the desktop emulator
335   // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
336   configAttribs.PushBack( 0 );
337 #endif // _ARCH_ARM_
338
339   configAttribs.PushBack( EGL_DEPTH_SIZE );
340   configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
341   configAttribs.PushBack( EGL_STENCIL_SIZE );
342   configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
343
344 #ifndef DALI_PROFILE_UBUNTU
345   if( mMultiSamplingLevel != EGL_DONT_CARE )
346   {
347     configAttribs.PushBack( EGL_SAMPLES );
348     configAttribs.PushBack( mMultiSamplingLevel );
349     configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
350     configAttribs.PushBack( 1 );
351   }
352 #endif // DALI_PROFILE_UBUNTU
353   configAttribs.PushBack( EGL_NONE );
354
355   if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
356   {
357     EGLint error = eglGetError();
358     switch (error)
359     {
360       case EGL_BAD_DISPLAY:
361       {
362         DALI_LOG_ERROR("Display is not an EGL display connection\n");
363         break;
364       }
365       case EGL_BAD_ATTRIBUTE:
366       {
367         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");
368         break;
369       }
370       case EGL_NOT_INITIALIZED:
371       {
372         DALI_LOG_ERROR("Display has not been initialized\n");
373         break;
374       }
375       case EGL_BAD_PARAMETER:
376       {
377         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
378         break;
379       }
380       default:
381       {
382         DALI_LOG_ERROR("Unknown error.\n");
383       }
384     }
385     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
386   }
387
388   if ( numConfigs != 1 )
389   {
390     DALI_LOG_ERROR("No configurations found.\n");
391
392     TEST_EGL_ERROR("eglChooseConfig");
393   }
394 }
395
396 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
397 {
398   DALI_ASSERT_ALWAYS( ( mCurrentEglSurface == 0 ) && "EGL surface already exists" );
399
400   mEglNativeWindow = window;
401   mColorDepth = depth;
402   mIsWindow = true;
403
404   // egl choose config
405   ChooseConfig(mIsWindow, mColorDepth);
406
407   mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
408   TEST_EGL_ERROR("eglCreateWindowSurface");
409
410   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
411 }
412
413 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
414 {
415   mCurrentEglNativePixmap = pixmap;
416   mColorDepth = depth;
417   mIsWindow = false;
418
419   // egl choose config
420   ChooseConfig(mIsWindow, mColorDepth);
421
422   mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
423   TEST_EGL_ERROR("eglCreatePixmapSurface");
424
425   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
426
427   return mCurrentEglSurface;
428 }
429
430 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
431 {
432   bool contextLost = false;
433
434   // display connection has not changed, then we can just create a new surface
435   //  the surface is bound to the context, so set the context to null
436   MakeContextNull();
437
438   // destroy the surface
439   DestroySurface();
440
441   // create the EGL surface
442   CreateSurfaceWindow( window, mColorDepth );
443
444   // set the context to be current with the new surface
445   MakeContextCurrent();
446
447   return contextLost;
448 }
449
450 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
451 {
452   bool contextLost = false;
453
454   // display connection has not changed, then we can just create a new surface
455   // create the EGL surface
456   eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
457
458   // set the eglSurface to be current
459   MakeCurrent( pixmap, eglSurface );
460
461   return contextLost;
462 }
463
464 EGLDisplay EglImplementation::GetDisplay() const
465 {
466   return mEglDisplay;
467 }
468
469 EGLDisplay EglImplementation::GetContext() const
470 {
471   return mEglContext;
472 }
473
474 } // namespace Adaptor
475
476 } // namespace Internal
477
478 } // namespace Dali
479
480 #pragma GCC diagnostic pop