QOpenGLDebugLogger: strengthen the code path in case of GL context destruction

Trying to move closer to GL semantics: it is allowed to destroy
a GL context at any time and that must free all of its resources.

Change-Id: I3daa81d721cf26baf86a1a6435b77e3c28feb1a2
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
bb10
Giuseppe D'Angelo 2016-07-19 14:57:12 +01:00
parent 65bad0d0f4
commit b694fe987c
1 changed files with 34 additions and 0 deletions

View File

@ -36,6 +36,7 @@
#include <QtCore/qvarlengtharray.h>
#include <QtGui/qopengl.h>
#include <QtGui/qopenglfunctions.h>
#include <QtGui/qoffscreensurface.h>
#include "qopengldebug.h"
@ -1281,8 +1282,41 @@ void QOpenGLDebugLoggerPrivate::controlDebugMessages(QOpenGLDebugMessage::Source
*/
void QOpenGLDebugLoggerPrivate::_q_contextAboutToBeDestroyed()
{
Q_ASSERT(context);
// Re-make our context current somehow, otherwise stopLogging will fail.
// Save the current context and its surface in case we need to set them back
QOpenGLContext *currentContext = QOpenGLContext::currentContext();
QSurface *currentSurface = 0;
QScopedPointer<QOffscreenSurface> offscreenSurface;
if (context != currentContext) {
// Make our old context current on a temporary surface
if (currentContext)
currentSurface = currentContext->surface();
offscreenSurface.reset(new QOffscreenSurface);
offscreenSurface->setFormat(context->format());
offscreenSurface->create();
if (!context->makeCurrent(offscreenSurface.data()))
qWarning("QOpenGLDebugLoggerPrivate::_q_contextAboutToBeDestroyed(): could not make the owning GL context current for cleanup");
}
Q_Q(QOpenGLDebugLogger);
q->stopLogging();
if (offscreenSurface) {
// We did change the current context: set it back
if (currentContext)
currentContext->makeCurrent(currentSurface);
else
context->doneCurrent();
}
QObject::disconnect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed()));
context = 0;
initialized = false;
}