Lifecycle controller
[platform/core/uifw/dali-adaptor.git] / adaptors / x11 / egl-implementation-x.cpp
1 /*
2  * Copyright (c) 2014 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 <gl/egl-implementation.h>
21
22 // EXTERNAL INCLUDES
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/common/dali-vector.h>
26
27 // INTERNAL INCLUDES
28 #include <ecore-x-render-surface.h>
29 #include <gl/gl-implementation.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 #define TEST_EGL_ERROR(lastCommand) \
41 { \
42   EGLint err = eglGetError(); \
43   if (err != EGL_SUCCESS) \
44   { \
45     DALI_LOG_ERROR("EGL error after %s code=%d\n", lastCommand,err); \
46     DALI_ASSERT_ALWAYS(0 && "EGL error");                            \
47   } \
48 }
49
50 EglImplementation::EglImplementation()
51   : mEglNativeDisplay(0),
52     mEglNativeWindow(0),
53     mEglNativePixmap(0),
54     mEglDisplay(0),
55     mEglConfig(0),
56     mEglContext(0),
57     mEglSurface(0),
58     mGlesInitialized(false),
59     mIsOwnSurface(true),
60     mContextCurrent(false),
61     mIsWindow(true),
62     mColorDepth(COLOR_DEPTH_24)
63 {
64 }
65
66 EglImplementation::~EglImplementation()
67 {
68   TerminateGles();
69 }
70
71 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
72 {
73   if ( !mGlesInitialized )
74   {
75     mEglNativeDisplay = display;
76
77     //@todo see if we can just EGL_DEFAULT_DISPLAY instead
78     mEglDisplay = eglGetDisplay(mEglNativeDisplay);
79
80     EGLint majorVersion = 0;
81     EGLint minorVersion = 0;
82     if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
83     {
84       return false;
85     }
86     eglBindAPI(EGL_OPENGL_ES_API);
87
88     mContextAttribs.Clear();
89
90 #if DALI_GLES_VERSION >= 30
91
92     mContextAttribs.Reserve(5);
93     mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
94     mContextAttribs.PushBack( 3 );
95     mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
96     mContextAttribs.PushBack( 0 );
97
98 #else // DALI_GLES_VERSION >= 30
99
100     mContextAttribs.Reserve(3);
101     mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
102     mContextAttribs.PushBack( 2 );
103
104 #endif // DALI_GLES_VERSION >= 30
105
106     mContextAttribs.PushBack( EGL_NONE );
107
108     mGlesInitialized = true;
109     mIsOwnSurface = isOwnSurface;
110   }
111
112   return mGlesInitialized;
113 }
114
115 bool EglImplementation::CreateContext()
116 {
117   // make sure a context isn't created twice
118   DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
119
120   mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
121   TEST_EGL_ERROR("eglCreateContext render thread");
122
123   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
124
125   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
126   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
127   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
128   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
129   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
130
131   return true;
132 }
133
134 void EglImplementation::DestroyContext()
135 {
136   DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
137
138   eglDestroyContext( mEglDisplay, mEglContext );
139   mEglContext = 0;
140 }
141
142 void EglImplementation::DestroySurface()
143 {
144   if(mIsOwnSurface && mEglSurface)
145   {
146     eglDestroySurface( mEglDisplay, mEglSurface );
147     mEglSurface = 0;
148   }
149 }
150
151 void EglImplementation::MakeContextCurrent()
152 {
153   mContextCurrent = true;
154
155   if(mIsOwnSurface)
156   {
157     eglMakeCurrent( mEglDisplay, mEglSurface, mEglSurface, mEglContext );
158   }
159
160   EGLint error = eglGetError();
161
162   if ( error != EGL_SUCCESS )
163   {
164     switch (error)
165     {
166       case EGL_BAD_DISPLAY:
167       {
168         DALI_LOG_ERROR("EGL_BAD_DISPLAY : Display is not an EGL display connection");
169         break;
170       }
171       case EGL_NOT_INITIALIZED:
172       {
173         DALI_LOG_ERROR("EGL_NOT_INITIALIZED : Display has not been initialized");
174         break;
175       }
176       case EGL_BAD_SURFACE:
177       {
178         DALI_LOG_ERROR("EGL_BAD_SURFACE : Draw or read is not an EGL surface");
179         break;
180       }
181       case EGL_BAD_CONTEXT:
182       {
183         DALI_LOG_ERROR("EGL_BAD_CONTEXT : Context is not an EGL rendering context");
184         break;
185       }
186       case EGL_BAD_MATCH:
187       {
188         DALI_LOG_ERROR("EGL_BAD_MATCH : Draw or read are not compatible with context, or if context is set to EGL_NO_CONTEXT and draw or read are not set to EGL_NO_SURFACE, or if draw or read are set to EGL_NO_SURFACE and context is not set to EGL_NO_CONTEXT");
189         break;
190       }
191       case EGL_BAD_ACCESS:
192       {
193         DALI_LOG_ERROR("EGL_BAD_ACCESS : Context is current to some other thread");
194         break;
195       }
196       case EGL_BAD_NATIVE_PIXMAP:
197       {
198         DALI_LOG_ERROR("EGL_BAD_NATIVE_PIXMAP : A native pixmap underlying either draw or read is no longer valid.");
199         break;
200       }
201       case EGL_BAD_NATIVE_WINDOW:
202       {
203         DALI_LOG_ERROR("EGL_BAD_NATIVE_WINDOW : A native window underlying either draw or read is no longer valid.");
204         break;
205       }
206       case EGL_BAD_CURRENT_SURFACE:
207       {
208         DALI_LOG_ERROR("EGL_BAD_CURRENT_SURFACE : The previous context has unflushed commands and the previous surface is no longer valid.");
209         break;
210       }
211       case EGL_BAD_ALLOC:
212       {
213         DALI_LOG_ERROR("EGL_BAD_ALLOC : Allocation of ancillary buffers for draw or read were delayed until eglMakeCurrent is called, and there are not enough resources to allocate them");
214         break;
215       }
216       case EGL_CONTEXT_LOST:
217       {
218         DALI_LOG_ERROR("EGL_CONTEXT_LOST : If a power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering");
219         break;
220       }
221       default:
222       {
223         DALI_LOG_ERROR("Unknown error");
224         break;
225       }
226     }
227     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
228   }
229
230   // We want to display this information all the time, so use the LogMessage directly
231   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
232       "            Vendor:        %s\n"
233       "            Version:       %s\n"
234       "            Client APIs:   %s\n"
235       "            Extensions:    %s\n",
236       eglQueryString(mEglDisplay, EGL_VENDOR),
237       eglQueryString(mEglDisplay, EGL_VERSION),
238       eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
239       eglQueryString(mEglDisplay, EGL_EXTENSIONS));
240 }
241
242 void EglImplementation::MakeContextNull()
243 {
244   mContextCurrent = false;
245   // clear the current context
246   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
247 }
248
249 void EglImplementation::TerminateGles()
250 {
251   if ( mGlesInitialized )
252   {
253     // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
254     // MakeContextNull should be called before eglDestroy surface
255     // to prevent crash in _mali_surface_destroy_callback
256     MakeContextNull();
257
258     if(mIsOwnSurface && mEglSurface)
259     {
260       eglDestroySurface(mEglDisplay, mEglSurface);
261     }
262     eglDestroyContext(mEglDisplay, mEglContext);
263
264     eglTerminate(mEglDisplay);
265
266     mEglDisplay = NULL;
267     mEglConfig  = NULL;
268     mEglContext = NULL;
269     mEglSurface = NULL;
270
271     mGlesInitialized = false;
272   }
273 }
274
275 bool EglImplementation::IsGlesInitialized() const
276 {
277   return mGlesInitialized;
278 }
279
280 void EglImplementation::SwapBuffers()
281 {
282   eglSwapBuffers( mEglDisplay, mEglSurface );
283 }
284
285 void EglImplementation::CopyBuffers()
286 {
287   eglCopyBuffers( mEglDisplay, mEglSurface, mEglNativePixmap );
288 }
289
290 void EglImplementation::WaitGL()
291 {
292   eglWaitGL();
293 }
294
295 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
296 {
297   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
298   {
299     return;
300   }
301
302   mIsWindow = isWindowType;
303
304   EGLint numConfigs;
305   Vector<EGLint> configAttribs;
306   configAttribs.Reserve(31);
307
308   if(isWindowType)
309   {
310     configAttribs.PushBack( EGL_SURFACE_TYPE );
311     configAttribs.PushBack( EGL_WINDOW_BIT );
312   }
313   else
314   {
315     configAttribs.PushBack( EGL_SURFACE_TYPE );
316     configAttribs.PushBack( EGL_PIXMAP_BIT );
317   }
318
319   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
320
321 #if DALI_GLES_VERSION >= 30
322
323 #ifdef _ARCH_ARM_
324   configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
325 #else
326   // There is a bug in the desktop emulator
327   // Requesting for ES3 causes eglCreateContext even though it allows to ask
328   // for a configuration that supports GLES 3.0
329   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
330 #endif // _ARCH_ARM_
331
332 #else // DALI_GLES_VERSION >= 30
333
334   DALI_LOG_WARNING( "Using OpenGL ES 2 \n" );
335   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
336
337 #endif //DALI_GLES_VERSION >= 30
338
339 #if DALI_GLES_VERSION >= 30
340 // TODO: enable this flag when it becomes supported
341 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
342 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
343 #endif //DALI_GLES_VERSION >= 30
344
345   configAttribs.PushBack( EGL_RED_SIZE );
346   configAttribs.PushBack( 8 );
347   configAttribs.PushBack( EGL_GREEN_SIZE );
348   configAttribs.PushBack( 8 );
349   configAttribs.PushBack( EGL_BLUE_SIZE );
350   configAttribs.PushBack( 8 );
351
352   configAttribs.PushBack( EGL_ALPHA_SIZE );
353 #ifdef _ARCH_ARM_
354   configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
355 #else
356   // There is a bug in the desktop emulator
357   // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
358   configAttribs.PushBack( 0 );
359 #endif // _ARCH_ARM_
360
361   configAttribs.PushBack( EGL_DEPTH_SIZE );
362   configAttribs.PushBack( 24 );
363   configAttribs.PushBack( EGL_STENCIL_SIZE );
364   configAttribs.PushBack( 8 );
365 #ifndef DALI_PROFILE_UBUNTU
366   configAttribs.PushBack( EGL_SAMPLES );
367   configAttribs.PushBack( 4 );
368   configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
369   configAttribs.PushBack( 1 );
370 #endif // DALI_PROFILE_UBUNTU
371   configAttribs.PushBack( EGL_NONE );
372
373   if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
374   {
375     EGLint error = eglGetError();
376     switch (error)
377     {
378       case EGL_BAD_DISPLAY:
379       {
380         DALI_LOG_ERROR("Display is not an EGL display connection\n");
381         break;
382       }
383       case EGL_BAD_ATTRIBUTE:
384       {
385         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");
386         break;
387       }
388       case EGL_NOT_INITIALIZED:
389       {
390         DALI_LOG_ERROR("Display has not been initialized\n");
391         break;
392       }
393       case EGL_BAD_PARAMETER:
394       {
395         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
396         break;
397       }
398       default:
399       {
400         DALI_LOG_ERROR("Unknown error.\n");
401       }
402     }
403     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
404   }
405
406   if ( numConfigs != 1 )
407   {
408     DALI_LOG_ERROR("No configurations found.\n");
409
410     TEST_EGL_ERROR("eglChooseConfig");
411   }
412 }
413
414
415 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
416 {
417   DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
418
419   mEglNativeWindow = window;
420   mColorDepth = depth;
421   mIsWindow = true;
422
423   // egl choose config
424   ChooseConfig(mIsWindow, mColorDepth);
425
426   mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
427   TEST_EGL_ERROR("eglCreateWindowSurface");
428
429   DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
430 }
431
432 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
433 {
434   DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
435
436   mEglNativePixmap = pixmap;
437   mColorDepth = depth;
438   mIsWindow = false;
439
440   // egl choose config
441   ChooseConfig(mIsWindow, mColorDepth);
442
443   mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
444   TEST_EGL_ERROR("eglCreatePixmapSurface");
445
446   DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
447 }
448
449 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLNativeDisplayType display )
450 {
451   bool contextLost = false;
452
453   // If the display connection has not changed, then we can just create a new surface
454   if ( display == mEglNativeDisplay )
455   {
456     //  the surface is bound to the context, so set the context to null
457     MakeContextNull();
458
459     // destroy the surface
460     DestroySurface();
461
462     // create the EGL surface
463     CreateSurfaceWindow( window, mColorDepth );
464
465     // set the context to be current with the new surface
466     MakeContextCurrent();
467   }
468   else  // the display connection has changed, we need to start egl with a new x-connection
469   {
470     TerminateGles();
471
472     // let the adaptor know that all resources have been lost
473     contextLost = true;
474
475     // re-initialise GLES with the new connection
476     InitializeGles( display );
477
478     // create the EGL surface
479     CreateSurfaceWindow( window, mColorDepth );
480
481      // create the OpenGL context
482     CreateContext();
483
484     // Make it current
485     MakeContextCurrent();
486   }
487
488   return contextLost;
489 }
490
491 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLNativeDisplayType display )
492 {
493   bool contextLost = false;
494
495   //  the surface is bound to the context, so set the context to null
496   MakeContextNull();
497
498   // destroy the surface
499   DestroySurface();
500
501   // If the display has not changed, then we can just create a new surface
502   if ( display == mEglNativeDisplay )
503   {
504     // create the EGL surface
505     CreateSurfacePixmap( pixmap, mColorDepth );
506
507     // set the context to be current with the new surface
508     MakeContextCurrent();
509   }
510   else  // the display has changed, we need to start egl with a new x-connection
511   {
512     // Note! this code path is untested
513
514     // this will release all EGL specific resources
515     eglTerminate( mEglDisplay );
516
517     mGlesInitialized = false;
518
519     // let the adaptor know that all resources have been lost
520     contextLost = true;
521
522     // re-initialise GLES with the new connection
523     InitializeGles( display );
524
525     // create the EGL surface
526     CreateSurfacePixmap( pixmap, mColorDepth );
527
528     // create the OpenGL context
529     CreateContext();
530
531     // Make it current
532     MakeContextCurrent();
533   }
534   return contextLost;
535 }
536
537 EGLDisplay EglImplementation::GetDisplay() const
538 {
539   return mEglDisplay;
540 }
541
542 EGLDisplay EglImplementation::GetContext() const
543 {
544   return mEglContext;
545 }
546
547 } // namespace Adaptor
548
549 } // namespace Internal
550
551 } // namespace Dali