1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtOpenGL module 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40// We have to include this before the X11 headers dragged in by
41// qglxconvenience_p.h.
42#include <QtCore/qbytearray.h>
43#include <QtCore/qmetatype.h>
44#include <QtCore/qscopedpointer.h>
45#include <QtCore/qtextstream.h>
46#include <QtGui/qcolorspace.h>
47#include "qglxconvenience_p.h"
48
49#include <QtCore/qloggingcategory.h>
50#include <QtCore/qvarlengtharray.h>
51
52
53#include <GL/glxext.h>
54
55enum {
56 XFocusOut = FocusOut,
57 XFocusIn = FocusIn,
58 XKeyPress = KeyPress,
59 XKeyRelease = KeyRelease,
60 XNone = None,
61 XRevertToParent = RevertToParent,
62 XGrayScale = GrayScale,
63 XCursorShape = CursorShape
64};
65#undef FocusOut
66#undef FocusIn
67#undef KeyPress
68#undef KeyRelease
69#undef None
70#undef RevertToParent
71#undef GrayScale
72#undef CursorShape
73
74#ifdef FontChange
75#undef FontChange
76#endif
77
78#ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
79#define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20B2
80#endif
81
82QT_BEGIN_NAMESPACE
83
84Q_LOGGING_CATEGORY(lcGlx, "qt.glx")
85
86QList<int> qglx_buildSpec(const QSurfaceFormat &format, int drawableBit, int flags)
87{
88 QList<int> spec;
89
90 spec << GLX_LEVEL
91 << 0
92
93 << GLX_RENDER_TYPE
94 << GLX_RGBA_BIT
95
96 << GLX_RED_SIZE
97 << qMax(1, format.redBufferSize())
98
99 << GLX_GREEN_SIZE
100 << qMax(1, format.greenBufferSize())
101
102 << GLX_BLUE_SIZE
103 << qMax(1, format.blueBufferSize())
104
105 << GLX_ALPHA_SIZE
106 << qMax(0, format.alphaBufferSize());
107
108 if (format.swapBehavior() != QSurfaceFormat::SingleBuffer)
109 spec << GLX_DOUBLEBUFFER
110 << True;
111
112 if (format.stereo())
113 spec << GLX_STEREO
114 << True;
115
116 if (format.depthBufferSize() != -1)
117 spec << GLX_DEPTH_SIZE
118 << format.depthBufferSize();
119
120 if (format.stencilBufferSize() != -1)
121 spec << GLX_STENCIL_SIZE
122 << format.stencilBufferSize();
123
124 if (format.samples() > 1)
125 spec << GLX_SAMPLE_BUFFERS_ARB
126 << 1
127 << GLX_SAMPLES_ARB
128 << format.samples();
129
130 if ((flags & QGLX_SUPPORTS_SRGB) && format.colorSpace() == QColorSpace::SRgb)
131 spec << GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
132 << True;
133
134 spec << GLX_DRAWABLE_TYPE
135 << drawableBit
136
137 << XNone;
138
139 return spec;
140}
141
142namespace {
143struct QXcbSoftwareOpenGLEnforcer {
144 QXcbSoftwareOpenGLEnforcer() {
145 // Allow forcing LIBGL_ALWAYS_SOFTWARE for Qt 5 applications only.
146 // This is most useful with drivers that only support OpenGL 1.
147 // We need OpenGL 2, but the user probably doesn't want
148 // LIBGL_ALWAYS_SOFTWARE in OpenGL 1 apps.
149
150 if (!checkedForceSoftwareOpenGL) {
151 // If LIBGL_ALWAYS_SOFTWARE is already set, don't mess with it.
152 // We want to unset LIBGL_ALWAYS_SOFTWARE at the end so it does not
153 // get inherited by other processes, of course only if it wasn't
154 // already set before.
155 if (!qEnvironmentVariableIsEmpty("QT_XCB_FORCE_SOFTWARE_OPENGL")
156 && !qEnvironmentVariableIsSet("LIBGL_ALWAYS_SOFTWARE"))
157 forceSoftwareOpenGL = true;
158
159 checkedForceSoftwareOpenGL = true;
160 }
161
162 if (forceSoftwareOpenGL)
163 qputenv("LIBGL_ALWAYS_SOFTWARE", QByteArrayLiteral("1"));
164 }
165
166 ~QXcbSoftwareOpenGLEnforcer() {
167 // unset LIBGL_ALWAYS_SOFTWARE now so other processes don't inherit it
168 if (forceSoftwareOpenGL)
169 qunsetenv("LIBGL_ALWAYS_SOFTWARE");
170 }
171
172 static bool checkedForceSoftwareOpenGL;
173 static bool forceSoftwareOpenGL;
174};
175
176bool QXcbSoftwareOpenGLEnforcer::checkedForceSoftwareOpenGL = false;
177bool QXcbSoftwareOpenGLEnforcer::forceSoftwareOpenGL = false;
178
179template <class T>
180struct QXlibScopedPointerDeleter {
181 static inline void cleanup(T *pointer) {
182 if (pointer)
183 XFree(pointer);
184 }
185};
186
187template <class T>
188using QXlibPointer = QScopedPointer<T, QXlibScopedPointerDeleter<T>>;
189
190template <class T>
191using QXlibArrayPointer = QScopedArrayPointer<T, QXlibScopedPointerDeleter<T>>;
192}
193
194GLXFBConfig qglx_findConfig(Display *display, int screen , QSurfaceFormat format, bool highestPixelFormat, int drawableBit, int flags)
195{
196 QXcbSoftwareOpenGLEnforcer softwareOpenGLEnforcer;
197
198 GLXFBConfig config = nullptr;
199
200 do {
201 const QList<int> spec = qglx_buildSpec(format, drawableBit, flags);
202
203 int confcount = 0;
204 QXlibArrayPointer<GLXFBConfig> configs(glXChooseFBConfig(display, screen, spec.constData(), &confcount));
205
206 if (!config && confcount > 0) {
207 config = configs[0];
208 if (highestPixelFormat && !format.hasAlpha())
209 break;
210 }
211
212 const int requestedRed = qMax(0, format.redBufferSize());
213 const int requestedGreen = qMax(0, format.greenBufferSize());
214 const int requestedBlue = qMax(0, format.blueBufferSize());
215 const int requestedAlpha = qMax(0, format.alphaBufferSize());
216
217 GLXFBConfig compatibleCandidate = nullptr;
218 for (int i = 0; i < confcount; i++) {
219 GLXFBConfig candidate = configs[i];
220
221 if ((flags & QGLX_SUPPORTS_SRGB) && format.colorSpace() == QColorSpace::SRgb) {
222 int srgbCapable = 0;
223 glXGetFBConfigAttrib(display, candidate, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &srgbCapable);
224 if (!srgbCapable)
225 continue;
226 }
227
228 QXlibPointer<XVisualInfo> visual(glXGetVisualFromFBConfig(display, candidate));
229 if (!visual)
230 continue;
231 int actualRed;
232 int actualGreen;
233 int actualBlue;
234 int actualAlpha;
235 glXGetFBConfigAttrib(display, candidate, GLX_RED_SIZE, &actualRed);
236 glXGetFBConfigAttrib(display, candidate, GLX_GREEN_SIZE, &actualGreen);
237 glXGetFBConfigAttrib(display, candidate, GLX_BLUE_SIZE, &actualBlue);
238 glXGetFBConfigAttrib(display, candidate, GLX_ALPHA_SIZE, &actualAlpha);
239 // Sometimes the visuals don't have a depth that includes the alpha channel.
240 actualAlpha = qMin(actualAlpha, visual->depth - actualRed - actualGreen - actualBlue);
241
242 if (requestedRed && actualRed < requestedRed)
243 continue;
244 if (requestedGreen && actualGreen < requestedGreen)
245 continue;
246 if (requestedBlue && actualBlue < requestedBlue)
247 continue;
248 if (requestedAlpha && actualAlpha < requestedAlpha)
249 continue;
250 if (!compatibleCandidate) // Only pick up the first compatible one offered by the server
251 compatibleCandidate = candidate;
252
253 if (requestedRed && actualRed != requestedRed)
254 continue;
255 if (requestedGreen && actualGreen != requestedGreen)
256 continue;
257 if (requestedBlue && actualBlue != requestedBlue)
258 continue;
259 if (requestedAlpha && actualAlpha != requestedAlpha)
260 continue;
261
262 return candidate;
263 }
264 if (compatibleCandidate) {
265 qCDebug(lcGlx) << "qglx_findConfig: Found non-matching but compatible FBConfig";
266 return compatibleCandidate;
267 }
268 } while (qglx_reduceFormat(&format));
269
270 if (!config)
271 qCWarning(lcGlx) << "qglx_findConfig: Failed to finding matching FBConfig for" << format;
272
273 return config;
274}
275
276XVisualInfo *qglx_findVisualInfo(Display *display, int screen, QSurfaceFormat *format, int drawableBit, int flags)
277{
278 Q_ASSERT(format);
279
280 XVisualInfo *visualInfo = nullptr;
281
282 GLXFBConfig config = qglx_findConfig(display, screen, *format, false, drawableBit, flags);
283 if (config)
284 visualInfo = glXGetVisualFromFBConfig(display, config);
285
286 if (visualInfo) {
287 qglx_surfaceFormatFromGLXFBConfig(format, display, config, flags);
288 return visualInfo;
289 }
290
291 // attempt to fall back to glXChooseVisual
292 do {
293 QList<int> attribs = qglx_buildSpec(*format, drawableBit, flags);
294 visualInfo = glXChooseVisual(display, screen, attribs.data());
295
296 if (visualInfo) {
297 qglx_surfaceFormatFromVisualInfo(format, display, visualInfo, flags);
298 return visualInfo;
299 }
300 } while (qglx_reduceFormat(format));
301
302 return visualInfo;
303}
304
305void qglx_surfaceFormatFromGLXFBConfig(QSurfaceFormat *format, Display *display, GLXFBConfig config, int flags)
306{
307 int redSize = 0;
308 int greenSize = 0;
309 int blueSize = 0;
310 int alphaSize = 0;
311 int depthSize = 0;
312 int stencilSize = 0;
313 int sampleBuffers = 0;
314 int sampleCount = 0;
315 int stereo = 0;
316 int srgbCapable = 0;
317
318 glXGetFBConfigAttrib(display, config, GLX_RED_SIZE, &redSize);
319 glXGetFBConfigAttrib(display, config, GLX_GREEN_SIZE, &greenSize);
320 glXGetFBConfigAttrib(display, config, GLX_BLUE_SIZE, &blueSize);
321 glXGetFBConfigAttrib(display, config, GLX_ALPHA_SIZE, &alphaSize);
322 glXGetFBConfigAttrib(display, config, GLX_DEPTH_SIZE, &depthSize);
323 glXGetFBConfigAttrib(display, config, GLX_STENCIL_SIZE, &stencilSize);
324 glXGetFBConfigAttrib(display, config, GLX_SAMPLE_BUFFERS_ARB, &sampleBuffers);
325 glXGetFBConfigAttrib(display, config, GLX_STEREO, &stereo);
326 if (flags & QGLX_SUPPORTS_SRGB)
327 glXGetFBConfigAttrib(display, config, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &srgbCapable);
328
329 format->setRedBufferSize(redSize);
330 format->setGreenBufferSize(greenSize);
331 format->setBlueBufferSize(blueSize);
332 format->setAlphaBufferSize(alphaSize);
333 format->setDepthBufferSize(depthSize);
334 format->setStencilBufferSize(stencilSize);
335 if (sampleBuffers) {
336 glXGetFBConfigAttrib(display, config, GLX_SAMPLES_ARB, &sampleCount);
337 format->setSamples(sampleCount);
338 }
339 if (srgbCapable)
340 format->setColorSpace(QColorSpace::SRgb);
341 else
342 format->setColorSpace(QColorSpace());
343
344 format->setStereo(stereo);
345}
346
347void qglx_surfaceFormatFromVisualInfo(QSurfaceFormat *format, Display *display, XVisualInfo *visualInfo, int flags)
348{
349 int redSize = 0;
350 int greenSize = 0;
351 int blueSize = 0;
352 int alphaSize = 0;
353 int depthSize = 0;
354 int stencilSize = 0;
355 int sampleBuffers = 0;
356 int sampleCount = 0;
357 int stereo = 0;
358 int srgbCapable = 0;
359
360 glXGetConfig(display, visualInfo, GLX_RED_SIZE, &redSize);
361 glXGetConfig(display, visualInfo, GLX_GREEN_SIZE, &greenSize);
362 glXGetConfig(display, visualInfo, GLX_BLUE_SIZE, &blueSize);
363 glXGetConfig(display, visualInfo, GLX_ALPHA_SIZE, &alphaSize);
364 glXGetConfig(display, visualInfo, GLX_DEPTH_SIZE, &depthSize);
365 glXGetConfig(display, visualInfo, GLX_STENCIL_SIZE, &stencilSize);
366 glXGetConfig(display, visualInfo, GLX_SAMPLE_BUFFERS_ARB, &sampleBuffers);
367 glXGetConfig(display, visualInfo, GLX_STEREO, &stereo);
368 if (flags & QGLX_SUPPORTS_SRGB)
369 glXGetConfig(display, visualInfo, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &srgbCapable);
370
371 format->setRedBufferSize(redSize);
372 format->setGreenBufferSize(greenSize);
373 format->setBlueBufferSize(blueSize);
374 format->setAlphaBufferSize(alphaSize);
375 format->setDepthBufferSize(depthSize);
376 format->setStencilBufferSize(stencilSize);
377 if (sampleBuffers) {
378 glXGetConfig(display, visualInfo, GLX_SAMPLES_ARB, &sampleCount);
379 format->setSamples(sampleCount);
380 }
381 if (srgbCapable)
382 format->setColorSpace(QColorSpace::SRgb);
383 else
384 format->setColorSpace(QColorSpace());
385
386 format->setStereo(stereo);
387}
388
389bool qglx_reduceFormat(QSurfaceFormat *format)
390{
391 Q_ASSERT(format);
392 if (std::max(std::max(format->redBufferSize(), format->greenBufferSize()), format->blueBufferSize()) > 8) {
393 if (format->alphaBufferSize() > 2) {
394 // First try to match 10 10 10 2
395 format->setAlphaBufferSize(2);
396 return true;
397 }
398
399 format->setRedBufferSize(std::min(format->redBufferSize(), 8));
400 format->setGreenBufferSize(std::min(format->greenBufferSize(), 8));
401 format->setBlueBufferSize(std::min(format->blueBufferSize(), 8));
402 return true;
403 }
404
405 if (format->redBufferSize() > 1) {
406 format->setRedBufferSize(1);
407 return true;
408 }
409
410 if (format->greenBufferSize() > 1) {
411 format->setGreenBufferSize(1);
412 return true;
413 }
414
415 if (format->blueBufferSize() > 1) {
416 format->setBlueBufferSize(1);
417 return true;
418 }
419
420 if (format->swapBehavior() != QSurfaceFormat::SingleBuffer){
421 format->setSwapBehavior(QSurfaceFormat::SingleBuffer);
422 return true;
423 }
424
425 if (format->samples() > 1) {
426 format->setSamples(qMin(16, format->samples() / 2));
427 return true;
428 }
429
430 if (format->depthBufferSize() >= 32) {
431 format->setDepthBufferSize(24);
432 return true;
433 }
434
435 if (format->depthBufferSize() > 1) {
436 format->setDepthBufferSize(1);
437 return true;
438 }
439
440 if (format->depthBufferSize() > 0) {
441 format->setDepthBufferSize(0);
442 return true;
443 }
444
445 if (format->hasAlpha()) {
446 format->setAlphaBufferSize(0);
447 return true;
448 }
449
450 if (format->stencilBufferSize() > 1) {
451 format->setStencilBufferSize(1);
452 return true;
453 }
454
455 if (format->stencilBufferSize() > 0) {
456 format->setStencilBufferSize(0);
457 return true;
458 }
459
460 if (format->stereo()) {
461 format->setStereo(false);
462 return true;
463 }
464
465 if (format->colorSpace() == QColorSpace::SRgb) {
466 format->setColorSpace(QColorSpace());
467 return true;
468 }
469
470 return false;
471}
472
473QT_END_NAMESPACE
474