diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2020-06-08 20:00:36 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2020-07-02 10:27:50 +0200 |
commit | 6ff79478a44fce12ca18832a56db4a370a9ff417 (patch) | |
tree | 2bcd7ab7bac46fe28e0828e0a704649bbc025e8f /src/plugins/platforms/xcb/gl_integrations/xcb_glx | |
parent | 037dce81fc27ec241edb6829f6df41927b80666e (diff) |
Introduce platform API abstraction for QOpenGLContext
The API is available by including qopenglcontext.h as usual,
but scoped in the QPlatformInterface namespace. The namespace
exposes platform specific type-safe interfaces that provide:
a) Factory functions for adopting native contexts, e.g.
QCocoaGLContext::fromNative(nsContext, shareContext);
b) Access to underlying native handles, e.g.
openGLContext->platformInterface<QCocoaGLContext>->nativeContext()
c) Platform specific functionality, e.g.
static QWGLContext::openGLModuleHandle()
openGLContext->platformInterface<QEGLContext>->doSomething();
The platform interfaces live close to the classes they extend,
removing the need for complex indirection and plumbing, and
avoids kitchen-sink modules and APIs such as the extras modules,
QPlatformFunctions, or QPlatformNativeInterface.
In the case of QOpenGLContext these platform APIs are backed
by the platform plugin, so dynamic_cast is used to ensure the
platform plugin supports the requested interface, but this is
and implementation detail. The interface APIs are agnostic
to where the implementation lives, while still being available
to the user as part of the APIs they extend/augment.
The documentation will be restored when the dust settles.
Task-number: QTBUG-80233
Change-Id: Iac612403383991c4b24064332542a6e4bcbb3293
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Diffstat (limited to 'src/plugins/platforms/xcb/gl_integrations/xcb_glx')
4 files changed, 48 insertions, 92 deletions
diff --git a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.cpp b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.cpp index ca9cb4c86a..feae5fa13c 100644 --- a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.cpp +++ b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.cpp @@ -56,7 +56,6 @@ #include "qglxintegration.h" #include <QtGlxSupport/private/qglxconvenience_p.h> -#include <QtPlatformHeaders/QGLXNativeContext> #include "qxcbglintegration.h" @@ -219,26 +218,11 @@ static void updateFormatFromContext(QSurfaceFormat &format) } } -QGLXContext::QGLXContext(QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share, - const QVariant &nativeHandle) +QGLXContext::QGLXContext(Display *display, QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share) : QPlatformOpenGLContext() - , m_display(static_cast<Display *>(screen->connection()->xlib_display())) - , m_config(nullptr) - , m_context(nullptr) - , m_shareContext(nullptr) + , m_display(display) , m_format(format) - , m_isPBufferCurrent(false) - , m_ownsContext(nativeHandle.isNull()) - , m_getGraphicsResetStatus(nullptr) - , m_lost(false) -{ - if (nativeHandle.isNull()) - init(screen, share); - else - init(screen, share, nativeHandle); -} - -void QGLXContext::init(QXcbScreen *screen, QPlatformOpenGLContext *share) + , m_ownsContext(true) { if (m_format.renderableType() == QSurfaceFormat::DefaultRenderableType) #if QT_CONFIG(opengles2) @@ -423,49 +407,15 @@ void QGLXContext::init(QXcbScreen *screen, QPlatformOpenGLContext *share) XDestroyWindow(m_display, window); } -void QGLXContext::init(QXcbScreen *screen, QPlatformOpenGLContext *share, const QVariant &nativeHandle) +QGLXContext::QGLXContext(Display *display, GLXContext context, void *visualInfo, QPlatformOpenGLContext *share) + : QPlatformOpenGLContext() + , m_display(display) { - if (!nativeHandle.canConvert<QGLXNativeContext>()) { - qWarning("QGLXContext: Requires a QGLXNativeContext"); - return; - } - QGLXNativeContext handle = qvariant_cast<QGLXNativeContext>(nativeHandle); - GLXContext context = handle.context(); - if (!context) { - qWarning("QGLXContext: No GLXContext given"); - return; - } + // Legacy contexts created using glXCreateContext are created using a + // XVisualInfo. If the user passed one we should use that. + XVisualInfo *vinfo = static_cast<XVisualInfo*>(visualInfo); - // Legacy contexts created using glXCreateContext are created using a visual - // and the FBConfig cannot be queried. The only way to adapt these contexts - // is to figure out the visual id. - XVisualInfo *vinfo = nullptr; - // If the VisualID is provided use it. - VisualID vid = handle.visualId(); - if (!vid) { - // In the absence of the VisualID figure it out from the window. - Window wnd = handle.window(); - if (wnd) { - XWindowAttributes attrs; - XGetWindowAttributes(m_display, wnd, &attrs); - vid = XVisualIDFromVisual(attrs.visual); - } - } - if (vid) { - XVisualInfo v; - v.screen = screen->screenNumber(); - v.visualid = vid; - int n = 0; - vinfo = XGetVisualInfo(m_display, VisualScreenMask | VisualIDMask, &v, &n); - if (n < 1) { - XFree(vinfo); - vinfo = nullptr; - } - } - - // For contexts created with an FBConfig using the modern functions providing the - // visual or window is not mandatory. Just query the config from the context. - GLXFBConfig config = nullptr; + // Otherwise assume the context was created with an FBConfig using the modern functions if (!vinfo) { int configId = 0; if (glXQueryContext(m_display, context, GLX_FBCONFIG_ID, &configId) != Success) { @@ -490,19 +440,17 @@ void QGLXContext::init(QXcbScreen *screen, QPlatformOpenGLContext *share, const if (configs && numConfigs > 1) // this is suspicious so warn but let it continue qWarning("QGLXContext: Multiple configs for FBConfig ID %d", configId); - config = configs[0]; - // Store the config. - m_config = config; + m_config = configs[0]; } - Q_ASSERT(vinfo || config); + Q_ASSERT(vinfo || m_config); int screenNumber = DefaultScreen(m_display); Window window; if (vinfo) window = createDummyWindow(m_display, vinfo, screenNumber, RootWindow(m_display, screenNumber)); else - window = createDummyWindow(m_display, config, screenNumber, RootWindow(m_display, screenNumber)); + window = createDummyWindow(m_display, m_config, screenNumber, RootWindow(m_display, screenNumber)); if (!window) { qWarning("QGLXContext: Failed to create dummy window"); return; @@ -522,7 +470,7 @@ void QGLXContext::init(QXcbScreen *screen, QPlatformOpenGLContext *share, const if (vinfo) qglx_surfaceFormatFromVisualInfo(&m_format, m_display, vinfo); else - qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, config); + qglx_surfaceFormatFromGLXFBConfig(&m_format, m_display, m_config); glXMakeCurrent(m_display, prevDrawable, prevContext); XDestroyWindow(m_display, window); @@ -553,11 +501,6 @@ static QXcbScreen *screenForPlatformSurface(QPlatformSurface *surface) return nullptr; } -QVariant QGLXContext::nativeHandle() const -{ - return QVariant::fromValue<QGLXNativeContext>(QGLXNativeContext(m_context)); -} - bool QGLXContext::makeCurrent(QPlatformSurface *surface) { bool success = false; diff --git a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.h b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.h index 2a88fd6e59..a7468c8fac 100644 --- a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.h +++ b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qglxintegration.h @@ -53,11 +53,12 @@ QT_BEGIN_NAMESPACE -class QGLXContext : public QPlatformOpenGLContext +class QGLXContext : public QPlatformOpenGLContext, + public QPlatformInterface::QGLXContext { public: - QGLXContext(QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share, - const QVariant &nativeHandle); + QGLXContext(Display *display, QXcbScreen *screen, const QSurfaceFormat &format, QPlatformOpenGLContext *share); + QGLXContext(Display *display, GLXContext context, void *visualInfo, QPlatformOpenGLContext *share); ~QGLXContext(); bool makeCurrent(QPlatformSurface *surface) override; @@ -69,27 +70,24 @@ public: bool isSharing() const override; bool isValid() const override; + GLXContext nativeContext() const override { return glxContext(); } + GLXContext glxContext() const { return m_context; } GLXFBConfig glxConfig() const { return m_config; } - QVariant nativeHandle() const; - static bool supportsThreading(); static void queryDummyContext(); private: - void init(QXcbScreen *screen, QPlatformOpenGLContext *share); - void init(QXcbScreen *screen, QPlatformOpenGLContext *share, const QVariant &nativeHandle); - - Display *m_display; - GLXFBConfig m_config; - GLXContext m_context; - GLXContext m_shareContext; + Display *m_display = nullptr; + GLXFBConfig m_config = nullptr; + GLXContext m_context = nullptr; + GLXContext m_shareContext = nullptr; QSurfaceFormat m_format; - bool m_isPBufferCurrent; - bool m_ownsContext; - GLenum (APIENTRY * m_getGraphicsResetStatus)(); - bool m_lost; + bool m_isPBufferCurrent = false; + bool m_ownsContext = false; + GLenum (APIENTRY * m_getGraphicsResetStatus)() = nullptr; + bool m_lost = false; static bool m_queriedDummyContext; static bool m_supportsThreading; }; diff --git a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp index 6814dbd844..733df7df51 100644 --- a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp +++ b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.cpp @@ -49,6 +49,7 @@ #include "qglxintegration.h" #include <QtGui/QOpenGLContext> +#include <QtGui/private/qopenglcontext_p.h> #include "qxcbglxnativeinterfacehandler.h" @@ -187,10 +188,22 @@ QXcbWindow *QXcbGlxIntegration::createWindow(QWindow *window) const QPlatformOpenGLContext *QXcbGlxIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const { QXcbScreen *screen = static_cast<QXcbScreen *>(context->screen()->handle()); - QGLXContext *platformContext = new QGLXContext(screen, screen->surfaceFormatFor(context->format()), - context->shareHandle(), context->nativeHandle()); - context->setNativeHandle(platformContext->nativeHandle()); - return platformContext; + return new QGLXContext(static_cast<Display *>(m_connection->xlib_display()), + screen, screen->surfaceFormatFor(context->format()), context->shareHandle()); +} + +QOpenGLContext *QXcbGlxIntegration::createOpenGLContext(GLXContext glxContext, void *visualInfo, QOpenGLContext *shareContext) const +{ + if (!glxContext) + return nullptr; + + QPlatformOpenGLContext *shareHandle = shareContext ? shareContext->handle() : nullptr; + + auto *context = new QOpenGLContext; + auto *contextPrivate = QOpenGLContextPrivate::get(context); + auto *display = static_cast<Display *>(m_connection->xlib_display()); + contextPrivate->adopt(new QGLXContext(display, glxContext, visualInfo, shareHandle)); + return context; } QPlatformOffscreenSurface *QXcbGlxIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const diff --git a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.h b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.h index 26cb233a59..18c1be9489 100644 --- a/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.h +++ b/src/plugins/platforms/xcb/gl_integrations/xcb_glx/qxcbglxintegration.h @@ -46,7 +46,8 @@ QT_BEGIN_NAMESPACE class QXcbNativeInterfaceHandler; -class QXcbGlxIntegration : public QXcbGlIntegration +class QXcbGlxIntegration : public QXcbGlIntegration, + public QPlatformInterface::Private::QGLXIntegration { public: QXcbGlxIntegration(); @@ -58,6 +59,7 @@ public: QXcbWindow *createWindow(QWindow *window) const override; QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const override; QPlatformOffscreenSurface *createPlatformOffscreenSurface(QOffscreenSurface *surface) const override; + QOpenGLContext *createOpenGLContext(GLXContext context, void *visualInfo, QOpenGLContext *shareContext) const override; bool supportsThreadedOpenGL() const override; bool supportsSwitchableWidgetComposition() const override; |