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>
25 #include <GLES3/gl3ext.h>
28 #include <GLES2/gl2.h>
29 #endif // DALI_GLES_VERSION >= 30
31 #include <GLES2/gl2ext.h>
33 #include <EGL/eglext.h>
35 #include <dali/integration-api/debug.h>
38 #include <gl/egl-implementation.h>
43 // function pointers assigned in InitializeEglImageKHR
44 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHRProc = 0;
45 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHRProc = 0;
46 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESProc = 0;
47 } // unnamed namespace
59 EglImageExtensions::EglImageExtensions(EglImplementation* eglImpl)
60 : mEglImplementation(eglImpl),
61 mImageKHRInitialized(false),
62 mImageKHRInitializeFailed(false)
64 DALI_ASSERT_ALWAYS( eglImpl && "EGL Implementation not instantiated" );
67 EglImageExtensions::~EglImageExtensions()
71 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
73 if (mImageKHRInitialized == false)
75 InitializeEglImageKHR();
78 if (mImageKHRInitialized == false)
83 // Use the EGL image extension
84 const EGLint attribs[] =
86 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
90 EGLImageKHR eglImage = eglCreateImageKHRProc( mEglImplementation->GetDisplay(),
92 EGL_NATIVE_PIXMAP_KHR,
96 DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
97 if( EGL_NO_IMAGE_KHR == eglImage )
99 switch( eglGetError() )
105 case EGL_BAD_DISPLAY:
107 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
110 case EGL_BAD_CONTEXT:
112 DALI_LOG_ERROR( "EGL_BAD_CONTEXT: Invalid EGLContext object\n" );
115 case EGL_BAD_PARAMETER:
117 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n" );
122 DALI_LOG_ERROR( "EGL_BAD_MATCH: attrib_list does not match target\n" );
127 DALI_LOG_ERROR( "EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n" );
132 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
145 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
147 DALI_ASSERT_DEBUG( mImageKHRInitialized );
149 if( ! mImageKHRInitialized )
154 if( eglImageKHR == NULL )
159 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
161 EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
163 if( EGL_FALSE == result )
165 switch( eglGetError() )
167 case EGL_BAD_DISPLAY:
169 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
172 case EGL_BAD_PARAMETER:
174 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
179 DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
190 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
192 DALI_ASSERT_DEBUG( mImageKHRInitialized );
194 if( eglImageKHR != NULL )
196 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
198 #ifdef EGL_ERROR_CHECKING
199 GLint glError = glGetError();
202 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
204 #ifdef EGL_ERROR_CHECKING
205 glError = glGetError();
206 if( GL_NO_ERROR != glError )
208 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
214 void EglImageExtensions::InitializeEglImageKHR()
216 // avoid trying to reload extended KHR functions, if it fails the first time
217 if( ! mImageKHRInitializeFailed )
219 eglCreateImageKHRProc = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
220 eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
221 glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
224 if (eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
226 mImageKHRInitialized = true;
230 mImageKHRInitializeFailed = true;
234 } // namespace Adaptor
236 } // namespace Internal