#include "threadrenderer.h"
#include "logorenderer.h"
#include <QtCore/QMutex>
#include <QtCore/QThread>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFramebufferObject>
#include <QtGui/QGuiApplication>
#include <QtGui/QOffscreenSurface>
#include <QtQuick/QQuickWindow>
#include <qsgsimpletexturenode.h>
QList<QThread *> ThreadRenderer::threads;
class RenderThread : public QThread
{
Q_OBJECT
public:
RenderThread(const QSize &size)
: surface(nullptr)
, context(nullptr)
, m_renderFbo(nullptr)
, m_displayFbo(nullptr)
, m_logoRenderer(nullptr)
, m_size(size)
{
ThreadRenderer::threads << this;
}
QOffscreenSurface *surface;
QOpenGLContext *context;
public slots:
void renderNext()
{
context->makeCurrent(surface);
if (!m_renderFbo) {
QOpenGLFramebufferObjectFormat format;
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
m_renderFbo = new QOpenGLFramebufferObject(m_size, format);
m_displayFbo = new QOpenGLFramebufferObject(m_size, format);
m_logoRenderer = new LogoRenderer();
m_logoRenderer->initialize();
}
m_renderFbo->bind();
context->functions()->glViewport(0, 0, m_size.width(), m_size.height());
m_logoRenderer->render();
context->functions()->glFlush();
m_renderFbo->bindDefault();
qSwap(m_renderFbo, m_displayFbo);
emit textureReady(m_displayFbo->texture(), m_size);
}
void shutDown()
{
context->makeCurrent(surface);
delete m_renderFbo;
delete m_displayFbo;
delete m_logoRenderer;
context->doneCurrent();
delete context;
surface->deleteLater();
exit();
moveToThread(QGuiApplication::instance()->thread());
}
signals:
void textureReady(int id, const QSize &size);
private:
QOpenGLFramebufferObject *m_renderFbo;
QOpenGLFramebufferObject *m_displayFbo;
LogoRenderer *m_logoRenderer;
QSize m_size;
};
class TextureNode : public QObject, public QSGSimpleTextureNode
{
Q_OBJECT
public:
TextureNode(QQuickWindow *window)
: m_id(0)
, m_size(0, 0)
, m_texture(nullptr)
, m_window(window)
{
m_texture = m_window->createTextureFromId(0, QSize(1, 1));
setTexture(m_texture);
setFiltering(QSGTexture::Linear);
}
~TextureNode() override
{
delete m_texture;
}
signals:
void textureInUse();
void pendingNewTexture();
public slots:
void newTexture(int id, const QSize &size) {
m_mutex.lock();
m_id = id;
m_size = size;
m_mutex.unlock();
emit pendingNewTexture();
}
void prepareNode() {
m_mutex.lock();
int newId = m_id;
QSize size = m_size;
m_id = 0;
m_mutex.unlock();
if (newId) {
delete m_texture;
m_texture = m_window->createTextureFromId(newId, size);
setTexture(m_texture);
markDirty(DirtyMaterial);
emit textureInUse();
}
}
private:
int m_id;
QSize m_size;
QMutex m_mutex;
QSGTexture *m_texture;
QQuickWindow *m_window;
};
ThreadRenderer::ThreadRenderer()
: m_renderThread(nullptr)
{
setFlag(ItemHasContents, true);
m_renderThread = new RenderThread(QSize(512, 512));
}
void ThreadRenderer::ready()
{
m_renderThread->surface = new QOffscreenSurface();
m_renderThread->surface->setFormat(m_renderThread->context->format());
m_renderThread->surface->create();
m_renderThread->moveToThread(m_renderThread);
connect(window(), &QQuickWindow::sceneGraphInvalidated, m_renderThread, &RenderThread::shutDown, Qt::QueuedConnection);
m_renderThread->start();
update();
}
QSGNode *ThreadRenderer::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
TextureNode *node = static_cast<TextureNode *>(oldNode);
if (!m_renderThread->context) {
QOpenGLContext *current = window()->openglContext();
current->doneCurrent();
m_renderThread->context = new QOpenGLContext();
m_renderThread->context->setFormat(current->format());
m_renderThread->context->setShareContext(current);
m_renderThread->context->create();
m_renderThread->context->moveToThread(m_renderThread);
current->makeCurrent(window());
QMetaObject::invokeMethod(this, "ready");
return nullptr;
}
if (!node) {
node = new TextureNode(window());
connect(m_renderThread, &RenderThread::textureReady, node, &TextureNode::newTexture, Qt::DirectConnection);
connect(node, &TextureNode::pendingNewTexture, window(), &QQuickWindow::update, Qt::QueuedConnection);
connect(window(), &QQuickWindow::beforeRendering, node, &TextureNode::prepareNode, Qt::DirectConnection);
connect(node, &TextureNode::textureInUse, m_renderThread, &RenderThread::renderNext, Qt::QueuedConnection);
QMetaObject::invokeMethod(m_renderThread, "renderNext", Qt::QueuedConnection);
}
node->setRect(boundingRect());
return node;
}
#include "threadrenderer.moc"