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/X11CopyGLES.h"
25 #include "GLES2/gl2.h"
27 bool X11CopyGLES::bindSurfaceTexture(Surface* surface)
29 XPlatformSurface* nativeSurface = NULL;
31 GLenum targetType = GL_RGBA;
32 GLenum sourceType = GL_RGBA;
33 unsigned char* swapedData = NULL;
35 bool includeAlpha = false;
38 nativeSurface = (XPlatformSurface*)surface->platform;
40 if( nativeSurface != NULL && surface->getNativeContent() != 0 && nativeSurface->isReadyForRendering())
42 pixmap = XCompositeNameWindowPixmap (dpy, surface->getNativeContent());
45 LOG_ERROR("X11CopyGLES", "didnt create pixmap!");
48 nativeSurface->pixmap = pixmap;
49 XImage * xim = XGetImage(dpy, nativeSurface->pixmap, 0, 0, surface->OriginalSourceWidth, surface->OriginalSourceHeight, AllPlanes, ZPixmap);
52 glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
53 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
55 if ( surface->getPixelFormat() == PIXELFORMAT_RGB888)
60 swapedData = new unsigned char[surface->OriginalSourceWidth*surface->OriginalSourceHeight*3];
62 else if ( surface->getPixelFormat() == PIXELFORMAT_RGBA8888)
68 swapedData = new unsigned char[surface->OriginalSourceWidth*surface->OriginalSourceHeight*4];
72 LOG_ERROR("X11CopyGLES","Pixelformat currently not supported : " << surface->getPixelFormat());
76 swapPixmap((unsigned char*)xim->data, swapedData, surface->OriginalSourceWidth, surface->OriginalSourceHeight,swaprgb,includeAlpha);
77 glTexImage2D(GL_TEXTURE_2D, 0, sourceType, surface->OriginalSourceWidth, surface->OriginalSourceHeight, 0, targetType, GL_UNSIGNED_BYTE, swapedData);
82 LOG_ERROR("X11CopyGLES", "X image data empty");
88 void X11CopyGLES::swapPixmap(unsigned char* src,unsigned char* dest, unsigned int width,unsigned int height,bool swaprgb,bool includeAlpha)
90 unsigned int count = 0;
94 for (uint j=0;j<count; j++) {
96 dest[j*4+1]=src[j*4+1];
98 dest[j*4+3]=src[j*4+3];
105 count = width*height;
106 for (uint j=0;j<count; j++)
108 dest[j*3]=src[j*3+2];
109 dest[j*3+1]=src[j*3+1];
110 dest[j*3+2]=src[j*3];
115 void X11CopyGLES::createClientBuffer(Surface* surface)
117 XPlatformSurface* nativeSurface = (XPlatformSurface*)surface->platform;
118 glGenTextures(1,&nativeSurface->texture);
121 void X11CopyGLES::destroyClientBuffer(Surface* surface)
123 XPlatformSurface* nativeSurface = (XPlatformSurface*) surface->platform;
124 if (nativeSurface && nativeSurface->pixmap)
126 glDeleteTextures(1,&nativeSurface->texture);
127 XFreePixmap(dpy, nativeSurface->pixmap);
128 nativeSurface->pixmap = None;