Revert "[3.0] Add default value into window member data"
[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     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   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   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
335
336 #endif //DALI_GLES_VERSION >= 30
337
338 #if DALI_GLES_VERSION >= 30
339 // TODO: enable this flag when it becomes supported
340 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
341 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
342 #endif //DALI_GLES_VERSION >= 30
343
344   configAttribs.PushBack( EGL_RED_SIZE );
345   configAttribs.PushBack( 8 );
346   configAttribs.PushBack( EGL_GREEN_SIZE );
347   configAttribs.PushBack( 8 );
348   configAttribs.PushBack( EGL_BLUE_SIZE );
349   configAttribs.PushBack( 8 );
350
351   configAttribs.PushBack( EGL_ALPHA_SIZE );
352 #ifdef _ARCH_ARM_
353   configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
354 #else
355   // There is a bug in the desktop emulator
356   // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
357   configAttribs.PushBack( 0 );
358 #endif // _ARCH_ARM_
359
360   configAttribs.PushBack( EGL_DEPTH_SIZE );
361   configAttribs.PushBack( 24 );
362   configAttribs.PushBack( EGL_STENCIL_SIZE );
363   configAttribs.PushBack( 8 );
364   configAttribs.PushBack( EGL_SAMPLES );
365   configAttribs.PushBack( 4 );
366   configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
367   configAttribs.PushBack( 1 );
368   configAttribs.PushBack( EGL_NONE );
369
370   if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
371   {
372     EGLint error = eglGetError();
373     switch (error)
374     {
375       case EGL_BAD_DISPLAY:
376       {
377         DALI_LOG_ERROR("Display is not an EGL display connection\n");
378         break;
379       }
380       case EGL_BAD_ATTRIBUTE:
381       {
382         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");
383         break;
384       }
385       case EGL_NOT_INITIALIZED:
386       {
387         DALI_LOG_ERROR("Display has not been initialized\n");
388         break;
389       }
390       case EGL_BAD_PARAMETER:
391       {
392         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
393         break;
394       }
395       default:
396       {
397         DALI_LOG_ERROR("Unknown error.\n");
398       }
399     }
400     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
401   }
402
403   if ( numConfigs != 1 )
404   {
405     DALI_LOG_ERROR("No configurations found.\n");
406
407     TEST_EGL_ERROR("eglChooseConfig");
408   }
409 }
410
411 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
412 {
413   DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
414
415   mEglNativeWindow = window;
416   mColorDepth = depth;
417   mIsWindow = true;
418
419   // egl choose config
420   ChooseConfig(mIsWindow, mColorDepth);
421
422   mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
423   TEST_EGL_ERROR("eglCreateWindowSurface");
424
425   DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
426 }
427
428 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
429 {
430   DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
431
432   mEglNativePixmap = pixmap;
433   mColorDepth = depth;
434   mIsWindow = false;
435
436   // egl choose config
437   ChooseConfig(mIsWindow, mColorDepth);
438
439   mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
440   TEST_EGL_ERROR("eglCreatePixmapSurface");
441
442   DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
443 }
444
445 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window )
446 {
447   bool contextLost = false;
448
449   // display connection has not changed, then we can just create a new surface
450   //  the surface is bound to the context, so set the context to null
451   MakeContextNull();
452
453   // destroy the surface
454   DestroySurface();
455
456   // create the EGL surface
457   CreateSurfaceWindow( window, mColorDepth );
458
459   // set the context to be current with the new surface
460   MakeContextCurrent();
461
462   return contextLost;
463 }
464
465 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap )
466 {
467   bool contextLost = false;
468
469   //  the surface is bound to the context, so set the context to null
470   MakeContextNull();
471
472   // destroy the surface
473   DestroySurface();
474
475   // display connection has not changed, then we can just create a new surface
476   // create the EGL surface
477   CreateSurfacePixmap( pixmap, mColorDepth );
478
479   // set the context to be current with the new surface
480   MakeContextCurrent();
481
482   return contextLost;
483 }
484
485 EGLDisplay EglImplementation::GetDisplay() const
486 {
487   return mEglDisplay;
488 }
489
490 EGLDisplay EglImplementation::GetContext() const
491 {
492   return mEglContext;
493 }
494
495 } // namespace Adaptor
496
497 } // namespace Internal
498
499 } // namespace Dali