Skip to content

Commit 64e6335

Browse files
committed
PenLayer: Use the correct context for FBO creation
1 parent e97cef2 commit 64e6335

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/penlayer.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
6868

6969
m_engine = newEngine;
7070

71-
if (m_engine && QOpenGLContext::currentContext()) {
71+
if (!m_glCtx) {
72+
m_glCtx = QOpenGLContext::currentContext();
73+
74+
if (m_glCtx)
75+
m_surface = m_glCtx->surface();
76+
}
77+
78+
if (m_engine && m_glCtx) {
7279
m_projectPenLayers[m_engine] = this;
7380

7481
if (!m_painter)
@@ -428,6 +435,10 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)
428435

429436
QNanoQuickItemPainter *PenLayer::createItemPainter() const
430437
{
438+
m_glCtx = QOpenGLContext::currentContext();
439+
Q_ASSERT(m_glCtx);
440+
m_surface = m_glCtx->surface();
441+
Q_ASSERT(m_surface);
431442
return new PenLayerPainter;
432443
}
433444

@@ -441,9 +452,17 @@ void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeomet
441452

442453
void PenLayer::createFbo()
443454
{
444-
if (!QOpenGLContext::currentContext() || !m_engine)
455+
if (!m_glCtx || !m_surface || !m_engine || !m_glF)
445456
return;
446457

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+
447466
QOpenGLFramebufferObjectFormat fboFormat;
448467
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
449468

@@ -456,6 +475,11 @@ void PenLayer::createFbo()
456475
m_fbo.reset(newFbo);
457476
m_texture = Texture(m_fbo->texture(), m_fbo->size());
458477
m_scale = width() / m_engine->stageWidth();
478+
479+
if (oldCtx != m_glCtx) {
480+
m_glCtx->doneCurrent();
481+
oldCtx->makeCurrent(oldSurface);
482+
}
459483
}
460484

461485
void PenLayer::updateTexture()

src/penlayer.h

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class PenLayer : public IPenLayer
6565
libscratchcpp::IEngine *m_engine = nullptr;
6666
bool m_hqPen = false;
6767
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
68+
mutable QOpenGLContext *m_glCtx = nullptr;
69+
mutable QSurface *m_surface = nullptr;
6870
double m_scale = 1;
6971
std::unique_ptr<QNanoPainter> m_painter;
7072
std::unique_ptr<QOpenGLExtraFunctions> m_glF;

0 commit comments

Comments
 (0)