-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix null pointer access and avoid nested allocations with QOpenGLTexture #13043
Changes from 1 commit
26a7049
ec4bf17
07f05f1
a0a0ddb
a0ef420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "util/opengltexture2d.h" | ||
|
||
#include <QPixmap> | ||
|
||
#include "widget/paintable.h" | ||
|
||
OpenGLTexture2D::OpenGLTexture2D() | ||
: QOpenGLTexture(QOpenGLTexture::Target2D){}; | ||
|
||
void OpenGLTexture2D::setData(const QImage& image) { | ||
destroy(); | ||
if (!image.isNull()) { | ||
QOpenGLTexture::setData(image); | ||
setMinMagFilters(QOpenGLTexture::Linear, QOpenGLTexture::Linear); | ||
setWrapMode(QOpenGLTexture::ClampToEdge); | ||
} | ||
}; | ||
|
||
void OpenGLTexture2D::setData(const QPixmap& pixmap) { | ||
setData(pixmap.toImage()); | ||
}; | ||
|
||
void OpenGLTexture2D::setData(const QSharedPointer<Paintable>& pPaintable) { | ||
if (pPaintable) { | ||
setData(pPaintable->toImage()); | ||
} | ||
}; | ||
|
||
void OpenGLTexture2D::setData(const std::shared_ptr<QImage>& pImage) { | ||
if (pImage) { | ||
setData(*pImage); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <QOpenGLTexture> | ||
#include <QSharedPointer> | ||
|
||
class Paintable; | ||
|
||
class OpenGLTexture2D : public QOpenGLTexture { | ||
public: | ||
OpenGLTexture2D(); | ||
|
||
void setData(const QImage& image); | ||
void setData(const QPixmap& pixmap); | ||
void setData(const QSharedPointer<Paintable>& pPaintable); | ||
void setData(const std::shared_ptr<QImage>& pImage); | ||
}; |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,13 @@ | |
#include <array> | ||
|
||
#include "skin/legacy/skincontext.h" | ||
#include "util/texture.h" | ||
#include "waveform/renderers/allshader/matrixforwidgetgeometry.h" | ||
#include "waveform/renderers/waveformwidgetrenderer.h" | ||
#include "widget/wskincolor.h" | ||
|
||
namespace { | ||
std::unique_ptr<QOpenGLTexture> generateTexture(float markerLength, | ||
void generateTexture(OpenGLTexture2D* pTexture, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't see how the tradeoff in code quality makes sense. There is no reason to introduce an out-parameter here IMO. If the extra allocation bothers you, why not return it by value directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is this no out parameter which may be considered as bad code quality these days. It just updates the texture via pointer because OpenGLTexture2D has no copy constructor . I kept just to avoid moving code around. I will now make it a normal member function as usual and rename it to avoid future confusion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored to become drawPrerollImage() returning the temporary QImage. |
||
float markerLength, | ||
float markerBreadth, | ||
float devicePixelRatio, | ||
QColor color) { | ||
|
@@ -54,7 +54,7 @@ std::unique_ptr<QOpenGLTexture> generateTexture(float markerLength, | |
painter.drawPath(path); | ||
painter.end(); | ||
|
||
return createTexture(image); | ||
pTexture->setData(image); | ||
} | ||
} // anonymous namespace | ||
|
||
|
@@ -120,13 +120,14 @@ void WaveformRendererPreroll::paintGL() { | |
// has changed size last time. | ||
m_markerLength = markerLength; | ||
m_markerBreadth = markerBreadth; | ||
m_pTexture = generateTexture(m_markerLength, | ||
generateTexture(&m_texture, | ||
m_markerLength, | ||
m_markerBreadth, | ||
m_waveformRenderer->getDevicePixelRatio(), | ||
m_color); | ||
} | ||
|
||
if (!m_pTexture) { | ||
if (!m_texture.isStorageAllocated()) { | ||
return; | ||
} | ||
|
||
|
@@ -146,7 +147,7 @@ void WaveformRendererPreroll::paintGL() { | |
m_shader.setUniformValue(matrixLocation, matrix); | ||
m_shader.setUniformValue(textureLocation, 0); | ||
|
||
m_pTexture->bind(); | ||
m_texture.bind(); | ||
|
||
const float end = m_waveformRenderer->getLength(); | ||
|
||
|
@@ -191,7 +192,7 @@ void WaveformRendererPreroll::paintGL() { | |
(end - x) / markerLength); | ||
} | ||
|
||
m_pTexture->release(); | ||
m_texture.release(); | ||
|
||
m_shader.disableAttributeArray(positionLocation); | ||
m_shader.disableAttributeArray(texcoordLocation); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a comment that explains that this is an extension of QOpenGLTexture, with additional methods to set the texture data, and default settings for filter and wrap mode.