| [436] | 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 | QColor c = QColor(rand() / (RAND_MAX / 255), | 
|---|
|  | 54 | rand() / (RAND_MAX / 255), | 
|---|
|  | 55 | rand() / (RAND_MAX / 255)); | 
|---|
|  | 56 | p.fillRect(r, c); | 
|---|
|  | 57 |  | 
|---|
|  | 58 | mNumPixels += r.width() * r.height(); | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | void timerEvent(QTimerEvent *aE) | 
|---|
|  | 62 | { | 
|---|
|  | 63 | if (aE->timerId() == mUpdateTID) { | 
|---|
|  | 64 | int w = width(); | 
|---|
|  | 65 | int h = height(); | 
|---|
|  | 66 | int x = w < 2 ? 0 : rand() / (RAND_MAX / (w - 1)); | 
|---|
|  | 67 | int y = h < 2 ? 0 : rand() / (RAND_MAX / (w - 1)); | 
|---|
|  | 68 | w -= x; | 
|---|
|  | 69 | h -= y; | 
|---|
|  | 70 | w = w < 2 ? 1 : rand() / (RAND_MAX / w); | 
|---|
|  | 71 | h = h < 2 ? 1 : rand() / (RAND_MAX / h); | 
|---|
|  | 72 |  | 
|---|
|  | 73 | update(x, y, w, h); | 
|---|
|  | 74 | } else if (aE->timerId() == mStatTID) { | 
|---|
|  | 75 | unsigned long long elapsed = mElapsedTimer.elapsed(); | 
|---|
|  | 76 | QString title = QLatin1String("Blit Stats: %1 pixels/ms"); | 
|---|
|  | 77 | title = title.arg(mNumPixels / elapsed); | 
|---|
|  | 78 | setWindowTitle(title); | 
|---|
|  | 79 | } | 
|---|
|  | 80 | } | 
|---|
|  | 81 |  | 
|---|
|  | 82 | private: | 
|---|
|  | 83 |  | 
|---|
|  | 84 | int mUpdateTID; | 
|---|
|  | 85 | int mStatTID; | 
|---|
|  | 86 | unsigned long long mNumPixels; | 
|---|
|  | 87 | QTime mElapsedTimer; | 
|---|
|  | 88 | }; | 
|---|
|  | 89 |  | 
|---|
|  | 90 | int main(int argc, char **argv) | 
|---|
|  | 91 | { | 
|---|
|  | 92 | QApplication app(argc, argv); | 
|---|
|  | 93 | app.setQuitOnLastWindowClosed(true); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | srand(QDateTime::currentDateTime().toTime_t()); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | MyWidget widget; | 
|---|
|  | 98 | widget.resize(300, 300); | 
|---|
|  | 99 | widget.show(); | 
|---|
|  | 100 |  | 
|---|
|  | 101 | QRect sr = QApplication::desktop()->availableGeometry(); | 
|---|
|  | 102 | QRect gr = widget.frameGeometry(); | 
|---|
|  | 103 | widget.move(sr.width() - gr.width(), sr.height() - gr.height()); | 
|---|
|  | 104 |  | 
|---|
|  | 105 | widget.startWork(); | 
|---|
|  | 106 |  | 
|---|
|  | 107 | if (!app.topLevelWidgets().isEmpty()) | 
|---|
|  | 108 | return app.exec(); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | return 0; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|