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