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 "qxlibwindow.h"
47 #include "qxlibscreen.h"
48 #include "qxlibdisplay.h"
49 #include "qxlibstatic.h"
50 
51 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_2)
52 #include "qglxintegration.h"
53 
54 #include "qglxconvenience.h"
55 
56 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
57 #include <dlfcn.h>
58 #endif
59 
61 
64  , m_screen(screen)
65  , m_drawable((Drawable)window)
66  , m_context(0)
67 {
68 
69  const QPlatformGLContext *sharePlatformContext;
70  sharePlatformContext = format.sharedGLContext();
71  GLXContext shareGlxContext = 0;
72  if (sharePlatformContext)
73  shareGlxContext = static_cast<const QGLXContext*>(sharePlatformContext)->glxContext();
74 
75  GLXFBConfig config = qglx_findConfig(screen->display()->nativeDisplay(),screen->xScreenNumber(),format);
76  m_context = glXCreateNewContext(screen->display()->nativeDisplay(),config,GLX_RGBA_TYPE,shareGlxContext,TRUE);
78 
79 #ifdef MYX11_DEBUG
80  qDebug() << "QGLXGLContext::create context" << m_context;
81 #endif
82 }
83 
84 QGLXContext::QGLXContext(QXlibScreen *screen, Drawable drawable, GLXContext context)
85  : QPlatformGLContext(), m_screen(screen), m_drawable(drawable), m_context(context)
86 {
87 
88 }
89 
91 {
92  if (m_context) {
93  qDebug("Destroying GLX context 0x%p", m_context);
94  glXDestroyContext(m_screen->display()->nativeDisplay(), m_context);
95  }
96 }
97 
99 {
101 #ifdef MYX11_DEBUG
102  qDebug("QGLXGLContext::makeCurrent(window=0x%x, ctx=0x%x)", m_drawable, m_context);
103 #endif
104  glXMakeCurrent(m_screen->display()->nativeDisplay(), m_drawable, m_context);
105 }
106 
108 {
110  glXMakeCurrent(m_screen->display()->nativeDisplay(), 0, 0);
111 }
112 
114 {
115  glXSwapBuffers(m_screen->display()->nativeDisplay(), m_drawable);
116 }
117 
118 void* QGLXContext::getProcAddress(const QString& procName)
119 {
120  typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
121  static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
122  static bool resolved = false;
123 
124  if (resolved && !glXGetProcAddressARB)
125  return 0;
126  if (!glXGetProcAddressARB) {
127  QList<QByteArray> glxExt = QByteArray(glXGetClientString(m_screen->display()->nativeDisplay(), GLX_EXTENSIONS)).split(' ');
128  if (glxExt.contains("GLX_ARB_get_proc_address")) {
129 #if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
130  void *handle = dlopen(NULL, RTLD_LAZY);
131  if (handle) {
132  glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
133  dlclose(handle);
134  }
135  if (!glXGetProcAddressARB)
136 #endif
137  {
138  extern const QString qt_gl_library_name();
139 // QLibrary lib(qt_gl_library_name());
140  QLibrary lib(QLatin1String("GL"));
141  glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
142  }
143  }
144  resolved = true;
145  }
146  if (!glXGetProcAddressARB)
147  return 0;
148  return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.toLatin1().data()));
149 }
150 
152 {
153  return m_windowFormat;
154 }
155 
157 
158 #endif
QPlatformWindowFormat platformWindowFormat() const
QWidget has the function qplatformWindowFormat().
GLXContext m_context
Drawable m_drawable
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
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)
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.
Display * nativeDisplay() const
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
Q_CORE_EXPORT void qDebug(const char *,...)
NSWindow * window
Q_OPENGL_EXPORT const QString qt_gl_library_name()
Definition: qgl.cpp:5827
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
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.
int xScreenNumber() const
QXcbScreen * m_screen
QPlatformWindowFormat m_windowFormat
#define GLX_RGBA_TYPE
GLXContext glxContext() const
QXlibDisplay * display() 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.