Ticket #101: blit.cpp

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