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
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module 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.h>
#include <qdebug.h>
#include <private/qgl_p.h>
#include <private/qt_x11_p.h>
#include <private/qpaintengine_opengl_p.h>
#include <qx11info_x11.h>
#include <GL/glx.h>
#include <qimage.h>
#include "qglpixelbuffer.h"
#include "qglpixelbuffer_p.h"
#if defined(Q_OS_LINUX) || defined(Q_OS_BSD4)
#include <dlfcn.h>
#endif
QT_BEGIN_NAMESPACE
#ifndef GLX_VERSION_1_3
#define GLX_RGBA_BIT 0x00000002
#define GLX_PBUFFER_BIT 0x00000004
#define GLX_DRAWABLE_TYPE 0x8010
#define GLX_RENDER_TYPE 0x8011
#define GLX_RGBA_TYPE 0x8014
#define GLX_PBUFFER_HEIGHT 0x8040
#define GLX_PBUFFER_WIDTH 0x8041
#endif
#ifndef GLX_ARB_multisample
#define GLX_SAMPLE_BUFFERS_ARB 100000
#define GLX_SAMPLES_ARB 100001
#endif
typedef GLXFBConfig* (*_glXChooseFBConfig) (Display *dpy, int screen, const int *attrib_list, int *nelements);
typedef int (*_glXGetFBConfigAttrib) (Display *dpy, GLXFBConfig config, int attribute, int *value);
typedef GLXPbuffer (*_glXCreatePbuffer) (Display *dpy, GLXFBConfig config, const int *attrib_list);
typedef void (*_glXDestroyPbuffer) (Display *dpy, GLXPbuffer pbuf);
typedef GLXContext (*_glXCreateNewContext) (Display *dpy, GLXFBConfig config, int render_type, GLXContext share_list, Bool direct);
typedef Bool (*_glXMakeContextCurrent) (Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx);
static _glXChooseFBConfig qt_glXChooseFBConfig = 0;
static _glXCreateNewContext qt_glXCreateNewContext = 0;
static _glXCreatePbuffer qt_glXCreatePbuffer = 0;
static _glXDestroyPbuffer qt_glXDestroyPbuffer = 0;
static _glXGetFBConfigAttrib qt_glXGetFBConfigAttrib = 0;
static _glXMakeContextCurrent qt_glXMakeContextCurrent = 0;
#define glXChooseFBConfig qt_glXChooseFBConfig
#define glXCreateNewContext qt_glXCreateNewContext
#define glXCreatePbuffer qt_glXCreatePbuffer
#define glXDestroyPbuffer qt_glXDestroyPbuffer
#define glXGetFBConfigAttrib qt_glXGetFBConfigAttrib
#define glXMakeContextCurrent qt_glXMakeContextCurrent
extern void* qglx_getProcAddress(const char* procName); // in qgl_x11.cpp
static bool qt_resolve_pbuffer_extensions()
{
static int resolved = false;
if (resolved && qt_glXMakeContextCurrent)
return true;
else if (resolved)
return false;
qt_glXChooseFBConfig = (_glXChooseFBConfig) qglx_getProcAddress("glXChooseFBConfig");
qt_glXCreateNewContext = (_glXCreateNewContext) qglx_getProcAddress("glXCreateNewContext");
qt_glXCreatePbuffer = (_glXCreatePbuffer) qglx_getProcAddress("glXCreatePbuffer");
qt_glXDestroyPbuffer = (_glXDestroyPbuffer) qglx_getProcAddress("glXDestroyPbuffer");
qt_glXGetFBConfigAttrib = (_glXGetFBConfigAttrib) qglx_getProcAddress("glXGetFBConfigAttrib");
qt_glXMakeContextCurrent = (_glXMakeContextCurrent) qglx_getProcAddress("glXMakeContextCurrent");
resolved = qt_glXMakeContextCurrent ? true : false;
return resolved;
}
static void qt_format_to_attrib_list(const QGLFormat &f, int attribs[])
{
int i = 0;
attribs[i++] = GLX_RENDER_TYPE;
attribs[i++] = GLX_RGBA_BIT;
attribs[i++] = GLX_DRAWABLE_TYPE;
attribs[i++] = GLX_PBUFFER_BIT;
attribs[i++] = GLX_RED_SIZE;
attribs[i++] = f.redBufferSize() == -1 ? 1 : f.redBufferSize();
attribs[i++] = GLX_GREEN_SIZE;
attribs[i++] = f.greenBufferSize() == -1 ? 1 : f.greenBufferSize();
attribs[i++] = GLX_BLUE_SIZE;
attribs[i++] = f.blueBufferSize() == -1 ? 1 : f.blueBufferSize();
if (f.doubleBuffer()) {
attribs[i++] = GLX_DOUBLEBUFFER;
attribs[i++] = true;
}
if (f.depth()) {
attribs[i++] = GLX_DEPTH_SIZE;
attribs[i++] = f.depthBufferSize() == -1 ? 1 : f.depthBufferSize();
}
if (f.stereo()) {
attribs[i++] = GLX_STEREO;
attribs[i++] = true;
}
if (f.stencil()) {
attribs[i++] = GLX_STENCIL_SIZE;
attribs[i++] = f.stencilBufferSize() == -1 ? 1 : f.stencilBufferSize();
}
if (f.alpha()) {
attribs[i++] = GLX_ALPHA_SIZE;
attribs[i++] = f.alphaBufferSize() == -1 ? 1 : f.alphaBufferSize();
}
if (f.accum()) {
attribs[i++] = GLX_ACCUM_RED_SIZE;
attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
attribs[i++] = GLX_ACCUM_GREEN_SIZE;
attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
attribs[i++] = GLX_ACCUM_BLUE_SIZE;
attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
if (f.alpha()) {
attribs[i++] = GLX_ACCUM_ALPHA_SIZE;
attribs[i++] = f.accumBufferSize() == -1 ? 1 : f.accumBufferSize();
}
}
if (f.sampleBuffers()) {
attribs[i++] = GLX_SAMPLE_BUFFERS_ARB;
attribs[i++] = 1;
attribs[i++] = GLX_SAMPLES_ARB;
attribs[i++] = f.samples() == -1 ? 4 : f.samples();
}
attribs[i] = XNone;
}
bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
{
if (!qt_resolve_pbuffer_extensions()) {
qWarning("QGLPixelBuffer: pbuffers are not supported on this system.");
return false;
}
int attribs[40];
int num_configs = 0;
qt_format_to_attrib_list(f, attribs);
int screen = X11->defaultScreen;
if (shareWidget)
screen = shareWidget->x11Info().screen();
GLXFBConfig *configs = glXChooseFBConfig(X11->display, screen, attribs, &num_configs);
if (configs && num_configs) {
int res;
glXGetFBConfigAttrib(X11->display, configs[0], GLX_LEVEL, &res);
format.setPlane(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_DOUBLEBUFFER, &res);
format.setDoubleBuffer(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_DEPTH_SIZE, &res);
format.setDepth(res);
if (format.depth())
format.setDepthBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_RGBA, &res);
format.setRgba(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_RED_SIZE, &res);
format.setRedBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_GREEN_SIZE, &res);
format.setGreenBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_BLUE_SIZE, &res);
format.setBlueBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_ALPHA_SIZE, &res);
format.setAlpha(res);
if (format.alpha())
format.setAlphaBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_ACCUM_RED_SIZE, &res);
format.setAccum(res);
if (format.accum())
format.setAccumBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_STENCIL_SIZE, &res);
format.setStencil(res);
if (format.stencil())
format.setStencilBufferSize(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_STEREO, &res);
format.setStereo(res);
glXGetFBConfigAttrib(X11->display, configs[0], GLX_SAMPLE_BUFFERS_ARB, &res);
format.setSampleBuffers(res);
if (format.sampleBuffers()) {
glXGetFBConfigAttrib(X11->display, configs[0], GLX_SAMPLES_ARB, &res);
format.setSamples(res);
}
int pb_attribs[] = {GLX_PBUFFER_WIDTH, size.width(), GLX_PBUFFER_HEIGHT, size.height(), XNone};
GLXContext shareContext = 0;
if (shareWidget && shareWidget->d_func()->glcx)
shareContext = (GLXContext) shareWidget->d_func()->glcx->d_func()->cx;
pbuf = glXCreatePbuffer(QX11Info::display(), configs[0], pb_attribs);
ctx = glXCreateNewContext(QX11Info::display(), configs[0], GLX_RGBA_TYPE, shareContext, true);
XFree(configs);
if (!pbuf || !ctx) {
qWarning("QGLPixelBuffer: Unable to create a pbuffer/context - giving up.");
return false;
}
return true;
} else {
qWarning("QGLPixelBuffer: Unable to find a context/format match - giving up.");
return false;
}
}
bool QGLPixelBufferPrivate::cleanup()
{
glXDestroyPbuffer(QX11Info::display(), pbuf);
return true;
}
bool QGLPixelBuffer::bindToDynamicTexture(GLuint)
{
return false;
}
void QGLPixelBuffer::releaseFromDynamicTexture()
{
}
bool QGLPixelBuffer::hasOpenGLPbuffers()
{
bool ret = qt_resolve_pbuffer_extensions();
if (!ret)
return false;
int attribs[40];
int num_configs = 0;
qt_format_to_attrib_list(QGLFormat::defaultFormat(), attribs);
GLXFBConfig *configs = glXChooseFBConfig(X11->display, X11->defaultScreen, attribs, &num_configs);
GLXPbuffer pbuf = 0;
GLXContext ctx = 0;
if (configs && num_configs) {
int pb_attribs[] = {GLX_PBUFFER_WIDTH, 128, GLX_PBUFFER_HEIGHT, 128, XNone};
pbuf = glXCreatePbuffer(X11->display, configs[0], pb_attribs);
ctx = glXCreateNewContext(X11->display, configs[0], GLX_RGBA_TYPE, 0, true);
XFree(configs);
glXDestroyContext(X11->display, ctx);
glXDestroyPbuffer(X11->display, pbuf);
}
return pbuf && ctx;
}
QT_END_NAMESPACE
|