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() = default;
63 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
65 if( mImageKHRInitialized == false )
67 InitializeEglImageKHR();
70 if( mImageKHRInitialized == false )
76 const EGLint attribs[] =
81 // EGL constants use C casts
82 #pragma GCC diagnostic push
83 #pragma GCC diagnostic ignored "-Wold-style-cast"
84 EGLImageKHR eglImage = eglCreateImageKHRProc( mEglImplementation->GetDisplay(),
86 EGL_NATIVE_BUFFER_ANDROID,
90 DALI_ASSERT_DEBUG( EGL_NO_IMAGE_KHR != eglImage && "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" );
135 #pragma GCC diagnostic pop
140 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
142 DALI_ASSERT_DEBUG( mImageKHRInitialized );
144 if( ! mImageKHRInitialized )
149 if( eglImageKHR == NULL )
154 EGLImageKHR eglImage = static_cast<EGLImageKHR>( eglImageKHR );
156 EGLBoolean result = eglDestroyImageKHRProc( mEglImplementation->GetDisplay(), eglImage );
158 if( EGL_FALSE == result )
160 switch( eglGetError() )
162 case EGL_BAD_DISPLAY:
164 DALI_LOG_ERROR( "EGL_BAD_DISPLAY: Invalid EGLDisplay object\n" );
167 case EGL_BAD_PARAMETER:
169 DALI_LOG_ERROR( "EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n" );
174 DALI_LOG_ERROR( "EGL_BAD_ACCESS: EGLImage sibling error\n" );
185 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
187 DALI_ASSERT_DEBUG( mImageKHRInitialized );
189 if( eglImageKHR != NULL )
191 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
193 #ifdef EGL_ERROR_CHECKING
194 GLint glError = glGetError();
197 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast< GLeglImageOES >( eglImage ) );
199 #ifdef EGL_ERROR_CHECKING
200 glError = glGetError();
201 if( GL_NO_ERROR != glError )
203 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError );
209 void EglImageExtensions::InitializeEglImageKHR()
211 // avoid trying to reload extended KHR functions, if it fails the first time
212 if( ! mImageKHRInitializeFailed )
214 eglCreateImageKHRProc = reinterpret_cast< PFNEGLCREATEIMAGEKHRPROC >( eglGetProcAddress("eglCreateImageKHR") );
215 eglDestroyImageKHRProc = reinterpret_cast< PFNEGLDESTROYIMAGEKHRPROC >( eglGetProcAddress("eglDestroyImageKHR") );
216 glEGLImageTargetTexture2DOESProc = reinterpret_cast< PFNGLEGLIMAGETARGETTEXTURE2DOESPROC >( eglGetProcAddress("glEGLImageTargetTexture2DOES") );
219 if( eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc )
221 mImageKHRInitialized = true;
225 mImageKHRInitializeFailed = true;
229 } // namespace Adaptor
231 } // namespace Internal