Enable GLES 3.0 by default.
[platform/core/uifw/dali-adaptor.git] / adaptors / common / gl / egl-image-extensions.cpp
1 /*
2  * Copyright (c) 2017 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 "egl-image-extensions.h"
21
22 // EXTERNAL INCLUDES
23 #if DALI_GLES_VERSION >= 30
24 #include <GLES3/gl3.h>
25 #else
26 #include <GLES2/gl2.h>
27 #endif // DALI_GLES_VERSION >= 30
28
29 #include <GLES2/gl2ext.h>
30
31 #include <EGL/eglext.h>
32
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
36 #include <gl/egl-implementation.h>
37
38
39 namespace
40 {
41 // function pointers assigned in InitializeEglImageKHR
42 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHRProc = 0;
43 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHRProc = 0;
44 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESProc = 0;
45 } // unnamed namespace
46
47
48 namespace Dali
49 {
50
51 namespace Internal
52 {
53
54 namespace Adaptor
55 {
56
57 EglImageExtensions::EglImageExtensions(EglImplementation* eglImpl)
58 : mEglImplementation(eglImpl),
59   mImageKHRInitialized(false),
60   mImageKHRInitializeFailed(false)
61 {
62   DALI_ASSERT_ALWAYS( eglImpl && "EGL Implementation not instantiated" );
63 }
64
65 EglImageExtensions::~EglImageExtensions()
66 {
67 }
68
69 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
70 {
71   if (mImageKHRInitialized == false)
72   {
73     InitializeEglImageKHR();
74   }
75
76   if (mImageKHRInitialized == false)
77   {
78     return NULL;
79   }
80
81   // Use the EGL image extension
82   const EGLint attribs[] =
83   {
84     EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
85     EGL_NONE
86   };
87
88 // EGL constants use C casts
89 #pragma GCC diagnostic push
90 #pragma GCC diagnostic ignored "-Wold-style-cast"
91   EGLImageKHR eglImage  = eglCreateImageKHRProc( mEglImplementation->GetDisplay(),
92                                              EGL_NO_CONTEXT,
93                                              EGL_NATIVE_PIXMAP_KHR,
94                                              clientBuffer,
95                                              attribs );
96
97   DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
98   if( EGL_NO_IMAGE_KHR == eglImage )
99   {
100     switch( eglGetError() )
101     {
102       case EGL_SUCCESS :
103       {
104         break;
105       }
106       case EGL_BAD_DISPLAY:
107       {
108         DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
109         break;
110       }
111       case EGL_BAD_CONTEXT:
112       {
113         DALI_LOG_ERROR( "EGL_BAD_CONTEXT: Invalid EGLContext object\n" );
114         break;
115       }
116       case EGL_BAD_PARAMETER:
117       {
118         DALI_LOG_ERROR( "EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n" );
119         break;
120       }
121       case EGL_BAD_MATCH:
122       {
123         DALI_LOG_ERROR( "EGL_BAD_MATCH: attrib_list does not match target\n" );
124         break;
125       }
126       case EGL_BAD_ACCESS:
127       {
128         DALI_LOG_ERROR( "EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n" );
129         break;
130       }
131       case EGL_BAD_ALLOC:
132       {
133         DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
134         break;
135       }
136       default:
137       {
138         break;
139       }
140     }
141   }
142 #pragma GCC diagnostic pop
143
144   return eglImage;
145 }
146
147 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
148 {
149   DALI_ASSERT_DEBUG( mImageKHRInitialized );
150
151   if( ! mImageKHRInitialized )
152   {
153     return;
154   }
155
156   if( eglImageKHR == NULL )
157   {
158     return;
159   }
160
161   EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
162
163   EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
164
165   if( EGL_FALSE == result )
166   {
167     switch( eglGetError() )
168     {
169       case EGL_BAD_DISPLAY:
170       {
171         DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
172         break;
173       }
174       case EGL_BAD_PARAMETER:
175       {
176         DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
177         break;
178       }
179       case EGL_BAD_ACCESS:
180       {
181         DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
182         break;
183       }
184       default:
185       {
186         break;
187       }
188     }
189   }
190 }
191
192 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
193 {
194   DALI_ASSERT_DEBUG( mImageKHRInitialized );
195
196   if( eglImageKHR != NULL )
197   {
198     EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
199
200 #ifdef EGL_ERROR_CHECKING
201     GLint glError = glGetError();
202 #endif
203
204     glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
205
206 #ifdef EGL_ERROR_CHECKING
207     glError = glGetError();
208     if( GL_NO_ERROR != glError )
209     {
210       DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
211     }
212 #endif
213   }
214 }
215
216 void EglImageExtensions::InitializeEglImageKHR()
217 {
218   // avoid trying to reload extended KHR functions, if it fails the first time
219   if( ! mImageKHRInitializeFailed )
220   {
221     eglCreateImageKHRProc  = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
222     eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
223     glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
224   }
225
226   if (eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
227   {
228     mImageKHRInitialized = true;
229   }
230   else
231   {
232     mImageKHRInitializeFailed = true;
233   }
234 }
235
236 } // namespace Adaptor
237
238 } // namespace Internal
239
240 } // namespace Dali