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