diff --git a/libraries/gl/src/gl/Context.cpp b/libraries/gl/src/gl/Context.cpp index 7d27b42909..a0d52ee223 100644 --- a/libraries/gl/src/gl/Context.cpp +++ b/libraries/gl/src/gl/Context.cpp @@ -195,6 +195,21 @@ GLAPI PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB; Q_GUI_EXPORT QOpenGLContext *qt_gl_global_share_context(); +#if defined(GL_CUSTOM_CONTEXT) +bool Context::makeCurrent() { + BOOL result = wglMakeCurrent(_hdc, _hglrc); + assert(result); + updateSwapchainMemoryCounter(); + return result; +} + void Context::swapBuffers() { + SwapBuffers(_hdc); +} + void Context::doneCurrent() { + wglMakeCurrent(0, 0); +} +#endif + void Context::create(QOpenGLContext* shareContext) { if (!shareContext) { shareContext = qt_gl_global_share_context(); @@ -297,7 +312,11 @@ void Context::create(QOpenGLContext* shareContext) { contextAttribs.push_back(0); } contextAttribs.push_back(0); - HGLRC shareHglrc = (HGLRC)QOpenGLContextWrapper::nativeContext(shareContext); + HGLRC shareHglrc = nullptr; + if (shareContext) { + auto nativeContextPointer = QOpenGLContextWrapper(shareContext).getNativeContext(); + shareHglrc = (HGLRC)nativeContextPointer->context(); + } _hglrc = wglCreateContextAttribsARB(_hdc, shareHglrc, &contextAttribs[0]); } diff --git a/libraries/gl/src/gl/ContextQt.cpp b/libraries/gl/src/gl/ContextQt.cpp index 82724dfd62..24ae29e4ca 100644 --- a/libraries/gl/src/gl/ContextQt.cpp +++ b/libraries/gl/src/gl/ContextQt.cpp @@ -61,12 +61,12 @@ void Context::debugMessageHandler(const QOpenGLDebugMessage& debugMessage) { switch (severity) { case QOpenGLDebugMessage::NotificationSeverity: case QOpenGLDebugMessage::LowSeverity: + qCDebug(glLogging) << debugMessage; return; default: + qCWarning(glLogging) << debugMessage; break; } - qWarning(glLogging) << debugMessage; - return; } void Context::setupDebugLogging(QOpenGLContext *context) { @@ -82,6 +82,8 @@ void Context::setupDebugLogging(QOpenGLContext *context) { } } + +#if !defined(GL_CUSTOM_CONTEXT) bool Context::makeCurrent() { updateSwapchainMemoryCounter(); bool result = _qglContext->makeCurrent(_window); @@ -98,6 +100,7 @@ void Context::doneCurrent() { _qglContext->doneCurrent(); } } +#endif Q_GUI_EXPORT QOpenGLContext *qt_gl_global_share_context(); const QSurfaceFormat& getDefaultOpenGLSurfaceFormat(); diff --git a/libraries/gl/src/gl/OffscreenGLCanvas.cpp b/libraries/gl/src/gl/OffscreenGLCanvas.cpp index f05acb50e9..69b41da821 100644 --- a/libraries/gl/src/gl/OffscreenGLCanvas.cpp +++ b/libraries/gl/src/gl/OffscreenGLCanvas.cpp @@ -65,21 +65,9 @@ bool OffscreenGLCanvas::create(QOpenGLContext* sharedContext) { _offscreenSurface->setFormat(_context->format()); _offscreenSurface->create(); - - // Due to a https://bugreports.qt.io/browse/QTBUG-65125 we can't rely on `isValid` - // to determine if the offscreen surface was successfully created, so we use - // makeCurrent as a proxy test. Bug is fixed in Qt 5.9.4 -#if defined(Q_OS_ANDROID) - if (!_context->makeCurrent(_offscreenSurface)) { - qFatal("Unable to make offscreen surface current"); - } - _context->doneCurrent(); -#else if (!_offscreenSurface->isValid()) { qFatal("Offscreen surface is invalid"); } -#endif - return true; } diff --git a/libraries/gl/src/gl/QOpenGLContextWrapper.cpp b/libraries/gl/src/gl/QOpenGLContextWrapper.cpp index fbebb1128d..842c7abd08 100644 --- a/libraries/gl/src/gl/QOpenGLContextWrapper.cpp +++ b/libraries/gl/src/gl/QOpenGLContextWrapper.cpp @@ -17,6 +17,22 @@ #include #endif +QOpenGLContextWrapper::Pointer QOpenGLContextWrapper::currentContextWrapper() { + return std::make_shared(QOpenGLContext::currentContext()); +} + + +QOpenGLContextWrapper::NativeContextPointer QOpenGLContextWrapper::getNativeContext() const { + QOpenGLContextWrapper::NativeContextPointer result; + auto nativeHandle = _context->nativeHandle(); + if (nativeHandle.canConvert()) { + result = std::make_shared(); + *result = nativeHandle.value(); + } + return result; +} + + uint32_t QOpenGLContextWrapper::currentContextVersion() { QOpenGLContext* context = QOpenGLContext::currentContext(); if (!context) { @@ -49,19 +65,6 @@ void QOpenGLContextWrapper::setFormat(const QSurfaceFormat& format) { _context->setFormat(format); } -#ifdef Q_OS_WIN -void* QOpenGLContextWrapper::nativeContext(QOpenGLContext* context) { - HGLRC result = 0; - if (context != nullptr) { - auto nativeHandle = context->nativeHandle(); - if (nativeHandle.canConvert()) { - result = nativeHandle.value().context(); - } - } - return result; -} -#endif - bool QOpenGLContextWrapper::create() { return _context->create(); } diff --git a/libraries/gl/src/gl/QOpenGLContextWrapper.h b/libraries/gl/src/gl/QOpenGLContextWrapper.h index 32ba7f22e8..1fade3e7fa 100644 --- a/libraries/gl/src/gl/QOpenGLContextWrapper.h +++ b/libraries/gl/src/gl/QOpenGLContextWrapper.h @@ -12,19 +12,31 @@ #ifndef hifi_QOpenGLContextWrapper_h #define hifi_QOpenGLContextWrapper_h -#include #include +#include class QOpenGLContext; class QSurface; class QSurfaceFormat; class QThread; +#if defined(Q_OS_ANDROID) +#include +#include +using QGLNativeContext = QEGLNativeContext; +#elif defined(Q_OS_WIN) +class QWGLNativeContext; +using QGLNativeContext = QWGLNativeContext; +#else +using QGLNativeContext = void*; +#endif + class QOpenGLContextWrapper { public: -#ifdef Q_OS_WIN - static void* nativeContext(QOpenGLContext* context); -#endif + using Pointer = std::shared_ptr; + using NativeContextPointer = std::shared_ptr; + static Pointer currentContextWrapper(); + QOpenGLContextWrapper(); QOpenGLContextWrapper(QOpenGLContext* context); @@ -37,6 +49,8 @@ public: void setShareContext(QOpenGLContext* otherContext); void moveToThread(QThread* thread); + NativeContextPointer getNativeContext() const; + static QOpenGLContext* currentContext(); static uint32_t currentContextVersion();