184QGLXContext::QGLXContext(Display *display, QXcbScreen *screen,
const QSurfaceFormat &format, QPlatformOpenGLContext *share)
185 : QPlatformOpenGLContext()
188 , m_ownsContext(
true)
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)
197 m_shareContext =
static_cast<
const QGLXContext*>(share)->glxContext();
199 GLXFBConfig config = qglx_findConfig(m_display, screen->screenNumber(), m_format);
201 XVisualInfo *visualInfo =
nullptr;
205 const QByteArrayList glxExt = QByteArray(glXQueryExtensionsString(m_display, screen->screenNumber())).split(
' ');
208 glXCreateContextAttribsARBProc glXCreateContextAttribsARB =
nullptr;
209 if (glxExt.contains(
"GLX_ARB_create_context"))
210 glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddress((
const GLubyte*)
"glXCreateContextAttribsARB");
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");
218 if (glXCreateContextAttribsARB !=
nullptr
219 && (m_format.renderableType() != QSurfaceFormat::OpenGLES || (supportsProfiles && glxExt.contains(
"GLX_EXT_create_context_es2_profile")))) {
222 const int requestedVersion = m_format.majorVersion() * 10 + qMin(m_format.minorVersion(), 9);
224 QList<
int> glVersions;
225 if (m_format.renderableType() == QSurfaceFormat::OpenGL) {
226 if (requestedVersion > 46)
227 glVersions << requestedVersion;
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;
236 glVersions << 32 << 31 << 30 << 20;
238 m_format.setOptions(QSurfaceFormat::FormatOptions());
241 if (share && share->format().testOption(QSurfaceFormat::ResetNotification))
242 m_format.setOption(QSurfaceFormat::ResetNotification);
243 Q_ASSERT(glVersions.size() > 0);
245 for (
int i = 0; !m_context && i < glVersions.size(); i++) {
246 const int version = glVersions[i];
247 if (version > requestedVersion)
250 const int majorVersion = version / 10;
251 const int minorVersion = version % 10;
253 QList<
int> contextAttributes;
254 contextAttributes << GLX_CONTEXT_MAJOR_VERSION_ARB << majorVersion
255 << GLX_CONTEXT_MINOR_VERSION_ARB << minorVersion;
258 if (m_format.renderableType() == QSurfaceFormat::OpenGL) {
260 if (version >= 32 && supportsProfiles) {
261 if (m_format.profile() == QSurfaceFormat::CoreProfile)
269 if (supportsRobustness)
272 if (m_format.testOption(QSurfaceFormat::DebugContext))
273 flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
276 if (version >= 30 && !m_format.testOption(QSurfaceFormat::DeprecatedFunctions))
277 flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
280 contextAttributes << GLX_CONTEXT_FLAGS_ARB << flags;
281 }
else if (m_format.renderableType() == QSurfaceFormat::OpenGLES) {
285 if (supportsRobustness && m_format.testOption(QSurfaceFormat::ResetNotification)) {
286 QList<
int> contextAttributesWithRobustness = contextAttributes;
288 if (supportsVideoMemoryPurge)
291 contextAttributesWithRobustness << None;
292 m_context = glXCreateContextAttribsARB(m_display, config, m_shareContext,
true,
293 contextAttributesWithRobustness.data());
299 m_getGraphicsResetStatus =
reinterpret_cast<GLenum (QOPENGLF_APIENTRYP)()>(getProcAddress(
"glGetGraphicsResetStatusARB"));
301 contextAttributes << None;
302 m_context = glXCreateContextAttribsARB(m_display, config, m_shareContext,
true, contextAttributes.data());
303 if (!m_context && m_shareContext) {
305 m_context = glXCreateContextAttribsARB(m_display, config,
nullptr,
true, contextAttributes.data());
307 m_shareContext =
nullptr;
316 if (m_format.renderableType() == QSurfaceFormat::OpenGLES)
319 m_context = glXCreateNewContext(m_display, config, GLX_RGBA_TYPE, m_shareContext,
true);
320 if (!m_context && m_shareContext) {
322 m_context = glXCreateNewContext(m_display, config, GLX_RGBA_TYPE,
nullptr,
true);
324 m_shareContext =
nullptr;
330 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, config);
333 window = createDummyWindow(m_display, config, screen->screenNumber(), screen->root());
336 if (m_format.renderableType() == QSurfaceFormat::OpenGLES)
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) {
346 m_shareContext =
nullptr;
347 m_context = glXCreateContext(m_display, visualInfo,
nullptr,
true);
351 window = createDummyWindow(m_display, visualInfo, screen->screenNumber(), screen->root());
356 if (m_context && window) {
357 GLXContext prevContext = glXGetCurrentContext();
358 GLXDrawable prevDrawable = glXGetCurrentDrawable();
359 glXMakeCurrent(m_display, window, m_context);
360 updateFormatFromContext(m_format);
363 glXMakeCurrent(m_display, prevDrawable, prevContext);
367 XDestroyWindow(m_display, window);
370QGLXContext::QGLXContext(Display *display, GLXContext context,
void *visualInfo, QPlatformOpenGLContext *share)
371 : QPlatformOpenGLContext()
376 XVisualInfo *vinfo =
static_cast<XVisualInfo*>(visualInfo);
381 if (glXQueryContext(m_display, context, GLX_FBCONFIG_ID, &configId) != Success) {
382 qWarning(
"QGLXContext: Failed to query config from the provided context");
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);
392 GLXFBConfig *configs;
394 static const int attribs[] = { GLX_FBCONFIG_ID, configId, None };
395 configs = glXChooseFBConfig(m_display, screenNumber, attribs, &numConfigs);
397 qWarning(
"QGLXContext: Failed to find config(invalid arguments for glXChooseFBConfig)");
399 }
else if (numConfigs < 1) {
400 qWarning(
"QGLXContext: Failed to find config");
404 if (configs && numConfigs > 1)
405 qWarning(
"QGLXContext: Multiple configs for FBConfig ID %d", configId);
407 m_config = configs[0];
411 Q_ASSERT(vinfo || m_config);
413 int screenNumber = DefaultScreen(m_display);
420 qWarning(
"QGLXContext: Failed to create dummy window");
425 GLXContext prevContext = glXGetCurrentContext();
426 GLXDrawable prevDrawable = glXGetCurrentDrawable();
427 if (!glXMakeCurrent(m_display, window, context)) {
428 qWarning(
"QGLXContext: Failed to make provided context current");
431 m_format = QSurfaceFormat();
432 m_format.setRenderableType(QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL
433 ? QSurfaceFormat::OpenGL : QSurfaceFormat::OpenGLES);
434 updateFormatFromContext(m_format);
436 qglx_surfaceFormatFromVisualInfo(&m_format, m_display, vinfo);
438 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, m_config);
439 glXMakeCurrent(m_display, prevDrawable, prevContext);
440 XDestroyWindow(m_display, window);
449 m_shareContext =
static_cast<
const QGLXContext*>(share)->glxContext();
469bool QGLXContext::makeCurrent(QPlatformSurface *surface)
471 bool success =
false;
472 Q_ASSERT(surface->surface()->supportsOpenGL());
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);
482 if (m_getGraphicsResetStatus && m_getGraphicsResetStatus() != GL_NO_ERROR) {
486 window->invalidateSurface();
488 }
else if (surfaceClass == QSurface::Offscreen) {
489 m_isPBufferCurrent =
true;
492 success = glXMakeContextCurrent(m_display, glxDrawable, glxDrawable, m_context);
494 if (m_getGraphicsResetStatus && m_getGraphicsResetStatus() != GL_NO_ERROR) {
500 if (success && surfaceClass == QSurface::Window) {
501 int interval = surface->format().swapInterval();
502 QXcbWindow *window =
static_cast<QXcbWindow *>(surface);
503 QXcbScreen *screen = screenForPlatformSurface(surface);
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;
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");
521 if (glXSwapIntervalEXT)
522 glXSwapIntervalEXT(m_display, glxDrawable, interval);
523 else if (glXSwapIntervalMESA)
524 glXSwapIntervalMESA(interval);
525 window->setSwapInterval(interval);
598void QGLXContext::queryDummyContext()
600 if (m_queriedDummyContext)
602 m_queriedDummyContext =
true;
604 static bool skip = qEnvironmentVariableIsSet(
"QT_OPENGL_NO_SANITY_CHECK");
608 QOpenGLContext *oldContext = QOpenGLContext::currentContext();
609 QSurface *oldSurface =
nullptr;
611 oldSurface = oldContext->surface();
613 QScopedPointer<QSurface> surface;
614 Display *display = glXGetCurrentDisplay();
617 if (QScreen *screen = QGuiApplication::primaryScreen()) {
618 QXcbScreen *xcbScreen =
static_cast<QXcbScreen *>(screen->handle());
619 display =
static_cast<Display *>(xcbScreen->connection()->xlib_display());
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);
628 surface.reset(window);
630 QOffscreenSurface *offSurface =
new QOffscreenSurface;
631 offSurface->create();
632 surface.reset(offSurface);
635 QOpenGLContext context;
636 if (!context.create() || !context.makeCurrent(surface.data())) {
637 qWarning(
"QGLXContext: Failed to create dummy context");
638 m_supportsThreading =
false;
642 m_supportsThreading =
true;
644 if (
const char *renderer = (
const char *) glGetString(GL_RENDERER)) {
647 qCDebug(lcQpaGl).nospace() <<
"Multithreaded OpenGL disabled: "
648 "blacklisted renderer \""
651 m_supportsThreading =
false;
656 if (
const char *vendor = (
const char *) glGetString(GL_VENDOR)) {
659 qCDebug(lcQpaGl).nospace() <<
"Multithreaded OpenGL disabled: "
660 "blacklisted vendor \""
663 m_supportsThreading =
false;
669 static bool nomultithread = qEnvironmentVariableIsSet(
"QT_XCB_NO_THREADED_OPENGL");
671 m_supportsThreading =
false;
673 context.doneCurrent();
674 if (oldContext && oldSurface)
675 oldContext->makeCurrent(oldSurface);
677 if (!m_supportsThreading) {
678 qCDebug(lcQpaGl) <<
"Force-enable multithreaded OpenGL by setting "
679 "environment variable QT_OPENGL_NO_SANITY_CHECK";
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()))
696 GLXFBConfig config = qglx_findConfig(m_display, m_screen->screenNumber(), m_format);
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,
707 m_pbuffer = glXCreatePbuffer(m_display, config, attributes);
710 qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, config);