From 417516e0057258400c23e09fc3b3930631e1cdd5 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sat, 2 Mar 2024 14:04:09 +0100 Subject: [PATCH] fix #110: Do not use OpenGL functions directly --- src/penlayer.cpp | 10 ++++++---- src/penlayer.h | 2 ++ src/renderedtarget.cpp | 8 +++++++- src/renderedtarget.h | 1 + src/skin.cpp | 7 +++++-- src/texture.cpp | 8 ++++++-- test/targetpainter/targetpainter_test.cpp | 7 +++++-- .../textbubblepainter/textbubblepainter_test.cpp | 14 ++++++++++---- test/texture/texture_test.cpp | 16 +++++++++++----- 9 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/penlayer.cpp b/src/penlayer.cpp index 2809806..26f1918 100644 --- a/src/penlayer.cpp +++ b/src/penlayer.cpp @@ -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() @@ -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(); diff --git a/src/penlayer.h b/src/penlayer.h index 7a98a0b..b8b5987 100644 --- a/src/penlayer.h +++ b/src/penlayer.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,7 @@ class PenLayer : public IPenLayer std::unique_ptr m_fbo; std::unique_ptr m_paintDevice; QOpenGLFramebufferObjectFormat m_fboFormat; + QOpenGLFunctions m_glF; }; } // namespace scratchcpprender diff --git a/src/renderedtarget.cpp b/src/renderedtarget.cpp index 438636f..bf2b5b0 100644 --- a/src/renderedtarget.cpp +++ b/src/renderedtarget.cpp @@ -513,6 +513,12 @@ void RenderedTarget::clearGraphicEffects() void RenderedTarget::updateHullPoints(QOpenGLFramebufferObject *fbo) { Q_ASSERT(fbo); + + if (!m_glF) { + m_glF = std::make_unique(); + m_glF->initializeOpenGLFunctions(); + } + int width = fbo->width(); int height = fbo->height(); m_hullPoints.clear(); @@ -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; diff --git a/src/renderedtarget.h b/src/renderedtarget.h index 75ed7af..d11f4d2 100644 --- a/src/renderedtarget.h +++ b/src/renderedtarget.h @@ -125,6 +125,7 @@ class RenderedTarget : public IRenderedTarget Skin *m_skin = nullptr; Texture m_texture; Texture m_oldTexture; + std::unique_ptr m_glF; std::unordered_map m_graphicEffects; double m_size = 1; double m_x = 0; diff --git a/src/skin.cpp b/src/skin.cpp index f28fd30..dd6c150 100644 --- a/src/skin.cpp +++ b/src/skin.cpp @@ -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()); @@ -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); diff --git a/src/texture.cpp b/src/texture.cpp index a0bf2da..3bca9c2 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -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; } } diff --git a/test/targetpainter/targetpainter_test.cpp b/test/targetpainter/targetpainter_test.cpp index 2d7d1df..1d4c7b3 100644 --- a/test/targetpainter/targetpainter_test.cpp +++ b/test/targetpainter/targetpainter_test.cpp @@ -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); @@ -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; diff --git a/test/textbubblepainter/textbubblepainter_test.cpp b/test/textbubblepainter/textbubblepainter_test.cpp index 739095b..0e38e1b 100644 --- a/test/textbubblepainter/textbubblepainter_test.cpp +++ b/test/textbubblepainter/textbubblepainter_test.cpp @@ -33,6 +33,9 @@ TEST_F(TextBubblePainterTest, PaintSayBubble) QOffscreenSurface surface; createContextAndSurface(&context, &surface); + QOpenGLExtraFunctions glF(&context); + glF.initializeOpenGLFunctions(); + QOpenGLFramebufferObjectFormat format; format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); @@ -40,8 +43,8 @@ TEST_F(TextBubblePainterTest, PaintSayBubble) 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); @@ -77,6 +80,9 @@ TEST_F(TextBubblePainterTest, PaintThinkBubble) QOffscreenSurface surface; createContextAndSurface(&context, &surface); + QOpenGLExtraFunctions glF(&context); + glF.initializeOpenGLFunctions(); + QOpenGLFramebufferObjectFormat format; format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); @@ -84,8 +90,8 @@ TEST_F(TextBubblePainterTest, PaintThinkBubble) 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); diff --git a/test/texture/texture_test.cpp b/test/texture/texture_test.cpp index ffdff19..4f639ec 100644 --- a/test/texture/texture_test.cpp +++ b/test/texture/texture_test.cpp @@ -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); @@ -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(); @@ -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();