Introduced QOpenGLContext::aboutToBeDestroyed() signal.
authorSamuel Rødal <samuel.rodal@nokia.com>
Wed, 4 Jan 2012 13:17:45 +0000 (14:17 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 5 Jan 2012 01:17:04 +0000 (02:17 +0100)
This signal can be used to clean up OpenGL resources in a safe way
before the context is destroyed.

Task-number: QTBUG-20083
Change-Id: I45a4be01b06af4ee7196fa502116f099d50afeab
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
src/gui/kernel/qopenglcontext.cpp
src/gui/kernel/qopenglcontext.h
tests/auto/gui/qopengl/tst_qopengl.cpp

index 6a9cb43..29a9e92 100644 (file)
@@ -174,6 +174,8 @@ bool QOpenGLContext::create()
 void QOpenGLContext::destroy()
 {
     Q_D(QOpenGLContext);
+    if (d->platformGLContext)
+        emit aboutToBeDestroyed();
     if (QOpenGLContext::currentContext() == this)
         doneCurrent();
     if (d->shareGroup)
@@ -186,6 +188,17 @@ void QOpenGLContext::destroy()
 }
 
 /*!
+    \fn void QOpenGLContext::aboutToBeDestroyed()
+
+    This signal is emitted before the underlying native OpenGL context is
+    destroyed, such that users may clean up OpenGL resources that might otherwise
+    be left dangling in the case of shared OpenGL contexts.
+
+    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.
+*/
+
+/*!
   If this is the current context for the thread, doneCurrent is called
 */
 QOpenGLContext::~QOpenGLContext()
index b5a19a0..0d02cfe 100644 (file)
@@ -118,6 +118,9 @@ public:
 
     QOpenGLFunctions *functions() const;
 
+Q_SIGNALS:
+    void aboutToBeDestroyed();
+
 private:
     friend class QGLContext;
     friend class QOpenGLContextResourceBase;
index aa3e70a..d6c587b 100644 (file)
@@ -50,6 +50,8 @@
 
 #include <QtTest/QtTest>
 
+#include <QSignalSpy>
+
 class tst_QOpenGL : public QObject
 {
 Q_OBJECT
@@ -62,6 +64,7 @@ private slots:
     void fboRendering();
     void fboHandleNulledAfterContextDestroyed();
     void openGLPaintDevice();
+    void aboutToBeDestroyed();
 };
 
 struct SharedResourceTracker
@@ -500,5 +503,24 @@ void tst_QOpenGL::openGLPaintDevice()
     QCOMPARE(image, fbo.toImage().convertToFormat(QImage::Format_RGB32));
 }
 
+void tst_QOpenGL::aboutToBeDestroyed()
+{
+    QWindow window;
+    window.setGeometry(0, 0, 128, 128);
+    window.create();
+
+    QOpenGLContext *context = new QOpenGLContext;
+    QSignalSpy spy(context, SIGNAL(aboutToBeDestroyed()));
+
+    context->create();
+    context->makeCurrent(&window);
+
+    QCOMPARE(spy.size(), 0);
+
+    delete context;
+
+    QCOMPARE(spy.size(), 1);
+}
+
 QTEST_MAIN(tst_QOpenGL)
 #include "tst_qopengl.moc"