@@ -68,9 +68,15 @@ 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 ;
73- createFbo ();
7480
7581 if (!m_painter)
7682 m_painter = std::make_unique<QNanoPainter>();
@@ -80,6 +86,8 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
8086 m_glF->initializeOpenGLFunctions ();
8187 }
8288
89+ createFbo ();
90+
8391 if (m_vao == 0 ) {
8492 // Set up VBO and VAO
8593 float vertices[] = { -1 .0f , -1 .0f , 0 .0f , 0 .0f , 1 .0f , -1 .0f , 1 .0f , 0 .0f , -1 .0f , 1 .0f , 0 .0f , 1 .0f , 1 .0f , -1 .0f , 1 .0f , 0 .0f , 1 .0f , 1 .0f , 1 .0f , 1 .0f , -1 .0f , 1 .0f , 0 .0f , 1 .0f };
@@ -124,8 +132,8 @@ void PenLayer::setHqPen(bool newHqPen)
124132 return ;
125133
126134 m_hqPen = newHqPen;
127- createFbo ();
128135 emit hqPenChanged ();
136+ createFbo ();
129137}
130138
131139void scratchcpprender::PenLayer::clear ()
@@ -427,6 +435,10 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)
427435
428436QNanoQuickItemPainter *PenLayer::createItemPainter () const
429437{
438+ m_glCtx = QOpenGLContext::currentContext ();
439+ Q_ASSERT (m_glCtx);
440+ m_surface = m_glCtx->surface ();
441+ Q_ASSERT (m_surface);
430442 return new PenLayerPainter;
431443}
432444
@@ -440,9 +452,17 @@ void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeomet
440452
441453void PenLayer::createFbo ()
442454{
443- if (!QOpenGLContext::currentContext () || !m_engine)
455+ if (!m_glCtx || !m_surface || ! m_engine || !m_glF )
444456 return ;
445457
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+
446466 QOpenGLFramebufferObjectFormat fboFormat;
447467 fboFormat.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
448468
@@ -455,6 +475,11 @@ void PenLayer::createFbo()
455475 m_fbo.reset (newFbo);
456476 m_texture = Texture (m_fbo->texture (), m_fbo->size ());
457477 m_scale = width () / m_engine->stageWidth ();
478+
479+ if (oldCtx != m_glCtx) {
480+ m_glCtx->doneCurrent ();
481+ oldCtx->makeCurrent (oldSurface);
482+ }
458483}
459484
460485void PenLayer::updateTexture ()
0 commit comments