Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow Item components to be assigned to Item.layer.effect.
Some complex effects are easier to implement as Items using
ShaderEffects internally rather than with a top-level ShaderEffect.
Auto-tests added.

Change-Id: I4b99811b87e7ca5054bf119b99207b7f5a7c666e
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
  • Loading branch information
Kim Motoyoshi Kalland authored and Qt by Nokia committed Feb 7, 2012
1 parent b057ac0 commit 5cc1870
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/quick/items/qquickitem.cpp
Expand Up @@ -5497,9 +5497,9 @@ void QQuickItemLayer::activateEffect()
Q_ASSERT(!m_effect);

QObject *created = m_effectComponent->create();
m_effect = qobject_cast<QQuickShaderEffect *>(created);
m_effect = qobject_cast<QQuickItem *>(created);
if (!m_effect) {
qWarning("Item: layer.effect is not a ShaderEffect.");
qWarning("Item: layer.effect is not a QML Item.");
delete created;
return;
}
Expand Down Expand Up @@ -5527,7 +5527,8 @@ void QQuickItemLayer::deactivateEffect()
Holds the effect that is applied to this layer.
The effect must be a \l ShaderEffect.
The effect is typically a \l ShaderEffect component, although any \l Item component can be
assigned. The effect should have a source texture property with a name matching \l samplerName.
\sa samplerName
*/
Expand Down
2 changes: 1 addition & 1 deletion src/quick/items/qquickitem_p.h
Expand Up @@ -225,7 +225,7 @@ class QQuickItemLayer : public QObject, public QQuickItemChangeListener
QRectF m_sourceRect;
QString m_name;
QDeclarativeComponent *m_effectComponent;
QQuickShaderEffect *m_effect;
QQuickItem *m_effect;
QQuickShaderEffectSource *m_effectSource;
};

Expand Down
23 changes: 23 additions & 0 deletions tests/auto/qtquick2/qquickitemlayer/data/ItemEffect.qml
@@ -0,0 +1,23 @@
import QtQuick 2.0

Item {
width: 200
height: 200
Rectangle {
anchors.fill: parent
anchors.margins: 99
gradient: Gradient {
GradientStop { position: 0.3; color: "red" }
GradientStop { position: 0.7; color: "blue" }
}
layer.enabled: true
layer.effect: Item {
property variant source
ShaderEffect {
anchors.fill: parent
anchors.margins: -99
property variant source: parent.source
}
}
}
}
22 changes: 22 additions & 0 deletions tests/auto/qtquick2/qquickitemlayer/data/RectangleEffect.qml
@@ -0,0 +1,22 @@
import QtQuick 2.0

Item {
width: 200
height: 200
Rectangle {
width: 100
height: 100
x: 50
y: 50
scale: 1.5
z: 1
rotation: 45
color: "#ff0000"
layer.enabled: true
layer.effect: Rectangle { color: "#0000ff" }
}
Rectangle {
anchors.fill: parent
color: "#00ff00"
}
}
4 changes: 3 additions & 1 deletion tests/auto/qtquick2/qquickitemlayer/qquickitemlayer.pro
Expand Up @@ -25,7 +25,9 @@ OTHER_FILES += \
data/ZOrderChange.qml \
data/ToggleLayerAndEffect.qml \
data/DisableLayer.qml \
data/SamplerNameChange.qml
data/SamplerNameChange.qml \
data/ItemEffect.qml \
data/RectangleEffect.qml



Expand Down
28 changes: 28 additions & 0 deletions tests/auto/qtquick2/qquickitemlayer/tst_qquickitemlayer.cpp
Expand Up @@ -86,6 +86,8 @@ private slots:
void toggleLayerAndEffect();
void disableLayer();
void changeSamplerName();
void itemEffect();
void rectangleEffect();

private:
bool m_isMesaSoftwareRasterizer;
Expand Down Expand Up @@ -403,6 +405,32 @@ void tst_QQuickItemLayer::changeSamplerName()
QCOMPARE(fb.pixel(0, 0), qRgb(0, 0, 0xff));
}

void tst_QQuickItemLayer::itemEffect()
{
if (m_isMesaSoftwareRasterizer && m_mesaVersion < QT_VERSION_CHECK(7, 11, 0))
QSKIP("Mesa Software Rasterizer below version 7.11 does not render this test correctly.");
QImage fb = runTest(testFile("ItemEffect.qml"));
QCOMPARE(fb.pixel(0, 0), qRgb(0xff, 0, 0));
QCOMPARE(fb.pixel(199, 0), qRgb(0xff, 0, 0));
QCOMPARE(fb.pixel(0, 199), qRgb(0, 0, 0xff));
QCOMPARE(fb.pixel(199, 199), qRgb(0, 0, 0xff));
}

void tst_QQuickItemLayer::rectangleEffect()
{
QImage fb = runTest(testFile("RectangleEffect.qml"));
QCOMPARE(fb.pixel(0, 0), qRgb(0, 0xff, 0));
QCOMPARE(fb.pixel(199, 0), qRgb(0, 0xff, 0));
QCOMPARE(fb.pixel(0, 199), qRgb(0, 0xff, 0));
QCOMPARE(fb.pixel(199, 199), qRgb(0, 0xff, 0));

QCOMPARE(fb.pixel(100, 0), qRgb(0, 0, 0xff));
QCOMPARE(fb.pixel(199, 100), qRgb(0, 0, 0xff));
QCOMPARE(fb.pixel(100, 199), qRgb(0, 0, 0xff));
QCOMPARE(fb.pixel(0, 100), qRgb(0, 0, 0xff));
}


QTEST_MAIN(tst_QQuickItemLayer)

#include "tst_qquickitemlayer.moc"

0 comments on commit 5cc1870

Please sign in to comment.