Add default value to inverted y on glx
[profile/ivi/qtwayland.git] / src / qt-compositor / hardware_integration / xcomposite_glx / xcompositeglxintegration.cpp
1 #include "xcompositeglxintegration.h"
2
3 #include "waylandobject.h"
4 #include "wayland_wrapper/wlcompositor.h"
5 #include "wayland-xcomposite-server-protocol.h"
6
7 #include <QtGui/QPlatformNativeInterface>
8 #include <QtGui/QPlatformGLContext>
9 #include <private/qapplication_p.h>
10
11 #include "xcompositebuffer.h"
12 #include "xcompositehandler.h"
13 #include <X11/extensions/Xcomposite.h>
14
15 #include <QtCore/QDebug>
16
17 QVector<int> qglx_buildSpec()
18 {
19     QVector<int> spec(48);
20     int i = 0;
21
22     spec[i++] = GLX_LEVEL;
23     spec[i++] = 0;
24     spec[i++] = GLX_DRAWABLE_TYPE; spec[i++] = GLX_PIXMAP_BIT;
25     spec[i++] = GLX_BIND_TO_TEXTURE_TARGETS_EXT; spec[i++] = GLX_TEXTURE_2D_BIT_EXT;
26     spec[i++] = GLX_BIND_TO_TEXTURE_RGB_EXT; spec[i++] = TRUE;
27
28     spec[i++] = 0;
29     return spec;
30 }
31
32
33 struct wl_xcomposite_interface XCompositeHandler::xcomposite_interface = {
34     XCompositeHandler::create_buffer
35 };
36
37 GraphicsHardwareIntegration *GraphicsHardwareIntegration::createGraphicsHardwareIntegration(WaylandCompositor *compositor)
38 {
39     return new XCompositeGLXIntegration(compositor);
40 }
41
42 XCompositeGLXIntegration::XCompositeGLXIntegration(WaylandCompositor *compositor)
43     : GraphicsHardwareIntegration(compositor)
44     , mDisplay(0)
45 {
46     QPlatformNativeInterface *nativeInterface = QApplicationPrivate::platformIntegration()->nativeInterface();
47     if (nativeInterface) {
48         mDisplay = static_cast<Display *>(nativeInterface->nativeResourceForWidget("Display",m_compositor->topLevelWidget()));
49         if (!mDisplay)
50             qFatal("could not retireve Display from platform integration");
51     } else {
52         qFatal("Platform integration doesn't have native interface");
53     }
54     mScreen = XDefaultScreen(mDisplay);
55 }
56
57 void XCompositeGLXIntegration::initializeHardware(Wayland::Display *waylandDisplay)
58 {
59     XCompositeHandler *handler = new XCompositeHandler(m_compositor->handle(),mDisplay,m_compositor->topLevelWidget());
60     waylandDisplay->addGlobalObject(handler->base(), &wl_xcomposite_interface, &XCompositeHandler::xcomposite_interface,XCompositeHandler::send_root_information);
61
62     QPlatformGLContext *glContext = m_compositor->topLevelWidget()->platformWindow()->glContext();
63
64     m_glxBindTexImageEXT = reinterpret_cast<PFNGLXBINDTEXIMAGEEXTPROC>(glContext->getProcAddress("glXBindTexImageEXT"));
65     if (!m_glxBindTexImageEXT) {
66         qDebug() << "Did not find glxBindTexImageExt, everything will FAIL!";
67     }
68     m_glxReleaseTexImageEXT = reinterpret_cast<PFNGLXRELEASETEXIMAGEEXTPROC>(glContext->getProcAddress("glXReleaseTexImageEXT"));
69     if (!m_glxReleaseTexImageEXT) {
70         qDebug() << "Did not find glxReleaseTexImageExt";
71     }
72 }
73
74 GLuint XCompositeGLXIntegration::createTextureFromBuffer(wl_buffer *buffer)
75 {
76     XCompositeBuffer *compositorBuffer = Wayland::wayland_cast<XCompositeBuffer *>(buffer);
77     Pixmap pixmap = XCompositeNameWindowPixmap(mDisplay, compositorBuffer->window());
78
79     QVector<int> glxConfigSpec = qglx_buildSpec();
80     int numberOfConfigs;
81     GLXFBConfig *configs = glXChooseFBConfig(mDisplay,mScreen,glxConfigSpec.constData(),&numberOfConfigs);
82
83     QVector<int> attribList;
84     attribList.append(GLX_TEXTURE_FORMAT_EXT);
85     attribList.append(GLX_TEXTURE_FORMAT_RGB_EXT);
86     attribList.append(GLX_TEXTURE_TARGET_EXT);
87     attribList.append(GLX_TEXTURE_2D_EXT);
88     attribList.append(0);
89     GLXPixmap glxPixmap = glXCreatePixmap(mDisplay,*configs,pixmap,attribList.constData());
90
91     uint inverted = 0;
92     glXQueryDrawable(mDisplay, glxPixmap, GLX_Y_INVERTED_EXT,&inverted);
93     compositorBuffer->setInvertedY(!inverted);
94
95     XFree(configs);
96
97     GLuint textureId;
98     glGenTextures(1,&textureId);
99     glBindTexture(GL_TEXTURE_2D, textureId);
100     m_glxBindTexImageEXT(mDisplay,glxPixmap,GLX_FRONT_EXT, 0);
101     //Do we need to change the api so that we do bind and release in the painevent?
102     //The specification states that when deleting the texture the color buffer is deleted
103 //    m_glxReleaseTexImageEXT(mDisplay,glxPixmap,GLX_FRONT_EXT);
104     return textureId;
105 }
106
107 bool XCompositeGLXIntegration::isYInverted(wl_buffer *buffer) const
108 {
109     XCompositeBuffer *compositorBuffer = Wayland::wayland_cast<XCompositeBuffer *>(buffer);
110     return compositorBuffer->isYInverted();
111 }