Preserve and restore the GL context when resizing QML surfaces

This commit is contained in:
Bradley Austin Davis 2016-12-06 16:09:20 -08:00 committed by Chris Collins
parent e7ca61480e
commit 87dd8a46f8
3 changed files with 46 additions and 28 deletions

View file

@ -69,3 +69,15 @@ QThread* RENDER_THREAD = nullptr;
bool isRenderThread() {
return QThread::currentThread() == RENDER_THREAD;
}
namespace gl {
void withSavedContext(const std::function<void()>& f) {
// Save the original GL context, because creating a QML surface will create a new context
QOpenGLContext * savedContext = QOpenGLContext::currentContext();
QSurface * savedSurface = savedContext ? savedContext->surface() : nullptr;
f();
if (savedContext) {
savedContext->makeCurrent(savedSurface);
}
}
}

View file

@ -10,6 +10,7 @@
#ifndef hifi_GLHelpers_h
#define hifi_GLHelpers_h
#include <functional>
#include <QJsonObject>
// 16 bits of depth precision
@ -34,4 +35,8 @@ int glVersionToInteger(QString glVersion);
bool isRenderThread();
namespace gl {
void withSavedContext(const std::function<void()>& f);
}
#endif

View file

@ -467,7 +467,7 @@ void OffscreenQmlSurface::resize(const QSize& newSize_, bool forceResize) {
}
qCDebug(glLogging) << "Offscreen UI resizing to " << newSize.width() << "x" << newSize.height();
gl::withSavedContext([&] {
_canvas->makeCurrent();
// Release hold on the textures of the old size
@ -501,6 +501,7 @@ void OffscreenQmlSurface::resize(const QSize& newSize_, bool forceResize) {
}
_canvas->doneCurrent();
});
}
QQuickItem* OffscreenQmlSurface::getRootItem() {