| 1 | #include <QDebug>
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <QtGui>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | /*
 | 
|---|
| 6 |  * This test calculates the roundtrip speed of copy offscreen buffer to window
 | 
|---|
| 7 |  * operations. Besides the time necessary for copying pixels, this also includes
 | 
|---|
| 8 |  * the time necessary to fill 2D rectangles in the offscreen buffer, deliver
 | 
|---|
| 9 |  * paint and timer messages and so on.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  * To also get the speed of only the copy operatoins themselves, add
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  *      #define QT_LOG_BLITSPEED
 | 
|---|
| 14 |  *
 | 
|---|
| 15 |  * to the beginning of qapplication_pm.cpp and qwindowsurface_raster.cpp.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * Hint: in order to get the most realistic numbers, let the test run until the
 | 
|---|
| 18 |  * average speed printed in the window title stabilizes (i.e. stops to grow).
 | 
|---|
| 19 |  **/
 | 
|---|
| 20 | 
 | 
|---|
| 21 | ////////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 22 | 
 | 
|---|
| 23 | class MyWidget : public QWidget
 | 
|---|
| 24 | {
 | 
|---|
| 25 | public:
 | 
|---|
| 26 | 
 | 
|---|
| 27 |     MyWidget() : mUpdateTID(0), mStatTID(0), mNumPixels(0)
 | 
|---|
| 28 |     {
 | 
|---|
| 29 |     };
 | 
|---|
| 30 | 
 | 
|---|
| 31 |     ~MyWidget()
 | 
|---|
| 32 |     {
 | 
|---|
| 33 |         unsigned long long elapsed = mElapsedTimer.elapsed();
 | 
|---|
| 34 |         qWarning() << "Stats:";
 | 
|---|
| 35 |         qWarning() << "  total pixels blitted           :" << mNumPixels;
 | 
|---|
| 36 |         qWarning() << "  total time, ms                 :" << elapsed;
 | 
|---|
| 37 |         qWarning() << "  average roundtrip speed, px/ms :" << mNumPixels / elapsed;
 | 
|---|
| 38 |     }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |     void startWork()
 | 
|---|
| 41 |     {
 | 
|---|
| 42 |         mElapsedTimer.start();
 | 
|---|
| 43 |         mUpdateTID = startTimer(0);
 | 
|---|
| 44 |         mStatTID = startTimer(500);
 | 
|---|
| 45 |     }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |     void paintEvent(QPaintEvent *aE)
 | 
|---|
| 48 |     {
 | 
|---|
| 49 |         QPainter p(this);
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         QRect r = aE->rect();
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #if 0
 | 
|---|
| 54 |         QColor c = QColor(rand() / (RAND_MAX / 255),
 | 
|---|
| 55 |                           rand() / (RAND_MAX / 255),
 | 
|---|
| 56 |                           rand() / (RAND_MAX / 255));
 | 
|---|
| 57 | #else
 | 
|---|
| 58 |         QColor c = QColor(rand() / (RAND_MAX / 255),
 | 
|---|
| 59 |                           rand() / (RAND_MAX / 255),
 | 
|---|
| 60 |                           rand() / (RAND_MAX / 255),
 | 
|---|
| 61 |                           50);
 | 
|---|
| 62 | #endif
 | 
|---|
| 63 |         p.fillRect(r, c);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         mNumPixels += r.width() * r.height();
 | 
|---|
| 66 |     }
 | 
|---|
| 67 | 
 | 
|---|
| 68 |     void timerEvent(QTimerEvent *aE)
 | 
|---|
| 69 |     {
 | 
|---|
| 70 |         if (aE->timerId() == mUpdateTID) {
 | 
|---|
| 71 |             int w = width();
 | 
|---|
| 72 |             int h = height();
 | 
|---|
| 73 |             int x = w < 2 ? 0 : rand() / (RAND_MAX / (w - 1));
 | 
|---|
| 74 |             int y = h < 2 ? 0 : rand() / (RAND_MAX / (w - 1));
 | 
|---|
| 75 |             w -= x;
 | 
|---|
| 76 |             h -= y;
 | 
|---|
| 77 |             w = w < 2 ? 1 : rand() / (RAND_MAX / w);
 | 
|---|
| 78 |             h = h < 2 ? 1 : rand() / (RAND_MAX / h);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |             update(x, y, w, h);
 | 
|---|
| 81 |         } else if (aE->timerId() == mStatTID) {
 | 
|---|
| 82 |             unsigned long long elapsed = mElapsedTimer.elapsed();
 | 
|---|
| 83 |             QString title = QLatin1String("Blit Stats: %1 pixels/ms");
 | 
|---|
| 84 |             title = title.arg(mNumPixels / elapsed);
 | 
|---|
| 85 |             setWindowTitle(title);
 | 
|---|
| 86 |         }
 | 
|---|
| 87 |     }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | private:
 | 
|---|
| 90 | 
 | 
|---|
| 91 |     int mUpdateTID;
 | 
|---|
| 92 |     int mStatTID;
 | 
|---|
| 93 |     unsigned long long mNumPixels;
 | 
|---|
| 94 |     QTime mElapsedTimer;
 | 
|---|
| 95 | };
 | 
|---|
| 96 | 
 | 
|---|
| 97 | int main(int argc, char **argv)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |     QApplication app(argc, argv);
 | 
|---|
| 100 |     app.setQuitOnLastWindowClosed(true);
 | 
|---|
| 101 | 
 | 
|---|
| 102 |     srand(QDateTime::currentDateTime().toTime_t());
 | 
|---|
| 103 | 
 | 
|---|
| 104 |     MyWidget widget;
 | 
|---|
| 105 |     widget.resize(300, 300);
 | 
|---|
| 106 |     widget.show();
 | 
|---|
| 107 | 
 | 
|---|
| 108 |     QRect sr = QApplication::desktop()->availableGeometry();
 | 
|---|
| 109 |     QRect gr = widget.frameGeometry();
 | 
|---|
| 110 |     widget.move(sr.width() - gr.width(), sr.height() - gr.height());
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     widget.startWork();
 | 
|---|
| 113 | 
 | 
|---|
| 114 |     if (!app.topLevelWidgets().isEmpty())
 | 
|---|
| 115 |         return app.exec();
 | 
|---|
| 116 | 
 | 
|---|
| 117 |     return 0;
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|