QQuickRhiItem API cleanup

Based on the API review. Also follow QRhiWidget for
symmetry.

Change-Id: Id04a0029ebeb9326b76b485414b58c83f384c466
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit 4d2248f89d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Laszlo Agocs 2024-01-09 12:53:45 +01:00 committed by Qt Cherry-pick Bot
parent fba6c6de70
commit defa67ef2d
6 changed files with 124 additions and 88 deletions

View File

@ -33,10 +33,10 @@ Item {
mirrorVertically: cbMirror.checked
sampleCount: cbMSAA.checked ? 4 : 1
explicitTextureWidth: cbSize.checked ? slSize.value.toFixed(0) : 0
explicitTextureHeight: cbSize.checked ? slSize.value.toFixed(0) : 0
fixedColorBufferWidth: cbSize.checked ? slSize.value.toFixed(0) : 0
fixedColorBufferHeight: cbSize.checked ? slSize.value.toFixed(0) : 0
alphaBlending: cbBlend.checked
textureFormat: rdFormatRGBA8.checked ? ExampleRhiItem.RGBA8
colorBufferFormat: rdFormatRGBA8.checked ? ExampleRhiItem.RGBA8
: rdFormatFP16.checked ? ExampleRhiItem.RGBA16F
: rdFormatFP32.checked ? ExampleRhiItem.RGBA32F
: ExampleRhiItem.RGB10A2
@ -159,7 +159,7 @@ Item {
CheckBox {
id: cbSize
text: qsTr("Use explicit size")
text: qsTr("Use fixed size")
checked: false
}
RowLayout {

View File

@ -77,8 +77,8 @@ QT_BEGIN_NAMESPACE
The size of the texture will by default adapt to the size of the item (with
the \l{QQuickWindow::effectiveDevicePixelRatio()}{device pixel ratio} taken
into account). If the item size changes, the texture is recreated with the
correct size. If a fixed size is preferred, set \l explicitTextureWidth and
\l explicitTextureHeight to non-zero values.
correct size. If a fixed size is preferred, set \l fixedColorBufferWidth and
\l fixedColorBufferHeight to non-zero values.
QQuickRhiItem is a \l{QSGTextureProvider}{texture provider} and can be used
directly in \l {ShaderEffect}{ShaderEffects} and other classes that consume
@ -200,7 +200,7 @@ void QQuickRhiItemNode::sync()
const int maxTexSize = m_rhi->resourceLimit(QRhi::TextureSizeMax);
QQuickRhiItemPrivate *itemD = m_item->d_func();
QSize newSize = QSize(itemD->explicitTextureWidth, itemD->explicitTextureHeight);
QSize newSize = QSize(itemD->fixedTextureWidth, itemD->fixedTextureHeight);
if (newSize.isEmpty())
newSize = QSize(int(m_item->width()), int(m_item->height())) * m_dpr;
@ -347,7 +347,7 @@ void QQuickRhiItemNode::sync()
if (newSize != itemD->effectiveTextureSize) {
itemD->effectiveTextureSize = newSize;
emit m_item->effectiveTextureSizeChanged();
emit m_item->effectiveColorBufferSizeChanged();
}
QRhiCommandBuffer *cb = queryCommandBuffer();
@ -403,6 +403,13 @@ QQuickRhiItem::QQuickRhiItem(QQuickItem *parent)
setFlag(ItemHasContents);
}
/*!
Destructor.
*/
QQuickRhiItem::~QQuickRhiItem()
{
}
/*!
\internal
*/
@ -423,7 +430,7 @@ QSGNode *QQuickRhiItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
if (!d->node->hasRenderer()) {
QQuickRhiItemRenderer *r = createRenderer();
if (r) {
r->data = d->node;
r->node = d->node;
d->node->setRenderer(r);
} else {
qWarning("No QQuickRhiItemRenderer was created; the item will not render");
@ -460,6 +467,14 @@ QSGNode *QQuickRhiItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
return n;
}
/*!
\reimp
*/
bool QQuickRhiItem::event(QEvent *e)
{
return QQuickItem::event(e);
}
/*!
\reimp
*/
@ -529,7 +544,7 @@ QSGTextureProvider *QQuickRhiItem::textureProvider() const
must not be used anymore. When the value changes, all color and
depth-stencil buffers are destroyed and recreated automatically, and
\l {QQuickRhiItemRenderer::}{initialize()} is invoked again. However, when
\l autoRenderTarget is \c false, it will be up to the application to
isAutoRenderTargetEnabled() is \c false, it will be up to the application to
manage this with regards to the depth-stencil buffer or additional color
buffers.
@ -565,7 +580,7 @@ void QQuickRhiItem::setSampleCount(int samples)
}
/*!
\property QQuickRhiItem::textureFormat
\property QQuickRhiItem::colorBufferFormat
This property controls the texture format for the texture used as the color
buffer. The default value is TextureFormat::RGBA8. QQuickRhiItem supports
@ -584,13 +599,13 @@ void QQuickRhiItem::setSampleCount(int samples)
creating new ones.
*/
QQuickRhiItem::TextureFormat QQuickRhiItem::textureFormat() const
QQuickRhiItem::TextureFormat QQuickRhiItem::colorBufferFormat() const
{
Q_D(const QQuickRhiItem);
return d->itemTextureFormat;
}
void QQuickRhiItem::setTextureFormat(TextureFormat format)
void QQuickRhiItem::setColorBufferFormat(TextureFormat format)
{
Q_D(QQuickRhiItem);
if (d->itemTextureFormat == format)
@ -611,16 +626,28 @@ void QQuickRhiItem::setTextureFormat(TextureFormat format)
d->rhiTextureFormat = QRhiTexture::RGB10A2;
break;
}
emit textureFormatChanged();
emit colorBufferFormatChanged();
update();
}
/*!
\property QQuickRhiItem::autoRenderTarget
\return the current automatic depth-stencil buffer and render target management setting.
This property controls if a depth-stencil QRhiRenderBuffer and a
QRhiTextureRenderTarget is created and maintained automatically by the
item. The default value is \c true.
By default this value is \c true.
\sa setAutoRenderTarget()
*/
bool QQuickRhiItem::isAutoRenderTargetEnabled() const
{
Q_D(const QQuickRhiItem);
return d->autoRenderTarget;
}
/*!
Controls if a depth-stencil QRhiRenderBuffer and a QRhiTextureRenderTarget
is created and maintained automatically by the item. The default value is
\c true. Call this function early on, for example from the derived class'
constructor, with \a enabled set to \c false to disable this.
In automatic mode, the size and sample count of the depth-stencil buffer
follows the color buffer texture's settings. In non-automatic mode,
@ -628,13 +655,6 @@ void QQuickRhiItem::setTextureFormat(TextureFormat format)
then up to the application's implementation of initialize() to take care of
setting up and managing these objects.
*/
bool QQuickRhiItem::isAutoRenderTargetEnabled() const
{
Q_D(const QQuickRhiItem);
return d->autoRenderTarget;
}
void QQuickRhiItem::setAutoRenderTarget(bool enabled)
{
Q_D(QQuickRhiItem);
@ -674,13 +694,14 @@ void QQuickRhiItem::setMirrorVertically(bool enable)
}
/*!
\property QQuickRhiItem::explicitTextureWidth
\property QQuickRhiItem::fixedColorBufferWidth
The fixed width, in pixels, of the item's associated texture. Relevant when
a fixed texture size is desired that does not depend on the item's size.
This size has no effect on the geometry of the item (its size and placement
within the scene), which means the texture's content will appear stretched
(scaled up) or scaled down onto the item's area.
The fixed width, in pixels, of the item's associated texture or
renderbuffer. Relevant when a fixed color buffer size is desired that does
not depend on the item's size. This size has no effect on the geometry of
the item (its size and placement within the scene), which means the
texture's content will appear stretched (scaled up) or scaled down onto the
item's area.
For example, setting a size that is exactly twice the item's (pixel) size
effectively performs 2x supersampling (rendering at twice the resolution
@ -691,25 +712,25 @@ void QQuickRhiItem::setMirrorVertically(bool enable)
follows the item's size. (\c{texture size} = \c{item size} * \c{device
pixel ratio}).
*/
int QQuickRhiItem::explicitTextureWidth() const
int QQuickRhiItem::fixedColorBufferWidth() const
{
Q_D(const QQuickRhiItem);
return d->explicitTextureWidth;
return d->fixedTextureWidth;
}
void QQuickRhiItem::setExplicitTextureWidth(int width)
void QQuickRhiItem::setFixedColorBufferWidth(int width)
{
Q_D(QQuickRhiItem);
if (d->explicitTextureWidth == width)
if (d->fixedTextureWidth == width)
return;
d->explicitTextureWidth = width;
emit explicitTextureWidthChanged();
d->fixedTextureWidth = width;
emit fixedColorBufferWidthChanged();
update();
}
/*!
\property QQuickRhiItem::explicitTextureHeight
\property QQuickRhiItem::fixedColorBufferHeight
The fixed height, in pixels, of the item's associated texture. Relevant when
a fixed texture size is desired that does not depend on the item's size.
@ -727,25 +748,25 @@ void QQuickRhiItem::setExplicitTextureWidth(int width)
pixel ratio}).
*/
int QQuickRhiItem::explicitTextureHeight() const
int QQuickRhiItem::fixedColorBufferHeight() const
{
Q_D(const QQuickRhiItem);
return d->explicitTextureHeight;
return d->fixedTextureHeight;
}
void QQuickRhiItem::setExplicitTextureHeight(int height)
void QQuickRhiItem::setFixedColorBufferHeight(int height)
{
Q_D(QQuickRhiItem);
if (d->explicitTextureHeight == height)
if (d->fixedTextureHeight == height)
return;
d->explicitTextureHeight = height;
emit explicitTextureHeightChanged();
d->fixedTextureHeight = height;
emit fixedColorBufferHeightChanged();
update();
}
/*!
\property QQuickRhiItem::effectiveTextureSize
\property QQuickRhiItem::effectiveColorBufferSize
This property exposes the size, in pixels, of the underlying color buffer
(the QRhiTexture or QRhiRenderBuffer). It is provided for use on the GUI
@ -756,10 +777,16 @@ void QQuickRhiItem::setExplicitTextureHeight(int height)
size from the
\l{QQuickRhiItemRenderer::renderTarget()}{render target}.
\note The value becomes available asynchronously from the main thread's
perspective in the sense that the value changes when rendering happens on
the render thread. This means that this property is useful mainly in QML
bindings. Application code must not assume that the value is up to date
already when the QQuickRhiItem object is constructed.
This is a read-only property.
*/
QSize QQuickRhiItem::effectiveTextureSize() const
QSize QQuickRhiItem::effectiveColorBufferSize() const
{
Q_D(const QQuickRhiItem);
return d->effectiveTextureSize;
@ -857,8 +884,8 @@ QQuickRhiItemRenderer::~QQuickRhiItemRenderer()
*/
void QQuickRhiItemRenderer::update()
{
if (data)
static_cast<QQuickRhiItemNode *>(data)->scheduleUpdate();
if (node)
node->scheduleUpdate();
}
/*!
@ -868,7 +895,7 @@ void QQuickRhiItemRenderer::update()
*/
QRhi *QQuickRhiItemRenderer::rhi() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_rhi : nullptr;
return node ? node->m_rhi : nullptr;
}
/*!
@ -893,7 +920,7 @@ QRhi *QQuickRhiItemRenderer::rhi() const
*/
QRhiTexture *QQuickRhiItemRenderer::colorTexture() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_colorTexture : nullptr;
return node ? node->m_colorTexture : nullptr;
}
/*!
@ -935,7 +962,7 @@ QRhiTexture *QQuickRhiItemRenderer::colorTexture() const
*/
QRhiRenderBuffer *QQuickRhiItemRenderer::msaaColorBuffer() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_msaaColorBuffer.get() : nullptr;
return node ? node->m_msaaColorBuffer.get() : nullptr;
}
/*!
@ -958,7 +985,7 @@ QRhiRenderBuffer *QQuickRhiItemRenderer::msaaColorBuffer() const
*/
QRhiTexture *QQuickRhiItemRenderer::resolveTexture() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_resolveTexture : nullptr;
return node ? node->m_resolveTexture : nullptr;
}
/*!
@ -975,7 +1002,7 @@ QRhiTexture *QQuickRhiItemRenderer::resolveTexture() const
*/
QRhiRenderBuffer *QQuickRhiItemRenderer::depthStencilBuffer() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_depthStencilBuffer.get() : nullptr;
return node ? node->m_depthStencilBuffer.get() : nullptr;
}
/*!
@ -1008,7 +1035,7 @@ QRhiRenderBuffer *QQuickRhiItemRenderer::depthStencilBuffer() const
*/
QRhiRenderTarget *QQuickRhiItemRenderer::renderTarget() const
{
return data ? static_cast<QQuickRhiItemNode *>(data)->m_renderTarget.get() : nullptr;
return node ? node->m_renderTarget.get() : nullptr;
}
/*!

View File

@ -10,18 +10,20 @@ QT_BEGIN_NAMESPACE
class QQuickRhiItem;
class QQuickRhiItemPrivate;
class QQuickRhiItemNode;
class QRhi;
class QRhiCommandBuffer;
class QRhiTexture;
class QRhiRenderBuffer;
class QRhiRenderTarget;
class Q_QUICK_EXPORT QQuickRhiItemRenderer : public QObject
class Q_QUICK_EXPORT QQuickRhiItemRenderer
{
public:
QQuickRhiItemRenderer();
virtual ~QQuickRhiItemRenderer();
protected:
virtual void initialize(QRhiCommandBuffer *cb) = 0;
virtual void synchronize(QQuickRhiItem *item) = 0;
virtual void render(QRhiCommandBuffer *cb) = 0;
@ -36,8 +38,11 @@ public:
QRhiRenderTarget *renderTarget() const;
private:
void *data;
QQuickRhiItemNode *node;
friend class QQuickRhiItem;
friend class QQuickRhiItemNode;
Q_DISABLE_COPY_MOVE(QQuickRhiItemRenderer)
};
class Q_QUICK_EXPORT QQuickRhiItem : public QQuickItem
@ -46,13 +51,12 @@ class Q_QUICK_EXPORT QQuickRhiItem : public QQuickItem
Q_DECLARE_PRIVATE(QQuickRhiItem)
Q_PROPERTY(int sampleCount READ sampleCount WRITE setSampleCount NOTIFY sampleCountChanged FINAL)
Q_PROPERTY(TextureFormat textureFormat READ textureFormat WRITE setTextureFormat NOTIFY textureFormatChanged FINAL)
Q_PROPERTY(bool autoRenderTarget READ isAutoRenderTargetEnabled WRITE setAutoRenderTarget NOTIFY autoRenderTargetChanged FINAL)
Q_PROPERTY(TextureFormat colorBufferFormat READ colorBufferFormat WRITE setColorBufferFormat NOTIFY colorBufferFormatChanged FINAL)
Q_PROPERTY(bool mirrorVertically READ isMirrorVerticallyEnabled WRITE setMirrorVertically NOTIFY mirrorVerticallyChanged FINAL)
Q_PROPERTY(bool alphaBlending READ alphaBlending WRITE setAlphaBlending NOTIFY alphaBlendingChanged FINAL)
Q_PROPERTY(int explicitTextureWidth READ explicitTextureWidth WRITE setExplicitTextureWidth NOTIFY explicitTextureWidthChanged FINAL)
Q_PROPERTY(int explicitTextureHeight READ explicitTextureHeight WRITE setExplicitTextureHeight NOTIFY explicitTextureHeightChanged FINAL)
Q_PROPERTY(QSize effectiveTextureSize READ effectiveTextureSize NOTIFY effectiveTextureSizeChanged FINAL)
Q_PROPERTY(int fixedColorBufferWidth READ fixedColorBufferWidth WRITE setFixedColorBufferWidth NOTIFY fixedColorBufferWidthChanged FINAL)
Q_PROPERTY(int fixedColorBufferHeight READ fixedColorBufferHeight WRITE setFixedColorBufferHeight NOTIFY fixedColorBufferHeightChanged FINAL)
Q_PROPERTY(QSize effectiveColorBufferSize READ effectiveColorBufferSize NOTIFY effectiveColorBufferSizeChanged FINAL)
public:
enum class TextureFormat {
@ -64,15 +68,13 @@ public:
Q_ENUM(TextureFormat)
QQuickRhiItem(QQuickItem *parent = nullptr);
~QQuickRhiItem();
int sampleCount() const;
void setSampleCount(int samples);
TextureFormat textureFormat() const;
void setTextureFormat(TextureFormat format);
bool isAutoRenderTargetEnabled() const;
void setAutoRenderTarget(bool enabled);
TextureFormat colorBufferFormat() const;
void setColorBufferFormat(TextureFormat format);
bool isMirrorVerticallyEnabled() const;
void setMirrorVertically(bool enable);
@ -80,27 +82,31 @@ public:
bool alphaBlending() const;
void setAlphaBlending(bool enable);
int explicitTextureWidth() const;
void setExplicitTextureWidth(int width);
int explicitTextureHeight() const;
void setExplicitTextureHeight(int height);
int fixedColorBufferWidth() const;
void setFixedColorBufferWidth(int width);
int fixedColorBufferHeight() const;
void setFixedColorBufferHeight(int height);
QSize effectiveTextureSize() const;
virtual QQuickRhiItemRenderer *createRenderer() = 0;
QSize effectiveColorBufferSize() const;
Q_SIGNALS:
void sampleCountChanged();
void textureFormatChanged();
void colorBufferFormatChanged();
void autoRenderTargetChanged();
void mirrorVerticallyChanged();
void alphaBlendingChanged();
void explicitTextureWidthChanged();
void explicitTextureHeightChanged();
void effectiveTextureSizeChanged();
void fixedColorBufferWidthChanged();
void fixedColorBufferHeightChanged();
void effectiveColorBufferSizeChanged();
protected:
virtual QQuickRhiItemRenderer *createRenderer() = 0;
bool isAutoRenderTargetEnabled() const;
void setAutoRenderTarget(bool enabled);
QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override;
bool event(QEvent *) override;
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
void releaseResources() override;
bool isTextureProvider() const override;

View File

@ -91,8 +91,8 @@ public:
bool autoRenderTarget = true;
bool mirrorVertically = false;
bool blend = false;
int explicitTextureWidth = 0;
int explicitTextureHeight = 0;
int fixedTextureWidth = 0;
int fixedTextureHeight = 0;
QSize effectiveTextureSize;
};

View File

@ -9,7 +9,7 @@
class TestRenderer : public QQuickRhiItemRenderer
{
public:
protected:
void initialize(QRhiCommandBuffer *cb) override;
void synchronize(QQuickRhiItem *item) override;
void render(QRhiCommandBuffer *cb) override;
@ -35,6 +35,8 @@ private:
void initScene();
void updateTexture();
friend class tst_QQuickRhiItem;
};
class TestRhiItem : public QQuickRhiItem

View File

@ -7,6 +7,7 @@
#include <QSGRendererInterface>
#include <private/qsgrhisupport_p.h>
#include <private/qquickrhiitem_p.h>
#include "testrhiitem.h"
class tst_QQuickRhiItem : public QQmlDataTest
{
@ -95,7 +96,7 @@ void tst_QQuickRhiItem::properties()
// not quite safe in theory (threads etc.) but we know it works in practice
QQuickRhiItemPrivate *d = QQuickRhiItemPrivate::get(item);
QVERIFY(d->node);
QQuickRhiItemRenderer *r = d->node->m_renderer.get();
TestRenderer *r = static_cast<TestRenderer *>(d->node->m_renderer.get());
QVERIFY(r);
QRhi *rhi = r->rhi();
QVERIFY(rhi);
@ -104,7 +105,7 @@ void tst_QQuickRhiItem::properties()
QVERIFY(!r->msaaColorBuffer());
QVERIFY(r->depthStencilBuffer());
QVERIFY(r->renderTarget());
QCOMPARE(item->effectiveTextureSize(), r->colorTexture()->pixelSize());
QCOMPARE(item->effectiveColorBufferSize(), r->colorTexture()->pixelSize());
QCOMPARE(item->sampleCount(), 1);
item->setSampleCount(4);
@ -116,7 +117,7 @@ void tst_QQuickRhiItem::properties()
QVERIFY(r->msaaColorBuffer());
QCOMPARE(r->msaaColorBuffer()->sampleCount(), 4);
QCOMPARE(r->depthStencilBuffer()->sampleCount(), 4);
QCOMPARE(item->effectiveTextureSize(), r->msaaColorBuffer()->pixelSize());
QCOMPARE(item->effectiveColorBufferSize(), r->msaaColorBuffer()->pixelSize());
}
QCOMPARE(item->alphaBlending(), false);
@ -126,13 +127,13 @@ void tst_QQuickRhiItem::properties()
item->setMirrorVertically(true);
item->setSampleCount(1);
item->setExplicitTextureWidth(123);
item->setExplicitTextureHeight(456);
item->setFixedColorBufferWidth(123);
item->setFixedColorBufferHeight(456);
view.grabWindow();
QCOMPARE(r->colorTexture()->pixelSize(), QSize(123, 456));
QCOMPARE(item->explicitTextureWidth(), 123);
QCOMPARE(item->explicitTextureHeight(), 456);
QCOMPARE(item->effectiveTextureSize(), QSize(123, 456));
QCOMPARE(item->fixedColorBufferWidth(), 123);
QCOMPARE(item->fixedColorBufferHeight(), 456);
QCOMPARE(item->effectiveColorBufferSize(), QSize(123, 456));
QImage result = view.grabWindow();
QVERIFY(!result.isNull());