QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / tests / auto / quick / rendernode / tst_rendernode.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43
44 #include <QtQuick/qquickitem.h>
45 #include <QtQuick/qquickview.h>
46 #include <QtGui/qopenglcontext.h>
47 #include <private/qsgrendernode_p.h>
48
49 #include "../../shared/util.h"
50
51 class tst_rendernode: public QQmlDataTest
52 {
53     Q_OBJECT
54 public:
55     tst_rendernode();
56
57     QImage runTest(const QString &fileName)
58     {
59         QQuickView view;
60         view.setSource(testFileUrl(fileName));
61
62         view.show();
63         QTest::qWaitForWindowShown(&view);
64
65         return view.grabWindow();
66     }
67
68 private slots:
69     void renderOrder();
70     void messUpState();
71 };
72
73 class ClearNode : public QSGRenderNode
74 {
75 public:
76     virtual StateFlags changedStates()
77     {
78         return ColorState;
79     }
80
81     virtual void render(const RenderState &)
82     {
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);
86     }
87
88     QColor color;
89 };
90
91 class ClearItem : public QQuickItem
92 {
93     Q_OBJECT
94     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
95 public:
96     ClearItem() : m_color(Qt::black)
97     {
98         setFlag(ItemHasContents, true);
99     }
100
101     QColor color() const { return m_color; }
102     void setColor(const QColor &color)
103     {
104         if (color == m_color)
105             return;
106         m_color = color;
107         emit colorChanged();
108     }
109
110 protected:
111     virtual QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
112     {
113         ClearNode *node = static_cast<ClearNode *>(oldNode);
114         if (!node)
115             node = new ClearNode;
116         node->color = m_color;
117         return node;
118     }
119
120 Q_SIGNALS:
121     void colorChanged();
122
123 private:
124     QColor m_color;
125 };
126
127 class MessUpNode : public QSGRenderNode
128 {
129 public:
130     virtual StateFlags changedStates()
131     {
132         return StateFlags(DepthState) | StencilState | ScissorState | ColorState | BlendState
133                 | CullState | ViewportState;
134     }
135
136     virtual void render(const RenderState &)
137     {
138         // Don't draw anything, just mess up the state
139         glViewport(10, 10, 10, 10);
140         glDisable(GL_SCISSOR_TEST);
141         glDepthMask(true);
142         glEnable(GL_DEPTH_TEST);
143         glDepthFunc(GL_EQUAL);
144 #if defined(QT_OPENGL_ES)
145         glClearDepthf(1);
146 #else
147         glClearDepth(1);
148 #endif
149         glClearStencil(42);
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);
156         GLint frontFace;
157         glGetIntegerv(GL_FRONT_FACE, &frontFace);
158         glFrontFace(frontFace == GL_CW ? GL_CCW : GL_CW);
159         glEnable(GL_CULL_FACE);
160     }
161 };
162
163 class MessUpItem : public QQuickItem
164 {
165     Q_OBJECT
166 public:
167     MessUpItem()
168     {
169         setFlag(ItemHasContents, true);
170     }
171
172 protected:
173     virtual QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
174     {
175         MessUpNode *node = static_cast<MessUpNode *>(oldNode);
176         if (!node)
177             node = new MessUpNode;
178         return node;
179     }
180 };
181
182 tst_rendernode::tst_rendernode()
183 {
184     qmlRegisterType<ClearItem>("Test", 1, 0, "ClearItem");
185     qmlRegisterType<MessUpItem>("Test", 1, 0, "MessUpItem");
186 }
187
188 static void fuzzyCompareColor(QRgb x, QRgb y)
189 {
190     QVERIFY(qAbs(qRed(x) - qRed(y)) < 4);
191     QVERIFY(qAbs(qGreen(x) - qGreen(y)) < 4);
192     QVERIFY(qAbs(qBlue(x) - qBlue(y)) < 4);
193 }
194
195 void tst_rendernode::renderOrder()
196 {
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;
206
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));
215 }
216
217 void tst_rendernode::messUpState()
218 {
219     QImage fb = runTest("MessUpState.qml");
220     int x1 = 0;
221     int x2 = fb.width() / 2;
222     int x3 = fb.width() - 1;
223     int y1 = 0;
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;
228
229     QCOMPARE(fb.pixel(x1, y3), qRgb(0xff, 0xff, 0xff));
230     QCOMPARE(fb.pixel(x3, y3), qRgb(0xff, 0xff, 0xff));
231
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));
237 }
238
239
240 QTEST_MAIN(tst_rendernode)
241
242 #include "tst_rendernode.moc"