Merge "(Partial update) Fix surface damage area" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles / egl-implementation.cpp
1 /*
2  * Copyright (c) 2020 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 <dali/internal/graphics/gles/egl-implementation.h>
21
22 // EXTERNAL INCLUDES
23 #include <sstream>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/dali-vector.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/public-api/dali-adaptor-common.h>
29 #include <dali/internal/graphics/gles/gl-implementation.h>
30 #include <dali/internal/graphics/gles/egl-debug.h>
31
32 // EGL constants use C style casts
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wold-style-cast"
35
36 namespace
37 {
38   const uint32_t THRESHOLD_SWAPBUFFER_COUNT = 5;
39   const uint32_t CHECK_EXTENSION_NUMBER = 2;
40   const std::string EGL_KHR_SURFACELESS_CONTEXT = "EGL_KHR_surfaceless_context";
41   const std::string EGL_KHR_CREATE_CONTEXT = "EGL_KHR_create_context";
42 }
43
44 namespace Dali
45 {
46
47 namespace Internal
48 {
49
50 namespace Adaptor
51 {
52
53 #define TEST_EGL_ERROR(lastCommand) \
54 { \
55   EGLint err = eglGetError(); \
56   if (err != EGL_SUCCESS) \
57   { \
58     DALI_LOG_ERROR("EGL error after %s\n", lastCommand); \
59     Egl::PrintError(err); \
60     DALI_ASSERT_ALWAYS(0 && "EGL error"); \
61   } \
62 }
63
64 EglImplementation::EglImplementation( int multiSamplingLevel,
65                                       Integration::DepthBufferAvailable depthBufferRequired,
66                                       Integration::StencilBufferAvailable stencilBufferRequired ,
67                                       Integration::PartialUpdateAvailable partialUpdateRequired )
68 : mContextAttribs(),
69   mEglNativeDisplay( 0 ),
70   mEglNativeWindow( 0 ),
71   mCurrentEglNativePixmap( 0 ),
72   mEglDisplay( 0 ),
73   mEglConfig( 0 ),
74   mEglContext( 0 ),
75   mCurrentEglSurface( 0 ),
76   mCurrentEglContext( EGL_NO_CONTEXT ),
77   mMultiSamplingLevel( multiSamplingLevel ),
78   mGlesVersion( 30 ),
79   mColorDepth( COLOR_DEPTH_24 ),
80   mGlesInitialized( false ),
81   mIsOwnSurface( true ),
82   mIsWindow( true ),
83   mDepthBufferRequired( depthBufferRequired == Integration::DepthBufferAvailable::TRUE ),
84   mStencilBufferRequired( stencilBufferRequired == Integration::StencilBufferAvailable::TRUE ),
85   mPartialUpdateRequired( partialUpdateRequired == Integration::PartialUpdateAvailable::TRUE ),
86   mIsSurfacelessContextSupported( false ),
87   mIsKhrCreateContextSupported( false ),
88   mSwapBufferCountAfterResume( 0 ),
89   mEglSetDamageRegionKHR( 0 ),
90   mEglSwapBuffersWithDamageKHR( 0 ),
91   mFullSwapNextFrame( true )
92 {
93 }
94
95 EglImplementation::~EglImplementation()
96 {
97   TerminateGles();
98 }
99
100 bool EglImplementation::InitializeGles( EGLNativeDisplayType display, bool isOwnSurface )
101 {
102   if ( !mGlesInitialized )
103   {
104     mEglNativeDisplay = display;
105
106     // Try to get the display connection for the native display first
107     mEglDisplay = eglGetDisplay( mEglNativeDisplay );
108
109     if( mEglDisplay == EGL_NO_DISPLAY )
110     {
111       // If failed, try to get the default display connection
112       mEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
113     }
114
115     if( mEglDisplay == EGL_NO_DISPLAY )
116     {
117       // Still failed to get a display connection
118       throw Dali::DaliException( "", "OpenGL ES is not supported");
119     }
120
121     EGLint majorVersion = 0;
122     EGLint minorVersion = 0;
123     if ( !eglInitialize( mEglDisplay, &majorVersion, &minorVersion ) )
124     {
125       return false;
126     }
127     eglBindAPI(EGL_OPENGL_ES_API);
128
129     mIsOwnSurface = isOwnSurface;
130   }
131
132   // Query EGL extensions to check whether surfaceless context is supported
133   const char* const extensionStr = eglQueryString( mEglDisplay, EGL_EXTENSIONS );
134   std::istringstream stream( extensionStr );
135   std::string currentExtension;
136   uint32_t extensionCheckCount = 0;
137   while( std::getline( stream, currentExtension, ' ' ) && extensionCheckCount < CHECK_EXTENSION_NUMBER )
138   {
139     if( currentExtension == EGL_KHR_SURFACELESS_CONTEXT )
140     {
141       mIsSurfacelessContextSupported = true;
142       extensionCheckCount++;
143     }
144     if( currentExtension == EGL_KHR_CREATE_CONTEXT )
145     {
146       mIsKhrCreateContextSupported = true;
147       extensionCheckCount++;
148     }
149   }
150
151   mGlesInitialized = true;
152
153   // We want to display this information all the time, so use the LogMessage directly
154   Integration::Log::LogMessage(Integration::Log::DebugInfo, "EGL Information\n"
155       "            PartialUpdate  %d\n"
156       "            Vendor:        %s\n"
157       "            Version:       %s\n"
158       "            Client APIs:   %s\n"
159       "            Extensions:    %s\n",
160       mPartialUpdateRequired,
161       eglQueryString( mEglDisplay, EGL_VENDOR ),
162       eglQueryString( mEglDisplay, EGL_VERSION ),
163       eglQueryString( mEglDisplay, EGL_CLIENT_APIS ),
164       extensionStr);
165
166   return mGlesInitialized;
167 }
168
169 bool EglImplementation::CreateContext()
170 {
171   // make sure a context isn't created twice
172   DALI_ASSERT_ALWAYS( (mEglContext == 0) && "EGL context recreated" );
173
174   mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, &(mContextAttribs[0]));
175   TEST_EGL_ERROR("eglCreateContext render thread");
176
177   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != mEglContext && "EGL context not created" );
178
179   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
180   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
181   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
182   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
183   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
184
185   mEglSetDamageRegionKHR = reinterpret_cast<PFNEGLSETDAMAGEREGIONKHRPROC>(eglGetProcAddress("eglSetDamageRegionKHR"));
186   if (!mEglSetDamageRegionKHR)
187   {
188     DALI_LOG_ERROR("Coudn't find eglSetDamageRegionKHR!\n");
189     mPartialUpdateRequired = false;
190   }
191   mEglSwapBuffersWithDamageKHR = reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>(eglGetProcAddress("eglSwapBuffersWithDamageKHR"));
192   if (!mEglSwapBuffersWithDamageKHR)
193   {
194     DALI_LOG_ERROR("Coudn't find eglSwapBuffersWithDamageKHR!\n");
195     mPartialUpdateRequired = false;
196   }
197   return true;
198 }
199
200 bool EglImplementation::CreateWindowContext( EGLContext& eglContext )
201 {
202   // make sure a context isn't created twice
203   DALI_ASSERT_ALWAYS( (eglContext == 0) && "EGL context recreated" );
204
205   eglContext = eglCreateContext(mEglDisplay, mEglConfig, mEglContext, &(mContextAttribs[0]));
206   TEST_EGL_ERROR("eglCreateContext render thread");
207
208   DALI_ASSERT_ALWAYS( EGL_NO_CONTEXT != eglContext && "EGL context not created" );
209
210   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VENDOR : %s ***\n", glGetString(GL_VENDOR));
211   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_RENDERER : %s ***\n", glGetString(GL_RENDERER));
212   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_VERSION : %s ***\n", glGetString(GL_VERSION));
213   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** GL_SHADING_LANGUAGE_VERSION : %s***\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
214   DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "*** Supported Extensions ***\n%s\n\n", glGetString(GL_EXTENSIONS));
215
216   mEglWindowContexts.push_back( eglContext );
217
218   mEglSetDamageRegionKHR = reinterpret_cast<PFNEGLSETDAMAGEREGIONKHRPROC>(eglGetProcAddress("eglSetDamageRegionKHR"));
219   if (!mEglSetDamageRegionKHR)
220   {
221     DALI_LOG_ERROR("Coudn't find eglSetDamageRegionKHR!\n");
222     mPartialUpdateRequired = false;
223   }
224   mEglSwapBuffersWithDamageKHR = reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>(eglGetProcAddress("eglSwapBuffersWithDamageKHR"));
225   if (!mEglSwapBuffersWithDamageKHR)
226   {
227     DALI_LOG_ERROR("Coudn't find eglSwapBuffersWithDamageKHR!\n");
228     mPartialUpdateRequired = false;
229   }
230   return true;
231 }
232
233 void EglImplementation::DestroyContext( EGLContext& eglContext )
234 {
235   if( eglContext )
236   {
237     eglDestroyContext( mEglDisplay, eglContext );
238     eglContext = 0;
239   }
240 }
241
242 void EglImplementation::DestroySurface( EGLSurface& eglSurface )
243 {
244   if(mIsOwnSurface && eglSurface)
245   {
246     // Make context null to prevent crash in driver side
247     MakeContextNull();
248     eglDestroySurface( mEglDisplay, eglSurface );
249     eglSurface = 0;
250   }
251 }
252
253 void EglImplementation::MakeContextCurrent( EGLSurface eglSurface, EGLContext eglContext )
254 {
255   if (mCurrentEglContext == eglContext)
256   {
257     return;
258   }
259
260   mCurrentEglSurface = eglSurface;
261
262   if(mIsOwnSurface)
263   {
264     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, eglContext );
265
266     mCurrentEglContext = eglContext;
267   }
268
269   EGLint error = eglGetError();
270
271   if ( error != EGL_SUCCESS )
272   {
273     Egl::PrintError(error);
274
275     DALI_ASSERT_ALWAYS(false && "MakeContextCurrent failed!");
276   }
277 }
278
279 void EglImplementation::MakeCurrent( EGLNativePixmapType pixmap, EGLSurface eglSurface )
280 {
281   if (mCurrentEglContext == mEglContext)
282   {
283     return;
284   }
285
286   mCurrentEglNativePixmap = pixmap;
287   mCurrentEglSurface = eglSurface;
288
289   if(mIsOwnSurface)
290   {
291     eglMakeCurrent( mEglDisplay, eglSurface, eglSurface, mEglContext );
292
293     mCurrentEglContext = mEglContext;
294   }
295
296   EGLint error = eglGetError();
297
298   if ( error != EGL_SUCCESS )
299   {
300     Egl::PrintError(error);
301
302     DALI_ASSERT_ALWAYS(false && "MakeCurrent failed!");
303   }
304 }
305
306 void EglImplementation::MakeContextNull()
307 {
308   // clear the current context
309   eglMakeCurrent( mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
310   mCurrentEglContext = EGL_NO_CONTEXT;
311 }
312
313 void EglImplementation::TerminateGles()
314 {
315   if ( mGlesInitialized )
316   {
317     // Make context null to prevent crash in driver side
318     MakeContextNull();
319
320     for ( auto eglSurface : mEglWindowSurfaces )
321     {
322       if(mIsOwnSurface && eglSurface)
323       {
324         eglDestroySurface(mEglDisplay, eglSurface);
325       }
326     }
327     eglDestroyContext(mEglDisplay, mEglContext);
328     for ( auto eglContext : mEglWindowContexts )
329     {
330       eglDestroyContext(mEglDisplay, eglContext);
331     }
332
333     eglTerminate(mEglDisplay);
334
335     mEglDisplay = NULL;
336     mEglConfig  = NULL;
337     mEglContext = NULL;
338     mCurrentEglSurface = NULL;
339     mCurrentEglContext = EGL_NO_CONTEXT;
340
341     mGlesInitialized = false;
342   }
343 }
344
345 bool EglImplementation::IsGlesInitialized() const
346 {
347   return mGlesInitialized;
348 }
349
350 void EglImplementation::SwapBuffers( EGLSurface& eglSurface )
351 {
352   if ( eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
353   {
354 #ifndef DALI_PROFILE_UBUNTU
355     if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
356     {
357       DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
358     }
359 #endif //DALI_PROFILE_UBUNTU
360
361     // DALI_LOG_ERROR("EglImplementation::SwapBuffers()\n");
362     eglSwapBuffers( mEglDisplay, eglSurface );
363     mFullSwapNextFrame = false;
364
365 #ifndef DALI_PROFILE_UBUNTU
366     if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
367     {
368       DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
369       mSwapBufferCountAfterResume++;
370     }
371 #endif //DALI_PROFILE_UBUNTU
372   }
373 }
374
375 EGLint EglImplementation::GetBufferAge(EGLSurface& eglSurface) const
376 {
377   EGLint age = 0;
378   eglQuerySurface(mEglDisplay, eglSurface, EGL_BUFFER_AGE_EXT, &age);
379   if (age < 0)
380   {
381     DALI_LOG_ERROR("eglQuerySurface(%d)\n", eglGetError());
382     age = 0;
383   }
384
385   // 0 - invalid buffer
386   // 1, 2, 3
387   if (age > 3)
388   {
389     DALI_LOG_ERROR("EglImplementation::GetBufferAge() buffer age %d > 3\n", age);
390     age = 0; // shoudn't be more than 3 back buffers, if there is just reset, I don't want to add extra history level
391   }
392
393   return age;
394 }
395
396 void EglImplementation::SetFullSwapNextFrame()
397 {
398   mFullSwapNextFrame = true;
399 }
400
401 void mergeRects(Rect<int>& mergingRect, const std::vector<Rect<int>>& rects)
402 {
403   uint32_t i = 0;
404   if (mergingRect.IsEmpty())
405   {
406     for (;i < rects.size(); i++)
407     {
408       if (!rects[i].IsEmpty())
409       {
410         mergingRect = rects[i];
411         break;
412       }
413     }
414   }
415
416   for (;i < rects.size(); i++)
417   {
418     mergingRect.Merge(rects[i]);
419   }
420 }
421
422 void insertRects(std::list<std::vector<Rect<int>>>& damagedRectsList, const std::vector<Rect<int>>& damagedRects)
423 {
424   damagedRectsList.push_front(damagedRects);
425   if (damagedRectsList.size() > 4) // past triple buffers + current
426   {
427     damagedRectsList.pop_back();
428   }
429 }
430
431 void EglImplementation::SetDamage( EGLSurface& eglSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect )
432 {
433   if (!mPartialUpdateRequired)
434   {
435     return;
436   }
437
438   if (eglSurface != EGL_NO_SURFACE) // skip if using surfaceless context
439   {
440     EGLint width = 0;
441     EGLint height = 0;
442     eglQuerySurface(mEglDisplay, eglSurface, EGL_WIDTH, &width);
443     eglQuerySurface(mEglDisplay, eglSurface, EGL_HEIGHT, &height);
444     Rect<int> surfaceRect(0, 0, width, height);
445
446     mSurfaceRect = surfaceRect;
447
448     if (mFullSwapNextFrame)
449     {
450       insertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
451       clippingRect = Rect<int>();
452       return;
453     }
454
455     EGLint bufferAge = GetBufferAge(eglSurface);
456
457     // Buffer age 0 means the back buffer in invalid and requires full swap
458     if (!damagedRects.size() || bufferAge == 0)
459     {
460       // No damage or buffer is out of order or buffer age is reset
461       insertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
462       clippingRect = Rect<int>();
463       return;
464     }
465
466     // We push current frame damaged rects here, zero index for current frame
467     insertRects(mBufferDamagedRects, damagedRects);
468
469     // Merge damaged rects into clipping rect
470     auto bufferDamagedRects = mBufferDamagedRects.begin();
471     while (bufferAge-- >= 0 && bufferDamagedRects != mBufferDamagedRects.end())
472     {
473       const std::vector<Rect<int>>& rects = *bufferDamagedRects++;
474       mergeRects(clippingRect, rects);
475     }
476
477     if (!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * 0.8)
478     {
479       // clipping area too big or doesn't intersect surface rect
480       clippingRect = Rect<int>();
481       return;
482     }
483
484     // DALI_LOG_ERROR("eglSetDamageRegionKHR(%d, %d, %d, %d)\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
485     EGLBoolean result = mEglSetDamageRegionKHR(mEglDisplay, eglSurface, reinterpret_cast<int*>(&clippingRect), 1);
486     if (result == EGL_FALSE)
487     {
488       DALI_LOG_ERROR("eglSetDamageRegionKHR(%d)\n", eglGetError());
489     }
490   }
491 }
492
493 void EglImplementation::SwapBuffers(EGLSurface& eglSurface, const std::vector<Rect<int>>& damagedRects)
494 {
495   if (eglSurface != EGL_NO_SURFACE ) // skip if using surfaceless context
496   {
497     if (!mPartialUpdateRequired || mFullSwapNextFrame || !damagedRects.size() || (damagedRects[0].Area() > mSurfaceRect.Area() * 0.8) )
498     {
499       SwapBuffers(eglSurface);
500       return;
501     }
502
503 #ifndef DALI_PROFILE_UBUNTU
504     if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
505     {
506       DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers started.\n" );
507     }
508 #endif //DALI_PROFILE_UBUNTU
509
510     std::vector< Rect< int > > mergedRects = damagedRects;
511
512     // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
513     // Could be optional and can be removed, needs to be checked with and without on platform
514     const int n = mergedRects.size();
515     for(int i = 0; i < n-1; i++)
516     {
517       if (mergedRects[i].IsEmpty())
518       {
519         continue;
520       }
521
522       for (int j = i+1; j < n; j++)
523       {
524         if (mergedRects[j].IsEmpty())
525         {
526           continue;
527         }
528
529         if (mergedRects[i].Intersects(mergedRects[j]))
530         {
531           mergedRects[i].Merge(mergedRects[j]);
532           mergedRects[j].width = 0;
533           mergedRects[j].height = 0;
534         }
535       }
536     }
537
538     int j = 0;
539     for (int i = 0; i < n; i++)
540     {
541       if (!mergedRects[i].IsEmpty())
542       {
543         mergedRects[j++] = mergedRects[i];
544       }
545     }
546
547     if (j != 0)
548     {
549       mergedRects.resize(j);
550     }
551
552     if (!mergedRects.size() || (mergedRects[0].Area() > mSurfaceRect.Area() * 0.8))
553     {
554       SwapBuffers(eglSurface);
555       return;
556     }
557
558     EGLBoolean result = mEglSwapBuffersWithDamageKHR(mEglDisplay, eglSurface, reinterpret_cast<int*>(mergedRects.data()), mergedRects.size());
559     if (result == EGL_FALSE)
560     {
561       DALI_LOG_ERROR("eglSwapBuffersWithDamageKHR(%d)\n", eglGetError());
562     }
563
564 #ifndef DALI_PROFILE_UBUNTU
565     if( mSwapBufferCountAfterResume < THRESHOLD_SWAPBUFFER_COUNT )
566     {
567       DALI_LOG_RELEASE_INFO( "EglImplementation::SwapBuffers finished.\n" );
568       mSwapBufferCountAfterResume++;
569     }
570 #endif //DALI_PROFILE_UBUNTU
571   }
572 }
573
574 void EglImplementation::CopyBuffers( EGLSurface& eglSurface )
575 {
576   eglCopyBuffers( mEglDisplay, eglSurface, mCurrentEglNativePixmap );
577 }
578
579 void EglImplementation::WaitGL()
580 {
581   eglWaitGL();
582 }
583
584 bool EglImplementation::ChooseConfig( bool isWindowType, ColorDepth depth )
585 {
586   if(mEglConfig && isWindowType == mIsWindow && mColorDepth == depth)
587   {
588     return true;
589   }
590
591   mColorDepth = depth;
592   mIsWindow = isWindowType;
593
594   EGLint numConfigs;
595   Vector<EGLint> configAttribs;
596   configAttribs.Reserve(31);
597
598   if(isWindowType)
599   {
600     configAttribs.PushBack( EGL_SURFACE_TYPE );
601     configAttribs.PushBack( EGL_WINDOW_BIT );
602   }
603   else
604   {
605     configAttribs.PushBack( EGL_SURFACE_TYPE );
606     configAttribs.PushBack( EGL_PIXMAP_BIT );
607   }
608
609   configAttribs.PushBack( EGL_RENDERABLE_TYPE );
610
611   if( mGlesVersion >= 30 )
612   {
613     configAttribs.PushBack( EGL_OPENGL_ES3_BIT_KHR );
614   }
615   else
616   {
617     configAttribs.PushBack( EGL_OPENGL_ES2_BIT );
618   }
619
620 // TODO: enable this flag when it becomes supported
621 //  configAttribs.PushBack( EGL_CONTEXT_FLAGS_KHR );
622 //  configAttribs.PushBack( EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR );
623
624   configAttribs.PushBack( EGL_RED_SIZE );
625   configAttribs.PushBack( 8 );
626   configAttribs.PushBack( EGL_GREEN_SIZE );
627   configAttribs.PushBack( 8 );
628   configAttribs.PushBack( EGL_BLUE_SIZE );
629   configAttribs.PushBack( 8 );
630
631 //  For underlay video playback, we also need to set the alpha value of the 24/32bit window.
632   configAttribs.PushBack( EGL_ALPHA_SIZE );
633   configAttribs.PushBack( 8 );
634
635   configAttribs.PushBack( EGL_DEPTH_SIZE );
636   configAttribs.PushBack( mDepthBufferRequired ? 24 : 0 );
637   configAttribs.PushBack( EGL_STENCIL_SIZE );
638   configAttribs.PushBack( mStencilBufferRequired ? 8 : 0 );
639
640 #ifndef DALI_PROFILE_UBUNTU
641   if( mMultiSamplingLevel != EGL_DONT_CARE )
642   {
643     configAttribs.PushBack( EGL_SAMPLES );
644     configAttribs.PushBack( mMultiSamplingLevel );
645     configAttribs.PushBack( EGL_SAMPLE_BUFFERS );
646     configAttribs.PushBack( 1 );
647   }
648 #endif // DALI_PROFILE_UBUNTU
649   configAttribs.PushBack( EGL_NONE );
650
651   // Ensure number of configs is set to 1 as on some drivers,
652   // eglChooseConfig succeeds but does not actually create a proper configuration.
653   if ( ( eglChooseConfig( mEglDisplay, &(configAttribs[0]), &mEglConfig, 1, &numConfigs ) != EGL_TRUE ) ||
654        ( numConfigs != 1 ) )
655   {
656     if( mGlesVersion >= 30 )
657     {
658       mEglConfig = NULL;
659       DALI_LOG_ERROR("Fail to use OpenGL es 3.0. Retrying to use OpenGL es 2.0.");
660       return false;
661     }
662
663     if ( numConfigs != 1 )
664     {
665       DALI_LOG_ERROR("No configurations found.\n");
666
667       TEST_EGL_ERROR("eglChooseConfig");
668     }
669
670     EGLint error = eglGetError();
671     switch (error)
672     {
673       case EGL_BAD_DISPLAY:
674       {
675         DALI_LOG_ERROR("Display is not an EGL display connection\n");
676         break;
677       }
678       case EGL_BAD_ATTRIBUTE:
679       {
680         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");
681         break;
682       }
683       case EGL_NOT_INITIALIZED:
684       {
685         DALI_LOG_ERROR("Display has not been initialized\n");
686         break;
687       }
688       case EGL_BAD_PARAMETER:
689       {
690         DALI_LOG_ERROR("The parameter numConfig is NULL\n");
691         break;
692       }
693       default:
694       {
695         DALI_LOG_ERROR("Unknown error.\n");
696       }
697     }
698     DALI_ASSERT_ALWAYS(false && "eglChooseConfig failed!");
699     return false;
700   }
701   Integration::Log::LogMessage(Integration::Log::DebugInfo, "Using OpenGL es %d.%d.\n", mGlesVersion / 10, mGlesVersion % 10 );
702
703   mContextAttribs.Clear();
704   if( mIsKhrCreateContextSupported )
705   {
706     mContextAttribs.Reserve(5);
707     mContextAttribs.PushBack( EGL_CONTEXT_MAJOR_VERSION_KHR );
708     mContextAttribs.PushBack( mGlesVersion / 10 );
709     mContextAttribs.PushBack( EGL_CONTEXT_MINOR_VERSION_KHR );
710     mContextAttribs.PushBack( mGlesVersion % 10 );
711   }
712   else
713   {
714     mContextAttribs.Reserve(3);
715     mContextAttribs.PushBack( EGL_CONTEXT_CLIENT_VERSION );
716     mContextAttribs.PushBack( mGlesVersion / 10 );
717   }
718   mContextAttribs.PushBack( EGL_NONE );
719
720   return true;
721 }
722
723 EGLSurface EglImplementation::CreateSurfaceWindow( EGLNativeWindowType window, ColorDepth depth )
724 {
725   mEglNativeWindow = window;
726   mColorDepth = depth;
727   mIsWindow = true;
728
729   // egl choose config
730   ChooseConfig(mIsWindow, mColorDepth);
731
732   mCurrentEglSurface = eglCreateWindowSurface( mEglDisplay, mEglConfig, mEglNativeWindow, NULL );
733   TEST_EGL_ERROR("eglCreateWindowSurface");
734
735   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create window surface failed" );
736
737   return mCurrentEglSurface;
738 }
739
740 EGLSurface EglImplementation::CreateSurfacePixmap( EGLNativePixmapType pixmap, ColorDepth depth )
741 {
742   mCurrentEglNativePixmap = pixmap;
743   mColorDepth = depth;
744   mIsWindow = false;
745
746   // egl choose config
747   ChooseConfig(mIsWindow, mColorDepth);
748
749   mCurrentEglSurface = eglCreatePixmapSurface( mEglDisplay, mEglConfig, mCurrentEglNativePixmap, NULL );
750   TEST_EGL_ERROR("eglCreatePixmapSurface");
751
752   DALI_ASSERT_ALWAYS( mCurrentEglSurface && "Create pixmap surface failed" );
753
754   return mCurrentEglSurface;
755 }
756
757 bool EglImplementation::ReplaceSurfaceWindow( EGLNativeWindowType window, EGLSurface& eglSurface, EGLContext& eglContext )
758 {
759   bool contextLost = false;
760
761   // display connection has not changed, then we can just create a new surface
762   //  the surface is bound to the context, so set the context to null
763   MakeContextNull();
764
765   if( eglSurface )
766   {
767     // destroy the surface
768     DestroySurface( eglSurface );
769   }
770
771   // create the EGL surface
772   EGLSurface newEglSurface = CreateSurfaceWindow( window, mColorDepth );
773
774   // set the context to be current with the new surface
775   MakeContextCurrent( newEglSurface, eglContext );
776
777   return contextLost;
778 }
779
780 bool EglImplementation::ReplaceSurfacePixmap( EGLNativePixmapType pixmap, EGLSurface& eglSurface )
781 {
782   bool contextLost = false;
783
784   // display connection has not changed, then we can just create a new surface
785   // create the EGL surface
786   eglSurface = CreateSurfacePixmap( pixmap, mColorDepth );
787
788   // set the eglSurface to be current
789   MakeCurrent( pixmap, eglSurface );
790
791   return contextLost;
792 }
793
794 void EglImplementation::SetGlesVersion( const int32_t glesVersion )
795 {
796   mGlesVersion = glesVersion;
797 }
798
799 void EglImplementation::SetFirstFrameAfterResume()
800 {
801   mSwapBufferCountAfterResume = 0;
802 }
803
804 EGLDisplay EglImplementation::GetDisplay() const
805 {
806   return mEglDisplay;
807 }
808
809 EGLContext EglImplementation::GetContext() const
810 {
811   return mEglContext;
812 }
813
814 int32_t EglImplementation::GetGlesVersion() const
815 {
816   return mGlesVersion;
817 }
818
819 bool EglImplementation::IsSurfacelessContextSupported() const
820 {
821   return mIsSurfacelessContextSupported;
822 }
823
824 void EglImplementation::WaitClient()
825 {
826   // Wait for EGL to finish executing all rendering calls for the current context
827   if ( eglWaitClient() != EGL_TRUE )
828   {
829     TEST_EGL_ERROR("eglWaitClient");
830   }
831 }
832
833 } // namespace Adaptor
834
835 } // namespace Internal
836
837 } // namespace Dali
838
839 #pragma GCC diagnostic pop