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