tests/benchmarks/opengl/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QtGui>
       
    42 #include <QtOpenGL>
       
    43 
       
    44 #include <qtest.h>
       
    45 
       
    46 #include <private/qpaintengine_opengl_p.h>
       
    47 
       
    48 class OpenGLBench : public QObject
       
    49 {
       
    50     Q_OBJECT
       
    51 
       
    52 private slots:
       
    53     void initTestCase();
       
    54     void cleanupTestCase();
       
    55 
       
    56     void imageDrawing_data();
       
    57     void imageDrawing();
       
    58 
       
    59     void pathDrawing_data();
       
    60     void pathDrawing();
       
    61 
       
    62     void painterOverhead();
       
    63 
       
    64     void startupCost_data();
       
    65     void startupCost();
       
    66 
       
    67     void lineDrawing();
       
    68 
       
    69     void textDrawing_data();
       
    70     void textDrawing();
       
    71 
       
    72     void clippedPainting_data();
       
    73     void clippedPainting();
       
    74 
       
    75     void gradients_data();
       
    76     void gradients();
       
    77 
       
    78 private:
       
    79     QGLPixelBuffer *pb;
       
    80 };
       
    81 
       
    82 void OpenGLBench::initTestCase()
       
    83 {
       
    84     pb = new QGLPixelBuffer(512, 512);
       
    85 
       
    86     QPainter p(pb);
       
    87     p.setRenderHint(QPainter::Antialiasing);
       
    88     p.setRenderHint(QPainter::HighQualityAntialiasing);
       
    89 
       
    90     p.drawImage(0, 0, QImage(256, 256, QImage::Format_ARGB32_Premultiplied));
       
    91 }
       
    92 
       
    93 void OpenGLBench::cleanupTestCase()
       
    94 {
       
    95     delete pb;
       
    96 }
       
    97 
       
    98 void OpenGLBench::imageDrawing_data()
       
    99 {
       
   100     QTest::addColumn<bool>("smoothPixmapTransform");
       
   101     QTest::addColumn<bool>("highQualityAntialiasing");
       
   102     QTest::addColumn<bool>("pixmap");
       
   103 
       
   104     for (int i = 0; i < (1 << 3); ++i) {
       
   105         bool smoothPixmapTransform = i & 1;
       
   106         bool highQualityAntialiasing = i & 2;
       
   107         bool pixmap = i & 4;
       
   108 
       
   109         QTest::newRow(QString("pixmap=%1 highQualityAntialiasing=%2 smoothPixmapTransform=%3")
       
   110                       .arg(pixmap).arg(highQualityAntialiasing).arg(smoothPixmapTransform).toAscii().data())
       
   111             << pixmap << highQualityAntialiasing << smoothPixmapTransform;
       
   112     }
       
   113 }
       
   114 
       
   115 void OpenGLBench::imageDrawing()
       
   116 {
       
   117     QFETCH(bool, smoothPixmapTransform);
       
   118     QFETCH(bool, highQualityAntialiasing);
       
   119     QFETCH(bool, pixmap);
       
   120 
       
   121     QImage img;
       
   122     QPixmap pm;
       
   123 
       
   124     if (pixmap)
       
   125         pm = QPixmap(800, 800);
       
   126     else
       
   127         img = QImage(800, 800, QImage::Format_ARGB32_Premultiplied);
       
   128 
       
   129     QPainter p(pb);
       
   130     p.setRenderHint(QPainter::SmoothPixmapTransform, smoothPixmapTransform);
       
   131     p.setRenderHint(QPainter::Antialiasing, highQualityAntialiasing);
       
   132     p.setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
       
   133 
       
   134     QBENCHMARK {
       
   135         if (pixmap) {
       
   136             pm.detach();
       
   137             p.drawPixmap(0, 0, pm);
       
   138         } else {
       
   139             img.detach();
       
   140             p.drawImage(0, 0, img);
       
   141         }
       
   142     }
       
   143 }
       
   144 
       
   145 Q_DECLARE_METATYPE(QPainterPath)
       
   146 
       
   147 void OpenGLBench::pathDrawing_data()
       
   148 {
       
   149     QTest::addColumn<QPainterPath>("path");
       
   150     QTest::addColumn<bool>("highQualityAntialiasing");
       
   151 
       
   152     QList<QPair<QPainterPath, QLatin1String> > paths;
       
   153 
       
   154     {
       
   155         QPainterPath path;
       
   156         path.addRect(-100, -100, 200, 200);
       
   157         paths << qMakePair(path, QLatin1String("plain rect"));
       
   158     }
       
   159 
       
   160     {
       
   161         QPainterPath path;
       
   162         path.addRoundedRect(-100, -100, 200, 200, 50, 50);
       
   163         paths << qMakePair(path, QLatin1String("rounded rect"));
       
   164     }
       
   165 
       
   166     {
       
   167         QPainterPath path;
       
   168         path.addEllipse(-100, -100, 200, 200);
       
   169         paths << qMakePair(path, QLatin1String("ellipse"));
       
   170     }
       
   171 
       
   172     for (int j = 0; j < (1 << 1); ++j) {
       
   173         bool highQualityAntialiasing = j & 1;
       
   174 
       
   175         for (int i = 0; i < paths.size(); ++i) {
       
   176             QTest::newRow(QString("path=%1 highQualityAntialiasing=%2")
       
   177                     .arg(paths[i].second).arg(highQualityAntialiasing).toAscii().data())
       
   178                 << paths[i].first << highQualityAntialiasing;
       
   179         }
       
   180     }
       
   181 }
       
   182 
       
   183 void OpenGLBench::pathDrawing()
       
   184 {
       
   185     QFETCH(QPainterPath, path);
       
   186     QFETCH(bool, highQualityAntialiasing);
       
   187 
       
   188     // warm-up
       
   189     {
       
   190         QPainterPath dummy;
       
   191         dummy.addRect(-1, -1, 2, 2);
       
   192         QPainter p(pb);
       
   193         p.setRenderHint(QPainter::Antialiasing, highQualityAntialiasing);
       
   194         p.setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
       
   195         p.translate(pb->width() / 2, pb->height() / 2);
       
   196         p.rotate(30);
       
   197         p.drawPath(dummy);
       
   198         p.end();
       
   199     }
       
   200 
       
   201     QPainter p(pb);
       
   202     p.setPen(Qt::NoPen);
       
   203     p.setBrush(Qt::black);
       
   204     p.translate(pb->width() / 2, pb->height() / 2);
       
   205 
       
   206     QBENCHMARK {
       
   207         p.setRenderHint(QPainter::Antialiasing, highQualityAntialiasing);
       
   208         p.setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
       
   209 
       
   210         p.rotate(0.01);
       
   211         p.drawPath(path);
       
   212     }
       
   213 }
       
   214 
       
   215 void OpenGLBench::painterOverhead()
       
   216 {
       
   217     QBENCHMARK {
       
   218         QPainter p(pb);
       
   219     }
       
   220 }
       
   221 
       
   222 void OpenGLBench::startupCost_data()
       
   223 {
       
   224     QTest::addColumn<bool>("highQualityAntialiasing");
       
   225 
       
   226     QTest::newRow("highQualityAntialiasing=0") << false;
       
   227     QTest::newRow("highQualityAntialiasing=1") << true;
       
   228 }
       
   229 
       
   230 void OpenGLBench::startupCost()
       
   231 {
       
   232     QFETCH(bool, highQualityAntialiasing);
       
   233     QPainterPath path;
       
   234     path.addRoundedRect(-100, -100, 200, 200, 20, 20);
       
   235     QBENCHMARK {
       
   236         QGLPixelBuffer buffer(512, 512);
       
   237         QPainter p(&buffer);
       
   238         p.setRenderHint(QPainter::Antialiasing, highQualityAntialiasing);
       
   239         p.setRenderHint(QPainter::HighQualityAntialiasing, highQualityAntialiasing);
       
   240 
       
   241         p.translate(buffer.width() / 2, buffer.height() / 2);
       
   242         p.drawPath(path);
       
   243     }
       
   244 }
       
   245 
       
   246 void OpenGLBench::lineDrawing()
       
   247 {
       
   248     QPainter p(pb);
       
   249 
       
   250     QBENCHMARK {
       
   251         p.drawLine(10, 10, 500, 500);
       
   252     }
       
   253 }
       
   254 
       
   255 void OpenGLBench::textDrawing_data()
       
   256 {
       
   257     QTest::addColumn<int>("lines");
       
   258 
       
   259     int lines[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
       
   260 
       
   261     QTest::newRow("text lines=1 (warmup run)") << 1;
       
   262     for (unsigned int i = 0; i < sizeof(lines) / sizeof(int); ++i)
       
   263         QTest::newRow(QString("text lines=%0").arg(lines[i]).toAscii().data()) << lines[i];
       
   264 }
       
   265 
       
   266 void OpenGLBench::textDrawing()
       
   267 {
       
   268     QPainter p(pb);
       
   269 
       
   270     QFETCH(int, lines);
       
   271 
       
   272     p.translate(0, 16);
       
   273     QBENCHMARK {
       
   274         for (int i = 0; i < lines; ++i)
       
   275             p.drawText(0, i, "Hello World!");
       
   276     }
       
   277 }
       
   278 
       
   279 void OpenGLBench::clippedPainting_data()
       
   280 {
       
   281     QTest::addColumn<QPainterPath>("path");
       
   282 
       
   283     QRectF rect = QRectF(0, 0, pb->width(), pb->height()).adjusted(5, 5, -5, -5);
       
   284 
       
   285     {
       
   286         QPainterPath path;
       
   287         path.addRect(rect);
       
   288         QTest::newRow("rect path") << path;
       
   289     }
       
   290 
       
   291     {
       
   292         QPainterPath path;
       
   293         path.addRoundedRect(rect, 5, 5);
       
   294         QTest::newRow("rounded rect path") << path;
       
   295     }
       
   296 
       
   297     {
       
   298         QPainterPath path;
       
   299         path.addEllipse(rect);
       
   300         QTest::newRow("ellipse path") << path;
       
   301     }
       
   302 }
       
   303 
       
   304 void OpenGLBench::clippedPainting()
       
   305 {
       
   306     QFETCH(QPainterPath, path);
       
   307 
       
   308     QBENCHMARK {
       
   309         QPainter p(pb);
       
   310         p.setPen(Qt::NoPen);
       
   311         p.setBrush(Qt::black);
       
   312 
       
   313         p.setClipPath(path);
       
   314         p.drawRect(0, 0, pb->width(), pb->height());
       
   315     }
       
   316 }
       
   317 
       
   318 Q_DECLARE_METATYPE(QGradient::Type)
       
   319 
       
   320 void OpenGLBench::gradients_data()
       
   321 {
       
   322     QTest::addColumn<QGradient::Type>("gradientType");
       
   323     QTest::addColumn<bool>("objectBoundingMode");
       
   324 
       
   325     QTest::newRow("warmup run") << QGradient::LinearGradient << false;
       
   326 
       
   327     QTest::newRow("linear gradient") << QGradient::LinearGradient << false;
       
   328     QTest::newRow("radial gradient") << QGradient::RadialGradient << false;
       
   329     QTest::newRow("conical gradient") << QGradient::ConicalGradient << false;
       
   330 
       
   331     QTest::newRow("linear gradient, object bounding mode") << QGradient::LinearGradient << true;
       
   332     QTest::newRow("radial gradient, object bounding mode") << QGradient::RadialGradient << true;
       
   333     QTest::newRow("conical gradient, object bounding mode") << QGradient::ConicalGradient << true;
       
   334 }
       
   335 
       
   336 void OpenGLBench::gradients()
       
   337 {
       
   338     QFETCH(QGradient::Type, gradientType);
       
   339     QFETCH(bool, objectBoundingMode);
       
   340 
       
   341     QPointF a;
       
   342     QPointF b = objectBoundingMode ? QPointF(1, 1) : QPointF(pb->width(), pb->height());
       
   343 
       
   344     QGradient gradient;
       
   345     switch (gradientType) {
       
   346     case QGradient::LinearGradient:
       
   347         gradient = QLinearGradient(a, b);
       
   348         break;
       
   349     case QGradient::RadialGradient:
       
   350         gradient = QRadialGradient(a, b.x() / 2, b);
       
   351         break;
       
   352     case QGradient::ConicalGradient:
       
   353         gradient = QConicalGradient((a + b)/2, 0);
       
   354         break;
       
   355     default:
       
   356         break;
       
   357     }
       
   358 
       
   359     if (objectBoundingMode)
       
   360         gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
       
   361 
       
   362     gradient.setColorAt(0, Qt::red);
       
   363     gradient.setColorAt(0.2, Qt::blue);
       
   364     gradient.setColorAt(0.4, Qt::transparent);
       
   365     gradient.setColorAt(0.6, Qt::green);
       
   366     gradient.setColorAt(0.8, Qt::black);
       
   367     gradient.setColorAt(1, Qt::white);
       
   368 
       
   369     QPainter p(pb);
       
   370 
       
   371     QBENCHMARK {
       
   372         p.fillRect(0, 0, pb->width(), pb->height(), gradient);
       
   373         glFinish();
       
   374     }
       
   375 }
       
   376 
       
   377 QTEST_MAIN(OpenGLBench)
       
   378 
       
   379 #include "main.moc"