2 * Copyright (c) 2019 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 <dali/internal/graphics/common/egl-image-extensions.h>
23 #include <GLES2/gl2.h>
24 #include <GLES2/gl2ext.h>
25 #include <GLES3/gl3.h>
27 #include <EGL/eglext.h>
29 #include <dali/integration-api/debug.h>
32 #include <dali/internal/graphics/gles/egl-implementation.h>
37 // function pointers assigned in InitializeEglImageKHR
38 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHRProc = 0;
39 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHRProc = 0;
40 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESProc = 0;
41 } // unnamed namespace
53 EglImageExtensions::EglImageExtensions(EglImplementation* eglImpl)
54 : mEglImplementation(eglImpl),
55 mImageKHRInitialized(false),
56 mImageKHRInitializeFailed(false)
58 DALI_ASSERT_ALWAYS( eglImpl && "EGL Implementation not instantiated" );
61 EglImageExtensions::~EglImageExtensions()
65 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
67 if (mImageKHRInitialized == false)
69 InitializeEglImageKHR();
72 if (mImageKHRInitialized == false)
77 // Use the EGL image extension
78 const EGLint attribs[] =
80 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
84 EGLImageKHR eglImage = eglCreateImageKHRProc( mEglImplementation->GetDisplay(),
86 EGL_NATIVE_PIXMAP_KHR,
90 DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
91 if( EGL_NO_IMAGE_KHR == eglImage )
93 switch( eglGetError() )
101 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
104 case EGL_BAD_CONTEXT:
106 DALI_LOG_ERROR( "EGL_BAD_CONTEXT: Invalid EGLContext object\n" );
109 case EGL_BAD_PARAMETER:
111 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n" );
116 DALI_LOG_ERROR( "EGL_BAD_MATCH: attrib_list does not match target\n" );
121 DALI_LOG_ERROR( "EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n" );
126 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
139 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
141 DALI_ASSERT_DEBUG( mImageKHRInitialized );
143 if( ! mImageKHRInitialized )
148 if( eglImageKHR == NULL )
153 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
155 EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
157 if( EGL_FALSE == result )
159 switch( eglGetError() )
161 case EGL_BAD_DISPLAY:
163 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
166 case EGL_BAD_PARAMETER:
168 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
173 DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
184 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
186 DALI_ASSERT_DEBUG( mImageKHRInitialized );
188 if( eglImageKHR != NULL )
190 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
192 #ifdef EGL_ERROR_CHECKING
193 GLint glError = glGetError();
196 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
198 #ifdef EGL_ERROR_CHECKING
199 glError = glGetError();
200 if( GL_NO_ERROR != glError )
202 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
208 void EglImageExtensions::InitializeEglImageKHR()
210 // avoid trying to reload extended KHR functions, if it fails the first time
211 if( ! mImageKHRInitializeFailed )
213 eglCreateImageKHRProc = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
214 eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
215 glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
218 if (eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
220 mImageKHRInitialized = true;
224 mImageKHRInitializeFailed = true;
228 } // namespace Adaptor
230 } // namespace Internal