bf58721d38eaefb03c8bf0cc724cba0b9b8ee1a7
[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 #include <QGLFormat>
45
46 #include "qxcbwindow.h"
47 #include "qxcbscreen.h"
48
49 #include <X11/Xlib.h>
50 #include <X11/Xutil.h>
51 #include <GL/glx.h>
52
53 #include <QtGui/QOpenGLContext>
54
55 #include "qglxintegration.h"
56 #include <QtPlatformSupport/private/qglxconvenience_p.h>
57
58 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
59 #include <dlfcn.h>
60 #endif
61
62 QGLXContext::QGLXContext(QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share)
63     : QPlatformOpenGLContext()
64     , m_screen(screen)
65     , m_context(0)
66 {
67     GLXContext shareGlxContext = 0;
68     if (share)
69         shareGlxContext = static_cast<const QGLXContext*>(share)->glxContext();
70
71     GLXFBConfig config = qglx_findConfig(DISPLAY_FROM_XCB(screen),screen->screenNumber(),format);
72     m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, shareGlxContext, TRUE);
73     m_format = qglx_surfaceFormatFromGLXFBConfig(DISPLAY_FROM_XCB(screen), config, m_context);
74 }
75
76 QGLXContext::~QGLXContext()
77 {
78     glXDestroyContext(DISPLAY_FROM_XCB(m_screen), m_context);
79 }
80
81 bool QGLXContext::makeCurrent(QPlatformSurface *surface)
82 {
83     Q_ASSERT(surface);
84
85     GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
86
87     return glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), glxDrawable, m_context);
88 }
89
90 void QGLXContext::doneCurrent()
91 {
92     glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), 0, 0);
93 }
94
95 void QGLXContext::swapBuffers(QPlatformSurface *surface)
96 {
97     GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
98     glXSwapBuffers(DISPLAY_FROM_XCB(m_screen), glxDrawable);
99 }
100
101 void (*QGLXContext::getProcAddress(const QByteArray &procName)) ()
102 {
103     typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
104     static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
105     static bool resolved = false;
106
107     if (resolved && !glXGetProcAddressARB)
108         return 0;
109     if (!glXGetProcAddressARB) {
110         QList<QByteArray> glxExt = QByteArray(glXGetClientString(DISPLAY_FROM_XCB(m_screen), GLX_EXTENSIONS)).split(' ');
111         if (glxExt.contains("GLX_ARB_get_proc_address")) {
112 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
113             void *handle = dlopen(NULL, RTLD_LAZY);
114             if (handle) {
115                 glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
116                 dlclose(handle);
117             }
118             if (!glXGetProcAddressARB)
119 #endif
120             {
121                 extern const QString qt_gl_library_name();
122 //                QLibrary lib(qt_gl_library_name());
123                 QLibrary lib(QLatin1String("GL"));
124                 glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
125             }
126         }
127         resolved = true;
128     }
129     if (!glXGetProcAddressARB)
130         return 0;
131     return (void (*)())glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.constData()));
132 }
133
134 QSurfaceFormat QGLXContext::format() const
135 {
136     return m_format;
137 }