aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick/qquickrhiitem/tst_qquickrhiitem.cpp
blob: e6c5b079caf1aa207260e4ca9cdd0fbce2be254c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <qtest.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include <QQuickView>
#include <QSGRendererInterface>
#include <private/qsgrhisupport_p.h>
#include <private/qquickrhiitem_p.h>
#include "testrhiitem.h"

class tst_QQuickRhiItem : public QQmlDataTest
{
    Q_OBJECT

public:
    tst_QQuickRhiItem();

private slots:
    void initTestCase() override;
    void cleanupTestCase();
    void rhiItem();
    void properties();
};

tst_QQuickRhiItem::tst_QQuickRhiItem()
    : QQmlDataTest(QT_QMLTEST_DATADIR)
{
}

void tst_QQuickRhiItem::initTestCase()
{
    QQmlDataTest::initTestCase();
    qDebug() << "RHI backend:" << QSGRhiSupport::instance()->rhiBackendName();
}

void tst_QQuickRhiItem::cleanupTestCase()
{
}

void tst_QQuickRhiItem::rhiItem()
{
    if (QGuiApplication::platformName() == QLatin1String("offscreen")
        || QGuiApplication::platformName() == QLatin1String("minimal"))
    {
        QSKIP("Skipping on offscreen/minimal");
    }

    // Load up a scene that instantiates a TestRhiItem, which in turn
    // renders a triangle with QRhi into a QRhiTexture and then draws
    // a quad textured with it.

    QQuickView view;
    view.setSource(testFileUrl(QLatin1String("test.qml")));
    view.setResizeMode(QQuickView::SizeViewToRootObject);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    if (!QSGRendererInterface::isApiRhiBased(view.rendererInterface()->graphicsApi()))
        QSKIP("Scenegraph does not use QRhi, test is pointless");

    QImage result = view.grabWindow();
    QVERIFY(!result.isNull());

    const int tolerance = 5;

    // The result is a red triangle in the center of the view, confirm at least one pixel.
    QRgb a = result.pixel(result.width() / 2, result.height() / 2);
    QRgb b = QColor(Qt::red).rgb();
    QVERIFY(qAbs(qRed(a) - qRed(b)) <= tolerance
            && qAbs(qGreen(a) - qGreen(b)) <= tolerance
            && qAbs(qBlue(a) - qBlue(b)) <= tolerance);
}

void tst_QQuickRhiItem::properties()
{
    if (QGuiApplication::platformName() == QLatin1String("offscreen")
        || QGuiApplication::platformName() == QLatin1String("minimal"))
    {
        QSKIP("Skipping on offscreen/minimal");
    }

    QQuickView view;
    view.setSource(testFileUrl(QLatin1String("test.qml")));
    view.setResizeMode(QQuickView::SizeViewToRootObject);
    view.show();
    QVERIFY(QTest::qWaitForWindowExposed(&view));

    if (!QSGRendererInterface::isApiRhiBased(view.rendererInterface()->graphicsApi()))
        QSKIP("Scenegraph does not use QRhi, test is pointless");

    QQuickRhiItem *item = view.rootObject()->findChild<QQuickRhiItem *>("rhiitem");
    QVERIFY(item);

    view.grabWindow(); // to ensure there's a frame
    // not quite safe in theory (threads etc.) but we know it works in practice
    QQuickRhiItemPrivate *d = QQuickRhiItemPrivate::get(item);
    QVERIFY(d->node);
    TestRenderer *r = static_cast<TestRenderer *>(d->node->m_renderer.get());
    QVERIFY(r);
    QRhi *rhi = r->rhi();
    QVERIFY(rhi);

    QVERIFY(r->colorTexture());
    QVERIFY(!r->msaaColorBuffer());
    QVERIFY(r->depthStencilBuffer());
    QVERIFY(r->renderTarget());
    QCOMPARE(item->effectiveColorBufferSize(), r->colorTexture()->pixelSize());

    QCOMPARE(item->sampleCount(), 1);
    item->setSampleCount(4);

    view.grabWindow(); // just to ensure the render thread rendered with the changed properties

    if (rhi->supportedSampleCounts().contains(4)) {
        QVERIFY(!r->colorTexture());
        QVERIFY(r->msaaColorBuffer());
        QCOMPARE(r->msaaColorBuffer()->sampleCount(), 4);
        QCOMPARE(r->depthStencilBuffer()->sampleCount(), 4);
        QCOMPARE(item->effectiveColorBufferSize(), r->msaaColorBuffer()->pixelSize());
    }

    QCOMPARE(item->alphaBlending(), false);
    item->setAlphaBlending(true);

    QCOMPARE(item->isMirrorVerticallyEnabled(), false);
    item->setMirrorVertically(true);

    item->setSampleCount(1);
    item->setFixedColorBufferWidth(123);
    item->setFixedColorBufferHeight(456);
    view.grabWindow();
    QCOMPARE(r->colorTexture()->pixelSize(), QSize(123, 456));
    QCOMPARE(item->fixedColorBufferWidth(), 123);
    QCOMPARE(item->fixedColorBufferHeight(), 456);
    QCOMPARE(item->effectiveColorBufferSize(), QSize(123, 456));

    QImage result = view.grabWindow();
    QVERIFY(!result.isNull());
    const int tolerance = 5;
    QRgb a = result.pixel(result.width() / 2, result.height() / 2);
    QRgb b = QColor(Qt::red).rgb();
    QVERIFY(qAbs(qRed(a) - qRed(b)) <= tolerance
            && qAbs(qGreen(a) - qGreen(b)) <= tolerance
            && qAbs(qBlue(a) - qBlue(b)) <= tolerance);
}

#include "tst_qquickrhiitem.moc"

QTEST_MAIN(tst_QQuickRhiItem)