Change a service name of the indicator and remove a unused indicator style
[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 <wayland-egl.h>
21 #include <gl/egl-implementation.h>
22
23 // EXTERNAL INCLUDES
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/dali-vector.h>
27
28 // INTERNAL INCLUDES
29 #include <ecore-wl-render-surface.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   return true;
126 }
127
128 void EglImplementation::DestroyContext()
129 {
130   DALI_ASSERT_ALWAYS( mEglContext && "no EGL context" );
131
132   eglDestroyContext( mEglDisplay, mEglContext );
133   mEglContext = 0;
134 }
135
136 void EglImplementation::DestroySurface()
137 {
138   if(mIsOwnSurface && mEglSurface)
139   {
140     eglDestroySurface( mEglDisplay, mEglSurface );
141     mEglSurface = 0;
142   }
143 }
144
145 void EglImplementation::MakeContextCurrent()
146 {
147   mContextCurrent = true;
148
149   if(mIsOwnSurface)
150   {
151     eglMakeCurrent( mEglDisplay, mEglSurface, mEglSurface, mEglContext );
152   }
153
154   EGLint error = eglGetError();
155
156   if ( error != EGL_SUCCESS )
157   {
158     switch (error)
159     {
160       case EGL_BAD_DISPLAY:
161       {
162         DALI_LOG_ERROR("EGL_BAD_DISPLAY : Display is not an EGL display connection");
163         break;
164       }
165       case EGL_NOT_INITIALIZED:
166       {
167         DALI_LOG_ERROR("EGL_NOT_INITIALIZED : Display has not been initialized");
168         break;
169       }
170       case EGL_BAD_SURFACE:
171       {
172         DALI_LOG_ERROR("EGL_BAD_SURFACE : Draw or read is not an EGL surface");
173         break;
174       }
175       case EGL_BAD_CONTEXT:
176       {
177         DALI_LOG_ERROR("EGL_BAD_CONTEXT : Context is not an EGL rendering context");
178         break;
179       }
180       case EGL_BAD_MATCH:
181       {
182         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");
183         break;
184       }
185       case EGL_BAD_ACCESS:
186       {
187         DALI_LOG_ERROR("EGL_BAD_ACCESS : Context is current to some other thread");
188         break;
189       }
190       case EGL_BAD_NATIVE_PIXMAP:
191       {
192         DALI_LOG_ERROR("EGL_BAD_NATIVE_PIXMAP : A native pixmap underlying either draw or read is no longer valid.");
193         break;
194       }
195       case EGL_BAD_NATIVE_WINDOW:
196       {
197         DALI_LOG_ERROR("EGL_BAD_NATIVE_WINDOW : A native window underlying either draw or read is no longer valid.");
198         break;
199       }
200       case EGL_BAD_CURRENT_SURFACE:
201       {
202         DALI_LOG_ERROR("EGL_BAD_CURRENT_SURFACE : The previous context has unflushed commands and the previous surface is no longer valid.");
203         break;
204       }
205       case EGL_BAD_ALLOC:
206       {
207         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");
208         break;
209       }
210       case EGL_CONTEXT_LOST:
211       {
212         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");
213         break;
214       }
215       default:
216       {
217         DALI_LOG_ERROR("Unknown error");
218         break;
219       }
220     }
221     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
222   }
223
224   // We want to display this information all the time, so use the LogMessage directly
225   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
226       "            Vendor:        %s\n"
227       "            Version:       %s\n"
228       "            Client APIs:   %s\n"
229       "            Extensions:    %s\n",
230       eglQueryString(mEglDisplay, EGL_VENDOR),
231       eglQueryString(mEglDisplay, EGL_VERSION),
232       eglQueryString(mEglDisplay, EGL_CLIENT_APIS),
233       eglQueryString(mEglDisplay, EGL_EXTENSIONS));
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\n");
372         break;
373       }
374       case EGL_BAD_ATTRIBUTE:
375       {
376         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");
377         break;
378       }
379       case EGL_NOT_INITIALIZED:
380       {
381         DALI_LOG_ERROR("Display has not been initialized\n");
382         break;
383       }
384       case EGL_BAD_PARAMETER:
385       {
386         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
387         break;
388       }
389       default:
390       {
391         DALI_LOG_ERROR("Unknown error.\n");
392       }
393     }
394     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
395   }
396
397   if ( numConfigs != 1 )
398   {
399     DALI_LOG_ERROR("No configurations found.\n");
400
401     TEST_EGL_ERROR("eglChooseConfig");
402   }
403 }
404
405 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
406 {
407   DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
408
409   mEglNativeWindow = window;
410   mColorDepth = depth;
411   mIsWindow = true;
412
413   // egl choose config
414   ChooseConfig(mIsWindow, mColorDepth);
415
416   mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
417   TEST_EGL_ERROR("eglCreateWindowSurface");
418
419   DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
420 }
421
422 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
423 {
424   DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
425
426   mEglNativePixmap = pixmap;
427   mColorDepth = depth;
428   mIsWindow = false;
429
430   // egl choose config
431   ChooseConfig(mIsWindow, mColorDepth);
432
433   mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
434   TEST_EGL_ERROR("eglCreatePixmapSurface");
435
436   DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
437 }
438
439 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
440 {
441   bool contextLost = false;
442
443   // display connection has not changed, then we can just create a new surface
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   // create the EGL surface
451   CreateSurfaceWindow( window, mColorDepth );
452
453   // set the context to be current with the new surface
454   MakeContextCurrent();
455
456   return contextLost;
457 }
458
459 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap )
460 {
461   bool contextLost = false;
462
463   //  the surface is bound to the context, so set the context to null
464   MakeContextNull();
465
466   // destroy the surface
467   DestroySurface();
468
469   // display connection has not changed, then we can just create a new surface
470   // create the EGL surface
471   CreateSurfacePixmap( pixmap, mColorDepth );
472
473   // set the context to be current with the new surface
474   MakeContextCurrent();
475
476   return contextLost;
477 }
478
479 EGLDisplay EglImplementation::GetDisplay() const
480 {
481   return mEglDisplay;
482 }
483
484 EGLDisplay EglImplementation::GetContext() const
485 {
486   return mEglContext;
487 }
488
489 } // namespace Adaptor
490
491 } // namespace Internal
492
493 } // namespace Dali