Skip to content

Commit 417516e

Browse files
committed
fix #110: Do not use OpenGL functions directly
1 parent 4b66e98 commit 417516e

File tree

9 files changed

+53
-20
lines changed

9 files changed

+53
-20
lines changed

src/penlayer.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PenLayer::PenLayer(QNanoQuickItem *parent) :
1313
{
1414
m_fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
1515
m_fboFormat.setSamples(m_antialiasingEnabled ? 4 : 0);
16+
17+
m_glF.initializeOpenGLFunctions();
1618
}
1719

1820
PenLayer::~PenLayer()
@@ -65,10 +67,10 @@ void scratchcpprender::PenLayer::clear()
6567
return;
6668

6769
m_fbo->bind();
68-
glDisable(GL_SCISSOR_TEST);
69-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
70-
glClear(GL_COLOR_BUFFER_BIT);
71-
glEnable(GL_SCISSOR_TEST);
70+
m_glF.glDisable(GL_SCISSOR_TEST);
71+
m_glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
72+
m_glF.glClear(GL_COLOR_BUFFER_BIT);
73+
m_glF.glEnable(GL_SCISSOR_TEST);
7274
m_fbo->release();
7375

7476
update();

src/penlayer.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ipenlayer.h>
66
#include <QOpenGLFramebufferObject>
77
#include <QOpenGLPaintDevice>
8+
#include <QOpenGLFunctions>
89
#include <QPainter>
910
#include <scratchcpp/iengine.h>
1011

@@ -49,6 +50,7 @@ class PenLayer : public IPenLayer
4950
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
5051
std::unique_ptr<QOpenGLPaintDevice> m_paintDevice;
5152
QOpenGLFramebufferObjectFormat m_fboFormat;
53+
QOpenGLFunctions m_glF;
5254
};
5355

5456
} // namespace scratchcpprender

src/renderedtarget.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,12 @@ void RenderedTarget::clearGraphicEffects()
513513
void RenderedTarget::updateHullPoints(QOpenGLFramebufferObject *fbo)
514514
{
515515
Q_ASSERT(fbo);
516+
517+
if (!m_glF) {
518+
m_glF = std::make_unique<QOpenGLFunctions>();
519+
m_glF->initializeOpenGLFunctions();
520+
}
521+
516522
int width = fbo->width();
517523
int height = fbo->height();
518524
m_hullPoints.clear();
@@ -521,7 +527,7 @@ void RenderedTarget::updateHullPoints(QOpenGLFramebufferObject *fbo)
521527
// Read pixels from framebuffer
522528
size_t size = width * height * 4;
523529
GLubyte *pixelData = new GLubyte[size];
524-
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
530+
m_glF->glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
525531

526532
// Flip vertically
527533
int rowSize = width * 4;

src/renderedtarget.h

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class RenderedTarget : public IRenderedTarget
125125
Skin *m_skin = nullptr;
126126
Texture m_texture;
127127
Texture m_oldTexture;
128+
std::unique_ptr<QOpenGLFunctions> m_glF;
128129
std::unordered_map<ShaderManager::Effect, double> m_graphicEffects;
129130
double m_size = 1;
130131
double m_x = 0;

src/skin.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Texture Skin::createAndPaintTexture(int width, int height, bool multisampled)
1818
if (!context || !context->isValid() || (width <= 0 || height <= 0))
1919
return Texture();
2020

21+
QOpenGLFunctions glF(context);
22+
glF.initializeOpenGLFunctions();
23+
2124
// Create offscreen surface
2225
QOffscreenSurface surface;
2326
surface.setFormat(context->format());
@@ -48,8 +51,8 @@ Texture Skin::createAndPaintTexture(int width, int height, bool multisampled)
4851
QPainter painter(&device);
4952
painter.beginNativePainting();
5053
painter.setRenderHint(QPainter::Antialiasing, false);
51-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
52-
glClear(GL_COLOR_BUFFER_BIT);
54+
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
55+
glF.glClear(GL_COLOR_BUFFER_BIT);
5356

5457
// Call the skin-specific paint method
5558
paint(&painter);

src/texture.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ QImage Texture::toImage() const
114114

115115
void Texture::release()
116116
{
117-
if (m_isValid) {
118-
glDeleteTextures(1, &m_handle);
117+
QOpenGLContext *context = QOpenGLContext::currentContext();
118+
119+
if (m_isValid && context) {
120+
QOpenGLExtraFunctions glF(context);
121+
glF.initializeOpenGLFunctions();
122+
glF.glDeleteTextures(1, &m_handle);
119123
m_isValid = false;
120124
}
121125
}

test/targetpainter/targetpainter_test.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ TEST_F(TargetPainterTest, Paint)
3737
QOpenGLFramebufferObjectFormat format;
3838
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
3939

40+
QOpenGLExtraFunctions glF(&m_context);
41+
glF.initializeOpenGLFunctions();
42+
4043
// Begin painting reference
4144
QNanoPainter refPainter;
4245
QOpenGLFramebufferObject refFbo(40, 60, format);
@@ -87,8 +90,8 @@ TEST_F(TargetPainterTest, Paint)
8790
ASSERT_EQ(fbo.toImage(), refFbo.toImage());
8891

8992
// Paint with color effects
90-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
91-
glClear(GL_COLOR_BUFFER_BIT);
93+
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
94+
glF.glClear(GL_COLOR_BUFFER_BIT);
9295
effects[ShaderManager::Effect::Color] = 46;
9396
effects[ShaderManager::Effect::Brightness] = 20;
9497
effects[ShaderManager::Effect::Ghost] = 84;

test/textbubblepainter/textbubblepainter_test.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ TEST_F(TextBubblePainterTest, PaintSayBubble)
3333
QOffscreenSurface surface;
3434
createContextAndSurface(&context, &surface);
3535

36+
QOpenGLExtraFunctions glF(&context);
37+
glF.initializeOpenGLFunctions();
38+
3639
QOpenGLFramebufferObjectFormat format;
3740
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
3841

3942
// Begin painting
4043
QNanoPainter painter;
4144
QOpenGLFramebufferObject fbo(425, 250, format);
4245
fbo.bind();
43-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
44-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
46+
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
47+
glF.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
4548
painter.beginFrame(fbo.width(), fbo.height());
4649
painter.setAntialias(0);
4750

@@ -77,15 +80,18 @@ TEST_F(TextBubblePainterTest, PaintThinkBubble)
7780
QOffscreenSurface surface;
7881
createContextAndSurface(&context, &surface);
7982

83+
QOpenGLExtraFunctions glF(&context);
84+
glF.initializeOpenGLFunctions();
85+
8086
QOpenGLFramebufferObjectFormat format;
8187
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
8288

8389
// Begin painting
8490
QNanoPainter painter;
8591
QOpenGLFramebufferObject fbo(156, 117, format);
8692
fbo.bind();
87-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
88-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
93+
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
94+
glF.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
8995
painter.beginFrame(fbo.width(), fbo.height());
9096
painter.setAntialias(0);
9197

test/texture/texture_test.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ TEST(TextureTest, ToImage)
4949
Q_ASSERT(surface.isValid());
5050
context.makeCurrent(&surface);
5151

52+
QOpenGLExtraFunctions glF(&context);
53+
glF.initializeOpenGLFunctions();
54+
5255
QOpenGLFramebufferObjectFormat format;
5356
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
5457

@@ -59,8 +62,8 @@ TEST(TextureTest, ToImage)
5962
QPainter painter(&device);
6063
painter.beginNativePainting();
6164
painter.setRenderHint(QPainter::Antialiasing, false);
62-
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
63-
glClear(GL_COLOR_BUFFER_BIT);
65+
glF.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
66+
glF.glClear(GL_COLOR_BUFFER_BIT);
6467
painter.drawEllipse(0, 0, fbo.width(), fbo.height());
6568
painter.endNativePainting();
6669
painter.end();
@@ -86,15 +89,18 @@ TEST(TextureTest, Release)
8689
Q_ASSERT(surface.isValid());
8790
context.makeCurrent(&surface);
8891

92+
QOpenGLExtraFunctions glF(&context);
93+
glF.initializeOpenGLFunctions();
94+
8995
QOpenGLFramebufferObject fbo(1, 1);
9096
GLuint handle = fbo.takeTexture();
91-
ASSERT_TRUE(glIsTexture(handle));
97+
ASSERT_TRUE(glF.glIsTexture(handle));
9298

9399
Texture tex(handle, fbo.width(), fbo.height());
94-
ASSERT_TRUE(glIsTexture(handle));
100+
ASSERT_TRUE(glF.glIsTexture(handle));
95101

96102
tex.release();
97-
ASSERT_FALSE(glIsTexture(handle));
103+
ASSERT_FALSE(glF.glIsTexture(handle));
98104
ASSERT_FALSE(tex.isValid());
99105

100106
context.doneCurrent();

0 commit comments

Comments
 (0)