From 4b9a774a4c1c5fbefd056f2d1f617b657b7238b5 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 12 Oct 2020 22:18:04 +0300 Subject: qcam: viewfinder_gl: Fix fragment shader rebuild when setting format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When setting a new format, the existing fragment shader is deleted and a new shader should be created. However, the shader pointer isn't set to nullptr after deleting it, resulting in the deleter shader being reused. Fix it by managing shader pointers with std::unique_ptr<> to prevent similar bugs from happening in the future. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/qcam/viewfinder_gl.cpp | 19 ++++++------------- src/qcam/viewfinder_gl.h | 6 ++++-- 2 files changed, 10 insertions(+), 15 deletions(-) (limited to 'src/qcam') diff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp index 0b5c9426..03a576ba 100644 --- a/src/qcam/viewfinder_gl.cpp +++ b/src/qcam/viewfinder_gl.cpp @@ -33,7 +33,6 @@ static const QList supportedFormats{ ViewFinderGL::ViewFinderGL(QWidget *parent) : QOpenGLWidget(parent), buffer_(nullptr), yuvData_(nullptr), - vertexShader_(nullptr), fragmentShader_(nullptr), vertexBuffer_(QOpenGLBuffer::VertexBuffer), textureU_(QOpenGLTexture::Target2D), textureV_(QOpenGLTexture::Target2D), @@ -58,8 +57,8 @@ int ViewFinderGL::setFormat(const libcamera::PixelFormat &format, if (fragmentShader_) { if (shaderProgram_.isLinked()) { shaderProgram_.release(); - shaderProgram_.removeShader(fragmentShader_); - delete fragmentShader_; + shaderProgram_.removeShader(fragmentShader_.get()); + fragmentShader_.reset(); } } @@ -185,7 +184,7 @@ bool ViewFinderGL::selectFormat(const libcamera::PixelFormat &format) bool ViewFinderGL::createVertexShader() { /* Create Vertex Shader */ - vertexShader_ = new QOpenGLShader(QOpenGLShader::Vertex, this); + vertexShader_ = std::make_unique(QOpenGLShader::Vertex, this); /* Compile the vertex shader */ if (!vertexShader_->compileSourceFile(":YUV.vert")) { @@ -193,7 +192,7 @@ bool ViewFinderGL::createVertexShader() return false; } - shaderProgram_.addShader(vertexShader_); + shaderProgram_.addShader(vertexShader_.get()); return true; } @@ -207,7 +206,7 @@ bool ViewFinderGL::createFragmentShader() * program. The #define macros stored in fragmentShaderDefines_, if * any, are prepended to the source code. */ - fragmentShader_ = new QOpenGLShader(QOpenGLShader::Fragment, this); + fragmentShader_ = std::make_unique(QOpenGLShader::Fragment, this); QFile file(fragmentShaderFile_); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -224,7 +223,7 @@ bool ViewFinderGL::createFragmentShader() return false; } - shaderProgram_.addShader(fragmentShader_); + shaderProgram_.addShader(fragmentShader_.get()); /* Link shader pipeline */ if (!shaderProgram_.link()) { @@ -287,12 +286,6 @@ void ViewFinderGL::removeShader() shaderProgram_.release(); shaderProgram_.removeAllShaders(); } - - if (fragmentShader_) - delete fragmentShader_; - - if (vertexShader_) - delete vertexShader_; } void ViewFinderGL::initializeGL() diff --git a/src/qcam/viewfinder_gl.h b/src/qcam/viewfinder_gl.h index ad1e195e..40c04dc5 100644 --- a/src/qcam/viewfinder_gl.h +++ b/src/qcam/viewfinder_gl.h @@ -8,6 +8,8 @@ #ifndef __VIEWFINDER_GL_H__ #define __VIEWFINDER_GL_H__ +#include + #include #include #include @@ -67,8 +69,8 @@ private: /* Shaders */ QOpenGLShaderProgram shaderProgram_; - QOpenGLShader *vertexShader_; - QOpenGLShader *fragmentShader_; + std::unique_ptr vertexShader_; + std::unique_ptr fragmentShader_; QString fragmentShaderFile_; QStringList fragmentShaderDefines_; -- cgit v1.2.1