1 /***************************************************************************
3 * Copyright 2010, 2011 BMW Car IT GmbH
4 * Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ****************************************************************************/
21 #include "TextureBinders/WaylandGLESTexture.h"
23 #include <EGL/eglext.h>
24 #include <GLES2/gl2ext.h>
25 #include "PlatformSurfaces/EglWaylandPlatformSurface.h"
27 #include "wayland-server.h"
30 WaylandGLESTexture::WaylandGLESTexture(EGLDisplay eglDisplay, struct wl_display* wlDisplay)
31 : m_eglDisplay(eglDisplay)
32 , m_wlDisplay(wlDisplay)
34 // pseudo require EGL to have been initialised
35 // we dont really need the EGL handle as such
37 m_pfGLEglImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
38 m_pfEglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
39 m_pfEglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
41 if (!m_pfEglCreateImageKHR || !m_pfEglDestroyImageKHR || !m_pfGLEglImageTargetTexture2DOES)
43 LOG_ERROR("WaylandGLESTexture", "Query EGL Extensions failed");
47 WaylandGLESTexture::~WaylandGLESTexture()
51 bool WaylandGLESTexture::bindSurfaceTexture(Surface* surface)
53 LOG_DEBUG("WaylandGLESTexture", "bindSurfaceTexture IN");
54 EglWaylandPlatformSurface* nativeSurface = (EglWaylandPlatformSurface*)surface->platform;
55 if (nativeSurface && nativeSurface->isReadyForRendering())
57 struct wl_buffer* buffer = (struct wl_buffer*)surface->getNativeContent();
59 * @todo Port to Wayland 1.3.
61 if (0 // wl_buffer_is_shm(buffer)
64 /* Wayland SHM buffer */
66 * @todo Port to Wayland 1.3.
68 unsigned int* pBuf = (unsigned int*)wl_shm_buffer_get_data(NULL // buffer
70 if ((NULL != buffer) && (NULL != pBuf))
72 LOG_DEBUG("WaylandGLESTexture", "SHM buffer address:" << pBuf);
73 unsigned int* pTmp = (unsigned int*)pBuf;
76 unsigned int* pCnvImg = (unsigned int*)malloc(sizeof(unsigned int)
77 * surface->OriginalSourceWidth * surface->OriginalSourceHeight);
78 unsigned int* pCnvImgTmp = pCnvImg;
79 for (cnt = 0; cnt < surface->OriginalSourceWidth * surface->OriginalSourceHeight; cnt++, pTmp++, pCnvImgTmp++)
81 col = (unsigned int)*pTmp;
83 *pCnvImgTmp = (col&0xFF00FF00) | ((col&0x00FF0000)>>16) | ((col&0x000000FF)<<16);
86 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
87 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
88 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
89 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexImage2D(GL_TEXTURE_2D,
94 surface->OriginalSourceWidth,
95 surface->OriginalSourceHeight,
106 /* Wayland Not SHM buffer */
107 if (nativeSurface->eglImage)
109 LOG_DEBUG("WaylandGLESTexture", "nativeSurface->eglImage:" << nativeSurface->eglImage);
110 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
115 LOG_DEBUG("WaylandGLESTexture", "bindSurfaceTexture OUT");
118 LOG_DEBUG("WaylandGLESTexture", "bindSurfaceTexture ERROR OUT");
122 bool WaylandGLESTexture::unbindSurfaceTexture(Surface* surface)
124 LOG_DEBUG("WaylandGLESTexture", "unbindSurfaceTexture:surface" << surface);
128 void WaylandGLESTexture::createClientBuffer(Surface* surface)
130 LOG_DEBUG("WaylandGLESTexture", "creating client buffer with native display: " << m_wlDisplay << " for native handle: " << surface->getNativeContent());
131 EglWaylandPlatformSurface* nativeSurface = (EglWaylandPlatformSurface*)surface->platform;
133 if (NULL != nativeSurface)
135 struct wl_buffer* buffer = (struct wl_buffer*)surface->getNativeContent();
137 * @todo Port to Wayland 1.3.
139 if (0 // wl_buffer_is_shm(buffer)
142 if (nativeSurface->texture)
144 glDeleteTextures(1, &nativeSurface->texture);
145 nativeSurface->texture = 0;
147 glGenTextures(1, &nativeSurface->texture);
151 EGLImageKHR eglImage = 0;
152 LOG_DEBUG("WaylandGLESTexture", "creating EGL Image from client buffer");
153 if (nativeSurface->eglImage)
155 m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
156 glDeleteTextures(1, &nativeSurface->texture);
157 nativeSurface->eglImage = 0;
158 nativeSurface->texture = 0;
160 eglImage = m_pfEglCreateImageKHR(m_eglDisplay,
162 EGL_WAYLAND_BUFFER_WL,
163 (EGLClientBuffer)buffer,
167 LOG_DEBUG("WaylandGLESTexture", "could not allocate EGL Image for window");
171 nativeSurface->eglImage = eglImage;
172 glGenTextures(1, &nativeSurface->texture);
173 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
174 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
175 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
176 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
178 m_pfGLEglImageTargetTexture2DOES(GL_TEXTURE_2D, nativeSurface->eglImage);
184 PlatformSurface* WaylandGLESTexture::createPlatformSurface(Surface* surface)
186 return new EglWaylandPlatformSurface(surface);
189 void WaylandGLESTexture::destroyClientBuffer(Surface* surface)
191 EglWaylandPlatformSurface* nativeSurface = (EglWaylandPlatformSurface*)surface->platform;
193 struct wl_buffer* buffer = (struct wl_buffer*)surface->getNativeContent();
195 * @todo Port to Wayland 1.3.
197 if (0 // wl_buffer_is_shm(buffer)
200 if (nativeSurface && nativeSurface->texture)
202 glDeleteTextures(1, &nativeSurface->texture);
203 nativeSurface->texture = 0;
208 if (nativeSurface && nativeSurface->eglImage)
210 m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
211 glDeleteTextures(1, &nativeSurface->texture);
212 nativeSurface->eglImage = 0;
213 nativeSurface->texture = 0;