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 // EGL constants use C casts
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Wold-style-cast"
87 EGLImageKHR eglImage = eglCreateImageKHRProc( mEglImplementation->GetDisplay(),
89 EGL_NATIVE_PIXMAP_KHR,
93 DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
94 if( EGL_NO_IMAGE_KHR == eglImage )
96 switch( eglGetError() )
102 case EGL_BAD_DISPLAY:
104 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
107 case EGL_BAD_CONTEXT:
109 DALI_LOG_ERROR( "EGL_BAD_CONTEXT: Invalid EGLContext object\n" );
112 case EGL_BAD_PARAMETER:
114 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n" );
119 DALI_LOG_ERROR( "EGL_BAD_MATCH: attrib_list does not match target\n" );
124 DALI_LOG_ERROR( "EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n" );
129 DALI_LOG_ERROR( "EGL_BAD_ALLOC: Insufficient memory is available\n" );
138 #pragma GCC diagnostic pop
143 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
145 DALI_ASSERT_DEBUG( mImageKHRInitialized );
147 if( ! mImageKHRInitialized )
152 if( eglImageKHR == NULL )
157 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
159 EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
161 if( EGL_FALSE == result )
163 switch( eglGetError() )
165 case EGL_BAD_DISPLAY:
167 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
170 case EGL_BAD_PARAMETER:
172 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
177 DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
188 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
190 DALI_ASSERT_DEBUG( mImageKHRInitialized );
192 if( eglImageKHR != NULL )
194 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
196 #ifdef EGL_ERROR_CHECKING
197 GLint glError = glGetError();
200 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
202 #ifdef EGL_ERROR_CHECKING
203 glError = glGetError();
204 if( GL_NO_ERROR != glError )
206 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
212 void EglImageExtensions::InitializeEglImageKHR()
214 // avoid trying to reload extended KHR functions, if it fails the first time
215 if( ! mImageKHRInitializeFailed )
217 eglCreateImageKHRProc = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
218 eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
219 glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
222 if (eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
224 mImageKHRInitialized = true;
228 mImageKHRInitializeFailed = true;
232 } // namespace Adaptor
234 } // namespace Internal