Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qglxintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <QDebug>
6
7#include "qxcbwindow.h"
8#include "qxcbscreen.h"
9
10#define register /* C++17 deprecated register */
11#include <X11/Xlib.h>
12#include <X11/Xutil.h>
13#undef register
14#include <GL/glx.h>
15
16#include <QtGui/qguiapplication.h>
17#include <QtGui/QOpenGLContext>
18#include <QtGui/QOffscreenSurface>
19
21#include <QtGui/private/qglxconvenience_p.h>
22
24
26
27typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
28typedef const GLubyte *(*glGetStringiProc)(GLenum, GLuint);
29
31#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
32#endif
33
35#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
36#endif
37
39#define GLX_CONTEXT_ES2_PROFILE_BIT_EXT 0x00000004
40#endif
41
43#define GLX_CONTEXT_PROFILE_MASK_ARB 0x9126
44#endif
45
47#define GL_CONTEXT_FLAG_DEBUG_BIT 0x00000002
48#endif
49
51#define GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB 0x00000004
52#endif
53
55#define GL_RESET_NOTIFICATION_STRATEGY_ARB 0x8256
56#endif
57
59#define GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB 0x8256
60#endif
61
63#define GL_LOSE_CONTEXT_ON_RESET_ARB 0x8252
64#endif
65
67#define GLX_LOSE_CONTEXT_ON_RESET_ARB 0x8252
68#endif
69
71#define GLX_GENERATE_RESET_ON_VIDEO_MEMORY_PURGE_NV 0x20F7
72#endif
73
74static Window createDummyWindow(Display *dpy, XVisualInfo *visualInfo, int screenNumber, Window rootWin)
75{
76 Colormap cmap = XCreateColormap(dpy, rootWin, visualInfo->visual, AllocNone);
77 XSetWindowAttributes a;
78 a.background_pixel = WhitePixel(dpy, screenNumber);
79 a.border_pixel = BlackPixel(dpy, screenNumber);
80 a.colormap = cmap;
81 a.override_redirect = true;
82
83 Window window = XCreateWindow(dpy, rootWin,
84 0, 0, 100, 100,
85 0, visualInfo->depth, InputOutput, visualInfo->visual,
86 CWBackPixel|CWBorderPixel|CWColormap|CWOverrideRedirect, &a);
87#ifndef QT_NO_DEBUG
88 XStoreName(dpy, window, "Qt GLX dummy window");
89#endif
90 XFreeColormap(dpy, cmap);
91 return window;
92}
93
94static Window createDummyWindow(Display *dpy, GLXFBConfig config, int screenNumber, Window rootWin)
95{
96 XVisualInfo *visualInfo = glXGetVisualFromFBConfig(dpy, config);
97 if (Q_UNLIKELY(!visualInfo))
98 qFatal("Could not initialize GLX");
99 Window window = createDummyWindow(dpy, visualInfo, screenNumber, rootWin);
100 XFree(visualInfo);
101 return window;
102}
103
104static inline QByteArray getGlString(GLenum param)
105{
106 if (const GLubyte *s = glGetString(param))
107 return QByteArray(reinterpret_cast<const char*>(s));
108 return QByteArray();
109}
110
111static bool hasGlExtension(const QSurfaceFormat &format, const char *ext)
112{
113 if (format.majorVersion() < 3) {
114 auto exts = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS));
115 return exts && strstr(exts, ext);
116 } else {
117 auto glGetStringi = reinterpret_cast<glGetStringiProc>(
118 glXGetProcAddress(reinterpret_cast<const GLubyte*>("glGetStringi")));
119 if (glGetStringi) {
120 GLint n = 0;
121 glGetIntegerv(GL_NUM_EXTENSIONS, &n);
122 for (GLint i = 0; i < n; ++i) {
123 const char *p = reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i));
124 if (p && !strcmp(p, ext))
125 return true;
126 }
127 }
128 return false;
129 }
130}
131
132static void updateFormatFromContext(QSurfaceFormat &format)
133{
134 // Update the version, profile, and context bit of the format
135 int major = 0, minor = 0;
136 QByteArray versionString(getGlString(GL_VERSION));
137 if (QPlatformOpenGLContext::parseOpenGLVersion(versionString, major, minor)) {
138 format.setMajorVersion(major);
139 format.setMinorVersion(minor);
140 }
141
142 format.setProfile(QSurfaceFormat::NoProfile);
143 const bool isStereo = format.testOption(QSurfaceFormat::StereoBuffers);
144 format.setOptions(QSurfaceFormat::FormatOptions());
145 // Restore flags that come from the VisualInfo/FBConfig.
146 if (isStereo)
147 format.setOption(QSurfaceFormat::StereoBuffers);
148
149 if (format.renderableType() == QSurfaceFormat::OpenGL) {
150 if (hasGlExtension(format, "GL_ARB_robustness")) {
151 GLint value = 0;
152 glGetIntegerv(GL_RESET_NOTIFICATION_STRATEGY_ARB, &value);
153 if (value == GL_LOSE_CONTEXT_ON_RESET_ARB)
154 format.setOption(QSurfaceFormat::ResetNotification);
155 }
156
157 if (format.version() < std::pair(3, 0)) {
158 format.setOption(QSurfaceFormat::DeprecatedFunctions);
159 return;
160 }
161
162 // Version 3.0 onwards - check if it includes deprecated functionality or is
163 // a debug context
164 GLint value = 0;
165 glGetIntegerv(GL_CONTEXT_FLAGS, &value);
167 format.setOption(QSurfaceFormat::DeprecatedFunctions);
168 if (value & GL_CONTEXT_FLAG_DEBUG_BIT)
169 format.setOption(QSurfaceFormat::DebugContext);
170 if (format.version() < std::pair(3, 2))
171 return;
172
173 // Version 3.2 and newer have a profile
174 value = 0;
175 glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &value);
176
177 if (value & GL_CONTEXT_CORE_PROFILE_BIT)
178 format.setProfile(QSurfaceFormat::CoreProfile);
180 format.setProfile(QSurfaceFormat::CompatibilityProfile);
181 }
182}
183
184QGLXContext::QGLXContext(Display *display, QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share)
185 : QPlatformOpenGLContext()
186 , m_display(display)
187 , m_format(format)
188 , m_ownsContext(true)
189{
190 if (m_format.renderableType() == QSurfaceFormat::DefaultRenderableType)
191 m_format.setRenderableType(QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL
192 ? QSurfaceFormat::OpenGL : QSurfaceFormat::OpenGLES);
193 if (m_format.renderableType() != QSurfaceFormat::OpenGL && m_format.renderableType() != QSurfaceFormat::OpenGLES)
194 return;
195
196 if (share)
197 m_shareContext = static_cast<const QGLXContext*>(share)->glxContext();
198
199 GLXFBConfig config = qglx_findConfig(m_display, screen->screenNumber(), m_format);
200 m_config = config;
201 XVisualInfo *visualInfo = nullptr;
202 Window window = 0; // Temporary window used to query OpenGL context
203
204 if (config) {
205 const QByteArrayList glxExt = QByteArray(glXQueryExtensionsString(m_display, screen->screenNumber())).split(' ');
206
207 // Resolve entry point for glXCreateContextAttribsARB
208 glXCreateContextAttribsARBProc glXCreateContextAttribsARB = nullptr;
209 if (glxExt.contains("GLX_ARB_create_context"))
210 glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
211
212 const bool supportsProfiles = glxExt.contains("GLX_ARB_create_context_profile");
213 const bool supportsRobustness = glxExt.contains("GLX_ARB_create_context_robustness");
214 const bool supportsVideoMemoryPurge = glxExt.contains("GLX_NV_robustness_video_memory_purge");
215
216 // Use glXCreateContextAttribsARB if available
217 // Also, GL ES context creation requires GLX_EXT_create_context_es2_profile
218 if (glXCreateContextAttribsARB != nullptr
219 && (m_format.renderableType() != QSurfaceFormat::OpenGLES || (supportsProfiles && glxExt.contains("GLX_EXT_create_context_es2_profile")))) {
220 // Try to create an OpenGL context for each known OpenGL version in descending
221 // order from the requested version.
222 const int requestedVersion = m_format.majorVersion() * 10 + qMin(m_format.minorVersion(), 9);
223
224 QList<int> glVersions;
225 if (m_format.renderableType() == QSurfaceFormat::OpenGL) {
226 if (requestedVersion > 46)
227 glVersions << requestedVersion;
228
229 // Don't bother with versions below 2.0
230 glVersions << 46 << 45 << 44 << 43 << 42 << 41 << 40 << 33 << 32 << 31 << 30 << 21 << 20;
231 } else if (m_format.renderableType() == QSurfaceFormat::OpenGLES) {
232 if (requestedVersion > 32)
233 glVersions << requestedVersion;
234
235 // Don't bother with versions below ES 2.0
236 glVersions << 32 << 31 << 30 << 20;
237 // ES does not support any format option
238 m_format.setOptions(QSurfaceFormat::FormatOptions());
239 }
240 // Robustness must match that of the shared context.
241 if (share && share->format().testOption(QSurfaceFormat::ResetNotification))
242 m_format.setOption(QSurfaceFormat::ResetNotification);
243 Q_ASSERT(glVersions.size() > 0);
244
245 for (int i = 0; !m_context && i < glVersions.size(); i++) {
246 const int version = glVersions[i];
247 if (version > requestedVersion)
248 continue;
249
250 const int majorVersion = version / 10;
251 const int minorVersion = version % 10;
252
253 QList<int> contextAttributes;
254 contextAttributes << GLX_CONTEXT_MAJOR_VERSION_ARB << majorVersion
255 << GLX_CONTEXT_MINOR_VERSION_ARB << minorVersion;
256
257
258 if (m_format.renderableType() == QSurfaceFormat::OpenGL) {
259 // If asking for OpenGL 3.2 or newer we should also specify a profile
260 if (version >= 32 && supportsProfiles) {
261 if (m_format.profile() == QSurfaceFormat::CoreProfile)
263 else
265 }
266
267 int flags = 0;
268
269 if (supportsRobustness)
271
272 if (m_format.testOption(QSurfaceFormat::DebugContext))
273 flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
274
275 // A forward-compatible context may be requested for 3.0 and later
276 if (version >= 30 && !m_format.testOption(QSurfaceFormat::DeprecatedFunctions))
277 flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
278
279 if (flags != 0)
280 contextAttributes << GLX_CONTEXT_FLAGS_ARB << flags;
281 } else if (m_format.renderableType() == QSurfaceFormat::OpenGLES) {
283 }
284
285 if (supportsRobustness && m_format.testOption(QSurfaceFormat::ResetNotification)) {
286 QList<int> contextAttributesWithRobustness = contextAttributes;
288 if (supportsVideoMemoryPurge)
289 contextAttributesWithRobustness << GLX_GENERATE_RESET_ON_VIDEO_MEMORY_PURGE_NV << GL_TRUE;
290
291 contextAttributesWithRobustness << None;
292 m_context = glXCreateContextAttribsARB(m_display, config, m_shareContext, true,
293 contextAttributesWithRobustness.data());
294 // Context creation against a shared context may fail specifically due to this request, so try
295 // without before dropping sharing.
296 }
297
298 if (m_context) {
299 m_getGraphicsResetStatus = reinterpret_cast<GLenum (QOPENGLF_APIENTRYP)()>(getProcAddress("glGetGraphicsResetStatusARB"));
300 } else {
301 contextAttributes << None;
302 m_context = glXCreateContextAttribsARB(m_display, config, m_shareContext, true, contextAttributes.data());
303 if (!m_context && m_shareContext) {
304 // re-try without a shared glx context
305 m_context = glXCreateContextAttribsARB(m_display, config, nullptr, true, contextAttributes.data());
306 if (m_context)
307 m_shareContext = nullptr;
308 }
309 }
310 }
311 }
312
313 // Could not create a context using glXCreateContextAttribsARB, falling back to glXCreateNewContext.
314 if (!m_context) {
315 // requesting an OpenGL ES context requires glXCreateContextAttribsARB, so bail out
316 if (m_format.renderableType() == QSurfaceFormat::OpenGLES)
317 return;
318
319 m_context = glXCreateNewContext(m_display, config, GLX_RGBA_TYPE, m_shareContext, true);
320 if (!m_context && m_shareContext) {
321 // re-try without a shared glx context
322 m_context = glXCreateNewContext(m_display, config, GLX_RGBA_TYPE, nullptr, true);
323 if (m_context)
324 m_shareContext = nullptr;
325 }
326 }
327
328 // Get the basic surface format details
329 if (m_context)
330 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, config);
331
332 // Create a temporary window so that we can make the new context current
333 window = createDummyWindow(m_display, config, screen->screenNumber(), screen->root());
334 } else {
335 // requesting an OpenGL ES context requires glXCreateContextAttribsARB, so bail out
336 if (m_format.renderableType() == QSurfaceFormat::OpenGLES)
337 return;
338
339 // Note that m_format gets updated with the used surface format
340 visualInfo = qglx_findVisualInfo(m_display, screen->screenNumber(), &m_format);
341 if (Q_UNLIKELY(!visualInfo))
342 qFatal("Could not initialize GLX");
343 m_context = glXCreateContext(m_display, visualInfo, m_shareContext, true);
344 if (!m_context && m_shareContext) {
345 // re-try without a shared glx context
346 m_shareContext = nullptr;
347 m_context = glXCreateContext(m_display, visualInfo, nullptr, true);
348 }
349
350 // Create a temporary window so that we can make the new context current
351 window = createDummyWindow(m_display, visualInfo, screen->screenNumber(), screen->root());
352 XFree(visualInfo);
353 }
354
355 // Query the OpenGL version and profile
356 if (m_context && window) {
357 GLXContext prevContext = glXGetCurrentContext();
358 GLXDrawable prevDrawable = glXGetCurrentDrawable();
359 glXMakeCurrent(m_display, window, m_context);
360 updateFormatFromContext(m_format);
361
362 // Make our context non-current
363 glXMakeCurrent(m_display, prevDrawable, prevContext);
364 }
365
366 // Destroy our temporary window
367 XDestroyWindow(m_display, window);
368}
369
370QGLXContext::QGLXContext(Display *display, GLXContext context, void *visualInfo, QPlatformOpenGLContext *share)
371 : QPlatformOpenGLContext()
372 , m_display(display)
373{
374 // Legacy contexts created using glXCreateContext are created using a
375 // XVisualInfo. If the user passed one we should use that.
376 XVisualInfo *vinfo = static_cast<XVisualInfo*>(visualInfo);
377
378 // Otherwise assume the context was created with an FBConfig using the modern functions
379 if (!vinfo) {
380 int configId = 0;
381 if (glXQueryContext(m_display, context, GLX_FBCONFIG_ID, &configId) != Success) {
382 qWarning("QGLXContext: Failed to query config from the provided context");
383 return;
384 }
385
386 int screenNumber = 0;
387 if (glXQueryContext(m_display, context, GLX_SCREEN, &screenNumber) != Success) {
388 qWarning("QGLXContext: Failed to query screen from the provided context");
389 screenNumber = DefaultScreen(m_display);
390 }
391
392 GLXFBConfig *configs;
393 int numConfigs = 0;
394 static const int attribs[] = { GLX_FBCONFIG_ID, configId, None };
395 configs = glXChooseFBConfig(m_display, screenNumber, attribs, &numConfigs);
396 if (!configs) {
397 qWarning("QGLXContext: Failed to find config(invalid arguments for glXChooseFBConfig)");
398 return;
399 } else if (numConfigs < 1) {
400 qWarning("QGLXContext: Failed to find config");
401 XFree(configs);
402 return;
403 }
404 if (configs && numConfigs > 1) // this is suspicious so warn but let it continue
405 qWarning("QGLXContext: Multiple configs for FBConfig ID %d", configId);
406
407 m_config = configs[0];
408 XFree(configs);
409 }
410
411 Q_ASSERT(vinfo || m_config);
412
413 int screenNumber = DefaultScreen(m_display);
414 Window window;
415 if (vinfo)
416 window = createDummyWindow(m_display, vinfo, screenNumber, RootWindow(m_display, screenNumber));
417 else
418 window = createDummyWindow(m_display, m_config, screenNumber, RootWindow(m_display, screenNumber));
419 if (!window) {
420 qWarning("QGLXContext: Failed to create dummy window");
421 return;
422 }
423
424 // Update OpenGL version and buffer sizes in our format.
425 GLXContext prevContext = glXGetCurrentContext();
426 GLXDrawable prevDrawable = glXGetCurrentDrawable();
427 if (!glXMakeCurrent(m_display, window, context)) {
428 qWarning("QGLXContext: Failed to make provided context current");
429 return;
430 }
431 m_format = QSurfaceFormat();
432 m_format.setRenderableType(QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL
433 ? QSurfaceFormat::OpenGL : QSurfaceFormat::OpenGLES);
434 updateFormatFromContext(m_format);
435 if (vinfo)
436 qglx_surfaceFormatFromVisualInfo(&m_format, m_display, vinfo);
437 else
438 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, m_config);
439 glXMakeCurrent(m_display, prevDrawable, prevContext);
440 XDestroyWindow(m_display, window);
441
442 if (vinfo)
443 XFree(vinfo);
444
445 // Success. Store the context. From this point on isValid() is true.
446 m_context = context;
447
448 if (share)
449 m_shareContext = static_cast<const QGLXContext*>(share)->glxContext();
450}
451
452QGLXContext::~QGLXContext()
453{
454 if (m_ownsContext)
455 glXDestroyContext(m_display, m_context);
456}
457
458static QXcbScreen *screenForPlatformSurface(QPlatformSurface *surface)
459{
460 QSurface::SurfaceClass surfaceClass = surface->surface()->surfaceClass();
461 if (surfaceClass == QSurface::Window) {
462 return static_cast<QXcbScreen *>(static_cast<QXcbWindow *>(surface)->screen());
463 } else if (surfaceClass == QSurface::Offscreen) {
464 return static_cast<QXcbScreen *>(static_cast<QGLXPbuffer *>(surface)->screen());
465 }
466 return nullptr;
467}
468
469bool QGLXContext::makeCurrent(QPlatformSurface *surface)
470{
471 bool success = false;
472 Q_ASSERT(surface->surface()->supportsOpenGL());
473
474 GLXDrawable glxDrawable = 0;
475 QSurface::SurfaceClass surfaceClass = surface->surface()->surfaceClass();
476 if (surfaceClass == QSurface::Window) {
477 m_isPBufferCurrent = false;
478 QXcbWindow *window = static_cast<QXcbWindow *>(surface);
479 glxDrawable = window->xcb_window();
480 success = glXMakeCurrent(m_display, glxDrawable, m_context);
481 m_lost = false;
482 if (m_getGraphicsResetStatus && m_getGraphicsResetStatus() != GL_NO_ERROR) {
483 m_lost = true;
484 success = false;
485 // Drop the surface. Will recreate on the next makeCurrent.
486 window->invalidateSurface();
487 }
488 } else if (surfaceClass == QSurface::Offscreen) {
489 m_isPBufferCurrent = true;
490 QGLXPbuffer *pbuffer = static_cast<QGLXPbuffer *>(surface);
491 glxDrawable = pbuffer->pbuffer();
492 success = glXMakeContextCurrent(m_display, glxDrawable, glxDrawable, m_context);
493 m_lost = false;
494 if (m_getGraphicsResetStatus && m_getGraphicsResetStatus() != GL_NO_ERROR) {
495 m_lost = true;
496 success = false;
497 }
498 }
499
500 if (success && surfaceClass == QSurface::Window) {
501 int interval = surface->format().swapInterval();
502 QXcbWindow *window = static_cast<QXcbWindow *>(surface);
503 QXcbScreen *screen = screenForPlatformSurface(surface);
504 // Always re-apply the swap interval to have it set correctly when the
505 // same drawable is used with multiple contexts. See QTBUG-147854.
506 if (interval >= 0 && screen) {
507 typedef void (*qt_glXSwapIntervalEXT)(Display *, GLXDrawable, int);
508 typedef void (*qt_glXSwapIntervalMESA)(unsigned int);
509 static qt_glXSwapIntervalEXT glXSwapIntervalEXT = nullptr;
510 static qt_glXSwapIntervalMESA glXSwapIntervalMESA = nullptr;
511 static bool resolved = false;
512 if (!resolved) {
513 resolved = true;
514 QList<QByteArray> glxExt = QByteArray(glXQueryExtensionsString(m_display,
515 screen->screenNumber())).split(' ');
516 if (glxExt.contains("GLX_EXT_swap_control"))
517 glXSwapIntervalEXT = (qt_glXSwapIntervalEXT) getProcAddress("glXSwapIntervalEXT");
518 if (glxExt.contains("GLX_MESA_swap_control"))
519 glXSwapIntervalMESA = (qt_glXSwapIntervalMESA) getProcAddress("glXSwapIntervalMESA");
520 }
521 if (glXSwapIntervalEXT)
522 glXSwapIntervalEXT(m_display, glxDrawable, interval);
523 else if (glXSwapIntervalMESA)
524 glXSwapIntervalMESA(interval);
525 window->setSwapInterval(interval);
526 }
527 }
528
529 return success;
530}
531
532void QGLXContext::doneCurrent()
533{
534 if (m_isPBufferCurrent)
535 glXMakeContextCurrent(m_display, 0, 0, nullptr);
536 else
537 glXMakeCurrent(m_display, 0, nullptr);
538 m_isPBufferCurrent = false;
539}
540
541void QGLXContext::swapBuffers(QPlatformSurface *surface)
542{
543 GLXDrawable glxDrawable = 0;
544 if (surface->surface()->surfaceClass() == QSurface::Offscreen)
545 glxDrawable = static_cast<QGLXPbuffer *>(surface)->pbuffer();
546 else
547 glxDrawable = static_cast<QXcbWindow *>(surface)->xcb_window();
548 glXSwapBuffers(m_display, glxDrawable);
549
550 if (surface->surface()->surfaceClass() == QSurface::Window) {
551 QXcbWindow *platformWindow = static_cast<QXcbWindow *>(surface);
552 // OpenGL context might be bound to a non-gui thread use QueuedConnection to sync
553 // the window from the platformWindow's thread as QXcbWindow is no QObject, an
554 // event is sent to QXcbConnection. (this is faster than a metacall)
555 if (platformWindow->needsSync())
556 platformWindow->postSyncWindowRequest();
557 }
558}
559
560QFunctionPointer QGLXContext::getProcAddress(const char *procName)
561{
562 return glXGetProcAddress(reinterpret_cast<const GLubyte *>(procName));
563}
564
565QSurfaceFormat QGLXContext::format() const
566{
567 return m_format;
568}
569
570bool QGLXContext::isSharing() const
571{
572 return m_shareContext != nullptr;
573}
574
575bool QGLXContext::isValid() const
576{
577 return m_context != nullptr && !m_lost;
578}
579
580bool QGLXContext::m_queriedDummyContext = false;
581bool QGLXContext::m_supportsThreading = true;
582
583
584// If this list grows to any significant size, change it a
585// proper string table and make the implementation below use
586// binary search.
588 "Chromium", // QTBUG-32225 (initialization fails)
589 nullptr
590};
591
592static const char *qglx_threadedgl_blacklist_vendor[] = {
593 "llvmpipe", // QTCREATORBUG-10666
594 "nouveau", // https://bugs.freedesktop.org/show_bug.cgi?id=91632
595 nullptr
596};
597
598void QGLXContext::queryDummyContext()
599{
600 if (m_queriedDummyContext)
601 return;
602 m_queriedDummyContext = true;
603
604 static bool skip = qEnvironmentVariableIsSet("QT_OPENGL_NO_SANITY_CHECK");
605 if (skip)
606 return;
607
608 QOpenGLContext *oldContext = QOpenGLContext::currentContext();
609 QSurface *oldSurface = nullptr;
610 if (oldContext)
611 oldSurface = oldContext->surface();
612
613 QScopedPointer<QSurface> surface;
614 Display *display = glXGetCurrentDisplay();
615 if (!display) {
616 // FIXME: Since Qt 5.6 we don't need to check whether primary screen is NULL
617 if (QScreen *screen = QGuiApplication::primaryScreen()) {
618 QXcbScreen *xcbScreen = static_cast<QXcbScreen *>(screen->handle());
619 display = static_cast<Display *>(xcbScreen->connection()->xlib_display());
620 }
621 }
622 const char *glxvendor = glXGetClientString(display, GLX_VENDOR);
623 if (glxvendor && !strcmp(glxvendor, "ATI")) {
624 QWindow *window = new QWindow;
625 window->resize(64, 64);
626 window->setSurfaceType(QSurface::OpenGLSurface);
627 window->create();
628 surface.reset(window);
629 } else {
630 QOffscreenSurface *offSurface = new QOffscreenSurface;
631 offSurface->create();
632 surface.reset(offSurface);
633 }
634
635 QOpenGLContext context;
636 if (!context.create() || !context.makeCurrent(surface.data())) {
637 qWarning("QGLXContext: Failed to create dummy context");
638 m_supportsThreading = false;
639 return;
640 }
641
642 m_supportsThreading = true;
643
644 if (const char *renderer = (const char *) glGetString(GL_RENDERER)) {
645 for (int i = 0; qglx_threadedgl_blacklist_renderer[i]; ++i) {
646 if (strstr(renderer, qglx_threadedgl_blacklist_renderer[i]) != nullptr) {
647 qCDebug(lcQpaGl).nospace() << "Multithreaded OpenGL disabled: "
648 "blacklisted renderer \""
650 << "\"";
651 m_supportsThreading = false;
652 break;
653 }
654 }
655 }
656 if (const char *vendor = (const char *) glGetString(GL_VENDOR)) {
657 for (int i = 0; qglx_threadedgl_blacklist_vendor[i]; ++i) {
658 if (strstr(vendor, qglx_threadedgl_blacklist_vendor[i]) != nullptr) {
659 qCDebug(lcQpaGl).nospace() << "Multithreaded OpenGL disabled: "
660 "blacklisted vendor \""
662 << "\"";
663 m_supportsThreading = false;
664 break;
665 }
666 }
667 }
668
669 static bool nomultithread = qEnvironmentVariableIsSet("QT_XCB_NO_THREADED_OPENGL");
670 if (nomultithread)
671 m_supportsThreading = false;
672
673 context.doneCurrent();
674 if (oldContext && oldSurface)
675 oldContext->makeCurrent(oldSurface);
676
677 if (!m_supportsThreading) {
678 qCDebug(lcQpaGl) << "Force-enable multithreaded OpenGL by setting "
679 "environment variable QT_OPENGL_NO_SANITY_CHECK";
680 }
681}
682
683bool QGLXContext::supportsThreading()
684{
685 queryDummyContext();
686 return m_supportsThreading;
687}
688
689QGLXPbuffer::QGLXPbuffer(QOffscreenSurface *offscreenSurface)
690 : QPlatformOffscreenSurface(offscreenSurface)
691 , m_screen(static_cast<QXcbScreen *>(offscreenSurface->screen()->handle()))
692 , m_format(m_screen->surfaceFormatFor(offscreenSurface->requestedFormat()))
693 , m_display(static_cast<Display *>(m_screen->connection()->xlib_display()))
694 , m_pbuffer(0)
695{
696 GLXFBConfig config = qglx_findConfig(m_display, m_screen->screenNumber(), m_format);
697
698 if (config) {
699 const int attributes[] = {
700 GLX_PBUFFER_WIDTH, offscreenSurface->size().width(),
701 GLX_PBUFFER_HEIGHT, offscreenSurface->size().height(),
702 GLX_LARGEST_PBUFFER, False,
703 GLX_PRESERVED_CONTENTS, False,
704 None
705 };
706
707 m_pbuffer = glXCreatePbuffer(m_display, config, attributes);
708
709 if (m_pbuffer)
710 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, config);
711 }
712}
713
715{
716 if (m_pbuffer)
717 glXDestroyPbuffer(m_display, m_pbuffer);
718}
719
720
721QT_END_NAMESPACE
GLXPbuffer pbuffer() const
Combined button and popup list for selecting options.
#define GL_CONTEXT_PROFILE_MASK
#define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
#define GL_CONTEXT_CORE_PROFILE_BIT
#define GL_CONTEXT_FLAGS
#define GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
#define GL_CONTEXT_FLAG_DEBUG_BIT
static Window createDummyWindow(Display *dpy, GLXFBConfig config, int screenNumber, Window rootWin)
#define GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
static const char * qglx_threadedgl_blacklist_renderer[]
static Window createDummyWindow(Display *dpy, XVisualInfo *visualInfo, int screenNumber, Window rootWin)
static const char * qglx_threadedgl_blacklist_vendor[]
#define GLX_GENERATE_RESET_ON_VIDEO_MEMORY_PURGE_NV
static void updateFormatFromContext(QSurfaceFormat &format)
static QByteArray getGlString(GLenum param)
static bool hasGlExtension(const QSurfaceFormat &format, const char *ext)
#define GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB
static QXcbScreen * screenForPlatformSurface(QPlatformSurface *surface)
#define GLX_CONTEXT_PROFILE_MASK_ARB
#define GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB
#define GLX_CONTEXT_ES2_PROFILE_BIT_EXT
#define GLX_LOSE_CONTEXT_ON_RESET_ARB
#define GL_RESET_NOTIFICATION_STRATEGY_ARB
#define GL_LOSE_CONTEXT_ON_RESET_ARB
#define GL_NUM_EXTENSIONS
Definition qopenglext.h:906