2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "egl-image-extensions.h"
23 #if DALI_GLES_VERSION >= 30
24 #include <GLES3/gl3.h>
26 #include <GLES2/gl2.h>
27 #endif // DALI_GLES_VERSION >= 30
29 #include <GLES2/gl2ext.h>
31 #include <EGL/eglext.h>
33 #include <dali/integration-api/debug.h>
36 #include <gl/egl-implementation.h>
41 // function pointers assigned in InitializeEglImageKHR
42 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHRProc = 0;
43 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHRProc = 0;
44 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESProc = 0;
45 } // unnamed namespace
57 EglImageExtensions::EglImageExtensions(EglImplementation* eglImpl)
58 : mEglImplementation(eglImpl),
59 mImageKHRInitialized(false),
60 mImageKHRInitializeFailed(false)
62 DALI_ASSERT_ALWAYS( eglImpl && "EGL Implementation not instantiated" );
65 EglImageExtensions::~EglImageExtensions()
69 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
71 if (mImageKHRInitialized == false)
73 InitializeEglImageKHR();
76 if (mImageKHRInitialized == false)
81 // Use the EGL image extension
82 const EGLint attribs[] =
84 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
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(),
93 EGL_NATIVE_PIXMAP_KHR,
97 DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
98 if( EGL_NO_IMAGE_KHR == eglImage )
100 switch( eglGetError() )
106 case EGL_BAD_DISPLAY:
108 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
111 case EGL_BAD_CONTEXT:
113 DALI_LOG_ERROR( "EGL_BAD_CONTEXT: Invalid EGLContext object\n" );
116 case EGL_BAD_PARAMETER:
118 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n" );
123 DALI_LOG_ERROR( "EGL_BAD_MATCH: attrib_list does not match target\n" );
128 DALI_LOG_ERROR( "EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n" );
133 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
142 #pragma GCC diagnostic pop
147 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
149 DALI_ASSERT_DEBUG( mImageKHRInitialized );
151 if( ! mImageKHRInitialized )
156 if( eglImageKHR == NULL )
161 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
163 EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
165 if( EGL_FALSE == result )
167 switch( eglGetError() )
169 case EGL_BAD_DISPLAY:
171 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
174 case EGL_BAD_PARAMETER:
176 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
181 DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
192 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
194 DALI_ASSERT_DEBUG( mImageKHRInitialized );
196 if( eglImageKHR != NULL )
198 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
200 #ifdef EGL_ERROR_CHECKING
201 GLint glError = glGetError();
204 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
206 #ifdef EGL_ERROR_CHECKING
207 glError = glGetError();
208 if( GL_NO_ERROR != glError )
210 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
216 void EglImageExtensions::InitializeEglImageKHR()
218 // avoid trying to reload extended KHR functions, if it fails the first time
219 if( ! mImageKHRInitializeFailed )
221 eglCreateImageKHRProc = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
222 eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
223 glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
226 if (eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
228 mImageKHRInitialized = true;
232 mImageKHRInitializeFailed = true;
236 } // namespace Adaptor
238 } // namespace Internal