1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the plugins of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
45
#include "qxcbwindow.h"
46
#include "qxcbscreen.h"
49
#include <X11/Xutil.h>
52
#include <QtGui/QOpenGLContext>
54
#include "qglxintegration.h"
55
#include <QtPlatformSupport/private/qglxconvenience_p.h>
57
#if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
63
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
65
#ifndef GLX_CONTEXT_CORE_PROFILE_BIT_ARB
66
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
69
#ifndef GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
70
#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
73
#ifndef GLX_CONTEXT_PROFILE_MASK_ARB
74
#define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
77
static Window createDummyWindow(QXcbScreen *screen, XVisualInfo *visualInfo)
79
Colormap cmap = XCreateColormap(DISPLAY_FROM_XCB(screen), screen->root(), visualInfo->visual, AllocNone);
80
XSetWindowAttributes a;
81
a.background_pixel = WhitePixel(DISPLAY_FROM_XCB(screen), screen->screenNumber());
82
a.border_pixel = BlackPixel(DISPLAY_FROM_XCB(screen), screen->screenNumber());
85
Window window = XCreateWindow(DISPLAY_FROM_XCB(screen), screen->root(),
87
0, visualInfo->depth, InputOutput, visualInfo->visual,
88
CWBackPixel|CWBorderPixel|CWColormap, &a);
89
XFreeColormap(DISPLAY_FROM_XCB(screen), cmap);
93
static Window createDummyWindow(QXcbScreen *screen, GLXFBConfig config)
95
XVisualInfo *visualInfo = glXGetVisualFromFBConfig(DISPLAY_FROM_XCB(screen), config);
97
qFatal("Could not initialize GLX");
98
Window window = createDummyWindow(screen, visualInfo);
103
// Per-window data for active OpenGL contexts.
104
struct QOpenGLContextData
106
QOpenGLContextData(Display *display, Window window, GLXContext context)
107
: m_display(display),
120
GLXContext m_context;
123
static inline QOpenGLContextData currentOpenGLContextData()
125
QOpenGLContextData result;
126
result.m_display = glXGetCurrentDisplay();
127
result.m_window = glXGetCurrentDrawable();
128
result.m_context = glXGetCurrentContext();
132
static inline QOpenGLContextData createDummyWindowOpenGLContextData(QXcbScreen *screen)
134
QOpenGLContextData result;
135
result.m_display = DISPLAY_FROM_XCB(screen);
137
QSurfaceFormat format;
138
GLXFBConfig config = qglx_findConfig(DISPLAY_FROM_XCB(screen), screen->screenNumber(), format);
140
result.m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, 0, true);
141
result.m_window = createDummyWindow(screen, config);
143
XVisualInfo *visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(screen), screen->screenNumber(), &format);
145
qFatal("Could not initialize GLX");
146
result.m_context = glXCreateContext(DISPLAY_FROM_XCB(screen), visualInfo, 0, true);
147
result.m_window = createDummyWindow(screen, visualInfo);
154
static inline QByteArray getGlString(GLenum param)
156
if (const GLubyte *s = glGetString(param))
157
return QByteArray(reinterpret_cast<const char*>(s));
161
static void updateFormatFromContext(QSurfaceFormat &format)
163
// Update the version, profile, and context bit of the format
164
int major = 0, minor = 0;
165
QByteArray versionString(getGlString(GL_VERSION));
166
if (QPlatformOpenGLContext::parseOpenGLVersion(versionString, major, minor)) {
167
format.setMajorVersion(major);
168
format.setMinorVersion(minor);
171
const int version = (major << 8) + minor;
172
if (version < 0x0300) {
173
format.setProfile(QSurfaceFormat::NoProfile);
174
format.setOption(QSurfaceFormat::DeprecatedFunctions);
178
// Version 3.0 onwards - check if it includes deprecated functionality or is
181
glGetIntegerv(GL_CONTEXT_FLAGS, &value);
182
if (value & ~GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT)
183
format.setOption(QSurfaceFormat::DeprecatedFunctions);
184
if (value & GLX_CONTEXT_DEBUG_BIT_ARB)
185
format.setOption(QSurfaceFormat::DebugContext);
186
if (version < 0x0302)
189
// Version 3.2 and newer have a profile
191
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &value);
193
case GLX_CONTEXT_CORE_PROFILE_BIT_ARB:
194
format.setProfile(QSurfaceFormat::CoreProfile);
196
case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB:
197
format.setProfile(QSurfaceFormat::CompatibilityProfile);
200
format.setProfile(QSurfaceFormat::NoProfile);
206
\class QOpenGLTemporaryContext
207
\brief A temporary context that can be instantiated on the stack.
209
Functions like glGetString() only work if there is a current GL context.
212
\ingroup qt-lighthouse-xcb
214
class QOpenGLTemporaryContext
216
Q_DISABLE_COPY(QOpenGLTemporaryContext)
218
QOpenGLTemporaryContext(QXcbScreen *screen);
219
~QOpenGLTemporaryContext();
222
const QOpenGLContextData m_previous;
223
const QOpenGLContextData m_current;
226
QOpenGLTemporaryContext::QOpenGLTemporaryContext(QXcbScreen *screen)
227
: m_previous(currentOpenGLContextData()),
228
m_current(createDummyWindowOpenGLContextData(screen))
230
// Make our temporary context current on our temporary window
231
glXMakeCurrent(m_current.m_display, m_current.m_window, m_current.m_context);
234
QOpenGLTemporaryContext::~QOpenGLTemporaryContext()
236
// Restore the previous context if possible, otherwise just release our temporary context
237
if (m_previous.m_display)
238
glXMakeCurrent(m_previous.m_display, m_previous.m_window, m_previous.m_context);
240
glXMakeCurrent(m_current.m_display, 0, 0);
242
// Destroy our temporary window
243
XDestroyWindow(m_current.m_display, m_current.m_window);
245
// Finally destroy our temporary context itself
246
glXDestroyContext(m_current.m_display, m_current.m_context);
249
QOpenGLDefaultContextInfo::QOpenGLDefaultContextInfo()
250
: vendor(getGlString(GL_VENDOR)),
251
renderer(getGlString(GL_RENDERER))
253
updateFormatFromContext(format);
256
QOpenGLDefaultContextInfo *QOpenGLDefaultContextInfo::create(QXcbScreen *screen)
258
// We need a current context for getGLString() to work. To have
259
// the QOpenGLDefaultContextInfo contain the latest supported
260
// context version, we rely upon the QOpenGLTemporaryContext to
261
// correctly obtain a context with the latest version
262
QScopedPointer<QOpenGLTemporaryContext> temporaryContext(new QOpenGLTemporaryContext(screen));
263
QOpenGLDefaultContextInfo *result = new QOpenGLDefaultContextInfo;
268
QGLXContext::QGLXContext(QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share, QOpenGLDefaultContextInfo *defaultContextInfo)
269
: QPlatformOpenGLContext()
276
m_shareContext = static_cast<const QGLXContext*>(share)->glxContext();
278
GLXFBConfig config = qglx_findConfig(DISPLAY_FROM_XCB(screen),screen->screenNumber(),format);
279
XVisualInfo *visualInfo = 0;
280
Window window = 0; // Temporary window used to query OpenGL context
283
// Resolve entry point for glXCreateContextAttribsARB
284
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
285
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
287
// Use glXCreateContextAttribsARB if is available
288
if (glXCreateContextAttribsARB != 0) {
289
// We limit the requested version by the version of the static context as
290
// glXCreateContextAttribsARB fails and returns NULL if the requested context
291
// version is not supported. This means that we will get the closest supported
292
// context format that that which was requested and is supported by the driver
293
const int maxSupportedVersion = (defaultContextInfo->format.majorVersion() << 8)
294
+ defaultContextInfo->format.minorVersion();
295
const int requestedVersion = qMin((format.majorVersion() << 8) + format.minorVersion(),
296
maxSupportedVersion);
297
const int majorVersion = requestedVersion >> 8;
298
const int minorVersion = requestedVersion & 0xFF;
300
QVector<int> contextAttributes;
301
contextAttributes << GLX_CONTEXT_MAJOR_VERSION_ARB << majorVersion
302
<< GLX_CONTEXT_MINOR_VERSION_ARB << minorVersion;
304
// If asking for OpenGL 3.2 or newer we should also specify a profile
305
if (m_format.majorVersion() > 3 || (m_format.majorVersion() == 3 && m_format.minorVersion() > 1)) {
306
if (m_format.profile() == QSurfaceFormat::CoreProfile) {
307
contextAttributes << GLX_CONTEXT_FLAGS_ARB << GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
308
<< GLX_CONTEXT_PROFILE_MASK_ARB << GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
310
contextAttributes << GLX_CONTEXT_PROFILE_MASK_ARB << GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
314
contextAttributes << None;
316
m_context = glXCreateContextAttribsARB(DISPLAY_FROM_XCB(screen), config, m_shareContext, true, contextAttributes.data());
317
if (!m_context && m_shareContext) {
318
// re-try without a shared glx context
319
m_context = glXCreateContextAttribsARB(DISPLAY_FROM_XCB(screen), config, 0, true, contextAttributes.data());
325
// Could not create a context using glXCreateContextAttribsARB, falling back to glXCreateNewContext.
327
m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, m_shareContext, true);
328
if (!m_context && m_shareContext) {
329
// re-try without a shared glx context
330
m_context = glXCreateNewContext(DISPLAY_FROM_XCB(screen), config, GLX_RGBA_TYPE, 0, true);
336
// Get the basic surface format details
338
m_format = qglx_surfaceFormatFromGLXFBConfig(DISPLAY_FROM_XCB(screen), config, m_context);
340
// Create a temporary window so that we can make the new context current
341
window = createDummyWindow(screen, config);
343
// Note that m_format gets updated with the used surface format
344
visualInfo = qglx_findVisualInfo(DISPLAY_FROM_XCB(screen), screen->screenNumber(), &m_format);
346
qFatal("Could not initialize GLX");
347
m_context = glXCreateContext(DISPLAY_FROM_XCB(screen), visualInfo, m_shareContext, true);
348
if (!m_context && m_shareContext) {
349
// re-try without a shared glx context
351
m_context = glXCreateContext(DISPLAY_FROM_XCB(screen), visualInfo, 0, true);
354
// Create a temporary window so that we can make the new context current
355
window = createDummyWindow(screen, visualInfo);
359
// Query the OpenGL version and profile
360
if (m_context && window) {
361
glXMakeCurrent(DISPLAY_FROM_XCB(screen), window, m_context);
362
updateFormatFromContext(m_format);
364
// Make our context non-current
365
glXMakeCurrent(DISPLAY_FROM_XCB(screen), 0, 0);
368
// Destroy our temporary window
369
XDestroyWindow(DISPLAY_FROM_XCB(screen), window);
372
QGLXContext::~QGLXContext()
374
glXDestroyContext(DISPLAY_FROM_XCB(m_screen), m_context);
377
bool QGLXContext::makeCurrent(QPlatformSurface *surface)
379
Q_ASSERT(surface->surface()->surfaceType() == QSurface::OpenGLSurface);
381
GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
383
return glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), glxDrawable, m_context);
386
void QGLXContext::doneCurrent()
388
glXMakeCurrent(DISPLAY_FROM_XCB(m_screen), 0, 0);
391
void QGLXContext::swapBuffers(QPlatformSurface *surface)
393
GLXDrawable glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
394
glXSwapBuffers(DISPLAY_FROM_XCB(m_screen), glxDrawable);
397
void (*QGLXContext::getProcAddress(const QByteArray &procName)) ()
399
typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
400
static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
401
static bool resolved = false;
403
if (resolved && !glXGetProcAddressARB)
405
if (!glXGetProcAddressARB) {
406
QList<QByteArray> glxExt = QByteArray(glXGetClientString(DISPLAY_FROM_XCB(m_screen), GLX_EXTENSIONS)).split(' ');
407
if (glxExt.contains("GLX_ARB_get_proc_address")) {
408
#if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
409
void *handle = dlopen(NULL, RTLD_LAZY);
411
glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
414
if (!glXGetProcAddressARB)
417
extern const QString qt_gl_library_name();
418
// QLibrary lib(qt_gl_library_name());
419
QLibrary lib(QLatin1String("GL"));
420
glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
425
if (!glXGetProcAddressARB)
427
return (void (*)())glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.constData()));
430
QSurfaceFormat QGLXContext::format() const
435
bool QGLXContext::isSharing() const
437
return m_shareContext != 0;
440
bool QGLXContext::isValid() const
442
return m_context != 0;