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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QSignalSpy>
#include <QQuickView>
#include <QQmlEngine>
#include <QQmlComponent>
#include <rhi/qrhi.h>
#include "../shared/util.h"
class tst_MultiWindow : public QQuick3DDataTest
{
Q_OBJECT
private slots:
void initTestCase() override;
void separateCubeSceneInMultipleWindows_data();
void separateCubeSceneInMultipleWindows();
void cubeMultiViewportSameWindow();
void cubeMultiViewportMultiWindow();
void cubeMultiWindow();
};
void tst_MultiWindow::initTestCase()
{
QQuick3DDataTest::initTestCase();
if (!initialized())
return;
}
const int FUZZ = 5;
void tst_MultiWindow::separateCubeSceneInMultipleWindows_data()
{
QTest::addColumn<int>("viewCount");
QTest::newRow("2 windows") << 2;
QTest::newRow("3 windows") << 3;
QTest::newRow("4 windows") << 4;
}
// Open 2-4 windows, each with an independent source QML scene.
void tst_MultiWindow::separateCubeSceneInMultipleWindows()
{
QFETCH(int, viewCount);
QVector<QQuickView *> views;
views.resize(viewCount);
for (int i = 0; i < viewCount; ++i) {
views[i] = createView(QLatin1String("texturedcube.qml"), QSize(640, 480));
views[i]->setGeometry(i * 100, i * 100, 640, 480);
QVERIFY(views[i]);
}
for (int i = 0; i < viewCount; ++i)
QVERIFY(QTest::qWaitForWindowExposed(views[i]));
for (int i = 0; i < viewCount; ++i) {
const QImage result = grab(views[i]);
if (result.isNull())
return;
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(59, 192, 77), FUZZ));
}
qDeleteAll(views);
}
// Have a single window with 4 View3Ds rendering the same scene via importScene.
// Then change the (shared) Texture's source to be a QQuickItem.
void tst_MultiWindow::cubeMultiViewportSameWindow()
{
QScopedPointer<QQuickView> view(createView(QLatin1String("texturedcube_multiviewport.qml"), QSize(640, 480)));
QVERIFY(view);
QVERIFY(QTest::qWaitForWindowExposed(view.data()));
QImage result = grab(view.data());
if (result.isNull())
return;
const qreal dpr = view->devicePixelRatio();
// First the cube is textured with a Qt logo.
QVERIFY(comparePixel(result, 25, 25, dpr, QColor::fromRgb(0, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 160, 128, dpr, QColor::fromRgb(59, 192, 77), FUZZ));
QVERIFY(comparePixel(result, 25 + 320, 25, dpr, QColor::fromRgb(255, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 160 + 320, 128, dpr, QColor::fromRgb(59, 192, 77), FUZZ));
QVERIFY(comparePixel(result, 25, 25 + 240, dpr, QColor::fromRgb(0, 128, 0), FUZZ));
QVERIFY(comparePixel(result, 160, 128 + 240, dpr, QColor::fromRgb(59, 192, 77), FUZZ));
QVERIFY(comparePixel(result, 25 + 320, 25 + 240, dpr, QColor::fromRgb(0, 0, 255), FUZZ));
QVERIFY(comparePixel(result, 160 + 320, 128 + 240, dpr, QColor::fromRgb(59, 192, 77), FUZZ));
QMetaObject::invokeMethod(view->rootObject(), "changeToSourceItemBasedTexture");
result = grab(view.data());
if (result.isNull())
return;
// Now the texture is a filled red rectangle.
QVERIFY(comparePixel(result, 25, 25, dpr, QColor::fromRgb(0, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 160, 128, dpr, QColor::fromRgb(239, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 25 + 320, 25, dpr, QColor::fromRgb(255, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 160 + 320, 128, dpr, QColor::fromRgb(239, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 25, 25 + 240, dpr, QColor::fromRgb(0, 128, 0), FUZZ));
QVERIFY(comparePixel(result, 160, 128 + 240, dpr, QColor::fromRgb(239, 0, 0), FUZZ));
QVERIFY(comparePixel(result, 25 + 320, 25 + 240, dpr, QColor::fromRgb(0, 0, 255), FUZZ));
QVERIFY(comparePixel(result, 160 + 320, 128 + 240, dpr, QColor::fromRgb(239, 0, 0), FUZZ));
}
// Two View3Ds in two separate windows, importing the same scene.
void tst_MultiWindow::cubeMultiViewportMultiWindow()
{
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(testFileUrl("texturedcube_multiviewport_multiwindow.qml"));
QObject *obj = component.create();
QScopedPointer<QObject> cleanup(obj);
QVERIFY(obj);
QQuickWindow *window1 = qobject_cast<QQuickWindow*>(obj);
QVERIFY(window1);
QQuickWindow *window2 = window1->findChild<QQuickWindow *>("window2");
QVERIFY(window2);
QVERIFY(QTest::qWaitForWindowExposed(window1));
QVERIFY(QTest::qWaitForWindowExposed(window2));
QImage result = grab(window1);
if (result.isNull())
return;
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(59, 192, 77), FUZZ));
result = grab(window2);
if (result.isNull())
return;
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(59, 192, 77), FUZZ));
{
QQuick3DTestMessageHandler msgCatcher;
QSignalSpy sceneUpdate1(window1, &QQuickWindow::afterFrameEnd);
QSignalSpy sceneUpdate2(window2, &QQuickWindow::afterFrameEnd);
QMetaObject::invokeMethod(window1, "changeToSourceItemBasedTexture");
bool b = QTest::qWaitFor([&](){ return sceneUpdate1.size() > 1; });
result = grab(window1);
if (!b || result.isNull()) {
qWarning("Test skipped!");
return;
}
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(239, 0, 0), FUZZ));
b = QTest::qWaitFor([&](){ return sceneUpdate2.size() > 1; });
result = grab(window2);
if (!b || result.isNull()) {
qWarning("Test skipped!");
return;
}
QRhi *rhi1 = static_cast<QRhi *>(window1->rendererInterface()->getResource(window1, QSGRendererInterface::RhiResource));
QVERIFY(rhi1);
QRhi *rhi2 = static_cast<QRhi *>(window2->rendererInterface()->getResource(window2, QSGRendererInterface::RhiResource));
QVERIFY(rhi2);
if (rhi1 != rhi2) {
QVERIFY(!msgCatcher.messageString().contains(QLatin1String("Cannot use QSGTexture")));
} else {
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(239, 0, 0), FUZZ));
}
}
}
// One View3D moved between two separate windows
void tst_MultiWindow::cubeMultiWindow()
{
QQmlEngine engine;
QQmlComponent component(&engine);
component.loadUrl(testFileUrl("texturedcube_multiwindow.qml"));
QObject *obj = component.create();
QScopedPointer<QObject> cleanup(obj);
QVERIFY(obj);
QQuickWindow *window1 = qobject_cast<QQuickWindow *>(obj);
QVERIFY(window1);
QQuickWindow *window2 = window1->findChild<QQuickWindow *>("window2");
QVERIFY(window2);
QVERIFY(QTest::qWaitForWindowExposed(window1));
QVERIFY(QTest::qWaitForWindowExposed(window2));
QImage result = grab(window1);
if (result.isNull())
return;
// Check first window OK
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(59, 192, 77), FUZZ));
QMetaObject::invokeMethod(window1, "reparentView");
result = grab(window2);
if (result.isNull())
return;
// Check scene moved to second window
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(59, 192, 77), FUZZ));
result = grab(window1);
if (result.isNull())
return;
// Check first window empty
QVERIFY(comparePixelNormPos(result, 0.5, 0.5, QColor::fromRgb(255, 255, 255), FUZZ));
}
QTEST_MAIN(tst_MultiWindow)
#include "tst_multiwindow.moc"
|