From 67f78a1e7fe2acb7a8f0ec43e0710b32379926c5 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 24 Apr 2023 10:47:44 +0200 Subject: [PATCH] gl: Check for image validity in readback Returning a null QImage is preferable over passing a null bits() to glReadPixels. (matters when QImage's malloc() gives null, thus 'd' is null -> isNull() == true) Fixes: QTBUG-113127 Change-Id: Ieca4d91eefdea47da5251dabe77cc31b48eb0e28 Reviewed-by: Allan Sandfeld Jensen (cherry picked from commit 11209cfde68e9b8b8722859c690e57c7d6e8a338) Reviewed-by: Qt Cherry-pick Bot --- src/opengl/qopenglframebufferobject.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/opengl/qopenglframebufferobject.cpp b/src/opengl/qopenglframebufferobject.cpp index ab5e500edaf..bde45772727 100644 --- a/src/opengl/qopenglframebufferobject.cpp +++ b/src/opengl/qopenglframebufferobject.cpp @@ -1370,7 +1370,8 @@ static inline QImage qt_gl_read_framebuffer_rgba8(const QSize &size, bool includ bool isOpenGL12orBetter = !context->isOpenGLES() && (context->format().majorVersion() >= 2 || context->format().minorVersion() >= 2); if (isOpenGL12orBetter) { QImage img(size, include_alpha ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32); - funcs->glReadPixels(0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, img.bits()); + if (!img.isNull()) + funcs->glReadPixels(0, 0, w, h, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, img.bits()); return img; } @@ -1380,7 +1381,8 @@ static inline QImage qt_gl_read_framebuffer_rgba8(const QSize &size, bool includ // BGRA capable impl would return BGRA from there) QImage rgbaImage(size, include_alpha ? QImage::Format_RGBA8888_Premultiplied : QImage::Format_RGBX8888); - funcs->glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rgbaImage.bits()); + if (!rgbaImage.isNull()) + funcs->glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, rgbaImage.bits()); return rgbaImage; } @@ -1388,7 +1390,8 @@ static inline QImage qt_gl_read_framebuffer_rgb10a2(const QSize &size, bool incl { // We assume OpenGL 1.2+ or ES 3.0+ here. QImage img(size, include_alpha ? QImage::Format_A2BGR30_Premultiplied : QImage::Format_BGR30); - context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, img.bits()); + if (!img.isNull()) + context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, img.bits()); return img; } @@ -1396,7 +1399,8 @@ static inline QImage qt_gl_read_framebuffer_rgba16(const QSize &size, bool inclu { // We assume OpenGL 1.2+ or ES 3.0+ here. QImage img(size, include_alpha ? QImage::Format_RGBA64_Premultiplied : QImage::Format_RGBX64); - context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_SHORT, img.bits()); + if (!img.isNull()) + context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_UNSIGNED_SHORT, img.bits()); return img; } @@ -1404,14 +1408,16 @@ static inline QImage qt_gl_read_framebuffer_rgba16f(const QSize &size, bool incl { // We assume OpenGL (ES) 3.0+ here. QImage img(size, include_alpha ? QImage::Format_RGBA16FPx4_Premultiplied : QImage::Format_RGBX16FPx4); - context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_HALF_FLOAT, img.bits()); + if (!img.isNull()) + context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_HALF_FLOAT, img.bits()); return img; } static inline QImage qt_gl_read_framebuffer_rgba32f(const QSize &size, bool include_alpha, QOpenGLContext *context) { QImage img(size, include_alpha ? QImage::Format_RGBA32FPx4_Premultiplied : QImage::Format_RGBX32FPx4); - context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_FLOAT, img.bits()); + if (!img.isNull()) + context->functions()->glReadPixels(0, 0, size.width(), size.height(), GL_RGBA, GL_FLOAT, img.bits()); return img; }