diff options
author | Gunnar Sletta <gunnar.sletta@nokia.com> | 2011-12-20 00:50:15 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-03 17:58:59 +0100 |
commit | 42d6acd0a3ea5fa5cf7ab69f1f8f2206559107ea (patch) | |
tree | 84377465ecf6dbc45634656631c72f24e0e77f51 /tests/auto/qtquick2/qquickitemlayer | |
parent | a9b103d02831a03e8f3815af2b7d043c5cf4eae6 (diff) |
Introduce layerering support (ShaderEffectSource) directly in Item
This is enabled by doing "Item.layer.enabled: true". The implementation
is solely based on the existing shader effect (source) and simply
swaps in a sibling next to the item when enabled.
This change also adds the QSGTextureProvider to the public API,
as it is now a natural part of the QQuickItem API since all items
can be textures.
Change-Id: I26705c11e92d5623a5121300acc123782b784077
Reviewed-by: Kim M. Kalland <kim.kalland@nokia.com>
Diffstat (limited to 'tests/auto/qtquick2/qquickitemlayer')
10 files changed, 592 insertions, 0 deletions
diff --git a/tests/auto/qtquick2/qquickitemlayer/data/Effect.qml b/tests/auto/qtquick2/qquickitemlayer/data/Effect.qml new file mode 100644 index 0000000000..630c8f90ed --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/Effect.qml @@ -0,0 +1,34 @@ +import QtQuick 2.0 + +Item +{ + width: 100 + height: 100 + + Rectangle { + id: box + width: 100 + height: 100 + + color: "#0000ff" + + Rectangle { + x: 50 + width: 50 + height: 100 + color: "#00ff00" + } + + layer.enabled: true + layer.effect: ShaderEffect { + fragmentShader: " + uniform lowp sampler2D source; + uniform lowp float qt_Opacity; + varying highp vec2 qt_TexCoord0; + void main() { + gl_FragColor = texture2D(source, qt_TexCoord0).bgra * qt_Opacity; + }" + } + + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/Enabled.qml b/tests/auto/qtquick2/qquickitemlayer/data/Enabled.qml new file mode 100644 index 0000000000..0e7d4f56b8 --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/Enabled.qml @@ -0,0 +1,25 @@ +import QtQuick 2.0 + +Item +{ + width: 200 + height: 200 + + Item { + width: 20 + height: 20 + scale: 10 + + layer.enabled: true + anchors.centerIn: parent + + Rectangle { + width: 20 + height: 20 + gradient: Gradient { + GradientStop { position: 0; color: "white" } + GradientStop { position: 1; color: "black" } + } + } + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/Mipmap.qml b/tests/auto/qtquick2/qquickitemlayer/data/Mipmap.qml new file mode 100644 index 0000000000..8de41076e9 --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/Mipmap.qml @@ -0,0 +1,30 @@ +import QtQuick 2.0 + +Item +{ + width: 100 + height: 100 + + Rectangle { + id: box + width: 600 + height: 600 + + scale: 1 / 6. + + color: "black" + + layer.enabled: true + layer.mipmap: true + layer.smooth: true + + anchors.centerIn: parent + + Rectangle { + x: 1 + width: 1 + height: parent.height + color: "white" + } + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/Smooth.qml b/tests/auto/qtquick2/qquickitemlayer/data/Smooth.qml new file mode 100644 index 0000000000..3f9575bb0b --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/Smooth.qml @@ -0,0 +1,23 @@ +import QtQuick 2.0 + + +Item { + width: 200 + height: 100 + + Row { + id: layerRoot + + width: 20 + height: 10 + + Rectangle { width: 10; height: 10; color: "red" } + Rectangle { width: 10; height: 10; color: "blue" } + + layer.enabled: true + layer.smooth: true + + anchors.centerIn: parent + scale: 10 + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/SourceRect.qml b/tests/auto/qtquick2/qquickitemlayer/data/SourceRect.qml new file mode 100644 index 0000000000..7cc7e8b21e --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/SourceRect.qml @@ -0,0 +1,33 @@ +import QtQuick 2.0 + +Item +{ + width: 100 + height: 100 + + Rectangle { + id: box + width: 100 + height: 100 + + color: "#ff0000" + + layer.enabled: true + layer.sourceRect: Qt.rect(-10, -10, box.width + 20, box.height + 20); + + // A shader that pads the transparent pixels with blue. + layer.effect: ShaderEffect { + fragmentShader: " + uniform lowp sampler2D source; + uniform lowp float qt_Opacity; + varying highp vec2 qt_TexCoord0; + void main() { + vec4 c = texture2D(source, qt_TexCoord0); + if (c.a == 0.) + c = vec4(0, 0, 1, 1); + gl_FragColor = c * qt_Opacity; + } + " + } + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/TextureProvider.qml b/tests/auto/qtquick2/qquickitemlayer/data/TextureProvider.qml new file mode 100644 index 0000000000..ccd515652a --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/TextureProvider.qml @@ -0,0 +1,40 @@ +import QtQuick 2.0 + +Item +{ + width: 100 + height: 100 + + Rectangle { + id: box + width: 100 + height: 100 + + color: "#0000ff" + + Rectangle { + x: 50 + width: 50 + height: 100 + color: "#00ff00" + } + + visible: false + + layer.enabled: true + } + + ShaderEffect { + anchors.fill: parent + property variant source: box + + fragmentShader: " + uniform lowp sampler2D source; + uniform lowp float qt_Opacity; + varying highp vec2 qt_TexCoord0; + void main() { + gl_FragColor = texture2D(source, qt_TexCoord0).bgra * qt_Opacity; + }" + } + +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/Visible.qml b/tests/auto/qtquick2/qquickitemlayer/data/Visible.qml new file mode 100644 index 0000000000..8267f18250 --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/Visible.qml @@ -0,0 +1,56 @@ +import QtQuick 2.0 + +Item +{ + id: root + + width: 100 + height: 100 + + property bool layerEffect: false; + onLayerEffectChanged: root.maybeUse(); + Component.onCompleted: root.maybeUse(); + + property real layerOpacity: 1; + property bool layerVisible: true; + + function maybeUse() { + if (root.layerEffect) + box.layer.effect = shaderEffect + } + + Component { + id: shaderEffect + ShaderEffect { + fragmentShader: " + uniform lowp sampler2D source; + uniform lowp float qt_Opacity; + varying highp vec2 qt_TexCoord0; + void main() { + gl_FragColor = texture2D(source, qt_TexCoord0).bgra * qt_Opacity; + } + " + } + + } + + Rectangle { + id: box + width: 100 + height: 100 + + color: "#0000ff" + visible: parent.layerVisible; + opacity: parent.layerOpacity; + + Rectangle { + x: 50 + width: 50 + height: 100 + color: "#00ff00" + } + + layer.enabled: true + + } +} diff --git a/tests/auto/qtquick2/qquickitemlayer/data/ZOrder.qml b/tests/auto/qtquick2/qquickitemlayer/data/ZOrder.qml new file mode 100644 index 0000000000..59ccb32224 --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/data/ZOrder.qml @@ -0,0 +1,52 @@ +import QtQuick 2.0 + +Item +{ + id: root + + width: 200 + height: 200 + + Component { + id: shaderEffect + ShaderEffect { } + } + + property bool layerEffect: false; + onLayerEffectChanged: root.maybeUse(); + Component.onCompleted: root.maybeUse(); + + function maybeUse() { + if (root.layerEffect) + box.layer.effect = shaderEffect + } + + + Rectangle { + color: "red" + anchors.left: parent.left + anchors.top: parent.top + width: 100 + height: 100 + z: 1 + } + + Rectangle { + color: "#00ff00" + anchors.bottom: parent.bottom + anchors.right: parent.right + width: 100 + height: 100 + z: 3 + } + + Rectangle { + id: box + color: "blue" + anchors.fill: parent + anchors.margins: 10 + layer.enabled: true + z: 2 + } + +} diff --git a/tests/auto/qtquick2/qquickitemlayer/qquickitemlayer.pro b/tests/auto/qtquick2/qquickitemlayer/qquickitemlayer.pro new file mode 100644 index 0000000000..bf22d04f23 --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/qquickitemlayer.pro @@ -0,0 +1,32 @@ +CONFIG += testcase +TARGET = tst_qquickitemlayer +SOURCES += tst_qquickitemlayer.cpp + +macx:CONFIG -= app_bundle + +testDataFiles.files = data +testDataFiles.path = . +DEPLOYMENT += testDataFiles + +include(../../shared/util.pri) + +CONFIG += parallel_test +QT += core-private gui-private v8-private declarative-private quick-private testlib + +OTHER_FILES += \ + data/Smooth.qml \ + data/Enabled.qml \ + data/Mipmap.qml \ + data/Effect.qml \ + data/SourceRect.qml \ + data/TextureProvider.qml \ + data/Visible.qml \ + data/ZOrder.qml + + + + + + + + diff --git a/tests/auto/qtquick2/qquickitemlayer/tst_qquickitemlayer.cpp b/tests/auto/qtquick2/qquickitemlayer/tst_qquickitemlayer.cpp new file mode 100644 index 0000000000..2b7a16997c --- /dev/null +++ b/tests/auto/qtquick2/qquickitemlayer/tst_qquickitemlayer.cpp @@ -0,0 +1,267 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qtest.h> + +#include <QtQuick/qquickitem.h> +#include <QtQuick/qquickview.h> + +#include "../../shared/util.h" + +class tst_QQuickItemLayer: public QDeclarativeDataTest +{ + Q_OBJECT +public: + tst_QQuickItemLayer(); + + QImage runTest(const QString &url) + { + QQuickView view; + view.setSource(QUrl(url)); + + view.show(); + QTest::qWaitForWindowShown(&view); + + return view.grabFrameBuffer(); + } + +private slots: + void layerEnabled(); + void layerSmooth(); + void layerMipmap(); + void layerEffect(); + + void layerVisibility_data(); + void layerVisibility(); + + void layerSourceRect(); + + + void layerZOrder_data(); + void layerZOrder(); + + void layerIsTextureProvider(); +}; + +tst_QQuickItemLayer::tst_QQuickItemLayer() +{ +} + + + +// The test draws a red and a blue box next to each other and tests that the +// output is still red and blue on the left and right and a combination of +// the two in the middle. + +void tst_QQuickItemLayer::layerSmooth() +{ + QImage fb = runTest(testFile("Smooth.qml")); + QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0)); + QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0, 0xff)); + + uint pixel = fb.pixel(fb.width() / 2, 0); + QVERIFY(qRed(pixel) > 0); + QVERIFY(qBlue(pixel) > 0); +} + + + +// The test draws a gradient at a small size into a layer and scales the +// layer. If the layer is enabled there should be very visible bands in +// the gradient. + +void tst_QQuickItemLayer::layerEnabled() +{ + QImage fb = runTest(testFile("Enabled.qml")); + // Verify the banding + QCOMPARE(fb.pixel(0, 0), fb.pixel(0, 1)); + // Verify the gradient + QVERIFY(fb.pixel(0, 0) != fb.pixel(0, fb.height() - 1)); +} + + + +// The test draws a one pixel wide line and scales it down by more than a a factor 2 +// If mipmpping works, the pixels should be gray, not white or black + +void tst_QQuickItemLayer::layerMipmap() +{ + QImage fb = runTest(testFile("Mipmap.qml")); + QVERIFY(fb.pixel(0, 0) != 0xff000000); + QVERIFY(fb.pixel(0, 0) != 0xffffffff); +} + + + +// The test implements an rgb swapping effect sourced from a blue rectangle. The +// resulting pixel should be red + +void tst_QQuickItemLayer::layerEffect() +{ + QImage fb = runTest(testFile("Effect.qml")); + QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0)); + QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0xff, 0)); +} + + + +// The test draws a rectangle and verifies that there is padding on each side +// as the source rect spans outside the item. The padding is verified using +// a shader that pads transparent to blue. Everything else is red. +void tst_QQuickItemLayer::layerSourceRect() +{ + QImage fb = runTest(testFile("SourceRect.qml")); + + // Check that the edges are converted to blue + QCOMPARE(fb.pixel(0, 0), qRgb(0, 0, 0xff)); + QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0, 0xff)); + QCOMPARE(fb.pixel(0, fb.height() - 1), qRgb(0, 0, 0xff)); + QCOMPARE(fb.pixel(fb.width() - 1, fb.height() - 1), qRgb(0, 0, 0xff)); + + // The center pixel should be red + QCOMPARE(fb.pixel(fb.width() / 2, fb.height() / 2), qRgb(0xff, 0, 0)); +} + + + +// Same as the effect test up above, but this time use the item +// directly in a stand alone ShaderEffect +void tst_QQuickItemLayer::layerIsTextureProvider() +{ + QImage fb = runTest(testFile("TextureProvider.qml")); + QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0)); + QCOMPARE(fb.pixel(fb.width() - 1, 0), qRgb(0, 0xff, 0)); +} + + +void tst_QQuickItemLayer::layerVisibility_data() +{ + QTest::addColumn<bool>("visible"); + QTest::addColumn<bool>("effect"); + QTest::addColumn<qreal>("opacity"); + + QTest::newRow("!effect, !visible, a=1") << false << false << 1.; + QTest::newRow("!effect, visible, a=1") << false << true << 1.; + QTest::newRow("effect, !visible, a=1") << true << false << 1.; + QTest::newRow("effect, visible, a=1") << true << true << 1.; + + QTest::newRow("!effect, !visible, a=.5") << false << false << .5; + QTest::newRow("!effect, visible, a=.5") << false << true << .5; + QTest::newRow("effect, !visible, a=.5") << true << false << .5; + QTest::newRow("effect, visible, a=.5") << true << true << .5; + + QTest::newRow("!effect, !visible, a=0") << false << false << 0.; + QTest::newRow("!effect, visible, a=0") << false << true << 0.; + QTest::newRow("effect, !visible, a=0") << true << false << 0.; + QTest::newRow("effect, visible, a=0") << true << true << 0.; +} + +void tst_QQuickItemLayer::layerVisibility() +{ + QFETCH(bool, visible); + QFETCH(bool, effect); + QFETCH(qreal, opacity); + + QQuickView view; + view.setSource(testFile("Visible.qml")); + + QQuickItem *child = view.rootItem()->childItems().at(0); + child->setProperty("layerVisible", visible); + child->setProperty("layerEffect", effect); + child->setProperty("layerOpacity", opacity); + + view.show(); + + QTest::qWaitForWindowShown(&view); + + QImage fb = view.grabFrameBuffer(); + uint pixel = fb.pixel(0, 0); + + if (!visible || opacity == 0) { + QCOMPARE(pixel, qRgb(0xff, 0xff, 0xff)); + } else if (effect) { + QCOMPARE(qRed(pixel), 0xff); + QVERIFY(qGreen(pixel) < 0xff); + QVERIFY(qBlue(pixel) < 0xff); + } else { // no effect + QCOMPARE(qBlue(pixel), 0xff); + QVERIFY(qGreen(pixel) < 0xff); + QVERIFY(qRed(pixel) < 0xff); + } +} + + + + +void tst_QQuickItemLayer::layerZOrder_data() +{ + QTest::addColumn<bool>("effect"); + + QTest::newRow("!effect") << false; + QTest::newRow("effect") << true; +} + +void tst_QQuickItemLayer::layerZOrder() +{ + QFETCH(bool, effect); + + QQuickView view; + view.setSource(testFile("ZOrder.qml")); + + QQuickItem *child = view.rootItem()->childItems().at(0); + child->setProperty("layerEffect", effect); + + view.show(); + + QTest::qWaitForWindowShown(&view); + + QImage fb = view.grabFrameBuffer(); + + QCOMPARE(fb.pixel(50, 50), qRgb(0, 0, 0xff)); + QCOMPARE(fb.pixel(150, 150), qRgb(0, 0xff, 00)); + +} + + + +QTEST_MAIN(tst_QQuickItemLayer) + +#include "tst_qquickitemlayer.moc" |