1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QLibrary>
#include <QGLFormat>
#include "qtestlitewindow.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glx.h>
#include "qglxintegration.h"
#if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
#include <dlfcn.h>
#endif
QT_BEGIN_NAMESPACE
GLXFBConfig qt_glx_integration_choose_config(MyDisplay* xd, QGLFormat& format, int drawableType)
{
int configAttribs[] = {
GLX_DRAWABLE_TYPE, drawableType,
GLX_LEVEL, format.plane(),
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, format.doubleBuffer() ? True : False,
GLX_STEREO, format.stereo() ? True : False,
GLX_DEPTH_SIZE, (format.depthBufferSize() == -1) ? 0 : format.depthBufferSize(),
GLX_STENCIL_SIZE, (format.stencilBufferSize() == -1) ? 0 : format.stencilBufferSize(),
GLX_SAMPLE_BUFFERS_ARB, (format.samples() == -1) ? 0 : format.samples(),
GLX_RED_SIZE, (format.redBufferSize() == -1) ? 1 : format.redBufferSize(),
GLX_GREEN_SIZE, (format.greenBufferSize() == -1) ? 1 : format.greenBufferSize(),
GLX_BLUE_SIZE, (format.blueBufferSize() == -1) ? 1 : format.blueBufferSize(),
GLX_ALPHA_SIZE, (format.alphaBufferSize() == -1) ? 0 : format.alphaBufferSize(),
GLX_ACCUM_RED_SIZE, (format.accumBufferSize() == -1) ? 0 : format.accumBufferSize(),
GLX_ACCUM_GREEN_SIZE, (format.accumBufferSize() == -1) ? 0 : format.accumBufferSize(),
GLX_ACCUM_BLUE_SIZE, (format.accumBufferSize() == -1) ? 0 : format.accumBufferSize(),
GLX_ACCUM_ALPHA_SIZE, (format.accumBufferSize() == -1) ? 0 : format.accumBufferSize(),
XNone
};
GLXFBConfig *configs;
int configCount = 0;
configs = glXChooseFBConfig(xd->display, xd->screen, configAttribs, &configCount);
if (!configs)
return 0;
GLXFBConfig chosenConfig = 0;
for (int i = 0; i < configCount; ++i) {
chosenConfig = configs[i];
// Make sure we try to get an ARGB visual if the format asked for an alpha:
if (format.alpha()) {
XVisualInfo* vi;
vi = glXGetVisualFromFBConfig(xd->display, configs[i]);
if (!vi)
continue;
XRenderPictFormat *pictFormat = XRenderFindVisualFormat(xd->display, vi->visual);
XFree(vi);
if (pictFormat && (pictFormat->type == PictTypeDirect) && pictFormat->direct.alphaMask) {
// The pict format for the visual matching the FBConfig indicates ARGB
break;
}
} else
break; // Just choose the first in the list if there's no alpha requested
}
// TODO: Populate the QGLFormat with the values of the GLXFBConfig
XFree(configs);
return chosenConfig;
}
QGLXGLWidgetSurface::QGLXGLWidgetSurface(MyDisplay* xd)
: QPlatformGLWidgetSurface()
, m_xd(xd)
, m_config(0)
, m_winId(0)
, m_widget(0)
{
}
QGLXGLWidgetSurface::~QGLXGLWidgetSurface()
{
}
static Colormap qt_glx_integration_colormap = 0;
bool QGLXGLWidgetSurface::create(QGLWidget *widget, QGLFormat& format)
{
m_widget = widget;
m_config = qt_glx_integration_choose_config(m_xd, format, GLX_WINDOW_BIT);
Window parentWindow = widget->window()->winId();
XVisualInfo* visualInfo;
visualInfo = glXGetVisualFromFBConfig(m_xd->display, m_config);
if (!qt_glx_integration_colormap) {
qt_glx_integration_colormap = XCreateColormap(m_xd->display, parentWindow,
visualInfo->visual, AllocNone);
}
XSetWindowAttributes windowAttribs;
windowAttribs.background_pixel = m_xd->whitePixel();
windowAttribs.border_pixel = m_xd->blackPixel();
windowAttribs.colormap = qt_glx_integration_colormap;
m_winId = XCreateWindow(m_xd->display, parentWindow,
widget->x(), widget->y(), widget->width(), widget->height(),
0, visualInfo->depth, InputOutput, visualInfo->visual,
CWBackPixel|CWBorderPixel|CWColormap, &windowAttribs);
XMapWindow(m_xd->display, m_winId);
XFree(visualInfo);
return true;
}
void QGLXGLWidgetSurface::setGeometry(const QRect& rect)
{
XMoveResizeWindow(m_xd->display, m_winId, rect.x(), rect.y(), rect.width(), rect.height());
}
bool QGLXGLWidgetSurface::filterEvent(QEvent *e)
{
if (e->type() == QEvent::ParentAboutToChange) {
// We temporarily hide the window and re-parent it with the root window
// as it's quite likely that the parent window is about to be deleted,
// which would otherwise destroy our window along with it.
XUnmapWindow(m_xd->display, m_winId);
XReparentWindow(m_xd->display, m_winId, m_xd->rootWindow(), 0, 0);
}
if (e->type() == QEvent::ParentChange) {
// Once we've got a new parent, we need to reparent the window and show it again:
XReparentWindow(m_xd->display, m_winId, m_widget->window()->winId(), m_widget->x(), m_widget->y());
XMapWindow(m_xd->display, m_winId);
}
return false; // Allow the event to pass through to QGLWidget
}
QGLXGLContext::QGLXGLContext(MyDisplay *xd)
: QPlatformGLContext()
, m_xd(xd)
, m_drawable(0)
, m_config(0)
, m_context(0)
{
}
QGLXGLContext::~QGLXGLContext()
{
if (m_context) {
qDebug("Destroying GLX context 0x%x", m_context);
glXDestroyContext(m_xd->display, m_context);
}
}
bool QGLXGLContext::create(QPaintDevice* device, QGLFormat& format, QPlatformGLContext* shareContext)
{
Q_UNUSED(format);
if (device->devType() != QInternal::Widget) {
qWarning("Creating a GL context is only supported on QWidgets");
return false;
}
GLXContext shareGlxContext = 0;
if (shareContext)
shareGlxContext = static_cast<QGLXGLContext*>(shareContext)->glxContext();
QWidget* widget = static_cast<QWidget*>(device);
QGLWidget* glWidget = qobject_cast<QGLWidget*>(widget);
if (glWidget) {
// Take the config from the QGLWidget's glx surface:
QGLXGLWidgetSurface* surface = static_cast<QGLXGLWidgetSurface*>(glWidget->platformSurface());
m_config = surface->config();
m_drawable = (Drawable)surface->winId();
}
else {
if (!widget->isTopLevel()) {
qWarning("Creating a GL context is only supported on top-level QWidgets");
return false;
}
m_drawable = (Drawable)widget->platformWindow()->winId();
// ### This might choose a config with a visual that isn't compatable with the native window:
m_config = qt_glx_integration_choose_config(m_xd, format, GLX_WINDOW_BIT);
}
m_context = glXCreateNewContext(m_xd->display, m_config, GLX_RGBA_TYPE, shareGlxContext, True);
// Get the XVisualInfo for the window:
// XWindowAttributes windowAttribs;
// XGetWindowAttributes(m_display, m_widget->winId(), &windowAttribs);
// XVisualInfo visualInfoTemplate;
// visualInfoTemplate.visualid = 33; //XVisualIDFromVisual(windowAttribs.visual);
// XVisualInfo *visualInfo;
// int matchingCount = 0;
// visualInfo = XGetVisualInfo(m_xd->display, VisualIDMask, &visualInfoTemplate, &matchingCount);
// m_context = glXCreateContext(m_xd->display, visualInfo, 0, True);
// qDebug("Created GLX context 0x%x for visual ID %d", m_context, visualInfoTemplate.visualid);
return true;
}
void QGLXGLContext::makeCurrent()
{
qDebug("QGLXGLContext::makeCurrent(window=0x%x, ctx=0x%x)", m_drawable, m_context);
glXMakeCurrent(m_xd->display, m_drawable, m_context);
}
void QGLXGLContext::doneCurrent()
{
glXMakeCurrent(m_xd->display, 0, 0);
}
void QGLXGLContext::swapBuffers()
{
glXSwapBuffers(m_xd->display, m_drawable);
}
void* QGLXGLContext::getProcAddress(const QString& procName)
{
typedef void *(*qt_glXGetProcAddressARB)(const GLubyte *);
static qt_glXGetProcAddressARB glXGetProcAddressARB = 0;
static bool resolved = false;
if (resolved && !glXGetProcAddressARB)
return 0;
if (!glXGetProcAddressARB) {
QList<QByteArray> glxExt = QByteArray(glXGetClientString(m_xd->display, GLX_EXTENSIONS)).split(' ');
if (glxExt.contains("GLX_ARB_get_proc_address")) {
#if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
void *handle = dlopen(NULL, RTLD_LAZY);
if (handle) {
glXGetProcAddressARB = (qt_glXGetProcAddressARB) dlsym(handle, "glXGetProcAddressARB");
dlclose(handle);
}
if (!glXGetProcAddressARB)
#endif
{
extern const QString qt_gl_library_name();
// QLibrary lib(qt_gl_library_name());
QLibrary lib(QLatin1String("GL"));
glXGetProcAddressARB = (qt_glXGetProcAddressARB) lib.resolve("glXGetProcAddressARB");
}
}
resolved = true;
}
if (!glXGetProcAddressARB)
return 0;
return glXGetProcAddressARB(reinterpret_cast<const GLubyte *>(procName.toLatin1().data()));
}
QT_END_NAMESPACE
|