Introducing check to enable rendering dependent on the content state.
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / src / TextureBinders / X11CopyGLES.cpp
1 /***************************************************************************
2 *
3 * Copyright 2010,2011 BMW Car IT GmbH
4 *
5 *
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
9 *
10 *        http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 ****************************************************************************/
19
20 #include "TextureBinders/X11CopyGLES.h"
21 #include "Surface.h"
22 #include "X11/Xlib.h"
23 #include "Log.h"
24
25 #include "GLES2/gl2.h"
26
27 bool X11CopyGLES::bindSurfaceTexture(Surface* surface)
28 {   
29     XPlatformSurface* nativeSurface = NULL;
30     Pixmap pixmap = 0;
31     GLenum targetType = GL_RGBA;
32     GLenum sourceType = GL_RGBA;
33         unsigned char* swapedData = NULL;
34         bool swaprgb = false;
35     bool includeAlpha = false;
36     if (surface != NULL ) 
37     {
38         nativeSurface = (XPlatformSurface*)surface->platform;
39     } 
40     if( nativeSurface != NULL && surface->getNativeContent() != 0 && nativeSurface->isReadyForRendering())
41     {
42             pixmap = XCompositeNameWindowPixmap (dpy, surface->getNativeContent());
43             if (!pixmap)
44             {
45                     LOG_ERROR("X11CopyGLES", "didnt create pixmap!");
46                     return false;
47             }
48             nativeSurface->pixmap = pixmap;
49             XImage * xim = XGetImage(dpy, nativeSurface->pixmap, 0, 0, surface->OriginalSourceWidth, surface->OriginalSourceHeight, AllPlanes, ZPixmap);
50             if ( xim != NULL )
51             {
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)
56                     {
57                             targetType = GL_RGB;
58                             sourceType = GL_RGB;
59                             swaprgb = true;                
60                             swapedData = new unsigned char[surface->OriginalSourceWidth*surface->OriginalSourceHeight*3];
61                     } 
62             else if ( surface->getPixelFormat() == PIXELFORMAT_RGBA8888)
63             {
64                 if (xim->depth == 24) 
65                 {
66                                 includeAlpha = true;
67                 }
68                 swapedData = new unsigned char[surface->OriginalSourceWidth*surface->OriginalSourceHeight*4];
69                     } 
70             else 
71             {
72                 LOG_ERROR("X11CopyGLES","Pixelformat currently not supported : " << surface->getPixelFormat());
73                     XDestroyImage(xim);
74                 return false;
75             }
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);
78                     XDestroyImage(xim);
79                     delete[] swapedData;
80             return true;
81             } else {
82             LOG_ERROR("X11CopyGLES", "X image data empty");
83                     return false;
84         }
85     }   
86     return false;
87 }
88 void X11CopyGLES::swapPixmap(unsigned char* src,unsigned char* dest, unsigned int width,unsigned int height,bool swaprgb,bool includeAlpha) 
89 {
90         unsigned int count = 0; 
91         if (swaprgb == false)
92         {       
93                 count = width*height;
94                 for (uint j=0;j<count; j++) {
95                         dest[j*4]=src[j*4+2];
96                         dest[j*4+1]=src[j*4+1];
97                         dest[j*4+2]=src[j*4];
98             dest[j*4+3]=src[j*4+3];
99             if (includeAlpha) 
100             {    
101                         dest[j*4+3]=255;
102             } 
103                 }
104         } else {
105                 count = width*height;        
106                 for (uint j=0;j<count; j++)
107                 {
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];
111                 }
112         }
113 }
114
115 void X11CopyGLES::createClientBuffer(Surface* surface)
116 {
117     XPlatformSurface* nativeSurface = (XPlatformSurface*)surface->platform;
118     glGenTextures(1,&nativeSurface->texture);
119 }
120
121 void X11CopyGLES::destroyClientBuffer(Surface* surface)
122 {
123     XPlatformSurface* nativeSurface = (XPlatformSurface*) surface->platform;
124     if (nativeSurface && nativeSurface->pixmap)
125     {
126           glDeleteTextures(1,&nativeSurface->texture);
127           XFreePixmap(dpy, nativeSurface->pixmap);
128           nativeSurface->pixmap = None;
129     }
130 }