Skip to content

Fix #110: Do not use OpenGL functions directly #111

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

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/penlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ PenLayer::PenLayer(QNanoQuickItem *parent) :
{
m_fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
m_fboFormat.setSamples(m_antialiasingEnabled ? 4 : 0);

m_glF.initializeOpenGLFunctions();
}

PenLayer::~PenLayer()
Expand Down Expand Up @@ -65,10 +67,10 @@ void scratchcpprender::PenLayer::clear()
return;

m_fbo->bind();
glDisable(GL_SCISSOR_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_SCISSOR_TEST);
m_glF.glDisable(GL_SCISSOR_TEST);
m_glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
m_glF.glClear(GL_COLOR_BUFFER_BIT);
m_glF.glEnable(GL_SCISSOR_TEST);
m_fbo->release();

update();
Expand Down
2 changes: 2 additions & 0 deletions src/penlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ipenlayer.h>
#include <QOpenGLFramebufferObject>
#include <QOpenGLPaintDevice>
#include <QOpenGLFunctions>
#include <QPainter>
#include <scratchcpp/iengine.h>

Expand Down Expand Up @@ -49,6 +50,7 @@ class PenLayer : public IPenLayer
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
std::unique_ptr<QOpenGLPaintDevice> m_paintDevice;
QOpenGLFramebufferObjectFormat m_fboFormat;
QOpenGLFunctions m_glF;
};

} // namespace scratchcpprender
8 changes: 7 additions & 1 deletion src/renderedtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ void RenderedTarget::clearGraphicEffects()
void RenderedTarget::updateHullPoints(QOpenGLFramebufferObject *fbo)
{
Q_ASSERT(fbo);

if (!m_glF) {
m_glF = std::make_unique<QOpenGLFunctions>();
m_glF->initializeOpenGLFunctions();
}

int width = fbo->width();
int height = fbo->height();
m_hullPoints.clear();
Expand All @@ -521,7 +527,7 @@ void RenderedTarget::updateHullPoints(QOpenGLFramebufferObject *fbo)
// Read pixels from framebuffer
size_t size = width * height * 4;
GLubyte *pixelData = new GLubyte[size];
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
m_glF->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);

// Flip vertically
int rowSize = width * 4;
Expand Down
1 change: 1 addition & 0 deletions src/renderedtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class RenderedTarget : public IRenderedTarget
Skin *m_skin = nullptr;
Texture m_texture;
Texture m_oldTexture;
std::unique_ptr<QOpenGLFunctions> m_glF;
std::unordered_map<ShaderManager::Effect, double> m_graphicEffects;
double m_size = 1;
double m_x = 0;
Expand Down
7 changes: 5 additions & 2 deletions src/skin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Texture Skin::createAndPaintTexture(int width, int height, bool multisampled)
if (!context || !context->isValid() || (width <= 0 || height <= 0))
return Texture();

QOpenGLFunctions glF(context);
glF.initializeOpenGLFunctions();

// Create offscreen surface
QOffscreenSurface surface;
surface.setFormat(context->format());
Expand Down Expand Up @@ -48,8 +51,8 @@ Texture Skin::createAndPaintTexture(int width, int height, bool multisampled)
QPainter painter(&device);
painter.beginNativePainting();
painter.setRenderHint(QPainter::Antialiasing, false);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glF.glClear(GL_COLOR_BUFFER_BIT);

// Call the skin-specific paint method
paint(&painter);
Expand Down
8 changes: 6 additions & 2 deletions src/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ QImage Texture::toImage() const

void Texture::release()
{
if (m_isValid) {
glDeleteTextures(1, &m_handle);
QOpenGLContext *context = QOpenGLContext::currentContext();

if (m_isValid && context) {
QOpenGLExtraFunctions glF(context);
glF.initializeOpenGLFunctions();
glF.glDeleteTextures(1, &m_handle);
m_isValid = false;
}
}
Expand Down
7 changes: 5 additions & 2 deletions test/targetpainter/targetpainter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ TEST_F(TargetPainterTest, Paint)
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

QOpenGLExtraFunctions glF(&m_context);
glF.initializeOpenGLFunctions();

// Begin painting reference
QNanoPainter refPainter;
QOpenGLFramebufferObject refFbo(40, 60, format);
Expand Down Expand Up @@ -87,8 +90,8 @@ TEST_F(TargetPainterTest, Paint)
ASSERT_EQ(fbo.toImage(), refFbo.toImage());

// Paint with color effects
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glF.glClear(GL_COLOR_BUFFER_BIT);
effects[ShaderManager::Effect::Color] = 46;
effects[ShaderManager::Effect::Brightness] = 20;
effects[ShaderManager::Effect::Ghost] = 84;
Expand Down
14 changes: 10 additions & 4 deletions test/textbubblepainter/textbubblepainter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ TEST_F(TextBubblePainterTest, PaintSayBubble)
QOffscreenSurface surface;
createContextAndSurface(&context, &surface);

QOpenGLExtraFunctions glF(&context);
glF.initializeOpenGLFunctions();

QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

// Begin painting
QNanoPainter painter;
QOpenGLFramebufferObject fbo(425, 250, format);
fbo.bind();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glF.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
painter.beginFrame(fbo.width(), fbo.height());
painter.setAntialias(0);

Expand Down Expand Up @@ -77,15 +80,18 @@ TEST_F(TextBubblePainterTest, PaintThinkBubble)
QOffscreenSurface surface;
createContextAndSurface(&context, &surface);

QOpenGLExtraFunctions glF(&context);
glF.initializeOpenGLFunctions();

QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

// Begin painting
QNanoPainter painter;
QOpenGLFramebufferObject fbo(156, 117, format);
fbo.bind();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glF.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
painter.beginFrame(fbo.width(), fbo.height());
painter.setAntialias(0);

Expand Down
16 changes: 11 additions & 5 deletions test/texture/texture_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ TEST(TextureTest, ToImage)
Q_ASSERT(surface.isValid());
context.makeCurrent(&surface);

QOpenGLExtraFunctions glF(&context);
glF.initializeOpenGLFunctions();

QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

Expand All @@ -59,8 +62,8 @@ TEST(TextureTest, ToImage)
QPainter painter(&device);
painter.beginNativePainting();
painter.setRenderHint(QPainter::Antialiasing, false);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glF.glClear(GL_COLOR_BUFFER_BIT);
painter.drawEllipse(0, 0, fbo.width(), fbo.height());
painter.endNativePainting();
painter.end();
Expand All @@ -86,15 +89,18 @@ TEST(TextureTest, Release)
Q_ASSERT(surface.isValid());
context.makeCurrent(&surface);

QOpenGLExtraFunctions glF(&context);
glF.initializeOpenGLFunctions();

QOpenGLFramebufferObject fbo(1, 1);
GLuint handle = fbo.takeTexture();
ASSERT_TRUE(glIsTexture(handle));
ASSERT_TRUE(glF.glIsTexture(handle));

Texture tex(handle, fbo.width(), fbo.height());
ASSERT_TRUE(glIsTexture(handle));
ASSERT_TRUE(glF.glIsTexture(handle));

tex.release();
ASSERT_FALSE(glIsTexture(handle));
ASSERT_FALSE(glF.glIsTexture(handle));
ASSERT_FALSE(tex.isValid());

context.doneCurrent();
Expand Down