diff options
Diffstat (limited to 'src/gui/kernel/qopenglcontext.cpp')
-rw-r--r-- | src/gui/kernel/qopenglcontext.cpp | 164 |
1 files changed, 103 insertions, 61 deletions
diff --git a/src/gui/kernel/qopenglcontext.cpp b/src/gui/kernel/qopenglcontext.cpp index 51bf5cf336..dd41318f72 100644 --- a/src/gui/kernel/qopenglcontext.cpp +++ b/src/gui/kernel/qopenglcontext.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <qpa/qplatformopenglcontext.h> #include <qpa/qplatformintegration.h> @@ -78,7 +42,7 @@ static QOpenGLContext *global_share_context = nullptr; #ifndef QT_NO_DEBUG QHash<QOpenGLContext *, bool> QOpenGLContextPrivate::makeCurrentTracker; -QMutex QOpenGLContextPrivate::makeCurrentTrackerMutex; +Q_CONSTINIT QMutex QOpenGLContextPrivate::makeCurrentTrackerMutex; #endif /*! @@ -105,6 +69,7 @@ QOpenGLContext *qt_gl_global_share_context() /*! \class QOpenGLContext + \ingroup painting-3D \inmodule QtGui \since 5.0 \brief The QOpenGLContext class represents a native OpenGL context, enabling @@ -173,6 +138,22 @@ QOpenGLContext *qt_gl_global_share_context() application is portable between different platforms. However, if you use QOpenGLFunctions::glBindFramebuffer(), this is done automatically for you. + \warning WebAssembly + + We recommend that only one QOpenGLContext is made current with a QSurface, + for the entire lifetime of the QSurface. Should more than once context be used, + it is important to understand that multiple QOpenGLContext instances may be + backed by the same native context underneath with the WebAssembly platform. + Therefore, calling makeCurrent() with the same QSurface on two QOpenGLContext + objects may not switch to a different native context in the second call. As + a result, any OpenGL state changes done after the second makeCurrent() may + alter the state of the first QOpenGLContext as well, as they are all backed + by the same native context. + + \note This means that when targeting WebAssembly with existing OpenGL-based + Qt code, some porting may be required to cater to these limitations. + + \sa QOpenGLFunctions, QOpenGLBuffer, QOpenGLShaderProgram, QOpenGLFramebufferObject */ @@ -189,6 +170,9 @@ QOpenGLContext *QOpenGLContextPrivate::setCurrentContext(QOpenGLContext *context qWarning("No QTLS available. currentContext won't work"); return nullptr; } + if (!context) + return nullptr; + threadContext = new QGuiGLThreadContext; qwindow_context_storage()->setLocalData(threadContext); } @@ -347,12 +331,14 @@ void QOpenGLContextPrivate::_q_screenDestroyed(QObject *object) } /*! - \fn T QOpenGLContext::platformInterface<T>() + \fn template <typename QNativeInterface> QNativeInterface *QOpenGLContext::nativeInterface() const - Returns a platform interface of type T for the context. + Returns a native interface of the given type for the context. This function provides access to platform specific functionality - of QOpenGLContext, as defined in the QPlatformInterface namespace. + of QOpenGLContext, as defined in the QNativeInterface namespace: + + \annotatedlist native-interfaces-qopenglcontext If the requested interface is not available a \nullptr is returned. */ @@ -394,6 +380,10 @@ bool QOpenGLContext::create() return isValid(); } +QOpenGLContextPrivate::~QOpenGLContextPrivate() +{ +} + void QOpenGLContextPrivate::adopt(QPlatformOpenGLContext *context) { Q_Q(QOpenGLContext); @@ -429,30 +419,47 @@ void QOpenGLContextPrivate::adopt(QPlatformOpenGLContext *context) void QOpenGLContext::destroy() { Q_D(QOpenGLContext); + + // Notify that the native context and the QPlatformOpenGLContext are going + // to go away. if (d->platformGLContext) emit aboutToBeDestroyed(); - if (QOpenGLContext::currentContext() == this) - doneCurrent(); - if (d->shareGroup) - d->shareGroup->d_func()->removeContext(this); - d->shareGroup = nullptr; - delete d->platformGLContext; - d->platformGLContext = nullptr; - delete d->functions; - d->functions = nullptr; + // Invoke callbacks for helpers and invalidate. if (d->textureFunctionsDestroyCallback) { d->textureFunctionsDestroyCallback(); d->textureFunctionsDestroyCallback = nullptr; } d->textureFunctions = nullptr; + delete d->versionFunctions; + d->versionFunctions = nullptr; + if (d->vaoHelperDestroyCallback) { Q_ASSERT(d->vaoHelper); d->vaoHelperDestroyCallback(d->vaoHelper); d->vaoHelperDestroyCallback = nullptr; } d->vaoHelper = nullptr; + + // Tear down function wrappers. + delete d->versionFunctions; + d->versionFunctions = nullptr; + + delete d->functions; + d->functions = nullptr; + + // Clean up and destroy the native context machinery. + if (QOpenGLContext::currentContext() == this) + doneCurrent(); + + if (d->shareGroup) + d->shareGroup->d_func()->removeContext(this); + + d->shareGroup = nullptr; + + delete d->platformGLContext; + d->platformGLContext = nullptr; } /*! @@ -464,6 +471,11 @@ void QOpenGLContext::destroy() If you wish to make the context current in order to do clean-up, make sure to only connect to the signal using a direct connection. + + \note In Qt for Python, this signal will not be received when emitted + from the destructor of QOpenGLWidget or QOpenGLWindow due to the Python + instance already being destroyed. We recommend doing cleanups + in QWidget::hideEvent() instead. */ /*! @@ -707,7 +719,8 @@ bool QOpenGLContext::makeCurrent(QSurface *surface) || qstrcmp(rendererString, "GC800 core") == 0 || qstrcmp(rendererString, "GC1000 core") == 0 || strstr(rendererString, "GC2000") != nullptr - || qstrcmp(rendererString, "Immersion.16") == 0; + || qstrcmp(rendererString, "Immersion.16") == 0 + || qstrncmp(rendererString, "Apple Mx", 7) == 0; } needsWorkaroundSet = true; } @@ -777,12 +790,6 @@ void QOpenGLContext::swapBuffers(QSurface *surface) return; } - if (surface->surfaceClass() == QSurface::Window - && !qt_window_private(static_cast<QWindow *>(surface))->receivedExpose) - { - qWarning("QOpenGLContext::swapBuffers() called with non-exposed window, behavior is undefined"); - } - QPlatformSurface *surfaceHandle = surface->surfaceHandle(); if (!surfaceHandle) return; @@ -1034,6 +1041,9 @@ QOpenGLContextGroup *QOpenGLContextGroup::currentContextGroup() return current ? current->shareGroup() : nullptr; } +QOpenGLContextGroupPrivate::~QOpenGLContextGroupPrivate() + = default; + void QOpenGLContextGroupPrivate::addContext(QOpenGLContext *ctx) { const auto locker = qt_scoped_lock(m_mutex); @@ -1175,6 +1185,10 @@ void QOpenGLSharedResource::free() \inmodule QtGui */ + +QOpenGLSharedResourceGuard::~QOpenGLSharedResourceGuard() + = default; + void QOpenGLSharedResourceGuard::freeResource(QOpenGLContext *context) { if (m_id) { @@ -1247,14 +1261,14 @@ void QOpenGLMultiGroupSharedResource::insert(QOpenGLContext *context, QOpenGLSha QOpenGLSharedResource *QOpenGLMultiGroupSharedResource::value(QOpenGLContext *context) { QOpenGLContextGroup *group = context->shareGroup(); - return group->d_func()->m_resources.value(this, 0); + return group->d_func()->m_resources.value(this, nullptr); } QList<QOpenGLSharedResource *> QOpenGLMultiGroupSharedResource::resources() const { QList<QOpenGLSharedResource *> result; for (QList<QOpenGLContextGroup *>::const_iterator it = m_groups.constBegin(); it != m_groups.constEnd(); ++it) { - QOpenGLSharedResource *resource = (*it)->d_func()->m_resources.value(const_cast<QOpenGLMultiGroupSharedResource *>(this), 0); + QOpenGLSharedResource *resource = (*it)->d_func()->m_resources.value(const_cast<QOpenGLMultiGroupSharedResource *>(this), nullptr); if (resource) result << resource; } @@ -1274,6 +1288,9 @@ void QOpenGLMultiGroupSharedResource::cleanup(QOpenGLContextGroup *group, QOpenG m_groups.removeOne(group); } +QOpenGLContextVersionFunctionHelper::~QOpenGLContextVersionFunctionHelper() + = default; + #ifndef QT_NO_DEBUG_STREAM QDebug operator<<(QDebug debug, const QOpenGLContext *ctx) { @@ -1313,6 +1330,31 @@ QDebug operator<<(QDebug debug, const QOpenGLContextGroup *cg) } #endif // QT_NO_DEBUG_STREAM -#include "moc_qopenglcontext.cpp" +using namespace QNativeInterface; + +void *QOpenGLContext::resolveInterface(const char *name, int revision) const +{ + Q_UNUSED(name); Q_UNUSED(revision); + + auto *platformContext = handle(); + Q_UNUSED(platformContext); + +#if defined(Q_OS_MACOS) + QT_NATIVE_INTERFACE_RETURN_IF(QCocoaGLContext, platformContext); +#endif +#if defined(Q_OS_WIN) + QT_NATIVE_INTERFACE_RETURN_IF(QWGLContext, platformContext); +#endif +#if QT_CONFIG(xcb_glx_plugin) + QT_NATIVE_INTERFACE_RETURN_IF(QGLXContext, platformContext); +#endif +#if QT_CONFIG(egl) + QT_NATIVE_INTERFACE_RETURN_IF(QEGLContext, platformContext); +#endif + + return nullptr; +} QT_END_NAMESPACE + +#include "moc_qopenglcontext.cpp" |