From 4bd788f7153ced2403def6478af29da7d0e3cbd5 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 14 May 2019 20:25:27 -0700 Subject: [PATCH] Qt: Port to QOpenGLWidget --- src/platform/qt/Display.cpp | 4 ++- src/platform/qt/DisplayGL.cpp | 63 ++++++++++++++++++----------------- src/platform/qt/DisplayGL.h | 23 +++++-------- src/platform/qt/main.cpp | 10 ++++++ 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/platform/qt/Display.cpp b/src/platform/qt/Display.cpp index fd91993bd..ee754c4f0 100644 --- a/src/platform/qt/Display.cpp +++ b/src/platform/qt/Display.cpp @@ -18,13 +18,15 @@ Display::Driver Display::s_driver = Display::Driver::QT; Display* Display::create(QWidget* parent) { #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY) - QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer)); + QSurfaceFormat format; format.setSwapInterval(1); + format.setSwapBehavior(QSurfaceFormat::TripleBuffer); #endif switch (s_driver) { #if defined(BUILD_GL) || defined(BUILD_GLES2) || defined(USE_EPOXY) case Driver::OPENGL: + format.setVersion(3, 0); return new DisplayGL(format, parent); #endif #ifdef BUILD_GL diff --git a/src/platform/qt/DisplayGL.cpp b/src/platform/qt/DisplayGL.cpp index 2960e005f..81d01f960 100644 --- a/src/platform/qt/DisplayGL.cpp +++ b/src/platform/qt/DisplayGL.cpp @@ -10,8 +10,11 @@ #include "CoreController.h" #include +#include +#include #include #include +#include #include #include @@ -27,16 +30,17 @@ using namespace QGBA; -DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent) +DisplayGL::DisplayGL(const QSurfaceFormat& format, QWidget* parent) : Display(parent) , m_gl(nullptr) { // This can spontaneously re-enter into this->resizeEvent before creation is done, so we // need to make sure it's initialized to nullptr before we assign the new object to it - m_gl = new EmptyGLWidget(format, this); - m_painter = new PainterGL(format.majorVersion() < 2 ? 1 : m_gl->format().majorVersion(), &m_videoProxy, m_gl); - m_gl->setMouseTracking(true); - m_gl->setAttribute(Qt::WA_TransparentForMouseEvents); // This doesn't seem to work? + m_gl = new QOpenGLContext; + m_gl->setFormat(format); + m_gl->create(); + setAttribute(Qt::WA_NativeWindow); + m_painter = new PainterGL(&m_videoProxy, windowHandle(), m_gl); setUpdatesEnabled(false); // Prevent paint events, which can cause race conditions connect(&m_videoProxy, &VideoProxy::dataAvailable, &m_videoProxy, &VideoProxy::processData); @@ -46,6 +50,7 @@ DisplayGL::DisplayGL(const QGLFormat& format, QWidget* parent) DisplayGL::~DisplayGL() { stopDrawing(); delete m_painter; + delete m_gl; } bool DisplayGL::supportsShaders() const { @@ -71,11 +76,10 @@ void DisplayGL::startDrawing(std::shared_ptr controller) { m_painter->setMessagePainter(messagePainter()); m_context = controller; m_painter->resize(size()); - m_gl->move(0, 0); m_drawThread = new QThread(this); m_drawThread->setObjectName("Painter Thread"); - m_gl->context()->doneCurrent(); - m_gl->context()->moveToThread(m_drawThread); + m_gl->doneCurrent(); + m_gl->moveToThread(m_drawThread); m_painter->moveToThread(m_drawThread); m_videoProxy.moveToThread(m_drawThread); connect(m_drawThread, &QThread::started, m_painter, &PainterGL::start); @@ -100,7 +104,7 @@ void DisplayGL::stopDrawing() { m_drawThread->exit(); m_drawThread = nullptr; - m_gl->context()->makeCurrent(); + m_gl->makeCurrent(windowHandle()); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif @@ -185,9 +189,6 @@ void DisplayGL::resizeEvent(QResizeEvent* event) { } void DisplayGL::resizePainter() { - if (m_gl) { - m_gl->resize(size()); - } if (m_drawThread) { QMetaObject::invokeMethod(m_painter, "resize", Qt::BlockingQueuedConnection, Q_ARG(QSize, size())); } @@ -204,8 +205,9 @@ int DisplayGL::framebufferHandle() { return m_painter->glTex(); } -PainterGL::PainterGL(int majorVersion, VideoProxy* proxy, QGLWidget* parent) +PainterGL::PainterGL(VideoProxy* proxy, QWindow* surface, QOpenGLContext* parent) : m_gl(parent) + , m_surface(surface) , m_videoProxy(proxy) { #ifdef BUILD_GL @@ -215,10 +217,12 @@ PainterGL::PainterGL(int majorVersion, VideoProxy* proxy, QGLWidget* parent) mGLES2Context* gl2Backend; #endif - m_gl->makeCurrent(); + m_gl->makeCurrent(m_surface); + m_window = new QOpenGLPaintDevice; #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif + int majorVersion = m_gl->format().majorVersion(); QStringList extensions = QString(reinterpret_cast(glGetString(GL_EXTENSIONS))).split(' '); @@ -241,13 +245,13 @@ PainterGL::PainterGL(int majorVersion, VideoProxy* proxy, QGLWidget* parent) #endif m_backend->swap = [](VideoBackend* v) { PainterGL* painter = static_cast(v->user); - if (!painter->m_gl->isVisible()) { + if (!painter->m_gl->isValid()) { return; } - painter->m_gl->swapBuffers(); + painter->m_gl->swapBuffers(painter->m_gl->surface()); }; - m_backend->init(m_backend, reinterpret_cast(m_gl->winId())); + m_backend->init(m_backend, 0); #ifdef BUILD_GLES2 if (m_supportsShaders) { m_shader.preprocessShader = static_cast(&reinterpret_cast(m_backend)->initialShader); @@ -271,7 +275,7 @@ PainterGL::~PainterGL() { for (auto item : m_free) { delete[] item; } - m_gl->makeCurrent(); + m_gl->makeCurrent(m_surface); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif @@ -284,6 +288,7 @@ PainterGL::~PainterGL() { m_gl->doneCurrent(); free(m_backend); m_backend = nullptr; + delete m_window; } void PainterGL::setContext(std::shared_ptr context) { @@ -297,7 +302,7 @@ void PainterGL::resizeContext() { } if (!m_active) { - m_gl->makeCurrent(); + m_gl->makeCurrent(m_surface); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif @@ -316,11 +321,7 @@ void PainterGL::setMessagePainter(MessagePainter* messagePainter) { void PainterGL::resize(const QSize& size) { m_size = size; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) - float r = m_gl->devicePixelRatioF(); -#else - float r = m_gl->devicePixelRatio(); -#endif + float r = m_surface->devicePixelRatio(); m_backend->resized(m_backend, m_size.width() * r, m_size.height() * r); if (m_started && !m_active) { forceDraw(); @@ -345,7 +346,7 @@ void PainterGL::filter(bool filter) { } void PainterGL::start() { - m_gl->makeCurrent(); + m_gl->makeCurrent(m_surface); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif @@ -368,7 +369,7 @@ void PainterGL::draw() { if (mCoreSyncWaitFrameStart(&m_context->thread()->impl->sync) || !m_queue.isEmpty()) { dequeue(); mCoreSyncWaitFrameEnd(&m_context->thread()->impl->sync); - m_painter.begin(m_gl->context()->device()); + m_painter.begin(m_window); performDraw(); m_painter.end(); m_backend->swap(m_backend); @@ -389,7 +390,7 @@ void PainterGL::draw() { } void PainterGL::forceDraw() { - m_painter.begin(m_gl->context()->device()); + m_painter.begin(m_window); performDraw(); m_painter.end(); m_backend->swap(m_backend); @@ -402,7 +403,7 @@ void PainterGL::stop() { m_backend->clear(m_backend); m_backend->swap(m_backend); m_gl->doneCurrent(); - m_gl->context()->moveToThread(m_gl->thread()); + m_gl->moveToThread(m_surface->thread()); m_context.reset(); moveToThread(m_gl->thread()); m_videoProxy->moveToThread(m_gl->thread()); @@ -474,9 +475,9 @@ void PainterGL::setShaders(struct VDir* dir) { if (!supportsShaders()) { return; } - if (!m_active) { #ifdef BUILD_GLES2 - m_gl->makeCurrent(); + if (!m_active) { + m_gl->makeCurrent(m_surface); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif @@ -501,7 +502,7 @@ void PainterGL::clearShaders() { } if (!m_active) { #ifdef BUILD_GLES2 - m_gl->makeCurrent(); + m_gl->makeCurrent(m_surface); #if defined(_WIN32) && defined(USE_EPOXY) epoxy_handle_external_wglMakeCurrent(); #endif diff --git a/src/platform/qt/DisplayGL.h b/src/platform/qt/DisplayGL.h index 990269fd0..2dbd1f2f0 100644 --- a/src/platform/qt/DisplayGL.h +++ b/src/platform/qt/DisplayGL.h @@ -17,9 +17,10 @@ #endif #include -#include +#include #include #include +#include #include #include @@ -29,22 +30,12 @@ namespace QGBA { -class EmptyGLWidget : public QGLWidget { -public: - EmptyGLWidget(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent) { setAutoBufferSwap(false); } - -protected: - void paintEvent(QPaintEvent* event) override { event->ignore(); } - void resizeEvent(QResizeEvent*) override {} - void mouseMoveEvent(QMouseEvent* event) override { event->ignore(); } -}; - class PainterGL; class DisplayGL : public Display { Q_OBJECT public: - DisplayGL(const QGLFormat& format, QWidget* parent = nullptr); + DisplayGL(const QSurfaceFormat& format, QWidget* parent = nullptr); ~DisplayGL(); void startDrawing(std::shared_ptr) override; @@ -75,7 +66,7 @@ private: void resizePainter(); bool m_isDrawing = false; - QGLWidget* m_gl; + QOpenGLContext* m_gl; PainterGL* m_painter; QThread* m_drawThread = nullptr; std::shared_ptr m_context; @@ -86,7 +77,7 @@ class PainterGL : public QObject { Q_OBJECT public: - PainterGL(int majorVersion, VideoProxy* proxy, QGLWidget* parent); + PainterGL(VideoProxy* proxy, QWindow* surface, QOpenGLContext* parent); ~PainterGL(); void setContext(std::shared_ptr); @@ -123,7 +114,9 @@ private: QQueue m_queue; QPainter m_painter; QMutex m_mutex; - QGLWidget* m_gl; + QWindow* m_surface; + QPaintDevice* m_window; + QOpenGLContext* m_gl; bool m_active = false; bool m_started = false; std::shared_ptr m_context = nullptr; diff --git a/src/platform/qt/main.cpp b/src/platform/qt/main.cpp index 9c3fa5072..e2659eae3 100644 --- a/src/platform/qt/main.cpp +++ b/src/platform/qt/main.cpp @@ -17,6 +17,10 @@ #include #include +#ifdef BUILD_GLES2 +#include +#endif + #ifdef QT_STATIC #include #ifdef Q_OS_WIN @@ -65,6 +69,12 @@ int main(int argc, char* argv[]) { QApplication::setApplicationName(projectName); QApplication::setApplicationVersion(projectVersion); +#ifdef BUILD_GLES2 + QSurfaceFormat format; + format.setVersion(3, 0); + QSurfaceFormat::setDefaultFormat(format); +#endif + GBAApp application(argc, argv, &configController); #ifndef Q_OS_MAC