2 * Copyright (c) 2021 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.
19 #include <dali/internal/graphics/common/egl-image-extensions.h>
22 #include <GLES2/gl2.h>
23 #include <GLES2/gl2ext.h>
24 #include <GLES3/gl3.h>
26 #include <EGL/eglext.h>
28 #include <dali/integration-api/debug.h>
31 #include <dali/internal/graphics/gles/egl-implementation.h>
35 // function pointers assigned in InitializeEglImageKHR
36 PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHRProc = 0;
37 PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHRProc = 0;
38 PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOESProc = 0;
39 } // unnamed namespace
47 EglImageExtensions::EglImageExtensions(EglImplementation* eglImpl)
48 : mEglImplementation(eglImpl),
49 mImageKHRInitialized(false),
50 mImageKHRInitializeFailed(false)
52 DALI_ASSERT_ALWAYS(eglImpl && "EGL Implementation not instantiated");
55 EglImageExtensions::~EglImageExtensions()
59 void* EglImageExtensions::CreateImageKHR(EGLClientBuffer clientBuffer)
61 if(mImageKHRInitialized == false)
63 InitializeEglImageKHR();
66 if(mImageKHRInitialized == false)
71 // Use the EGL image extension
72 const EGLint attribs[] =
74 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
76 EGLImageKHR eglImage = eglCreateImageKHRProc(mEglImplementation->GetDisplay(),
78 EGL_NATIVE_PIXMAP_KHR,
82 DALI_ASSERT_DEBUG(EGL_NO_IMAGE_KHR != eglImage && "X11Image::GlExtensionCreate eglCreateImageKHR failed!\n");
83 if(EGL_NO_IMAGE_KHR == eglImage)
93 DALI_LOG_ERROR("EGL_BAD_DISPLAY: Invalid EGLDisplay object\n");
98 DALI_LOG_ERROR("EGL_BAD_CONTEXT: Invalid EGLContext object\n");
101 case EGL_BAD_PARAMETER:
103 DALI_LOG_ERROR("EGL_BAD_PARAMETER: Invalid target parameter or attribute in attrib_list\n");
108 DALI_LOG_ERROR("EGL_BAD_MATCH: attrib_list does not match target\n");
113 DALI_LOG_ERROR("EGL_BAD_ACCESS: Previously bound off-screen, or EGLImage sibling error\n");
118 DALI_LOG_ERROR("EGL_BAD_ALLOC: Insufficient memory is available\n");
131 void EglImageExtensions::DestroyImageKHR(void* eglImageKHR)
133 DALI_ASSERT_DEBUG(mImageKHRInitialized);
135 if(!mImageKHRInitialized)
140 if(eglImageKHR == NULL)
145 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
147 EGLBoolean result = eglDestroyImageKHRProc(mEglImplementation->GetDisplay(), eglImage);
149 if(EGL_FALSE == result)
151 switch(eglGetError())
153 case EGL_BAD_DISPLAY:
155 DALI_LOG_ERROR("EGL_BAD_DISPLAY: Invalid EGLDisplay object\n");
158 case EGL_BAD_PARAMETER:
160 DALI_LOG_ERROR("EGL_BAD_PARAMETER: eglImage is not a valid EGLImageKHR object created with respect to EGLDisplay\n");
165 DALI_LOG_ERROR("EGL_BAD_ACCESS: EGLImage sibling error\n");
176 void EglImageExtensions::TargetTextureKHR(void* eglImageKHR)
178 DALI_ASSERT_DEBUG(mImageKHRInitialized);
180 if(eglImageKHR != NULL)
182 EGLImageKHR eglImage = static_cast<EGLImageKHR>(eglImageKHR);
184 #ifdef EGL_ERROR_CHECKING
185 GLint glError = glGetError();
188 glEGLImageTargetTexture2DOESProc(GL_TEXTURE_2D, reinterpret_cast<GLeglImageOES>(eglImage));
190 #ifdef EGL_ERROR_CHECKING
191 glError = glGetError();
192 if(GL_NO_ERROR != glError)
194 DALI_LOG_ERROR(" glEGLImageTargetTexture2DOES returned error %0x04x\n", glError);
200 void EglImageExtensions::InitializeEglImageKHR()
202 // avoid trying to reload extended KHR functions, if it fails the first time
203 if(!mImageKHRInitializeFailed)
205 eglCreateImageKHRProc = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
206 eglDestroyImageKHRProc = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
207 glEGLImageTargetTexture2DOESProc = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
210 if(eglCreateImageKHRProc && eglDestroyImageKHRProc && glEGLImageTargetTexture2DOESProc)
212 mImageKHRInitialized = true;
216 mImageKHRInitializeFailed = true;
220 } // namespace Adaptor
222 } // namespace Internal