@@ -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
429436QNanoQuickItemPainter *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
442453void 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
461485void PenLayer::updateTexture ()
0 commit comments