1 /***************************************************************************
3 * Copyright 2010,2011 BMW Car IT GmbH
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ****************************************************************************/
20 #include "TextureBinders/X11EglImage.h"
21 #include "X11/extensions/Xcomposite.h"
23 #include <EGL/eglext.h>
24 #include <GLES2/gl2ext.h>
25 #include "PlatformSurfaces/EglXPlatformSurface.h"
28 X11EglImage::X11EglImage(EGLDisplay eglDisplay, Display* x11display)
29 : m_eglDisplay(eglDisplay)
30 , m_x11display(x11display)
32 // pseudo require EGL to have been initialised
33 // we dont really need the EGL handle as such
35 m_pfGLEglImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
36 m_pfEglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
37 m_pfEglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
39 if (!m_pfEglCreateImageKHR || !m_pfEglDestroyImageKHR || !m_pfGLEglImageTargetTexture2DOES)
41 LOG_ERROR("X11EglImage", "Query EGL Extensions failed");
45 bool X11EglImage::bindSurfaceTexture(Surface* surface)
47 EglXPlatformSurface* nativeSurface = (EglXPlatformSurface*)surface->platform;
48 if (nativeSurface && nativeSurface->isReadyForRendering())
50 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
51 if (nativeSurface->eglImage)
53 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
54 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
55 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
56 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
57 m_pfGLEglImageTargetTexture2DOES(GL_TEXTURE_2D, nativeSurface->eglImage);
64 bool X11EglImage::unbindSurfaceTexture(Surface* surface)
66 (void)surface; // TODO: remove, only prevents warning
72 void X11EglImage::createClientBuffer(Surface* surface)
74 LOG_DEBUG("X11EglImage", "creating client buffer with native display: " << m_x11display << " for native handle: " << surface->getNativeContent());
75 EglXPlatformSurface* nativeSurface = (EglXPlatformSurface*)surface->platform;
76 if (NULL!=nativeSurface)
78 Pixmap windowPixmap = 0;
79 windowPixmap= XCompositeNameWindowPixmap (m_x11display, surface->getNativeContent());
82 LOG_ERROR("X11EglImage", "didnt create pixmap!");
85 EGLImageKHR eglImage = 0;
86 LOG_DEBUG("X11EglImage", "creating EGL Image from client buffer");
87 if (nativeSurface->eglImage)
89 m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
90 glDeleteTextures(1,&nativeSurface->texture);
91 nativeSurface->eglImage = 0;
92 nativeSurface->texture = 0;
94 eglImage = m_pfEglCreateImageKHR(m_eglDisplay,
96 EGL_NATIVE_PIXMAP_KHR,
97 (EGLClientBuffer)windowPixmap,
101 LOG_DEBUG("X11EglImage", "could not allocate EGL Image for window");
105 nativeSurface->eglImage = eglImage;
106 glGenTextures(1,&nativeSurface->texture);
111 PlatformSurface* X11EglImage::createPlatformSurface(Surface* surface)
113 return new EglXPlatformSurface(surface);
116 void X11EglImage::destroyClientBuffer(Surface* surface)
118 EglXPlatformSurface* nativeSurface = (EglXPlatformSurface*)surface->platform;
119 if (nativeSurface && nativeSurface->eglImage)
121 m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
122 glDeleteTextures(1,&nativeSurface->texture);
123 nativeSurface->eglImage = 0;
124 nativeSurface->texture = 0;