Introducing check to enable rendering dependent on the content state.
[profile/ivi/layer-management.git] / LayerManagerPlugins / Renderers / Graphic / src / TextureBinders / X11EglImage.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/X11EglImage.h"
21 #include "X11/extensions/Xcomposite.h"
22 #include <EGL/egl.h>
23 #include <EGL/eglext.h>
24 #include <GLES2/gl2ext.h>
25 #include "PlatformSurfaces/EglXPlatformSurface.h"
26 #include "Log.h"
27
28 X11EglImage::X11EglImage(EGLDisplay eglDisplay, Display* x11display)
29 : m_eglDisplay(eglDisplay)
30 , m_x11display(x11display)
31 {
32     // pseudo require EGL to have been initialised
33     // we dont really need the EGL handle as such
34
35     m_pfGLEglImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
36     m_pfEglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
37     m_pfEglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
38
39     if (!m_pfEglCreateImageKHR || !m_pfEglDestroyImageKHR || !m_pfGLEglImageTargetTexture2DOES)
40     {
41         LOG_ERROR("X11EglImage", "Query EGL Extensions failed");
42     }
43 }
44
45 bool X11EglImage::bindSurfaceTexture(Surface* surface)
46 {
47     EglXPlatformSurface* nativeSurface = (EglXPlatformSurface*)surface->platform;
48     if (nativeSurface && nativeSurface->isReadyForRendering())
49     {
50         glBindTexture(GL_TEXTURE_2D, nativeSurface->texture);
51         if (nativeSurface->eglImage)
52         {
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);
58             return true;
59         }
60     }
61     return false;
62 }
63
64 bool X11EglImage::unbindSurfaceTexture(Surface* surface)
65 {
66     (void)surface; // TODO: remove, only prevents warning
67
68     // TODO
69     return true;
70 }
71
72 void X11EglImage::createClientBuffer(Surface* surface)
73 {
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)
77     {
78         Pixmap windowPixmap = 0;
79         windowPixmap= XCompositeNameWindowPixmap (m_x11display, surface->getNativeContent());
80         if (windowPixmap==0)
81         {
82             LOG_ERROR("X11EglImage", "didnt create pixmap!");
83         }
84
85         EGLImageKHR eglImage = 0;
86         LOG_DEBUG("X11EglImage", "creating EGL Image from client buffer");
87         if (nativeSurface->eglImage)
88         {
89             m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
90             glDeleteTextures(1,&nativeSurface->texture);
91             nativeSurface->eglImage = 0;
92             nativeSurface->texture = 0;
93         }
94         eglImage = m_pfEglCreateImageKHR(m_eglDisplay,
95                                      EGL_NO_CONTEXT,
96                                      EGL_NATIVE_PIXMAP_KHR,
97                                      (EGLClientBuffer)windowPixmap,
98                                      NULL);
99         if (!eglImage)
100         {
101             LOG_DEBUG("X11EglImage", "could not allocate EGL Image for window");
102         }
103         else
104         {
105             nativeSurface->eglImage = eglImage;
106             glGenTextures(1,&nativeSurface->texture);
107         }
108     }
109 }
110
111 PlatformSurface* X11EglImage::createPlatformSurface(Surface* surface)
112 {
113     return new EglXPlatformSurface(surface);
114 }
115
116 void X11EglImage::destroyClientBuffer(Surface* surface)
117 {
118     EglXPlatformSurface* nativeSurface = (EglXPlatformSurface*)surface->platform;
119     if (nativeSurface && nativeSurface->eglImage)
120     {
121         m_pfEglDestroyImageKHR(m_eglDisplay, nativeSurface->eglImage);
122         glDeleteTextures(1,&nativeSurface->texture);
123         nativeSurface->eglImage = 0;
124         nativeSurface->texture = 0;
125     }
126 }