Skip to content

Commit ab5163a

Browse files
committed
PenLayer: Add refresh() method
1 parent bcd70ff commit ab5163a

File tree

3 files changed

+58
-36
lines changed

3 files changed

+58
-36
lines changed

src/penlayer.cpp

+35-35
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
8686
m_glF->initializeOpenGLFunctions();
8787
}
8888

89-
createFbo();
89+
refresh();
9090

9191
if (m_vao == 0) {
9292
// Set up VBO and VAO
@@ -133,7 +133,7 @@ void PenLayer::setHqPen(bool newHqPen)
133133

134134
m_hqPen = newHqPen;
135135
emit hqPenChanged();
136-
createFbo();
136+
refresh();
137137
}
138138

139139
void scratchcpprender::PenLayer::clear()
@@ -332,6 +332,38 @@ void PenLayer::stamp(IRenderedTarget *target)
332332
update();
333333
}
334334

335+
void PenLayer::refresh()
336+
{
337+
if (!m_glCtx || !m_surface || !m_engine || !m_glF)
338+
return;
339+
340+
QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
341+
QSurface *oldSurface = oldCtx->surface();
342+
343+
if (oldCtx != m_glCtx) {
344+
oldCtx->doneCurrent();
345+
m_glCtx->makeCurrent(m_surface);
346+
}
347+
348+
QOpenGLFramebufferObjectFormat fboFormat;
349+
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
350+
351+
QOpenGLFramebufferObject *newFbo = new QOpenGLFramebufferObject(width(), height(), fboFormat);
352+
Q_ASSERT(newFbo->isValid());
353+
354+
if (m_fbo)
355+
QOpenGLFramebufferObject::blitFramebuffer(newFbo, m_fbo.get());
356+
357+
m_fbo.reset(newFbo);
358+
m_texture = Texture(m_fbo->texture(), m_fbo->size());
359+
m_scale = width() / m_engine->stageWidth();
360+
361+
if (oldCtx != m_glCtx) {
362+
m_glCtx->doneCurrent();
363+
oldCtx->makeCurrent(oldSurface);
364+
}
365+
}
366+
335367
QOpenGLFramebufferObject *PenLayer::framebufferObject() const
336368
{
337369
return m_fbo.get();
@@ -445,43 +477,11 @@ QNanoQuickItemPainter *PenLayer::createItemPainter() const
445477
void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
446478
{
447479
if (m_hqPen && newGeometry != oldGeometry)
448-
createFbo();
480+
refresh();
449481

450482
QNanoQuickItem::geometryChange(newGeometry, oldGeometry);
451483
}
452484

453-
void PenLayer::createFbo()
454-
{
455-
if (!m_glCtx || !m_surface || !m_engine || !m_glF)
456-
return;
457-
458-
QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
459-
QSurface *oldSurface = oldCtx->surface();
460-
461-
if (oldCtx != m_glCtx) {
462-
oldCtx->doneCurrent();
463-
m_glCtx->makeCurrent(m_surface);
464-
}
465-
466-
QOpenGLFramebufferObjectFormat fboFormat;
467-
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
468-
469-
QOpenGLFramebufferObject *newFbo = new QOpenGLFramebufferObject(width(), height(), fboFormat);
470-
Q_ASSERT(newFbo->isValid());
471-
472-
if (m_fbo)
473-
QOpenGLFramebufferObject::blitFramebuffer(newFbo, m_fbo.get());
474-
475-
m_fbo.reset(newFbo);
476-
m_texture = Texture(m_fbo->texture(), m_fbo->size());
477-
m_scale = width() / m_engine->stageWidth();
478-
479-
if (oldCtx != m_glCtx) {
480-
m_glCtx->doneCurrent();
481-
oldCtx->makeCurrent(oldSurface);
482-
}
483-
}
484-
485485
void PenLayer::updateTexture()
486486
{
487487
if (!m_fbo)

src/penlayer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class PenLayer : public IPenLayer
3939
void drawLine(const PenAttributes &penAttributes, double x0, double y0, double x1, double y1) override;
4040
void stamp(IRenderedTarget *target) override;
4141

42+
Q_INVOKABLE void refresh();
43+
4244
QOpenGLFramebufferObject *framebufferObject() const override;
4345
QRgb colorAtScratchPoint(double x, double y) const override;
4446

@@ -56,7 +58,6 @@ class PenLayer : public IPenLayer
5658
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
5759

5860
private:
59-
void createFbo();
6061
void updateTexture();
6162

6263
static std::unordered_map<libscratchcpp::IEngine *, IPenLayer *> m_projectPenLayers;

test/penlayer/penlayer_test.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ TEST_F(PenLayerTest, FramebufferObject)
148148
ASSERT_EQ(fbo->format().samples(), 0);
149149
}
150150

151+
TEST_F(PenLayerTest, Refresh)
152+
{
153+
PenLayer penLayer;
154+
155+
EngineMock engine1, engine2, engine3;
156+
penLayer.setWidth(480);
157+
penLayer.setHeight(360);
158+
EXPECT_CALL(engine1, stageWidth()).WillOnce(Return(480));
159+
penLayer.setEngine(&engine1);
160+
161+
penLayer.setWidth(500);
162+
penLayer.setHeight(400);
163+
164+
EXPECT_CALL(engine1, stageWidth()).WillOnce(Return(500));
165+
penLayer.refresh();
166+
167+
QOpenGLFramebufferObject *fbo = penLayer.framebufferObject();
168+
ASSERT_EQ(fbo->width(), 500);
169+
ASSERT_EQ(fbo->height(), 400);
170+
}
171+
151172
TEST_F(PenLayerTest, GetProjectPenLayer)
152173
{
153174
PenLayer penLayer;

0 commit comments

Comments
 (0)