2014-03-31 13:59:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2017-09-28 13:54:49 +00:00
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-03-31 13:59:18 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2017-09-28 13:54:49 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2014-03-31 13:59:18 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:35:35 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2014-03-31 13:59:18 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
#include "window_singlethreaded.h"
|
|
|
|
#include "cuberenderer.h"
|
2014-03-31 13:59:18 +00:00
|
|
|
#include <QOpenGLContext>
|
|
|
|
#include <QOpenGLFunctions>
|
|
|
|
#include <QOpenGLFramebufferObject>
|
|
|
|
#include <QOpenGLShaderProgram>
|
|
|
|
#include <QOpenGLVertexArrayObject>
|
|
|
|
#include <QOpenGLBuffer>
|
|
|
|
#include <QOffscreenSurface>
|
2015-04-16 13:41:26 +00:00
|
|
|
#include <QScreen>
|
2014-03-31 13:59:18 +00:00
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QQmlComponent>
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QQuickWindow>
|
|
|
|
#include <QQuickRenderControl>
|
|
|
|
#include <QCoreApplication>
|
2020-05-06 16:56:01 +00:00
|
|
|
#include <QQuickRenderTarget>
|
|
|
|
#include <QQuickGraphicsDevice>
|
2014-03-31 13:59:18 +00:00
|
|
|
|
2015-04-16 13:56:33 +00:00
|
|
|
class RenderControl : public QQuickRenderControl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RenderControl(QWindow *w) : m_window(w) { }
|
2017-09-20 18:57:39 +00:00
|
|
|
QWindow *renderWindow(QPoint *offset) override;
|
2015-04-16 13:56:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
QWindow *m_window;
|
|
|
|
};
|
|
|
|
|
|
|
|
QWindow *RenderControl::renderWindow(QPoint *offset)
|
|
|
|
{
|
|
|
|
if (offset)
|
|
|
|
*offset = QPoint(0, 0);
|
|
|
|
return m_window;
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
WindowSingleThreaded::WindowSingleThreaded()
|
2018-02-21 09:41:54 +00:00
|
|
|
: m_rootItem(nullptr),
|
2020-05-06 16:56:01 +00:00
|
|
|
m_textureId(0),
|
2014-03-31 13:59:18 +00:00
|
|
|
m_quickInitialized(false),
|
2015-04-16 13:41:26 +00:00
|
|
|
m_quickReady(false),
|
|
|
|
m_dpr(0)
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
|
|
|
setSurfaceType(QSurface::OpenGLSurface);
|
|
|
|
|
|
|
|
QSurfaceFormat format;
|
|
|
|
// Qt Quick may need a depth and stencil buffer. Always make sure these are available.
|
|
|
|
format.setDepthBufferSize(16);
|
|
|
|
format.setStencilBufferSize(8);
|
|
|
|
setFormat(format);
|
|
|
|
|
|
|
|
m_context = new QOpenGLContext;
|
|
|
|
m_context->setFormat(format);
|
|
|
|
m_context->create();
|
|
|
|
|
|
|
|
m_offscreenSurface = new QOffscreenSurface;
|
|
|
|
// Pass m_context->format(), not format. Format does not specify and color buffer
|
|
|
|
// sizes, while the context, that has just been created, reports a format that has
|
|
|
|
// these values filled in. Pass this to the offscreen surface to make sure it will be
|
|
|
|
// compatible with the context's configuration.
|
|
|
|
m_offscreenSurface->setFormat(m_context->format());
|
|
|
|
m_offscreenSurface->create();
|
|
|
|
|
2014-12-16 11:49:08 +00:00
|
|
|
m_cubeRenderer = new CubeRenderer(m_offscreenSurface);
|
2014-11-25 10:38:46 +00:00
|
|
|
|
2015-04-16 13:56:33 +00:00
|
|
|
m_renderControl = new RenderControl(this);
|
2014-03-31 13:59:18 +00:00
|
|
|
|
|
|
|
// Create a QQuickWindow that is associated with out render control. Note that this
|
|
|
|
// window never gets created or shown, meaning that it will never get an underlying
|
|
|
|
// native (platform) window.
|
|
|
|
m_quickWindow = new QQuickWindow(m_renderControl);
|
|
|
|
|
|
|
|
// Create a QML engine.
|
|
|
|
m_qmlEngine = new QQmlEngine;
|
|
|
|
if (!m_qmlEngine->incubationController())
|
|
|
|
m_qmlEngine->setIncubationController(m_quickWindow->incubationController());
|
|
|
|
|
|
|
|
// When Quick says there is a need to render, we will not render immediately. Instead,
|
|
|
|
// a timer with a small interval is used to get better performance.
|
|
|
|
m_updateTimer.setSingleShot(true);
|
|
|
|
m_updateTimer.setInterval(5);
|
2014-11-25 10:38:46 +00:00
|
|
|
connect(&m_updateTimer, &QTimer::timeout, this, &WindowSingleThreaded::render);
|
2014-03-31 13:59:18 +00:00
|
|
|
|
|
|
|
// Now hook up the signals. For simplicy we don't differentiate between
|
|
|
|
// renderRequested (only render is needed, no sync) and sceneChanged (polish and sync
|
|
|
|
// is needed too).
|
2020-05-06 16:56:01 +00:00
|
|
|
connect(m_quickWindow, &QQuickWindow::sceneGraphInitialized, this, &WindowSingleThreaded::createTexture);
|
|
|
|
connect(m_quickWindow, &QQuickWindow::sceneGraphInvalidated, this, &WindowSingleThreaded::destroyTexture);
|
2014-11-25 10:38:46 +00:00
|
|
|
connect(m_renderControl, &QQuickRenderControl::renderRequested, this, &WindowSingleThreaded::requestUpdate);
|
|
|
|
connect(m_renderControl, &QQuickRenderControl::sceneChanged, this, &WindowSingleThreaded::requestUpdate);
|
2015-04-16 13:41:26 +00:00
|
|
|
|
2020-05-06 16:56:01 +00:00
|
|
|
// Just recreating the texture on resize is not sufficient, when moving between screens
|
|
|
|
// with different devicePixelRatio the QWindow size may remain the same but the texture
|
2015-04-16 13:41:26 +00:00
|
|
|
// dimension is to change regardless.
|
|
|
|
connect(this, &QWindow::screenChanged, this, &WindowSingleThreaded::handleScreenChange);
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
WindowSingleThreaded::~WindowSingleThreaded()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2014-09-17 14:18:00 +00:00
|
|
|
// Make sure the context is current while doing cleanup. Note that we use the
|
|
|
|
// offscreen surface here because passing 'this' at this point is not safe: the
|
|
|
|
// underlying platform window may already be destroyed. To avoid all the trouble, use
|
|
|
|
// another surface that is valid for sure.
|
|
|
|
m_context->makeCurrent(m_offscreenSurface);
|
2014-03-31 13:59:18 +00:00
|
|
|
|
|
|
|
delete m_qmlComponent;
|
|
|
|
delete m_qmlEngine;
|
2020-05-06 16:56:01 +00:00
|
|
|
delete m_quickWindow;
|
|
|
|
delete m_renderControl;
|
|
|
|
|
|
|
|
if (m_textureId)
|
|
|
|
m_context->functions()->glDeleteTextures(1, &m_textureId);
|
2014-03-31 13:59:18 +00:00
|
|
|
|
|
|
|
m_context->doneCurrent();
|
|
|
|
|
2014-12-16 11:49:08 +00:00
|
|
|
delete m_cubeRenderer;
|
|
|
|
|
2014-03-31 13:59:18 +00:00
|
|
|
delete m_offscreenSurface;
|
|
|
|
delete m_context;
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:56:01 +00:00
|
|
|
void WindowSingleThreaded::createTexture()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2020-05-06 16:56:01 +00:00
|
|
|
// The scene graph has been initialized. It is now time to create an texture and associate
|
2014-03-31 13:59:18 +00:00
|
|
|
// it with the QQuickWindow.
|
2015-04-16 13:41:26 +00:00
|
|
|
m_dpr = devicePixelRatio();
|
2020-05-06 16:56:01 +00:00
|
|
|
m_textureSize = size() * m_dpr;
|
|
|
|
QOpenGLFunctions *f = m_context->functions();
|
|
|
|
f->glGenTextures(1, &m_textureId);
|
|
|
|
f->glBindTexture(GL_TEXTURE_2D, m_textureId);
|
|
|
|
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
f->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_textureSize.width(), m_textureSize.height(), 0,
|
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
2020-06-26 15:18:37 +00:00
|
|
|
m_quickWindow->setRenderTarget(QQuickRenderTarget::fromOpenGLTexture(m_textureId, m_textureSize));
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 16:56:01 +00:00
|
|
|
void WindowSingleThreaded::destroyTexture()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2020-05-06 16:56:01 +00:00
|
|
|
m_context->functions()->glDeleteTextures(1, &m_textureId);
|
|
|
|
m_textureId = 0;
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::render()
|
|
|
|
{
|
2020-05-06 16:56:01 +00:00
|
|
|
if (!m_context->makeCurrent(m_offscreenSurface))
|
2014-11-25 10:38:46 +00:00
|
|
|
return;
|
|
|
|
|
2020-05-06 16:56:01 +00:00
|
|
|
// Polish, synchronize and render the next frame (into our texture). In this example
|
2014-11-25 10:38:46 +00:00
|
|
|
// everything happens on the same thread and therefore all three steps are performed
|
|
|
|
// in succession from here. In a threaded setup the render() call would happen on a
|
|
|
|
// separate thread.
|
2020-05-06 16:56:01 +00:00
|
|
|
m_renderControl->beginFrame();
|
2014-11-25 10:38:46 +00:00
|
|
|
m_renderControl->polishItems();
|
|
|
|
m_renderControl->sync();
|
|
|
|
m_renderControl->render();
|
2020-05-06 16:56:01 +00:00
|
|
|
m_renderControl->endFrame();
|
2014-11-25 10:38:46 +00:00
|
|
|
|
|
|
|
QOpenGLFramebufferObject::bindDefault();
|
2014-12-16 11:56:00 +00:00
|
|
|
m_context->functions()->glFlush();
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
m_quickReady = true;
|
|
|
|
|
|
|
|
// Get something onto the screen.
|
2020-05-06 16:56:01 +00:00
|
|
|
m_cubeRenderer->render(this, m_context, m_quickReady ? m_textureId : 0);
|
2014-11-25 10:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowSingleThreaded::requestUpdate()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
|
|
|
if (!m_updateTimer.isActive())
|
|
|
|
m_updateTimer.start();
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::run()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2015-07-21 14:20:38 +00:00
|
|
|
disconnect(m_qmlComponent, &QQmlComponent::statusChanged, this, &WindowSingleThreaded::run);
|
2014-03-31 13:59:18 +00:00
|
|
|
|
|
|
|
if (m_qmlComponent->isError()) {
|
2016-08-18 15:03:47 +00:00
|
|
|
const QList<QQmlError> errorList = m_qmlComponent->errors();
|
|
|
|
for (const QQmlError &error : errorList)
|
2014-03-31 13:59:18 +00:00
|
|
|
qWarning() << error.url() << error.line() << error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QObject *rootObject = m_qmlComponent->create();
|
|
|
|
if (m_qmlComponent->isError()) {
|
2016-08-18 15:03:47 +00:00
|
|
|
const QList<QQmlError> errorList = m_qmlComponent->errors();
|
|
|
|
for (const QQmlError &error : errorList)
|
2014-03-31 13:59:18 +00:00
|
|
|
qWarning() << error.url() << error.line() << error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_rootItem = qobject_cast<QQuickItem *>(rootObject);
|
|
|
|
if (!m_rootItem) {
|
|
|
|
qWarning("run: Not a QQuickItem");
|
|
|
|
delete rootObject;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The root item is ready. Associate it with the window.
|
|
|
|
m_rootItem->setParentItem(m_quickWindow->contentItem());
|
|
|
|
|
|
|
|
// Update item and rendering related geometries.
|
|
|
|
updateSizes();
|
|
|
|
|
|
|
|
// Initialize the render control and our OpenGL resources.
|
2020-05-06 16:56:01 +00:00
|
|
|
m_context->makeCurrent(m_offscreenSurface);
|
|
|
|
m_quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromOpenGLContext(m_context));
|
|
|
|
m_renderControl->initialize();
|
2014-03-31 13:59:18 +00:00
|
|
|
m_quickInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::updateSizes()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
|
|
|
// Behave like SizeRootObjectToView.
|
|
|
|
m_rootItem->setWidth(width());
|
|
|
|
m_rootItem->setHeight(height());
|
|
|
|
|
|
|
|
m_quickWindow->setGeometry(0, 0, width(), height());
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
m_cubeRenderer->resize(width(), height());
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::startQuick(const QString &filename)
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
|
|
|
m_qmlComponent = new QQmlComponent(m_qmlEngine, QUrl(filename));
|
|
|
|
if (m_qmlComponent->isLoading())
|
2014-11-25 10:38:46 +00:00
|
|
|
connect(m_qmlComponent, &QQmlComponent::statusChanged, this, &WindowSingleThreaded::run);
|
2014-03-31 13:59:18 +00:00
|
|
|
else
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::exposeEvent(QExposeEvent *)
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
|
|
|
if (isExposed()) {
|
2016-02-04 19:27:40 +00:00
|
|
|
if (!m_quickInitialized) {
|
2020-05-06 16:56:01 +00:00
|
|
|
m_cubeRenderer->render(this, m_context, m_quickReady ? m_textureId : 0);
|
2014-03-31 13:59:18 +00:00
|
|
|
startQuick(QStringLiteral("qrc:/rendercontrol/demo.qml"));
|
2016-02-04 19:27:40 +00:00
|
|
|
}
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 16:56:01 +00:00
|
|
|
void WindowSingleThreaded::resizeTexture()
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2020-05-06 16:56:01 +00:00
|
|
|
if (m_rootItem && m_context->makeCurrent(m_offscreenSurface)) {
|
|
|
|
m_context->functions()->glDeleteTextures(1, &m_textureId);
|
|
|
|
m_textureId = 0;
|
|
|
|
createTexture();
|
2014-03-31 13:59:18 +00:00
|
|
|
m_context->doneCurrent();
|
|
|
|
updateSizes();
|
2015-04-16 10:47:42 +00:00
|
|
|
render();
|
2014-03-31 13:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 13:41:26 +00:00
|
|
|
void WindowSingleThreaded::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
2020-05-06 16:56:01 +00:00
|
|
|
// If this is a resize after the scene is up and running, recreate the texture and the
|
2015-04-16 13:41:26 +00:00
|
|
|
// Quick item and scene.
|
2020-05-06 16:56:01 +00:00
|
|
|
if (m_textureId && m_textureSize != size() * devicePixelRatio())
|
|
|
|
resizeTexture();
|
2015-04-16 13:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowSingleThreaded::handleScreenChange()
|
|
|
|
{
|
|
|
|
if (m_dpr != devicePixelRatio())
|
2020-05-06 16:56:01 +00:00
|
|
|
resizeTexture();
|
2015-04-16 13:41:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::mousePressEvent(QMouseEvent *e)
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2020-09-16 08:34:53 +00:00
|
|
|
// Use the constructor taking position and globalPosition. That puts position into the
|
|
|
|
// event's position and scenePosition, and globalPosition into the event's globalPosition. This way
|
|
|
|
// the scenePosition in e is ignored and is replaced by position. This is necessary
|
2014-03-31 13:59:18 +00:00
|
|
|
// because QQuickWindow thinks of itself as a top-level window always.
|
2020-06-08 07:21:58 +00:00
|
|
|
QMouseEvent mappedEvent(e->type(), e->position(), e->globalPosition(), e->button(), e->buttons(), e->modifiers());
|
2014-03-31 13:59:18 +00:00
|
|
|
QCoreApplication::sendEvent(m_quickWindow, &mappedEvent);
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:38:46 +00:00
|
|
|
void WindowSingleThreaded::mouseReleaseEvent(QMouseEvent *e)
|
2014-03-31 13:59:18 +00:00
|
|
|
{
|
2020-06-08 07:21:58 +00:00
|
|
|
QMouseEvent mappedEvent(e->type(), e->position(), e->globalPosition(), e->button(), e->buttons(), e->modifiers());
|
2014-03-31 13:59:18 +00:00
|
|
|
QCoreApplication::sendEvent(m_quickWindow, &mappedEvent);
|
|
|
|
}
|