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/cocoa | |
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/cocoa')
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaglcontext.h | 8 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaglcontext.mm | 41 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaintegration.h | 5 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaintegration.mm | 16 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoanativeinterface.h | 8 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoanativeinterface.mm | 33 |
6 files changed, 38 insertions, 73 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.h b/src/plugins/platforms/cocoa/qcocoaglcontext.h index f1851a2336..9dbd1a11bf 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.h +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.h @@ -43,7 +43,8 @@ #include <QtCore/QPointer> #include <QtCore/private/qcore_mac_p.h> #include <qpa/qplatformopenglcontext.h> -#include <QtGui/QOpenGLContext> +#include <QtGui/qopenglcontext.h> +#include <QtGui/private/qopenglcontext_p.h> #include <QtGui/QWindow> Q_FORWARD_DECLARE_OBJC_CLASS(NSOpenGLContext); @@ -53,10 +54,11 @@ QT_BEGIN_NAMESPACE class QCocoaWindow; -class QCocoaGLContext : public QPlatformOpenGLContext +class QCocoaGLContext : public QPlatformOpenGLContext, public QPlatformInterface::QCocoaGLContext { public: QCocoaGLContext(QOpenGLContext *context); + QCocoaGLContext(NSOpenGLContext *context); ~QCocoaGLContext(); void initialize() override; @@ -71,7 +73,7 @@ public: bool isSharing() const override; bool isValid() const override; - NSOpenGLContext *nativeContext() const; + NSOpenGLContext *nativeContext() const override; QFunctionPointer getProcAddress(const char *procName) override; diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index c049ca60f2..c2fcb6cdbc 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -45,7 +45,6 @@ #include "qcocoascreen.h" #include <qdebug.h> -#include <QtPlatformHeaders/qcocoanativecontext.h> #include <dlfcn.h> static inline QByteArray getGlString(GLenum param) @@ -65,30 +64,23 @@ QCocoaGLContext::QCocoaGLContext(QOpenGLContext *context) { } -void QCocoaGLContext::initialize() +QCocoaGLContext::QCocoaGLContext(NSOpenGLContext *nativeContext) + : QPlatformOpenGLContext() { - QVariant nativeHandle = context()->nativeHandle(); - if (!nativeHandle.isNull()) { - if (!nativeHandle.canConvert<QCocoaNativeContext>()) { - qCWarning(lcQpaOpenGLContext, "QOpenGLContext native handle must be a QCocoaNativeContext"); - return; - } - m_context = nativeHandle.value<QCocoaNativeContext>().context(); - if (!m_context) { - qCWarning(lcQpaOpenGLContext, "QCocoaNativeContext's NSOpenGLContext cannot be null"); - return; - } - - [m_context retain]; - - // Note: We have no way of knowing whether the NSOpenGLContext was created with the - // share context as reported by the QOpenGLContext, but we just have to trust that - // it was. It's okey, as the only thing we're using it for is to report isShared(). - if (QPlatformOpenGLContext *shareContext = context()->shareHandle()) - m_shareContext = static_cast<QCocoaGLContext *>(shareContext)->nativeContext(); + m_context = [nativeContext retain]; +} - updateSurfaceFormat(); - return; +void QCocoaGLContext::initialize() +{ + if (m_context) { + // Note: We have no way of knowing whether the NSOpenGLContext was created with the + // share context as reported by the QOpenGLContext, but we just have to trust that + // it was. It's okey, as the only thing we're using it for is to report isShared(). + if (QPlatformOpenGLContext *shareContext = context()->shareHandle()) + m_shareContext = static_cast<QCocoaGLContext *>(shareContext)->nativeContext(); + + updateSurfaceFormat(); + return; } // ----------- Default case, we own the NSOpenGLContext ----------- @@ -139,9 +131,6 @@ void QCocoaGLContext::initialize() return; } - // The native handle should reflect the underlying context, even if we created it - context()->setNativeHandle(QVariant::fromValue<QCocoaNativeContext>(m_context)); - // --------------------- Set NSOpenGLContext properties --------------------- const GLint interval = m_format.swapInterval() >= 0 ? m_format.swapInterval() : 1; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index d2fffc8e73..f82e0050ae 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -56,12 +56,14 @@ #include <QtCore/QScopedPointer> #include <qpa/qplatformintegration.h> #include <QtGui/private/qcoretextfontdatabase_p.h> +#include <QtGui/private/qopenglcontext_p.h> Q_FORWARD_DECLARE_OBJC_CLASS(NSToolbar); QT_BEGIN_NAMESPACE -class QCocoaIntegration : public QObject, public QPlatformIntegration +class QCocoaIntegration : public QObject, public QPlatformIntegration, + public QPlatformInterface::Private::QCocoaGLIntegration { Q_OBJECT public: @@ -82,6 +84,7 @@ public: QPlatformOffscreenSurface *createPlatformOffscreenSurface(QOffscreenSurface *surface) const override; #ifndef QT_NO_OPENGL QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const override; + QOpenGLContext *createOpenGLContext(NSOpenGLContext *, QOpenGLContext *shareContext) const override; #endif QPlatformBackingStore *createPlatformBackingStore(QWindow *widget) const override; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index 49e8f8eada..b3d3bb34bf 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -64,9 +64,8 @@ #include <qpa/qplatformoffscreensurface.h> #include <QtCore/qcoreapplication.h> -#include <QtPlatformHeaders/qcocoanativecontext.h> - #include <QtGui/private/qcoregraphics_p.h> +#include <QtGui/private/qopenglcontext_p.h> #include <QtGui/private/qfontengine_coretext_p.h> @@ -308,6 +307,19 @@ QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLCo { return new QCocoaGLContext(context); } + +QOpenGLContext *QCocoaIntegration::createOpenGLContext(NSOpenGLContext *nativeContext, QOpenGLContext *shareContext) const +{ + if (!nativeContext) + return nullptr; + + auto *context = new QOpenGLContext; + context->setShareContext(shareContext); + auto *contextPrivate = QOpenGLContextPrivate::get(context); + contextPrivate->adopt(new QCocoaGLContext(nativeContext)); + return context; +} + #endif QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.h b/src/plugins/platforms/cocoa/qcocoanativeinterface.h index 45330cf1b8..7d4c5884b3 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.h +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.h @@ -59,18 +59,10 @@ class QCocoaNativeInterface : public QPlatformNativeInterface public: QCocoaNativeInterface(); -#ifndef QT_NO_OPENGL - void *nativeResourceForContext(const QByteArray &resourceString, QOpenGLContext *context) override; -#endif void *nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) override; NativeResourceForIntegrationFunction nativeResourceFunctionForIntegration(const QByteArray &resource) override; -#ifndef QT_NO_OPENGL - static void *cglContextForContext(QOpenGLContext *context); - static void *nsOpenGLContextForContext(QOpenGLContext* context); -#endif - QFunctionPointer platformFunction(const QByteArray &function) const override; public Q_SLOTS: diff --git a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm index ba4ad719d2..22bab0886d 100644 --- a/src/plugins/platforms/cocoa/qcocoanativeinterface.mm +++ b/src/plugins/platforms/cocoa/qcocoanativeinterface.mm @@ -76,20 +76,6 @@ QCocoaNativeInterface::QCocoaNativeInterface() { } -#ifndef QT_NO_OPENGL -void *QCocoaNativeInterface::nativeResourceForContext(const QByteArray &resourceString, QOpenGLContext *context) -{ - if (!context) - return nullptr; - if (resourceString.toLower() == "nsopenglcontext") - return nsOpenGLContextForContext(context); - if (resourceString.toLower() == "cglcontextobj") - return cglContextForContext(context); - - return nullptr; -} -#endif - void *QCocoaNativeInterface::nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) { if (!window->handle()) @@ -182,25 +168,6 @@ void QCocoaNativeInterface::onAppFocusWindowChanged(QWindow *window) QCocoaMenuBar::updateMenuBarImmediately(); } -#ifndef QT_NO_OPENGL -void *QCocoaNativeInterface::cglContextForContext(QOpenGLContext* context) -{ - NSOpenGLContext *nsOpenGLContext = static_cast<NSOpenGLContext*>(nsOpenGLContextForContext(context)); - if (nsOpenGLContext) - return [nsOpenGLContext CGLContextObj]; - return nullptr; -} - -void *QCocoaNativeInterface::nsOpenGLContextForContext(QOpenGLContext* context) -{ - if (context) { - if (QCocoaGLContext *cocoaGLContext = static_cast<QCocoaGLContext *>(context->handle())) - return cocoaGLContext->nativeContext(); - } - return nullptr; -} -#endif - QFunctionPointer QCocoaNativeInterface::platformFunction(const QByteArray &function) const { if (function == QCocoaWindowFunctions::bottomLeftClippedByNSWindowOffsetIdentifier()) |