gr-fosphor/qt: Qt5 - Associate GL context to the worker thread

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
This commit is contained in:
Sylvain Munaut 2019-12-02 20:13:26 +01:00
parent ef55b4f5af
commit caa88f0a31
4 changed files with 48 additions and 6 deletions

View File

@ -19,6 +19,8 @@
*/
#include <QtEvents>
#include <QThread>
#include "QGLSurface.h"
#include "qt_sink_c_impl.h"
@ -30,7 +32,10 @@ namespace gr {
QGLSurface::QGLSurface(QWidget *parent, qt_sink_c_impl *block)
: QGLWidget(parent), d_block(block)
{
this->doneCurrent();
/* Save the pointer to the main GUI thread */
this->d_gui_thread = this->thread();
/* QWidget policies */
this->setFocusPolicy(Qt::StrongFocus);
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
@ -102,5 +107,33 @@ QGLSurface::keyPressEvent(QKeyEvent *ke)
}
}
void
QGLSurface::grabContext()
{
QMetaObject::invokeMethod(
this,
"giveContext",
Qt::BlockingQueuedConnection,
Q_ARG(QThread*, QThread::currentThread())
);
this->makeCurrent();
}
void
QGLSurface::releaseContext()
{
this->doneCurrent();
this->context()->moveToThread(this->d_gui_thread);
}
void
QGLSurface::giveContext(QThread *thread)
{
this->doneCurrent();
this->context()->moveToThread(thread);
}
} /* namespace fosphor */
} /* namespace gr */

View File

@ -23,6 +23,8 @@
#include <QGLWidget>
class QThread;
namespace gr {
namespace fosphor {
@ -33,14 +35,21 @@ namespace gr {
Q_OBJECT
qt_sink_c_impl *d_block;
QThread *d_gui_thread;
protected:
void paintEvent(QPaintEvent *pe);
void resizeEvent(QResizeEvent *re);
void keyPressEvent(QKeyEvent *ke);
private slots:
void giveContext(QThread *thread);
public:
QGLSurface(QWidget *parent, qt_sink_c_impl *d_block);
void grabContext();
void releaseContext();
};
} // namespace fosphor

View File

@ -64,7 +64,7 @@ qt_sink_c_impl::qt_sink_c_impl(QWidget *parent)
void
qt_sink_c_impl::glctx_init()
{
this->d_gui->makeCurrent();
this->d_gui->grabContext();
this->d_gui->setFocus();
}
@ -83,7 +83,7 @@ qt_sink_c_impl::glctx_poll()
void
qt_sink_c_impl::glctx_fini()
{
this->d_gui->doneCurrent();
this->d_gui->releaseContext();
}
void

View File

@ -26,11 +26,11 @@
#include "base_sink_c_impl.h"
class QGLWidget;
namespace gr {
namespace fosphor {
class QGLSurface;
/*!
* \brief Qt version of fosphor sink (implementation)
* \ingroup fosphor
@ -40,7 +40,7 @@ namespace gr {
friend class QGLSurface;
private:
QGLWidget *d_gui;
QGLSurface *d_gui;
protected:
/* Delegated implementation of GL context management */