Fix texture leak in QML surface test

This commit is contained in:
Bradley Austin Davis 2018-05-04 12:41:39 -07:00
parent b657c28a9b
commit 81df7a1293

View file

@ -26,7 +26,7 @@
#include <QtGui/QWindow>
#include <QtGui/QGuiApplication>
#include <QtGui/QImage>
#include <QtGui/QOpenGLFunctions_4_5_Core>
#include <QtGui/QOpenGLFunctions_4_1_Core>
#include <QtGui/QOpenGLContext>
#include <QtQuick/QQuickItem>
#include <QtQml/QQmlContext>
@ -88,7 +88,7 @@ private:
OffscreenGLCanvas _sharedContext;
std::array<std::array<QmlInfo, DIVISIONS_Y>, DIVISIONS_X> _surfaces;
QOpenGLFunctions_4_5_Core _glf;
QOpenGLFunctions_4_1_Core _glf;
std::function<void(uint32_t, void*)> _discardLamdba;
QSize _size;
size_t _surfaceCount{ 0 };
@ -145,7 +145,7 @@ void TestWindow::initGl() {
gl::initModuleGl();
_glf.initializeOpenGLFunctions();
_glf.glCreateFramebuffers(1, &_fbo);
_glf.glGenFramebuffers(1, &_fbo);
if (!_sharedContext.create(&_glContext) || !_sharedContext.makeCurrent()) {
qFatal("Unable to intialize Shared GL context");
@ -196,6 +196,12 @@ void TestWindow::buildSurface(QmlInfo& qmlInfo, bool video) {
void TestWindow::destroySurface(QmlInfo& qmlInfo) {
auto& surface = qmlInfo.surface;
auto& currentTexture = qmlInfo.texture;
if (currentTexture) {
auto readFence = _glf.glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
glFlush();
_discardLamdba(currentTexture, readFence);
}
auto webView = surface->getRootItem();
if (webView) {
// stop loading
@ -274,16 +280,19 @@ void TestWindow::draw() {
if (!qmlInfo.surface || !qmlInfo.texture) {
continue;
}
_glf.glNamedFramebufferTexture(_fbo, GL_COLOR_ATTACHMENT0, qmlInfo.texture, 0);
_glf.glBlitNamedFramebuffer(_fbo, 0,
// src coordinates
0, 0, _qmlSize.width() - 1, _qmlSize.height() - 1,
// dst coordinates
incrementX * x, incrementY * y, incrementX * (x + 1), incrementY * (y + 1),
// blit mask and filter
GL_COLOR_BUFFER_BIT, GL_NEAREST);
_glf.glBindFramebuffer(GL_READ_FRAMEBUFFER, _fbo);
_glf.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, qmlInfo.texture, 0);
_glf.glBlitFramebuffer(
// src coordinates
0, 0, _qmlSize.width() - 1, _qmlSize.height() - 1,
// dst coordinates
incrementX * x, incrementY * y, incrementX * (x + 1), incrementY * (y + 1),
// blit mask and filter
GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
}
_glf.glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
_glf.glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
_glContext.swapBuffers(this);
}