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