summaryrefslogtreecommitdiffstats
path: root/src/gui/opengl/qopenglversionfunctions.cpp
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-03-24 16:57:23 +0100
committerLaszlo Agocs <laszlo.agocs@theqtcompany.com>2015-03-27 16:36:34 +0000
commit52781f2a7d4c63990c2484b267614450f80c5591 (patch)
tree1b393f62917899931ce2f0c6a76f76b4af445a5a /src/gui/opengl/qopenglversionfunctions.cpp
parentbc6e92bd2ed7e39dcb856c3e275886e1e69df4c3 (diff)
Make versioned OpenGL functions working with the subclass pattern
QOpenGLFunctions allows both deriving from it and getting an instance via QOpenGLContext::functions(). Unsurprisingly a large number of users attempt to use the versioned function wrappers in the same way. Unfortunately this approach was not that well supported. Besides some potential base class exporting issues the real blocker for QOpenGLWidget - or any versionfunction subclass whose associated context changes during its lifetime - is that the functions instances could only be initialized once. Unlike instances retrieved via QOpenGLContext::versionFunctions(), instances created "manually" were not deinitialized upon the destruction of the associated context because context did not know about them. A pattern like initializeOpenGLFunctions(); delete context; create new context and make it current initializeOpenGLFunctions(); is working fine in QOpenGLFunctions-derived classes but not with the versioned ones. To overcome this, start registering such instances to the context too. QOpenGLContext::destroy() can then reset the internal state so a subsequent initializeOpenGLFunctions() will reinitialize properly instead of bailing out mistakenly thinking that everything is ready to use. Task-number: QTBUG-45199 Change-Id: Ia1420bcccb33c51508698b7a1b036c7544a66e74 Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/gui/opengl/qopenglversionfunctions.cpp')
-rw-r--r--src/gui/opengl/qopenglversionfunctions.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/gui/opengl/qopenglversionfunctions.cpp b/src/gui/opengl/qopenglversionfunctions.cpp
index f3f709c3f0..346a526054 100644
--- a/src/gui/opengl/qopenglversionfunctions.cpp
+++ b/src/gui/opengl/qopenglversionfunctions.cpp
@@ -67,6 +67,17 @@ void QAbstractOpenGLFunctionsPrivate::removeFunctionsBackend(QOpenGLContext *con
context->removeFunctionsBackend(v);
}
+void QAbstractOpenGLFunctionsPrivate::insertExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
+{
+ Q_ASSERT(context);
+ context->insertExternalFunctions(f);
+}
+
+void QAbstractOpenGLFunctionsPrivate::removeExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
+{
+ Q_ASSERT(context);
+ context->removeExternalFunctions(f);
+}
/*!
\class QAbstractOpenGLFunctions
@@ -182,6 +193,9 @@ QAbstractOpenGLFunctions::QAbstractOpenGLFunctions()
QAbstractOpenGLFunctions::~QAbstractOpenGLFunctions()
{
+ Q_D(QAbstractOpenGLFunctions);
+ if (d->owningContext)
+ d->removeExternalFunctions(d->owningContext, this);
delete d_ptr;
}
@@ -191,6 +205,18 @@ bool QAbstractOpenGLFunctions::initializeOpenGLFunctions()
{
Q_D(QAbstractOpenGLFunctions);
d->initialized = true;
+
+ // For a subclass whose instance is not created via
+ // QOpenGLContext::versionFunctions() owningContext is not set. Set it now
+ // and register such instances to the context as external ones. These are
+ // not owned by the context but still need certain cleanup when the context
+ // is destroyed.
+ if (!d->owningContext) {
+ d->owningContext = QOpenGLContext::currentContext();
+ if (d->owningContext)
+ d->insertExternalFunctions(d->owningContext, this);
+ }
+
return true;
}