#include "window_singlethreaded.h"
#include "cuberenderer.h"
#include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QOpenGLFramebufferObject>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOffscreenSurface>
#include <QScreen>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQuickItem>
#include <QQuickWindow>
#include <QQuickRenderControl>
#include <QCoreApplication>
class RenderControl : public QQuickRenderControl
{
public:
RenderControl(QWindow *w) : m_window(w) { }
QWindow *renderWindow(QPoint *offset) override;
private:
QWindow *m_window;
};
QWindow *RenderControl::renderWindow(QPoint *offset)
{
if (offset)
*offset = QPoint(0, 0);
return m_window;
}
WindowSingleThreaded::WindowSingleThreaded()
: m_rootItem(nullptr),
m_fbo(nullptr),
m_quickInitialized(false),
m_quickReady(false),
m_dpr(0)
{
setSurfaceType(QSurface::OpenGLSurface);
m_onscreen = QCoreApplication::arguments().contains(QStringLiteral("--onscreen"));
QSurfaceFormat format;
format.setDepthBufferSize(16);
format.setStencilBufferSize(8);
setFormat(format);
m_context = new QOpenGLContext;
m_context->setFormat(format);
m_context->create();
m_offscreenSurface = new QOffscreenSurface;
m_offscreenSurface->setFormat(m_context->format());
m_offscreenSurface->create();
m_cubeRenderer = new CubeRenderer(m_offscreenSurface);
m_renderControl = new RenderControl(this);
m_quickWindow = new QQuickWindow(m_renderControl);
m_qmlEngine = new QQmlEngine;
if (!m_qmlEngine->incubationController())
m_qmlEngine->setIncubationController(m_quickWindow->incubationController());
m_updateTimer.setSingleShot(true);
m_updateTimer.setInterval(5);
connect(&m_updateTimer, &QTimer::timeout, this, &WindowSingleThreaded::render);
connect(m_quickWindow, &QQuickWindow::sceneGraphInitialized, this, &WindowSingleThreaded::createFbo);
connect(m_quickWindow, &QQuickWindow::sceneGraphInvalidated, this, &WindowSingleThreaded::destroyFbo);
connect(m_renderControl, &QQuickRenderControl::renderRequested, this, &WindowSingleThreaded::requestUpdate);
connect(m_renderControl, &QQuickRenderControl::sceneChanged, this, &WindowSingleThreaded::requestUpdate);
connect(this, &QWindow::screenChanged, this, &WindowSingleThreaded::handleScreenChange);
}
WindowSingleThreaded::~WindowSingleThreaded()
{
m_context->makeCurrent(m_offscreenSurface);
delete m_renderControl;
delete m_qmlComponent;
delete m_quickWindow;
delete m_qmlEngine;
delete m_fbo;
m_context->doneCurrent();
delete m_cubeRenderer;
delete m_offscreenSurface;
delete m_context;
}
void WindowSingleThreaded::createFbo()
{
m_dpr = devicePixelRatio();
if (!m_onscreen) {
m_fbo = new QOpenGLFramebufferObject(size() * m_dpr, QOpenGLFramebufferObject::CombinedDepthStencil);
m_quickWindow->setRenderTarget(m_fbo);
} else {
m_onscreenSize = size() * m_dpr;
m_quickWindow->setRenderTarget(0, m_onscreenSize);
}
}
void WindowSingleThreaded::destroyFbo()
{
delete m_fbo;
m_fbo = nullptr;
}
void WindowSingleThreaded::render()
{
QSurface *surface = m_offscreenSurface;
if (m_onscreen)
surface = this;
if (!m_context->makeCurrent(surface))
return;
m_renderControl->polishItems();
m_renderControl->sync();
m_renderControl->render();
m_quickWindow->resetOpenGLState();
QOpenGLFramebufferObject::bindDefault();
m_context->functions()->glFlush();
m_quickReady = true;
if (!m_onscreen)
m_cubeRenderer->render(this, m_context, m_quickReady ? m_fbo->texture() : 0);
else
m_context->swapBuffers(this);
}
void WindowSingleThreaded::requestUpdate()
{
if (!m_updateTimer.isActive())
m_updateTimer.start();
}
void WindowSingleThreaded::run()
{
disconnect(m_qmlComponent, &QQmlComponent::statusChanged, this, &WindowSingleThreaded::run);
if (m_qmlComponent->isError()) {
const QList<QQmlError> errorList = m_qmlComponent->errors();
for (const QQmlError &error : errorList)
qWarning() << error.url() << error.line() << error;
return;
}
QObject *rootObject = m_qmlComponent->create();
if (m_qmlComponent->isError()) {
const QList<QQmlError> errorList = m_qmlComponent->errors();
for (const QQmlError &error : errorList)
qWarning() << error.url() << error.line() << error;
return;
}
m_rootItem = qobject_cast<QQuickItem *>(rootObject);
if (!m_rootItem) {
qWarning("run: Not a QQuickItem");
delete rootObject;
return;
}
m_rootItem->setParentItem(m_quickWindow->contentItem());
updateSizes();
QSurface *surface = m_offscreenSurface;
if (m_onscreen)
surface = this;
m_context->makeCurrent(surface);
m_renderControl->initialize(m_context);
m_quickInitialized = true;
}
void WindowSingleThreaded::updateSizes()
{
m_rootItem->setWidth(width());
m_rootItem->setHeight(height());
m_quickWindow->setGeometry(0, 0, width(), height());
m_cubeRenderer->resize(width(), height());
}
void WindowSingleThreaded::startQuick(const QString &filename)
{
m_qmlComponent = new QQmlComponent(m_qmlEngine, QUrl(filename));
if (m_qmlComponent->isLoading())
connect(m_qmlComponent, &QQmlComponent::statusChanged, this, &WindowSingleThreaded::run);
else
run();
}
void WindowSingleThreaded::exposeEvent(QExposeEvent *)
{
if (isExposed()) {
if (!m_quickInitialized) {
if (!m_onscreen)
m_cubeRenderer->render(this, m_context, m_quickReady ? m_fbo->texture() : 0);
startQuick(QStringLiteral("qrc:/rendercontrol/demo.qml"));
}
}
}
void WindowSingleThreaded::resizeFbo()
{
QSurface *surface = m_offscreenSurface;
if (m_onscreen)
surface = this;
if (m_rootItem && m_context->makeCurrent(surface)) {
delete m_fbo;
createFbo();
m_context->doneCurrent();
updateSizes();
render();
}
}
void WindowSingleThreaded::resizeEvent(QResizeEvent *)
{
if (!m_onscreen) {
if (m_fbo && m_fbo->size() != size() * devicePixelRatio())
resizeFbo();
} else {
if (m_onscreenSize != size() * devicePixelRatio())
resizeFbo();
}
}
void WindowSingleThreaded::handleScreenChange()
{
if (m_dpr != devicePixelRatio())
resizeFbo();
}
void WindowSingleThreaded::mousePressEvent(QMouseEvent *e)
{
QMouseEvent mappedEvent(e->type(), e->localPos(), e->screenPos(), e->button(), e->buttons(), e->modifiers());
QCoreApplication::sendEvent(m_quickWindow, &mappedEvent);
}
void WindowSingleThreaded::mouseReleaseEvent(QMouseEvent *e)
{
QMouseEvent mappedEvent(e->type(), e->localPos(), e->screenPos(), e->button(), e->buttons(), e->modifiers());
QCoreApplication::sendEvent(m_quickWindow, &mappedEvent);
}