add painting benchmark
[presencevnc] / test / painting.cpp
1 #include <QtGui>
2 #include <iostream>
3
4 //use this rectangle in source image
5 const int src_xoff = 15;
6 const int src_yoff = 23;
7 const int src_width = 600;
8 const int src_height = 500;
9 const QRect src_rect(src_xoff, src_yoff, src_width, src_height);
10
11
12 class Widget : public QWidget {
13 public:
14         Widget():
15                 QWidget(0)
16         {
17                 msecs = paints = mode = 0;
18                 img = QImage("test.png");
19
20                 QTimer *timer = new QTimer(this);
21                 connect(timer, SIGNAL(timeout()),
22                         this, SLOT(repaint()));
23                 timer->start(150);
24
25         }
26 protected:
27         virtual void paintEvent(QPaintEvent*);
28 private:
29         QImage img;
30         int msecs, paints, mode;
31 };
32
33 void Widget::paintEvent(QPaintEvent*)
34 {
35         QPainter painter(this);
36
37         QTime t;
38         t.start();
39
40         switch(mode) {
41 //scaled (fit to window)
42         case 0: //this is how small updates in 0.6 are done
43                 painter.drawImage(rect(),
44                         img.copy(src_rect)
45                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
46                 break;
47         case 1: //fast transformation
48                 painter.drawImage(rect(),
49                         img.copy(src_rect)
50                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
51                 break;
52         case 2: //tell drawImage() that no further scaling is required
53         //why different from 1?
54                 painter.drawImage(rect().topLeft(),
55                         img.copy(src_rect)
56                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
57                 break;
58         case 3: //avoid scaled()
59                 painter.drawImage(rect(),
60                         img.copy(src_rect));
61                 break;
62         case 4: //avoid copy() and scaled()
63                 painter.drawImage(rect(), img, src_rect);
64                 break;
65
66 // not scaled - repeat preceeding tests with dimensions that just happen to require no transformation
67 // these are all comparable
68         case 5: //this is how small updates in 0.6 are done
69                 painter.drawImage(rect(),
70                         img.copy(rect())
71                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
72                 break;
73         case 6: //fast transformation
74                 painter.drawImage(rect(),
75                         img.copy(rect())
76                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation));
77                 break;
78         case 7: //tell drawImage() that no further scaling is required
79                 painter.drawImage(rect().topLeft(),
80                         img.copy(rect())
81                         .scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
82                 break;
83         case 8: //avoid scaled()
84                 painter.drawImage(rect(),
85                         img.copy(rect()));
86                 break;
87         case 9: //avoid copy() and scaled()
88                 painter.drawImage(rect(), img, rect());
89                 break;
90 //and a few extra
91         case 10:
92                 painter.drawImage(rect().topLeft(), img, rect());
93                 break;
94         case 11:
95                 painter.drawImage(rect().topLeft(), img);
96                 break;
97         case 12:
98                 painter.drawImage(rect().topLeft(),
99                         img.copy(rect()));
100                 break;
101         case 13: //5 with KeepAspectRatio
102                 painter.drawImage(rect(),
103                         img.copy(rect())
104                         .scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
105                 break;
106         }
107
108         msecs += t.elapsed();
109         paints++;
110
111         const int paints_per_mode = 20;
112         const int num_modes = 14;
113
114         if(paints > paints_per_mode) {
115                 std::cout << mode << ":\t" << double(msecs)/paints << " msecs per paint\n";
116                 msecs = paints = 0;
117                 mode++;
118                 if(mode >= num_modes)
119                         close();
120         }
121
122 }
123
124 int main(int argc, char* argv[])
125 {
126         QApplication app(argc, argv);
127
128         Widget w;
129         w.show();
130         
131         return app.exec();
132 }