Skip to content
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

Fix QNanoWidget OpenGL context usage #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions examples/gallery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ int main(int argc, char *argv[])
// load res from libqnanopainter
Q_INIT_RESOURCE(libqnanopainterdata);

QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

#ifdef Q_OS_WIN
// Select between OpenGL and OpenGL ES (Angle)
//QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
Expand Down
2 changes: 2 additions & 0 deletions examples/helloitem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ int main(int argc, char *argv[])
// load res from libqnanopainter
Q_INIT_RESOURCE(libqnanopainterdata);

QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

#ifdef Q_OS_WIN
// Select between OpenGL and OpenGL ES (Angle)
//QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
Expand Down
2 changes: 2 additions & 0 deletions examples/hellowidget/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int main(int argc, char *argv[])
// load res from libqnanopainter
Q_INIT_RESOURCE(libqnanopainterdata);

QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

#ifdef Q_OS_WIN
// Select between OpenGL and OpenGL ES (Angle)
//QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
Expand Down
2 changes: 2 additions & 0 deletions examples/hellowindow/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int main(int argc, char *argv[])
// load res from libqnanopainter
Q_INIT_RESOURCE(libqnanopainterdata);

QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

#ifdef Q_OS_WIN
// Select between OpenGL and OpenGL ES (Angle)
//QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
Expand Down
2 changes: 2 additions & 0 deletions examples/qnanopainter_vs_qpainter_demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ int main(int argc, char *argv[])
// load res from libqnanopainter
Q_INIT_RESOURCE(libqnanopainterdata);

QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

#ifdef Q_OS_WIN
// Select between OpenGL and OpenGL ES (Angle)
//QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
Expand Down
21 changes: 19 additions & 2 deletions libqnanopainter/qnanopainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ Q_GLOBAL_STATIC(QNanoPainter, instance)
QNanoPainter::QNanoPainter()
{
#ifndef QNANO_USE_RHI
// Save current OpenGL context so it can be restored later
QOpenGLContext *prevContext = QOpenGLContext::currentContext();

// Create new OpenGL context that can be owned by this QNanoPainter object
Q_ASSERT(QCoreApplication::testAttribute(Qt::AA_ShareOpenGLContexts));
m_openglContext.setShareContext(QOpenGLContext::globalShareContext());
m_openglContext.create();
m_openglSurface.setFormat(m_openglContext.format());
m_openglSurface.create();
m_openglContext.makeCurrent(&m_openglSurface);

// Request actual OpenGL context version and type
QOpenGLContext *context = QOpenGLContext::currentContext();
Expand All @@ -156,6 +166,10 @@ QNanoPainter::QNanoPainter()
m_nvgContext = m_backend->nvgCreate(NVG_ANTIALIAS);
Q_ASSERT_X(m_nvgContext, "QNanoPainter::QNanoPainter", "Could not init nanovg!");

// Restore previous OpenGL context
if (prevContext != nullptr)
prevContext->makeCurrent(prevContext->surface());

#else
m_backend.reset(QNanoBackendFactory::createBackend(0, 0, false));
#endif
Expand All @@ -171,9 +185,12 @@ QNanoPainter::QNanoPainter()

QNanoPainter::~QNanoPainter()
{
if (m_backend && m_nvgContext) {

#ifndef QNANO_USE_RHI
m_openglContext.makeCurrent(&m_openglSurface);
#endif

if (m_backend && m_nvgContext && QOpenGLContext::currentContext()) {
// Do NanoVG side cleanups only if OpenGL context still exists
m_backend->nvgDelete(m_nvgContext);
}

Expand Down
6 changes: 6 additions & 0 deletions libqnanopainter/qnanopainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <QTransform>
#include <QSharedPointer>
#include <QSurfaceFormat>
#include <QOpenGLContext>
#include <QOffscreenSurface>
#include "qnanocolor.h"
#include "private/qnanobrush.h"
#include "private/qnanodataelement.h"
Expand Down Expand Up @@ -299,6 +301,10 @@ class QNanoPainter
QSharedPointer<QNanoFont> m_defaultFont;
QString m_openglContextName;

#ifndef QNANO_USE_RHI
QOpenGLContext m_openglContext;
QOffscreenSurface m_openglSurface;
#endif
};

#endif // QNANOPAINTER_H