Debian Codegraph @

tests/benchmarks/gui/painting/lancebench/tst_lancebench.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include "paintcommands.h"
5
6 #include <qtest.h>
7 #include <QDir>
8 #include <QPainter>
9
10 #ifndef QT_NO_OPENGL
11 #include <QOpenGLFramebufferObjectFormat>
12 #include <QOpenGLContext>
13 #include <QOpenGLPaintDevice>
14 #endif
15
16 #include <algorithm>
17
18 class tst_LanceBench : public QObject
19 {
20 Q_OBJECT
21 public:
22 tst_LanceBench();
23
24 private:
25 enum GraphicsEngine {
26 Raster = 0,
27 OpenGL = 1
28 };
29
30 void setupTestSuite(const QStringList& blacklist = QStringList());
31 void runTestSuite(GraphicsEngine engine, QImage::Format format, const QSurfaceFormat &contextFormat = QSurfaceFormat());
32 void paint(QPaintDevice *device, GraphicsEngine engine, QImage::Format format, const QStringList &script, const QString &filePath);
33
34 QStringList qpsFiles;
35 QHash<QString, QStringList> scripts;
36 QString scriptsDir;
37
38 private slots:
39 void initTestCase();
40 void cleanupTestCase() {}
41
42 void testRasterARGB32PM_data();
43 void testRasterARGB32PM();
44 void testRasterRGB32_data();
45 void testRasterRGB32();
46 void testRasterARGB32_data();
47 void testRasterARGB32();
48 void testRasterRGB16_data();
49 void testRasterRGB16();
50 void testRasterBGR30_data();
51 void testRasterBGR30();
52 void testRasterARGB8565PM_data();
53 void testRasterARGB8565PM();
54 void testRasterGrayscale8_data();
55 void testRasterGrayscale8();
56
57 #ifndef QT_NO_OPENGL
58 void testOpenGL_data();
59 void testOpenGL();
60 void testCoreOpenGL_data();
61 void testCoreOpenGL();
62 private:
63 bool checkSystemGLSupport();
64 bool checkSystemCoreGLSupport();
65 #endif
66 };
67
68 tst_LanceBench::tst_LanceBench()
69 {
70 }
71
72 void tst_LanceBench::initTestCase()
73 {
74 QString baseDir = QFINDTESTDATA("../../../../baseline/painting/scripts/text.qps");
75 scriptsDir = baseDir.left(baseDir.lastIndexOf('/')) + '/';
76 QDir qpsDir(scriptsDir);
77 qpsFiles = qpsDir.entryList(QStringList() << QLatin1String("*.qps"), QDir::Files | QDir::Readable);
78 if (qpsFiles.isEmpty()) {
79 qWarning() << "No qps script files found in" << qpsDir.path();
80 QSKIP("Aborted due to errors.");
81 }
82
83 std::sort(qpsFiles.begin(), qpsFiles.end());
84 for (const QString& fileName : std::as_const(qpsFiles)) {
85 QFile file(scriptsDir + fileName);
86 QVERIFY2(file.open(QFile::ReadOnly),
87 qPrintable(QString::fromLatin1("Failed to open %1").arg(fileName)));
88 QByteArray cont = file.readAll();
89 scripts.insert(fileName, QString::fromUtf8(cont).split(QLatin1Char('\n'), Qt::SkipEmptyParts));
90 }
91 }
92
93 void tst_LanceBench::testRasterARGB32PM_data()
94 {
95 setupTestSuite();
96 }
97
98 void tst_LanceBench::testRasterARGB32PM()
99 {
100 runTestSuite(Raster, QImage::Format_ARGB32_Premultiplied);
101 }
102
103 void tst_LanceBench::testRasterRGB32_data()
104 {
105 setupTestSuite();
106 }
107
108 void tst_LanceBench::testRasterRGB32()
109 {
110 runTestSuite(Raster, QImage::Format_RGB32);
111 }
112
113 void tst_LanceBench::testRasterARGB32_data()
114 {
115 setupTestSuite();
116 }
117
118 void tst_LanceBench::testRasterARGB32()
119 {
120 runTestSuite(Raster, QImage::Format_ARGB32);
121 }
122
123 void tst_LanceBench::testRasterRGB16_data()
124 {
125 setupTestSuite();
126 }
127
128 void tst_LanceBench::testRasterRGB16()
129 {
130 runTestSuite(Raster, QImage::Format_RGB16);
131 }
132
133 void tst_LanceBench::testRasterBGR30_data()
134 {
135 setupTestSuite();
136 }
137
138 void tst_LanceBench::testRasterBGR30()
139 {
140 runTestSuite(Raster, QImage::Format_BGR30);
141 }
142
143 void tst_LanceBench::testRasterARGB8565PM_data()
144 {
145 setupTestSuite();
146 }
147
148 void tst_LanceBench::testRasterARGB8565PM()
149 {
150 runTestSuite(Raster, QImage::Format_ARGB8565_Premultiplied);
151 }
152
153 void tst_LanceBench::testRasterGrayscale8_data()
154 {
155 setupTestSuite();
156 }
157
158 void tst_LanceBench::testRasterGrayscale8()
159 {
160 runTestSuite(Raster, QImage::Format_Grayscale8);
161 }
162
163 #ifndef QT_NO_OPENGL
164 bool tst_LanceBench::checkSystemGLSupport()
165 {
166 QWindow win;
167 win.setSurfaceType(QSurface::OpenGLSurface);
168 win.create();
169 QOpenGLFramebufferObjectFormat fmt;
170 fmt.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
171 fmt.setSamples(4);
172 QOpenGLContext ctx;
173 if (!ctx.create() || !ctx.makeCurrent(&win))
174 return false;
175 QOpenGLFramebufferObject fbo(800, 800, fmt);
176 if (!fbo.isValid() || !fbo.bind())
177 return false;
178
179 return true;
180 }
181
182 bool tst_LanceBench::checkSystemCoreGLSupport()
183 {
184 if (QOpenGLContext::openGLModuleType() != QOpenGLContext::LibGL)
185 return false;
186
187 QSurfaceFormat coreFormat;
188 coreFormat.setVersion(3, 2);
189 coreFormat.setProfile(QSurfaceFormat::CoreProfile);
190 QWindow win;
191 win.setSurfaceType(QSurface::OpenGLSurface);
192 win.setFormat(coreFormat);
193 win.create();
194 QOpenGLFramebufferObjectFormat fmt;
195 fmt.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
196 fmt.setSamples(4);
197 QOpenGLContext ctx;
198 ctx.setFormat(coreFormat);
199 if (!ctx.create() || !ctx.makeCurrent(&win))
200 return false;
201 QOpenGLFramebufferObject fbo(800, 800, fmt);
202 if (!fbo.isValid() || !fbo.bind())
203 return false;
204
205 return true;
206 }
207
208 void tst_LanceBench::testOpenGL_data()
209 {
210 if (!checkSystemGLSupport())
211 QSKIP("System under test does not meet preconditions for GL testing. Skipping.");
212 QStringList localBlacklist = QStringList() << QLatin1String("rasterops.qps");
213 setupTestSuite(localBlacklist);
214 }
215
216 void tst_LanceBench::testOpenGL()
217 {
218 runTestSuite(OpenGL, QImage::Format_RGB32);
219 }
220
221 void tst_LanceBench::testCoreOpenGL_data()
222 {
223 if (!checkSystemCoreGLSupport())
224 QSKIP("System under test does not meet preconditions for Core Profile GL testing. Skipping.");
225 QStringList localBlacklist = QStringList() << QLatin1String("rasterops.qps");
226 setupTestSuite(localBlacklist);
227 }
228
229 void tst_LanceBench::testCoreOpenGL()
230 {
231 QSurfaceFormat coreFormat;
232 coreFormat.setVersion(3, 2);
233 coreFormat.setProfile(QSurfaceFormat::CoreProfile);
234 runTestSuite(OpenGL, QImage::Format_RGB32, coreFormat);
235 }
236 #endif
237
238 void tst_LanceBench::setupTestSuite(const QStringList& blacklist)
239 {
240 QTest::addColumn<QString>("qpsFile");
241 for (const QString &fileName : std::as_const(qpsFiles)) {
242 if (blacklist.contains(fileName))
243 continue;
244 QTest::newRow(fileName.toLatin1()) << fileName;
245 }
246 }
247
248 void tst_LanceBench::runTestSuite(GraphicsEngine engine, QImage::Format format, const QSurfaceFormat &contextFormat)
249 {
250 QFETCH(QString, qpsFile);
251
252 QString filePath = scriptsDir + qpsFile;
253 QStringList script = scripts.value(qpsFile);
254 QImage rendered;
255
256 if (engine == Raster) {
257 QImage img(800, 800, format);
258 paint(&img, engine, format, script, QFileInfo(filePath).absoluteFilePath());
259 rendered = img;
260 #ifndef QT_NO_OPENGL
261 } else if (engine == OpenGL) {
262 QWindow win;
263 win.setSurfaceType(QSurface::OpenGLSurface);
264 win.setFormat(contextFormat);
265 win.create();
266 QOpenGLFramebufferObjectFormat fmt;
267 fmt.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
268 fmt.setSamples(4);
269 QOpenGLContext ctx;
270 ctx.setFormat(contextFormat);
271 QVERIFY(ctx.create());
272 QVERIFY(ctx.makeCurrent(&win));
273 QOpenGLFramebufferObject fbo(800, 800, fmt);
274 fbo.bind();
275 QOpenGLPaintDevice pdv(800, 800);
276 paint(&pdv, engine, format, script, QFileInfo(filePath).absoluteFilePath());
277 rendered = fbo.toImage().convertToFormat(format);
278 #endif
279 }
280 }
281
282 void tst_LanceBench::paint(QPaintDevice *device, GraphicsEngine engine, QImage::Format format, const QStringList &script, const QString &filePath)
283 {
284 PaintCommands pcmd(script, 800, 800, format);
285 switch (engine) {
286 case OpenGL:
287 pcmd.setType(OpenGLBufferType); // version/profile is communicated through the context's format()
288 break;
289 case Raster:
290 pcmd.setType(ImageType);
291 break;
292 }
293 pcmd.setFilePath(filePath);
294 QBENCHMARK {
295 QPainter p(device);
296 pcmd.setPainter(&p);
297 pcmd.runCommands();
298 p.end();
299 }
300 }
301
302 QTEST_MAIN(tst_LanceBench)
303
304 #include "tst_lancebench.moc"