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
|
#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QtCore/QCoreApplication>
#include <qsgcontext.h>
#include <qsgtexturemanager.h>
class TextureManagerTest : public QObject
{
Q_OBJECT
public:
TextureManagerTest();
private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void upload();
void uploadSameImageTwice();
void requestUpload();
void gracefullyRunOutOfMemory();
void gracefullyFailOnTooLarge();
void maxTextureSize();
private:
QSGContext *context;
QGLWidget *glWidget;
QSGTextureManager *tm;
};
TextureManagerTest::TextureManagerTest()
{
}
void TextureManagerTest::initTestCase()
{
glWidget = new QGLWidget();
glWidget->resize(300, 200);
glWidget->show();
context = new QSGContext();
context->initialize(const_cast<QGLContext *>(glWidget->context()));
tm = context->textureManager();
}
void TextureManagerTest::cleanupTestCase()
{
delete context;
delete glWidget;
}
/*!
Verify that uploads work and are synchronous and that
size of returned texture must be at least image dims.
*/
void TextureManagerTest::upload()
{
QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
QSGTextureRef t = tm->upload(image);
QVERIFY(!t.isNull());
QVERIFY(t.isReady());
QVERIFY(t->textureId() > 0);
QVERIFY(t->textureSize().width() >= image.width());
QVERIFY(t->textureSize().height() >= image.height());
}
/*!
Verify that two requests of the same image returns the same texture.
*/
void TextureManagerTest::uploadSameImageTwice()
{
QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
QSGTextureRef t = tm->upload(image);
QImage shallowCopy = image;
QSGTextureRef t2 = tm->upload(shallowCopy);
QCOMPARE(t.texture(), t2.texture());
}
void TextureManagerTest::requestUpload()
{
QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
QSGTextureRef t = tm->requestUpload(image, 0, 0);
QVERIFY(t->status() == QSGTexture::Ready || t->status() == QSGTexture::Loading);
int maxWait = 1000;
while (t->status() == QSGTexture::Loading) {
QTest::qWait(50);
QApplication::processEvents();
}
QVERIFY(t->status() == QSGTexture::Ready);
QVERIFY(t->textureId() > 0);
QVERIFY(t->textureSize().width() >= image.width());
QVERIFY(t->textureSize().height() >= image.height());
}
/*!
Test that we don't fail horribly when allocating large amounts of
texture memory. Since some drivers page graphics memory out to disk
and thus never run out, cap the test at 128Mb to not run forever
*/
void TextureManagerTest::gracefullyRunOutOfMemory()
{
QImage image(512, 512, QImage::Format_ARGB32_Premultiplied);
QList<QSGTextureRef> refs;
int i = 128;
while (--i) {
QSGTextureRef t = tm->upload(image);
refs << t;
if (t.isNull())
break;
}
QVERIFY(true);
}
void TextureManagerTest::gracefullyFailOnTooLarge()
{
QImage image(32000, 2, QImage::Format_ARGB32_Premultiplied);
QSGTextureRef t = tm->upload(image);
QVERIFY(t.isNull());
}
void TextureManagerTest::maxTextureSize()
{
int size = tm->maxTextureSize();
if (size < 1024) {
qWarning("Texture limit is only %d", size);
}
// 64 is the lowest allowed by the spec...
// Any lower number would indicate an error
QVERIFY(size >=64); }
QTEST_MAIN(TextureManagerTest);
#include "tst_texturemanagertest.moc"
|