Merge "Implemented the Handle assignment operators properly" into tizen
[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     mSyncMode(FULL_SYNC),
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   if ( mIsWindow )
236   {
237     SetRefreshSync( mSyncMode );
238   }
239 }
240
241 void EglImplementation::MakeContextNull()
242 {
243   mContextCurrent = false;
244   // clear the current context
245   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
246 }
247
248 void EglImplementation::TerminateGles()
249 {
250   if ( mGlesInitialized )
251   {
252     // in latest Mali DDK (r2p3 ~ r3p0 in April, 2012),
253     // MakeContextNull should be called before eglDestroy surface
254     // to prevent crash in _mali_surface_destroy_callback
255     MakeContextNull();
256
257     if(mIsOwnSurface && mEglSurface)
258     {
259       eglDestroySurface(mEglDisplay, mEglSurface);
260     }
261     eglDestroyContext(mEglDisplay, mEglContext);
262
263     eglTerminate(mEglDisplay);
264
265     mEglDisplay = NULL;
266     mEglConfig  = NULL;
267     mEglContext = NULL;
268     mEglSurface = NULL;
269
270     mGlesInitialized = false;
271   }
272 }
273
274 bool EglImplementation::IsGlesInitialized() const
275 {
276   return mGlesInitialized;
277 }
278
279 bool EglImplementation::SetRefreshSync( SyncMode mode )
280 {
281   if ( mIsWindow == false )
282   {
283     return false;
284   }
285   mSyncMode = mode;
286
287   // eglSwapInterval specifies the minimum number of video frame periods
288   // per buffer swap for the window associated with the current context.
289
290   if ( !mContextCurrent )
291   {
292     return true;
293   }
294
295   EGLBoolean ok = eglSwapInterval( mEglDisplay, mode );
296   if ( !ok )
297   {
298     TEST_EGL_ERROR("eglSwapInterval");
299     return false;
300   }
301
302   return true;
303 }
304
305 void EglImplementation::SwapBuffers()
306 {
307   eglSwapBuffers( mEglDisplay, mEglSurface );
308 }
309
310 void EglImplementation::CopyBuffers()
311 {
312   eglCopyBuffers( mEglDisplay, mEglSurface, mEglNativePixmap );
313 }
314
315 void EglImplementation::WaitGL()
316 {
317   eglWaitGL();
318 }
319
320 void EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
321 {
322   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
323   {
324     return;
325   }
326
327   mIsWindow = isWindowType;
328
329   EGLint numConfigs;
330   Vector<EGLint> configAttribs;
331   configAttribs.Reserve(31);
332
333   if(isWindowType)
334   {
335     configAttribs.PushBack( EGL_SURFACE_TYPE );
336     configAttribs.PushBack( EGL_WINDOW_BIT );
337   }
338   else
339   {
340     configAttribs.PushBack( EGL_SURFACE_TYPE );
341     configAttribs.PushBack( EGL_PIXMAP_BIT );
342   }
343
344   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
345
346 #if DALI_GLES_VERSION >= 30
347
348 #ifdef _ARCH_ARM_
349   configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
350 #else
351   // There is a bug in the desktop emulator
352   // Requesting for ES3 causes eglCreateContext even though it allows to ask
353   // for a configuration that supports GLES 3.0
354   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
355 #endif // _ARCH_ARM_
356
357 #else // DALI_GLES_VERSION >= 30
358
359   configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
360
361 #endif //DALI_GLES_VERSION >= 30
362
363 #if DALI_GLES_VERSION >= 30
364 // TODO: enable this flag when it becomes supported
365 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
366 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
367 #endif //DALI_GLES_VERSION >= 30
368
369   configAttribs.PushBack( EGL_RED_SIZE );
370   configAttribs.PushBack( 8 );
371   configAttribs.PushBack( EGL_GREEN_SIZE );
372   configAttribs.PushBack( 8 );
373   configAttribs.PushBack( EGL_BLUE_SIZE );
374   configAttribs.PushBack( 8 );
375
376   configAttribs.PushBack( EGL_ALPHA_SIZE );
377 #ifdef _ARCH_ARM_
378   configAttribs.PushBack( (depth == COLOR_DEPTH_32) ? 8 : 0 );
379 #else
380   // There is a bug in the desktop emulator
381   // setting EGL_ALPHA_SIZE to 8 results in eglChooseConfig failing
382   configAttribs.PushBack( 0 );
383 #endif // _ARCH_ARM_
384
385   configAttribs.PushBack( EGL_DEPTH_SIZE );
386   configAttribs.PushBack( 24 );
387   configAttribs.PushBack( EGL_STENCIL_SIZE );
388   configAttribs.PushBack( 8 );
389   configAttribs.PushBack( EGL_SAMPLES );
390   configAttribs.PushBack( 4 );
391   configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
392   configAttribs.PushBack( 1 );
393   configAttribs.PushBack( EGL_NONE );
394
395   if ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE )
396   {
397     EGLint error = eglGetError();
398     switch (error)
399     {
400       case EGL_BAD_DISPLAY:
401       {
402         DALI_LOG_ERROR("Display is not an EGL display connection");
403         break;
404       }
405       case EGL_BAD_ATTRIBUTE:
406       {
407         DALI_LOG_ERROR("The parameter confirAttribs contains an invalid frame buffer configuration attribute or an attribute value that is unrecognized or out of range");
408         break;
409       }
410       case EGL_NOT_INITIALIZED:
411       {
412         DALI_LOG_ERROR("Display has not been initialized");
413         break;
414       }
415       case EGL_BAD_PARAMETER:
416       {
417         DALI_LOG_ERROR("The parameter numConfig is NULL");
418         break;
419       }
420       default:
421       {
422         DALI_LOG_ERROR("Unknown error");
423       }
424     }
425     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
426   }
427
428   if ( numConfigs != 1 )
429   {
430     DALI_LOG_ERROR("No configurations found.");
431
432     TEST_EGL_ERROR("eglChooseConfig");
433   }
434 }
435
436
437 void EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
438 {
439   DALI_ASSERT_ALWAYS( ( mEglSurface == 0 ) && "EGL surface already exists" );
440
441   mEglNativeWindow = window;
442   mColorDepth = depth;
443   mIsWindow = true;
444
445   // egl choose config
446   ChooseConfig(mIsWindow, mColorDepth);
447
448   mEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
449   TEST_EGL_ERROR("eglCreateWindowSurface");
450
451   DALI_ASSERT_ALWAYS( mEglSurface && "Create window surface failed" );
452 }
453
454 void EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
455 {
456   DALI_ASSERT_ALWAYS( mEglSurface == 0 && "Cannot create more than one instance of surface pixmap" );
457
458   mEglNativePixmap = pixmap;
459   mColorDepth = depth;
460   mIsWindow = false;
461
462   // egl choose config
463   ChooseConfig(mIsWindow, mColorDepth);
464
465   mEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mEglNativePixmap, NULL );
466   TEST_EGL_ERROR("eglCreatePixmapSurface");
467
468   DALI_ASSERT_ALWAYS( mEglSurface && "Create pixmap surface failed" );
469 }
470
471 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLNativeDisplayType display )
472 {
473   bool contextLost = false;
474
475   //  the surface is bound to the context, so set the context to null
476   MakeContextNull();
477
478   // destroy the surface
479   DestroySurface();
480
481   // If the display has not changed, then we can just create a new surface
482   if ( display == mEglNativeDisplay )
483   {
484     // create the EGL surface
485     CreateSurfaceWindow( window, mColorDepth );
486
487     // set the context to be current with the new surface
488     MakeContextCurrent();
489   }
490   else  // the display has changed, we need to start egl with a new x-connection
491   {
492     // Note! this code path is untested
493
494     // this will release all EGL specific resources
495     eglTerminate( mEglDisplay );
496
497     mGlesInitialized = false;
498
499     // let the adaptor know that all resources have been lost
500     contextLost = true;
501
502     // re-initialise GLES with the new connection
503     InitializeGles( display );
504
505     // create the EGL surface
506     CreateSurfaceWindow( window, mColorDepth );
507
508     // create the OpenGL context
509     CreateContext();
510
511     // Make it current
512     MakeContextCurrent();
513   }
514
515   return contextLost;
516 }
517
518 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLNativeDisplayType display )
519 {
520   bool contextLost = false;
521
522   //  the surface is bound to the context, so set the context to null
523   MakeContextNull();
524
525   // destroy the surface
526   DestroySurface();
527
528   // If the display has not changed, then we can just create a new surface
529   if ( display == mEglNativeDisplay )
530   {
531     // create the EGL surface
532     CreateSurfacePixmap( pixmap, mColorDepth );
533
534     // set the context to be current with the new surface
535     MakeContextCurrent();
536   }
537   else  // the display has changed, we need to start egl with a new x-connection
538   {
539     // Note! this code path is untested
540
541     // this will release all EGL specific resources
542     eglTerminate( mEglDisplay );
543
544     mGlesInitialized = false;
545
546     // let the adaptor know that all resources have been lost
547     contextLost = true;
548
549     // re-initialise GLES with the new connection
550     InitializeGles( display );
551
552     // create the EGL surface
553     CreateSurfacePixmap( pixmap, mColorDepth );
554
555     // create the OpenGL context
556     CreateContext();
557
558     // Make it current
559     MakeContextCurrent();
560   }
561   return contextLost;
562 }
563
564 EGLDisplay EglImplementation::GetDisplay() const
565 {
566   return mEglDisplay;
567 }
568
569 EGLDisplay EglImplementation::GetContext() const
570 {
571   return mEglContext;
572 }
573
574 } // namespace Adaptor
575
576 } // namespace Internal
577
578 } // namespace Dali
579