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