The XCB plugin should not rely on QT_CONFIG=opengl anymore
[profile/ivi/qtbase.git] / src / plugins / platforms / xcb / qglxintegration.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QDebug>
43 #include <QLibrary>
44
45 #include "qxcbwindow.h"
46 #include "qxcbscreen.h"
47
48 #include <X11/Xlib.h>
49 #include <X11/Xutil.h>
50 #include <GL/glx.h>
51
52 #include <QtGui/QOpenGLContext>
53
54 #include "qglxintegration.h"
55 #include <QtPlatformSupport/private/qglxconvenience_p.h>
56
57 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
58 #include <dlfcn.h>
59 #endif
60
61 QGLXContext::QGLXContext(QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share)
62     : QPlatformOpenGLContext()
63     , m_screen(screen)
64     , m_context(0)
65 {
66     GLXContext shareGlxContext = 0;
67     if (share)
68         shareGlxContext = static_cast<const QGLXContext*>(share)->glxContext();
69
70     GLXFBConfig config = qglx_findConfig(DISPLAY_FROM_XCB(screen),screen->screenNumber(),format);
71     m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, shareGlxContext, TRUE);
72     m_format = qglx_surfaceFormatFromGLXFBConfig(DISPLAY_FROM_XCB(screen), config, m_context);
73 }
74
75 QGLXContext::~QGLXContext()
76 {
77     glXDestroyContext(DISPLAY_FROM_XCB(m_screen), m_context);
78 }
79
80 bool QGLXContext::makeCurrent(QPlatformSurface *surface)
81 {
82     Q_ASSERT(surface);
83
84     GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
85
86     return glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), glxDrawable, m_context);
87 }
88
89 void QGLXContext::doneCurrent()
90 {
91     glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), 0, 0);
92 }
93
94 void QGLXContext::swapBuffers(QPlatformSurface *surface)
95 {
96     GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
97     glXSwapBuffers(DISPLAY_FROM_XCB(m_screen), glxDrawable);
98 }
99
100 void (*QGLXContext::getProcAddress(const QByteArray &procName)) ()
101 {
102     typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
103     static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
104     static bool resolved = false;
105
106     if (resolved && !glXGetProcAddressARB)
107         return 0;
108     if (!glXGetProcAddressARB) {
109         QList<QByteArray> glxExt = QByteArray(glXGetClientString(DISPLAY_FROM_XCB(m_screen), GLX_EXTENSIONS)).split(' ');
110         if (glxExt.contains("GLX_ARB_get_proc_address")) {
111 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
112             void *handle = dlopen(NULL, RTLD_LAZY);
113             if (handle) {
114                 glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
115                 dlclose(handle);
116             }
117             if (!glXGetProcAddressARB)
118 #endif
119             {
120                 extern const QString qt_gl_library_name();
121 //                QLibrary lib(qt_gl_library_name());
122                 QLibrary lib(QLatin1String("GL"));
123                 glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
124             }
125         }
126         resolved = true;
127     }
128     if (!glXGetProcAddressARB)
129         return 0;
130     return (void (*)())glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.constData()));
131 }
132
133 QSurfaceFormat QGLXContext::format() const
134 {
135     return m_format;
136 }