Skip to content

Fix HQ pen issues #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/penlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)

m_engine = newEngine;

if (m_engine && QOpenGLContext::currentContext()) {
if (!m_glCtx) {
m_glCtx = QOpenGLContext::currentContext();

if (m_glCtx)
m_surface = m_glCtx->surface();
}

if (m_engine && m_glCtx) {
m_projectPenLayers[m_engine] = this;
createFbo();

if (!m_painter)
m_painter = std::make_unique<QNanoPainter>();
Expand All @@ -80,6 +86,8 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
m_glF->initializeOpenGLFunctions();
}

createFbo();

if (m_vao == 0) {
// Set up VBO and VAO
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 };
Expand Down Expand Up @@ -124,8 +132,8 @@ void PenLayer::setHqPen(bool newHqPen)
return;

m_hqPen = newHqPen;
createFbo();
emit hqPenChanged();
createFbo();
}

void scratchcpprender::PenLayer::clear()
Expand Down Expand Up @@ -427,6 +435,10 @@ void PenLayer::addPenLayer(libscratchcpp::IEngine *engine, IPenLayer *penLayer)

QNanoQuickItemPainter *PenLayer::createItemPainter() const
{
m_glCtx = QOpenGLContext::currentContext();
Q_ASSERT(m_glCtx);
m_surface = m_glCtx->surface();
Q_ASSERT(m_surface);
return new PenLayerPainter;
}

Expand All @@ -440,9 +452,17 @@ void PenLayer::geometryChange(const QRectF &newGeometry, const QRectF &oldGeomet

void PenLayer::createFbo()
{
if (!QOpenGLContext::currentContext() || !m_engine)
if (!m_glCtx || !m_surface || !m_engine || !m_glF)
return;

QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
QSurface *oldSurface = oldCtx->surface();

if (oldCtx != m_glCtx) {
oldCtx->doneCurrent();
m_glCtx->makeCurrent(m_surface);
}

QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);

Expand All @@ -455,6 +475,11 @@ void PenLayer::createFbo()
m_fbo.reset(newFbo);
m_texture = Texture(m_fbo->texture(), m_fbo->size());
m_scale = width() / m_engine->stageWidth();

if (oldCtx != m_glCtx) {
m_glCtx->doneCurrent();
oldCtx->makeCurrent(oldSurface);
}
}

void PenLayer::updateTexture()
Expand Down
2 changes: 2 additions & 0 deletions src/penlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class PenLayer : public IPenLayer
libscratchcpp::IEngine *m_engine = nullptr;
bool m_hqPen = false;
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
mutable QOpenGLContext *m_glCtx = nullptr;
mutable QSurface *m_surface = nullptr;
double m_scale = 1;
std::unique_ptr<QNanoPainter> m_painter;
std::unique_ptr<QOpenGLExtraFunctions> m_glF;
Expand Down
25 changes: 25 additions & 0 deletions src/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
return;

if (m_engine) {
QOpenGLContext *oldCtx = QOpenGLContext::currentContext();
QSurface *oldSurface = nullptr;

if (!m_glCtx)
m_glCtx = oldCtx;

if (m_glCtx) {
if (!m_surface)
m_surface = m_glCtx->surface();

oldSurface = oldCtx->surface();

if (oldCtx != m_glCtx) {
oldCtx->doneCurrent();
m_glCtx->makeCurrent(m_surface);
}
}

for (Monitor *monitor : m_unpositionedMonitors)
monitor->autoPosition(m_engine->monitors());

Expand All @@ -230,6 +248,13 @@ void ProjectLoader::timerEvent(QTimerEvent *event)
m_renderTimer.restart();
} else
m_renderFpsCounter++;

if (m_glCtx) {
if (oldCtx != m_glCtx) {
m_glCtx->doneCurrent();
oldCtx->makeCurrent(oldSurface);
}
}
}

event->accept();
Expand Down
5 changes: 5 additions & 0 deletions src/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
Q_MOC_INCLUDE("spritemodel.h");
Q_MOC_INCLUDE("monitormodel.h");

class QSurface;
class QOpenGLContext;

namespace scratchcpprender
{

Expand Down Expand Up @@ -185,6 +188,8 @@ class ProjectLoader : public QObject
std::atomic<unsigned int> m_downloadedAssets = 0;
std::atomic<unsigned int> m_assetCount = 0;
std::atomic<bool> m_stopLoading = false;
QOpenGLContext *m_glCtx = nullptr;
QSurface *m_surface = nullptr;
};

} // namespace scratchcpprender
Binary file modified test/lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/lines_hq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions test/penlayer/penlayer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,11 @@ TEST_F(PenLayerTest, DrawLine)
penLayer.drawLine(attr, 130, 77, 125, -22);

attr.color = QNanoColor(0, 128, 0, 128);
attr.diameter = 10;
attr.diameter = 225;

penLayer.drawLine(attr, -225, 25, -175, -25);

penLayer.drawLine(attr, 152, -158, -228, 145);
attr.diameter = 10;
penLayer.drawLine(attr, -100, 139, 20, 72);

attr.color = QNanoColor(255, 50, 200, 185);
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/libqnanopainter/nanovg/nanovg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ void nvgStroke(NVGcontext* ctx)
{
NVGstate* state = nvg__getState(ctx);
float scale = nvg__getAverageScale(state->xform);
float strokeWidth = nvg__clampf(state->strokeWidth * scale, 0.0f, 200.0f);
float strokeWidth = state->strokeWidth * scale;
NVGpaint strokePaint = state->stroke;
const NVGpath* path;
int i;
Expand Down