aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/items/qquickrendercontrol.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/quick/items/qquickrendercontrol.cpp')
-rw-r--r--src/quick/items/qquickrendercontrol.cpp265
1 files changed, 198 insertions, 67 deletions
diff --git a/src/quick/items/qquickrendercontrol.cpp b/src/quick/items/qquickrendercontrol.cpp
index caf5a17dc6..3a92507141 100644
--- a/src/quick/items/qquickrendercontrol.cpp
+++ b/src/quick/items/qquickrendercontrol.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtQuick module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** 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.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquickrendercontrol.h"
#include "qquickrendercontrol_p.h"
@@ -62,7 +26,7 @@
#include <QtCore/private/qobject_p.h>
#include <QtQuick/private/qquickwindow_p.h>
-#include <QtGui/private/qrhi_p.h>
+#include <rhi/qrhi.h>
QT_BEGIN_NAMESPACE
@@ -84,15 +48,19 @@ QT_BEGIN_NAMESPACE
possible in a hardware accelerated manner, unlike the performance-wise limited
alternative of using QQuickWindow::grabWindow()
- When using a QQuickRenderControl, the QQuickWindow does not have to be shown
- or even created at all. This means there will not be an underlying native
- window for it. Instead, the QQuickWindow instance is associated with the
- render control, using the overload of the QQuickWindow constructor, and a
- texture or image object specified via QQuickWindow::setRenderTarget().
+ When using a QQuickRenderControl, the QQuickWindow must not be
+ \l{QWindow::show()}{shown} (it will not be visible on-screen) and there will
+ not be an underlying native window for it. Instead, the QQuickWindow instance
+ is associated with the render control object, using the overload of the
+ QQuickWindow constructor, and a texture or image object specified via
+ QQuickWindow::setRenderTarget(). The QQuickWindow object is still essential,
+ because it represents the Qt Quick scene and provides the bulk of the scene
+ management and event delivery mechanisms. It does not however act as a real
+ on-screen window from the windowing system's perspective.
Management of the graphics devices, contexts, image and texture objects is up
to the application. The device or context that will be used by Qt Quick must
- be created before calling initialize(). The creation of the the texture object
+ be created before calling initialize(). The creation of the texture object
can be deferred, see below. Qt 5.4 introduces the ability for QOpenGLContext
to adopt existing native contexts. Together with QQuickRenderControl this
makes it possible to create a QOpenGLContext that shares with an external
@@ -130,6 +98,12 @@ QT_BEGIN_NAMESPACE
To send events, for example mouse or keyboard events, to the scene, use
QCoreApplication::sendEvent() with the QQuickWindow instance as the receiver.
+ For key events it may be also necessary to set the focus manually on the
+ desired item. In practice this involves calling
+ \l{QQuickItem::forceActiveFocus()}{forceActiveFocus()} on the desired item,
+ for example the scene's root item, once it is associated with the scene (the
+ QQuickWindow).
+
\note In general QQuickRenderControl is supported in combination with all Qt
Quick backends. However, some functionality, in particular grab(), may not be
available in all cases.
@@ -144,9 +118,11 @@ QQuickRenderControlPrivate::QQuickRenderControlPrivate(QQuickRenderControl *rend
initialized(false),
window(nullptr),
rhi(nullptr),
+ ownRhi(true),
cb(nullptr),
offscreenSurface(nullptr),
- sampleCount(1)
+ sampleCount(1),
+ frameStatus(NotRecordingFrame)
{
if (!sg) {
qAddPostRoutine(cleanup);
@@ -171,6 +147,14 @@ QQuickRenderControl::QQuickRenderControl(QObject *parent)
}
/*!
+ \internal
+*/
+QQuickRenderControl::QQuickRenderControl(QQuickRenderControlPrivate &dd, QObject * parent)
+ : QObject(dd, parent)
+{
+}
+
+/*!
Destroys the instance. Releases all scenegraph resources.
\sa invalidate()
@@ -181,8 +165,12 @@ QQuickRenderControl::~QQuickRenderControl()
invalidate();
- if (d->window)
- QQuickWindowPrivate::get(d->window)->renderControl = nullptr;
+ QQuickGraphicsConfiguration config;
+ if (d->window) {
+ QQuickWindowPrivate *wd = QQuickWindowPrivate::get(d->window);
+ wd->renderControl = nullptr;
+ config = wd->graphicsConfig;
+ }
// It is likely that the cleanup in windowDestroyed() is not called since
// the standard pattern is to destroy the rendercontrol before the QQuickWindow.
@@ -191,7 +179,12 @@ QQuickRenderControl::~QQuickRenderControl()
delete d->rc;
- d->resetRhi();
+ // Only call rhi related cleanup when we actually got to initialize() and
+ // managed to get a QRhi. The software backend for instance would mean
+ // using the rendercontrol without ever calling initialize() - it is then
+ // important to completely skip calling any QSGRhiSupport functions.
+ if (d->rhi)
+ d->resetRhi(config);
}
void QQuickRenderControlPrivate::windowDestroyed()
@@ -203,9 +196,11 @@ void QQuickRenderControlPrivate::windowDestroyed()
rc->invalidate();
QQuickWindowPrivate::get(window)->animationController.reset();
+
#if QT_CONFIG(quick_shadereffect)
- QSGRhiShaderEffectNode::cleanupMaterialTypeCache();
+ QSGRhiShaderEffectNode::resetMaterialTypeCache(window);
#endif
+
window = nullptr;
}
}
@@ -286,6 +281,17 @@ int QQuickRenderControl::samples() const
\note This function does not need to be, and must not be, called when using
the \c software adaptation of Qt Quick.
+ With the default Qt Quick adaptation this function creates a new \l QRhi
+ object, similarly to what would happen with an on-screen QQuickWindow when
+ QQuickRenderControl was not used. To make this new QRhi object adopt some
+ existing device or context resource (e.g. use an existing QOpenGLContext
+ instead of creating a new one), use QQuickWindow::setGraphicsDevice() as
+ mentioned above. When the application wants to make the Qt Quick rendering
+ use an already existing \l QRhi object, that is possible as well via
+ \l QQuickGraphicsDevice::fromRhi(). When such a QQuickGraphicsDevice,
+ referencing an already existing QRhi, is set, there will be no new,
+ dedicated \l QRhi object created in initialize().
+
\since 6.0
\sa QQuickRenderTarget, QQuickGraphicsDevice, QQuickGraphicsConfiguration::preferredInstanceExtensions()
@@ -312,6 +318,7 @@ bool QQuickRenderControl::initialize()
params.initialSurfacePixelSize = d->window->size() * d->window->effectiveDevicePixelRatio();
params.maybeSurface = d->window;
renderContext->initialize(&params);
+ d->initialized = true;
} else {
qWarning("QRhi is only compatible with default adaptation");
return false;
@@ -331,7 +338,7 @@ void QQuickRenderControl::polishItems()
return;
QQuickWindowPrivate *cd = QQuickWindowPrivate::get(d->window);
- cd->flushFrameSynchronousEvents();
+ cd->deliveryAgentPrivate()->flushFrameSynchronousEvents(d->window);
if (!d->window)
return;
cd->polishItems();
@@ -361,7 +368,8 @@ bool QQuickRenderControl::sync()
return false;
}
if (!d->cb) {
- qWarning("QQuickRenderControl cannot be used with QRhi when no QRhiCommandBuffer is provided");
+ qWarning("QQuickRenderControl cannot be used with QRhi when no QRhiCommandBuffer is provided "
+ "(perhaps beginFrame() was not called or it was unsuccessful?)");
return false;
}
cd->setCustomCommandBuffer(d->cb);
@@ -408,6 +416,7 @@ void QQuickRenderControl::invalidate()
// also essential to allow a subsequent initialize() to succeed.
d->rc->invalidate();
+ d->frameStatus = QQuickRenderControlPrivate::NotRecordingFrame;
d->initialized = false;
}
@@ -434,7 +443,7 @@ void QQuickRenderControl::render()
cd->setCustomCommandBuffer(d->cb);
}
- cd->renderSceneGraph(d->window->size());
+ cd->renderSceneGraph();
}
/*!
@@ -518,7 +527,7 @@ void QQuickRenderControlPrivate::maybeUpdate()
Reimplemented in subclasses to return the real window this render control
is rendering into.
- If \a offset in non-null, it is set to the offset of the control
+ If \a offset is non-null, it is set to the offset of the control
inside the window.
\note While not mandatory, reimplementing this function becomes essential for
@@ -530,7 +539,7 @@ void QQuickRenderControlPrivate::maybeUpdate()
/*!
Returns the real window that \a win is being rendered to, if any.
- If \a offset in non-null, it is set to the offset of the rendering
+ If \a offset is non-null, it is set to the offset of the rendering
inside its window.
*/
@@ -544,6 +553,24 @@ QWindow *QQuickRenderControl::renderWindowFor(QQuickWindow *win, QPoint *offset)
return nullptr;
}
+bool QQuickRenderControlPrivate::isRenderWindowFor(QQuickWindow *quickWin, const QWindow *renderWin)
+{
+ QQuickRenderControl *rc = QQuickWindowPrivate::get(quickWin)->renderControl;
+ if (rc)
+ return QQuickRenderControlPrivate::get(rc)->isRenderWindow(renderWin);
+ return false;
+}
+
+bool QQuickRenderControlPrivate::isRenderWindow(const QWindow *w)
+{
+ Q_Q(QQuickRenderControl);
+
+ if (window && w)
+ return q->renderWindowFor(window, nullptr) == w;
+
+ return false;
+}
+
/*!
\return the QQuickWindow this QQuickRenderControl is associated with.
@@ -560,6 +587,53 @@ QQuickWindow *QQuickRenderControl::window() const
}
/*!
+ \return the QRhi this QQuickRenderControl is associated with.
+
+ \note The QRhi exists only when initialize() has successfully completed.
+ Before that the return value is null.
+
+ \note This function is not applicable and returns null when using the
+ \c software adaptation of Qt Quick.
+
+ \since 6.6
+
+ \sa commandBuffer(), beginFrame(), endFrame()
+ */
+QRhi *QQuickRenderControl::rhi() const
+{
+ Q_D(const QQuickRenderControl);
+ return d->rhi;
+}
+
+/*!
+ \return the current command buffer.
+
+ Once beginFrame() is called, a QRhiCommandBuffer is set up automatically.
+ That is the command buffer Qt Quick scenegraph uses, but in some cases
+ applications may also want to query it, for example to issue resource
+ updates (for example, a texture readback).
+
+ The returned command buffer reference should only be used between
+ beginFrame() and endFrame(). There are specific exceptions, for example
+ calling
+ \l{QRhiCommandBuffer::lastCompletedGpuTime()}{lastCompletedGpuTime()} on
+ the command buffer right after endFrame(), but before the next
+ beginFrame(), is valid.
+
+ \note This function is not applicable and returns null when using the
+ \c software adaptation of Qt Quick.
+
+ \since 6.6
+
+ \sa rhi(), beginFrame(), endFrame()
+ */
+QRhiCommandBuffer *QQuickRenderControl::commandBuffer() const
+{
+ Q_D(const QQuickRenderControl);
+ return d->cb;
+}
+
+/*!
Specifies the start of a graphics frame. Calls to sync() or render() must
be enclosed by calls to beginFrame() and endFrame().
@@ -569,11 +643,11 @@ QQuickWindow *QQuickRenderControl::window() const
to the user of QQuickRenderControl to specify these points.
A typical update step, including initialization of rendering into an
- existing texture, could like like the following. The example snippet
+ existing texture, could look like the following. The example snippet
assumes Direct3D 11 but the same concepts apply other graphics APIs as
well.
- \badcode
+ \code
if (!m_quickInitialized) {
m_quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromDeviceAndContext(m_engine->device(), m_engine->context()));
@@ -595,6 +669,15 @@ QQuickWindow *QQuickRenderControl::window() const
m_renderControl->endFrame(); // Qt Quick's rendering commands are submitted to the device context here
\endcode
+ \note This function does not need to be, and must not be, called when using
+ the \c software adaptation of Qt Quick.
+
+ \note Internally beginFrame() and endFrame() invoke
+ \l{QRhi::}{beginOffscreenFrame()} and \l{QRhi::}{endOffscreenFrame()},
+ respectively. This implies that there must not be a frame (neither
+ offscreen, nor swapchain-based) being recorded on the QRhi when
+ this function is called.
+
\since 6.0
\sa endFrame(), initialize(), sync(), render(), QQuickGraphicsDevice, QQuickRenderTarget
@@ -602,12 +685,38 @@ QQuickWindow *QQuickRenderControl::window() const
void QQuickRenderControl::beginFrame()
{
Q_D(QQuickRenderControl);
- if (!d->rhi || d->rhi->isRecordingFrame())
+ if (!d->rhi) {
+ qWarning("QQuickRenderControl: No QRhi in beginFrame()");
+ return;
+ }
+ if (d->frameStatus == QQuickRenderControlPrivate::RecordingFrame) {
+ qWarning("QQuickRenderControl: beginFrame() must be followed by a call to endFrame() before calling beginFrame() again");
return;
+ }
+ if (d->rhi->isRecordingFrame()) {
+ qWarning("QQuickRenderControl: Attempted to beginFrame() while the QRhi is already recording a frame");
+ return;
+ }
emit d->window->beforeFrameBegin();
- d->rhi->beginOffscreenFrame(&d->cb);
+ QRhi::FrameOpResult result = d->rhi->beginOffscreenFrame(&d->cb);
+
+ switch (result) {
+ case QRhi::FrameOpSuccess:
+ case QRhi::FrameOpSwapChainOutOfDate:
+ d->frameStatus = QQuickRenderControlPrivate::RecordingFrame;
+ break;
+ case QRhi::FrameOpError:
+ d->frameStatus = QQuickRenderControlPrivate::ErrorInBeginFrame;
+ break;
+ case QRhi::FrameOpDeviceLost:
+ d->frameStatus = QQuickRenderControlPrivate::DeviceLostInBeginFrame;
+ break;
+ default:
+ d->frameStatus = QQuickRenderControlPrivate::NotRecordingFrame;
+ break;
+ }
}
/*!
@@ -618,6 +727,9 @@ void QQuickRenderControl::beginFrame()
scenegraph are submitted to the context or command queue, whichever is
applicable.
+ \note This function does not need to be, and must not be, called when using
+ the \c software adaptation of Qt Quick.
+
\since 6.0
\sa beginFrame(), initialize(), sync(), render(), QQuickGraphicsDevice, QQuickRenderTarget
@@ -625,11 +737,23 @@ void QQuickRenderControl::beginFrame()
void QQuickRenderControl::endFrame()
{
Q_D(QQuickRenderControl);
- if (!d->rhi || !d->rhi->isRecordingFrame())
+ if (!d->rhi) {
+ qWarning("QQuickRenderControl: No QRhi in endFrame()");
return;
+ }
+ if (d->frameStatus != QQuickRenderControlPrivate::RecordingFrame) {
+ qWarning("QQuickRenderControl: endFrame() must only be called after a successful beginFrame()");
+ return;
+ }
+ if (!d->rhi->isRecordingFrame()) {
+ qWarning("QQuickRenderControl: Attempted to endFrame() while the QRhi is not recording a frame");
+ return;
+ }
d->rhi->endOffscreenFrame();
- d->cb = nullptr;
+ // do not null out d->cb; this allows calling lastCompletedGpuTime() for example
+
+ d->frameStatus = QQuickRenderControlPrivate::NotRecordingFrame;
emit d->window->afterFrameEnd();
}
@@ -637,8 +761,10 @@ void QQuickRenderControl::endFrame()
bool QQuickRenderControlPrivate::initRhi()
{
// initialize() - invalidate() - initialize() uses the QRhi the first
- // initialize() created, so if already exists, we are done
- if (rhi)
+ // initialize() created, so if already exists, we are done. Does not apply
+ // when wrapping an externally created QRhi, because we may be associated
+ // with a new one now.
+ if (rhi && ownRhi)
return true;
QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
@@ -655,18 +781,23 @@ bool QQuickRenderControlPrivate::initRhi()
if (!offscreenSurface)
offscreenSurface = rhiSupport->maybeCreateOffscreenSurface(window);
- rhi = rhiSupport->createRhi(window, offscreenSurface);
- if (!rhi) {
+ QSGRhiSupport::RhiCreateResult result = rhiSupport->createRhi(window, offscreenSurface);
+ if (!result.rhi) {
qWarning("QQuickRenderControl: Failed to initialize QRhi");
return false;
}
+ rhi = result.rhi;
+ ownRhi = result.own;
+
return true;
}
-void QQuickRenderControlPrivate::resetRhi()
+void QQuickRenderControlPrivate::resetRhi(const QQuickGraphicsConfiguration &config)
{
- QSGRhiSupport::instance()->destroyRhi(rhi);
+ if (ownRhi)
+ QSGRhiSupport::instance()->destroyRhi(rhi, config);
+
rhi = nullptr;
delete offscreenSurface;