Qt 4.8
qglxintegration.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 "qglxintegration.h"
54 #include "qglxconvenience.h"
55 
56 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
57 #include <dlfcn.h>
58 #endif
59 
62  , m_screen(screen)
63  , m_drawable((Drawable)window)
64  , m_context(0)
65 {
67  const QPlatformGLContext *sharePlatformContext;
68  sharePlatformContext = format.sharedGLContext();
69  GLXContext shareGlxContext = 0;
70  if (sharePlatformContext)
71  shareGlxContext = static_cast<const QGLXContext*>(sharePlatformContext)->glxContext();
72 
73  GLXFBConfig config = qglx_findConfig(DISPLAY_FROM_XCB(screen),screen->screenNumber(),format);
74  m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, shareGlxContext, TRUE);
77 }
78 
79 QGLXContext::QGLXContext(QXcbScreen *screen, Drawable drawable, GLXContext context)
80  : QPlatformGLContext(), m_screen(screen), m_drawable(drawable), m_context(context)
81 {
82 
83 }
84 
86 {
88  if (m_context)
89  glXDestroyContext(DISPLAY_FROM_XCB(m_screen), m_context);
91 }
92 
94 {
97  glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), m_drawable, m_context);
99 }
100 
102 {
105  glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), 0, 0);
107 }
108 
110 {
112  glXSwapBuffers(DISPLAY_FROM_XCB(m_screen), m_drawable);
114 }
115 
116 void* QGLXContext::getProcAddress(const QString& procName)
117 {
119  typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
120  static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
121  static bool resolved = false;
122 
123  if (resolved && !glXGetProcAddressARB)
124  return 0;
125  if (!glXGetProcAddressARB) {
126  QList<QByteArray> glxExt = QByteArray(glXGetClientString(DISPLAY_FROM_XCB(m_screen), GLX_EXTENSIONS)).split(' ');
127  if (glxExt.contains("GLX_ARB_get_proc_address")) {
128 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
129  void *handle = dlopen(NULL, RTLD_LAZY);
130  if (handle) {
131  glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
132  dlclose(handle);
133  }
134  if (!glXGetProcAddressARB)
135 #endif
136  {
137  extern const QString qt_gl_library_name();
138 // QLibrary lib(qt_gl_library_name());
139  QLibrary lib(QLatin1String("GL"));
140  glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
141  }
142  }
143  resolved = true;
144  }
145  if (!glXGetProcAddressARB)
146  return 0;
147  return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.toLatin1().data()));
148 }
149 
151 {
152  return m_windowFormat;
153 }
QPlatformWindowFormat platformWindowFormat() const
QWidget has the function qplatformWindowFormat().
GLXContext m_context
Drawable m_drawable
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QPlatformWindowFormat qglx_platformWindowFromGLXFBConfig(Display *display, GLXFBConfig config, GLXContext ctx)
#define DISPLAY_FROM_XCB(object)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
virtual void * getProcAddress(const QString &procName)
Reimplement in subclass to native getProcAddr calls.
virtual void doneCurrent()
Reimplement in subclass to release current context.
QGLXContext(Window window, QXcbScreen *xd, const QPlatformWindowFormat &format)
The QPlatformWindowFormat class specifies the display format of an OpenGL rendering context and if po...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void * resolve(const char *symbol)
Returns the address of the exported symbol symbol.
Definition: qlibrary.cpp:1155
The QString class provides a Unicode character string.
Definition: qstring.h:83
QXcbConnection * connection() const
Definition: qxcbobject.h:53
NSWindow * window
Q_OPENGL_EXPORT const QString qt_gl_library_name()
Definition: qgl.cpp:5827
QBool contains(const T &t) const
Returns true if the list contains an occurrence of value; otherwise returns false.
Definition: qlist.h:880
QPlatformGLContext * sharedGLContext() const
virtual void doneCurrent()
Reimplement in subclass to release current context.
virtual void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
virtual void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
GLXFBConfig qglx_findConfig(Display *display, int screen, const QPlatformWindowFormat &format, int drawableBit)
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays...
The QPlatformGLContext class provides an abstraction for native GL contexts.
QXcbScreen * m_screen
QPlatformWindowFormat m_windowFormat
#define GLX_RGBA_TYPE
#define Q_XCB_NOOP(c)
int screenNumber() const
Definition: qxcbscreen.cpp:131
GLXContext glxContext() const
#define glXCreateNewContext
The QLibrary class loads shared libraries at runtime.
Definition: qlibrary.h:62
virtual void swapBuffers()
Reimplement in subclass to native swap buffers calls.