@@ -68,7 +68,14 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
68
68
69
69
m_engine = newEngine;
70
70
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) {
72
79
m_projectPenLayers[m_engine] = this ;
73
80
74
81
if (!m_painter)
@@ -428,6 +435,10 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)
428
435
429
436
QNanoQuickItemPainter *PenLayer::createItemPainter () const
430
437
{
438
+ m_glCtx = QOpenGLContext::currentContext ();
439
+ Q_ASSERT (m_glCtx);
440
+ m_surface = m_glCtx->surface ();
441
+ Q_ASSERT (m_surface);
431
442
return new PenLayerPainter;
432
443
}
433
444
@@ -441,9 +452,17 @@ void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeomet
441
452
442
453
void PenLayer::createFbo ()
443
454
{
444
- if (!QOpenGLContext::currentContext () || !m_engine)
455
+ if (!m_glCtx || !m_surface || ! m_engine || !m_glF )
445
456
return ;
446
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
+
447
466
QOpenGLFramebufferObjectFormat fboFormat;
448
467
fboFormat.setAttachment (QOpenGLFramebufferObject::CombinedDepthStencil);
449
468
@@ -456,6 +475,11 @@ void PenLayer::createFbo()
456
475
m_fbo.reset (newFbo);
457
476
m_texture = Texture (m_fbo->texture (), m_fbo->size ());
458
477
m_scale = width () / m_engine->stageWidth ();
478
+
479
+ if (oldCtx != m_glCtx) {
480
+ m_glCtx->doneCurrent ();
481
+ oldCtx->makeCurrent (oldSurface);
482
+ }
459
483
}
460
484
461
485
void PenLayer::updateTexture ()
0 commit comments