1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtQuick/qquickitem.h>
45 #include <QtQuick/qquickview.h>
46 #include <QtGui/qopenglcontext.h>
47 #include <private/qsgrendernode_p.h>
49 #include "../../shared/util.h"
51 class tst_rendernode: public QQmlDataTest
57 QImage runTest(const QString &fileName)
60 view.setSource(testFileUrl(fileName));
63 QTest::qWaitForWindowShown(&view);
65 return view.grabFrameBuffer();
73 class ClearNode : public QSGRenderNode
76 virtual StateFlags changedStates()
81 virtual void render(const RenderState &)
83 // If clip has been set, scissoring will make sure the right area is cleared.
84 glClearColor(color.redF(), color.greenF(), color.blueF(), 1.0f);
85 glClear(GL_COLOR_BUFFER_BIT);
91 class ClearItem : public QQuickItem
94 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
96 ClearItem() : m_color(Qt::black)
98 setFlag(ItemHasContents, true);
101 QColor color() const { return m_color; }
102 void setColor(const QColor &color)
104 if (color == m_color)
111 virtual QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
113 ClearNode *node = static_cast<ClearNode *>(oldNode);
115 node = new ClearNode;
116 node->color = m_color;
127 class MessUpNode : public QSGRenderNode
130 virtual StateFlags changedStates()
132 return StateFlags(DepthState) | StencilState | ScissorState | ColorState | BlendState
133 | CullState | ViewportState;
136 virtual void render(const RenderState &)
138 // Don't draw anything, just mess up the state
139 glViewport(10, 10, 10, 10);
140 glDisable(GL_SCISSOR_TEST);
142 glEnable(GL_DEPTH_TEST);
143 glDepthFunc(GL_EQUAL);
144 #if defined(QT_OPENGL_ES)
150 glClearColor(1.0f, 0.5f, 1.0f, 0.0f);
151 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
152 glEnable(GL_SCISSOR_TEST);
153 glScissor(190, 190, 10, 10);
154 glStencilFunc(GL_EQUAL, 28, 0xff);
155 glBlendFunc(GL_ZERO, GL_ZERO);
157 glGetIntegerv(GL_FRONT_FACE, &frontFace);
158 glFrontFace(frontFace == GL_CW ? GL_CCW : GL_CW);
159 glEnable(GL_CULL_FACE);
163 class MessUpItem : public QQuickItem
169 setFlag(ItemHasContents, true);
173 virtual QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
175 MessUpNode *node = static_cast<MessUpNode *>(oldNode);
177 node = new MessUpNode;
182 tst_rendernode::tst_rendernode()
184 qmlRegisterType<ClearItem>("Test", 1, 0, "ClearItem");
185 qmlRegisterType<MessUpItem>("Test", 1, 0, "MessUpItem");
188 static void fuzzyCompareColor(QRgb x, QRgb y)
190 QVERIFY(qAbs(qRed(x) - qRed(y)) < 4);
191 QVERIFY(qAbs(qGreen(x) - qGreen(y)) < 4);
192 QVERIFY(qAbs(qBlue(x) - qBlue(y)) < 4);
195 void tst_rendernode::renderOrder()
197 QImage fb = runTest("RenderOrder.qml");
198 int x1 = fb.width() / 8;
199 int x2 = fb.width() * 3 / 8;
200 int x3 = fb.width() * 5 / 8;
201 int x4 = fb.width() * 7 / 8;
202 int y1 = fb.height() / 8;
203 int y2 = fb.height() * 3 / 8;
204 int y3 = fb.height() * 5 / 8;
205 int y4 = fb.height() * 7 / 8;
207 fuzzyCompareColor(fb.pixel(x1, y1), qRgb(0x7f, 0x00, 0x00));
208 QCOMPARE(fb.pixel(x2, y2), qRgb(0xff, 0xff, 0xff));
209 QCOMPARE(fb.pixel(x3, y2), qRgb(0x00, 0x00, 0xff));
210 QCOMPARE(fb.pixel(x4, y1), qRgb(0x00, 0x00, 0xff));
211 QCOMPARE(fb.pixel(x1, y4), qRgb(0xff, 0x00, 0x00));
212 QCOMPARE(fb.pixel(x2, y3), qRgb(0xff, 0xff, 0xff));
213 fuzzyCompareColor(fb.pixel(x3, y3), qRgb(0x7f, 0x7f, 0xff));
214 fuzzyCompareColor(fb.pixel(x4, y4), qRgb(0x00, 0x00, 0x7f));
217 void tst_rendernode::messUpState()
219 QImage fb = runTest("MessUpState.qml");
221 int x2 = fb.width() / 2;
222 int x3 = fb.width() - 1;
224 int y2 = fb.height() * 3 / 16;
225 int y3 = fb.height() / 2;
226 int y4 = fb.height() * 13 / 16;
227 int y5 = fb.height() - 1;
229 QCOMPARE(fb.pixel(x1, y3), qRgb(0xff, 0xff, 0xff));
230 QCOMPARE(fb.pixel(x3, y3), qRgb(0xff, 0xff, 0xff));
232 QCOMPARE(fb.pixel(x2, y1), qRgb(0x00, 0x00, 0x00));
233 QCOMPARE(fb.pixel(x2, y2), qRgb(0x00, 0x00, 0x00));
234 fuzzyCompareColor(fb.pixel(x2, y3), qRgb(0x7f, 0x00, 0x7f));
235 QCOMPARE(fb.pixel(x2, y4), qRgb(0x00, 0x00, 0x00));
236 QCOMPARE(fb.pixel(x2, y5), qRgb(0x00, 0x00, 0x00));
240 QTEST_MAIN(tst_rendernode)
242 #include "tst_rendernode.moc"