source: tests/blit/blit.cpp@ 812

Last change on this file since 812 was 812, checked in by Dmitry A. Kuminov, 15 years ago

tests/blit: Added transparent fillRect.

File size: 3.2 KB
Line 
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
23class MyWidget : public QWidget
24{
25public:
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
89private:
90
91 int mUpdateTID;
92 int mStatTID;