diff --git a/src/gui/kernel/qopenglcontext.cpp b/src/gui/kernel/qopenglcontext.cpp index c93752c..b084ae1 100644 --- a/src/gui/kernel/qopenglcontext.cpp +++ b/src/gui/kernel/qopenglcontext.cpp @@ -490,6 +490,27 @@ bool QOpenGLContext::create() } /*! + Initializes the QOpenGLContext using a platform-specific native context. + + On Mac, provide a CGLContextObj. The CGLContextObj must not already be attached to an NSOpenGLContext. +*/ +bool QOpenGLContext::createWithNativeContext(void *nativeContext) +{ + destroy(); + + Q_D(QOpenGLContext); + d->platformGLContext = QGuiApplicationPrivate::platformIntegration()->createPlatformOpenGLContextFromNativeContext(nativeContext); + if (!d->platformGLContext) + return false; + d->platformGLContext->setContext(this); + if (!d->platformGLContext->isSharing()) + d->shareContext = 0; + d->shareGroup = d->shareContext ? d->shareContext->shareGroup() : new QOpenGLContextGroup; + d->shareGroup->d_func()->addContext(this); + return d->platformGLContext; +} + +/*! Destroy the underlying platform context associated with this context. If any other context is directly or indirectly sharing resources with this diff --git a/src/gui/kernel/qopenglcontext.h b/src/gui/kernel/qopenglcontext.h index bfdb892..468079c 100644 --- a/src/gui/kernel/qopenglcontext.h +++ b/src/gui/kernel/qopenglcontext.h @@ -152,6 +152,7 @@ public: void setScreen(QScreen *screen); bool create(); + bool createWithNativeContext(void *context); bool isValid() const; QSurfaceFormat format() const; diff --git a/src/gui/kernel/qplatformintegration.cpp b/src/gui/kernel/qplatformintegration.cpp index e82e30d..9d372c3 100644 --- a/src/gui/kernel/qplatformintegration.cpp +++ b/src/gui/kernel/qplatformintegration.cpp @@ -247,6 +247,13 @@ QPlatformOpenGLContext *QPlatformIntegration::createPlatformOpenGLContext(QOpenG qWarning("This plugin does not support createPlatformOpenGLContext!"); return 0; } + +QPlatformOpenGLContext *QPlatformIntegration::createPlatformOpenGLContextFromNativeContext(void *nativeContext) const +{ + Q_UNUSED(nativeContext); + qWarning("This plugin does not support createPlatformOpenGLContextFromNativeContext!"); + return 0; +} #endif /*! diff --git a/src/gui/kernel/qplatformintegration.h b/src/gui/kernel/qplatformintegration.h index b7a44b1..d466924 100644 --- a/src/gui/kernel/qplatformintegration.h +++ b/src/gui/kernel/qplatformintegration.h @@ -103,6 +103,7 @@ public: virtual QPlatformBackingStore *createPlatformBackingStore(QWindow *window) const = 0; #ifndef QT_NO_OPENGL virtual QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const; + virtual QPlatformOpenGLContext *createPlatformOpenGLContextFromNativeContext(void *nativeContext) const; #endif virtual QPlatformSharedGraphicsCache *createPlatformSharedGraphicsCache(const char *cacheId) const; virtual QPaintEngine *createImagePaintEngine(QPaintDevice *paintDevice) const; diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.h b/src/plugins/platforms/cocoa/qcocoaglcontext.h index 29affb0..5e9364f 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.h +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.h @@ -56,6 +56,7 @@ class QCocoaGLContext : public QPlatformOpenGLContext { public: QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share); + QCocoaGLContext(CGLContextObj context); ~QCocoaGLContext(); QSurfaceFormat format() const; diff --git a/src/plugins/platforms/cocoa/qcocoaglcontext.mm b/src/plugins/platforms/cocoa/qcocoaglcontext.mm index 3dee137..121cf10 100644 --- a/src/plugins/platforms/cocoa/qcocoaglcontext.mm +++ b/src/plugins/platforms/cocoa/qcocoaglcontext.mm @@ -84,6 +84,14 @@ QCocoaGLContext::QCocoaGLContext(const QSurfaceFormat &format, QPlatformOpenGLCo } } +QCocoaGLContext::QCocoaGLContext(CGLContextObj cglContext) + : m_context(nil), + m_shareContext(nil), + m_format() +{ + m_context = [[NSOpenGLContext alloc] initWithCGLContextObj:cglContext]; +} + QCocoaGLContext::~QCocoaGLContext() { [m_context release]; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.h b/src/plugins/platforms/cocoa/qcocoaintegration.h index 6e690dd..c3bb823 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.h +++ b/src/plugins/platforms/cocoa/qcocoaintegration.h @@ -106,6 +106,7 @@ public: bool hasCapability(QPlatformIntegration::Capability cap) const; QPlatformWindow *createPlatformWindow(QWindow *window) const; QPlatformOpenGLContext *createPlatformOpenGLContext(QOpenGLContext *context) const; + QPlatformOpenGLContext *createPlatformOpenGLContextFromNativeContext(void *nativeContext) const; QPlatformBackingStore *createPlatformBackingStore(QWindow *widget) const; QAbstractEventDispatcher *guiThreadEventDispatcher() const; diff --git a/src/plugins/platforms/cocoa/qcocoaintegration.mm b/src/plugins/platforms/cocoa/qcocoaintegration.mm index 45038ee..714db09 100644 --- a/src/plugins/platforms/cocoa/qcocoaintegration.mm +++ b/src/plugins/platforms/cocoa/qcocoaintegration.mm @@ -371,6 +371,11 @@ QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContext(QOpenGLCo return new QCocoaGLContext(context->format(), context->shareHandle()); } +QPlatformOpenGLContext *QCocoaIntegration::createPlatformOpenGLContextFromNativeContext(void *nativeContext) const +{ + return new QCocoaGLContext((CGLContextObj)nativeContext); +} + QPlatformBackingStore *QCocoaIntegration::createPlatformBackingStore(QWindow *window) const { return new QCocoaBackingStore(window);