Add wxGCDC benchmarks to graphics benchmark for comparison with wxDC.
[wxWidgets.git] / tests / benchmarks / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: graphics.cpp
3 // Purpose: Some benchmarks for measuring graphics operations performance
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-13
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/app.h"
12 #include "wx/frame.h"
13 #include "wx/cmdline.h"
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include "wx/dcgraph.h"
17 #include "wx/stopwatch.h"
18
19 struct GraphicsBenchmarkOptions
20 {
21 GraphicsBenchmarkOptions()
22 {
23 mapMode = 0;
24 penWidth = 0;
25
26 width = 800;
27 height = 600;
28
29 numLines = 10000;
30 }
31
32 long mapMode,
33 penWidth,
34 width,
35 height,
36 numLines;
37 } opts;
38
39 class GraphicsBenchmarkFrame : public wxFrame
40 {
41 public:
42 GraphicsBenchmarkFrame()
43 : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
44 {
45 Connect(wxEVT_PAINT,
46 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
47
48 m_bitmap.Create(64, 64, 32);
49
50 Show();
51 SetClientSize(opts.width, opts.height);
52
53 wxClientDC dc(this);
54 BenchmarkLines("dc client", dc);
55 BenchmarkRectangles("dc client", dc);
56 BenchmarkBitmaps("dc client", dc);
57
58 wxGCDC gcdc( dc );
59 BenchmarkLines("gcdc client", gcdc);
60 BenchmarkRectangles("gcdc client", gcdc);
61 BenchmarkBitmaps("gcdc client", gcdc);
62
63 wxBitmap bmp(opts.width, opts.height);
64 wxMemoryDC dc2(bmp);
65 BenchmarkLines("dc memory", dc2);
66 BenchmarkRectangles("dc memory", dc2);
67 BenchmarkBitmaps("dc memory", dc2);
68
69 wxGCDC gcdc2( dc2 );
70 BenchmarkLines("gcdc memory", gcdc2);
71 BenchmarkRectangles("gcdc memory", gcdc2);
72 BenchmarkBitmaps("gcdc memory", gcdc2);
73 }
74
75 protected:
76 void OnPaint(wxPaintEvent& WXUNUSED(event))
77 {
78 wxPaintDC dc(this);
79 BenchmarkLines("dc paint", dc);
80 BenchmarkRectangles("dc paint", dc);
81 BenchmarkBitmaps("dc paint", dc);
82
83 wxGCDC gcdc( dc );
84 BenchmarkLines("gcdc paint", gcdc);
85 BenchmarkRectangles("gcdc paint", gcdc);
86 BenchmarkBitmaps("gcdc paint", gcdc);
87
88 wxTheApp->ExitMainLoop();
89 }
90
91
92 void BenchmarkLines(const char *msg, wxDC& dc)
93 {
94 if ( opts.mapMode != 0 )
95 dc.SetMapMode((wxMappingMode)opts.mapMode);
96 if ( opts.penWidth != 0 )
97 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
98
99 wxPrintf("Benchmarking %s DC: ", msg);
100
101 wxStopWatch sw;
102 int x = 0,
103 y = 0;
104 for ( int n = 0; n < opts.numLines; n++ )
105 {
106 int x1 = rand() % opts.width,
107 y1 = rand() % opts.height;
108
109 dc.DrawLine(x, y, x1, y1);
110
111 x = x1;
112 y = y1;
113 }
114
115 const long t = sw.Time();
116
117 wxPrintf("%d lines done in %lums = %gus/line\n",
118 opts.numLines, t, (1000. * t)/opts.numLines);
119 }
120
121
122 void BenchmarkRectangles(const char *msg, wxDC& dc)
123 {
124 if ( opts.mapMode != 0 )
125 dc.SetMapMode((wxMappingMode)opts.mapMode);
126 if ( opts.penWidth != 0 )
127 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
128
129 dc.SetBrush( *wxRED_BRUSH );
130
131 wxPrintf("Benchmarking %s DC: ", msg);
132
133 wxStopWatch sw;
134 for ( int n = 0; n < opts.numLines; n++ )
135 {
136 int x = rand() % opts.width,
137 y = rand() % opts.height;
138
139 dc.DrawRectangle(x, y, 32, 32);
140 }
141
142 const long t = sw.Time();
143
144 wxPrintf("%d rects done in %lums = %gus/rect\n",
145 opts.numLines, t, (1000. * t)/opts.numLines);
146 }
147
148 void BenchmarkBitmaps(const char *msg, wxDC& dc)
149 {
150 if ( opts.mapMode != 0 )
151 dc.SetMapMode((wxMappingMode)opts.mapMode);
152 if ( opts.penWidth != 0 )
153 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
154
155 wxPrintf("Benchmarking %s DC: ", msg);
156
157 wxStopWatch sw;
158 for ( int n = 0; n < opts.numLines; n++ )
159 {
160 int x = rand() % opts.width,
161 y = rand() % opts.height;
162
163 dc.DrawBitmap(m_bitmap, x, y, true);
164 }
165
166 const long t = sw.Time();
167
168 wxPrintf("%d bitmaps done in %lums = %gus/bitmap\n",
169 opts.numLines, t, (1000. * t)/opts.numLines);
170 }
171
172
173 wxBitmap m_bitmap;
174 };
175
176 class GraphicsBenchmarkApp : public wxApp
177 {
178 public:
179 virtual void OnInitCmdLine(wxCmdLineParser& parser)
180 {
181 static const wxCmdLineEntryDesc desc[] =
182 {
183 { wxCMD_LINE_OPTION, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER },
184 { wxCMD_LINE_OPTION, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER },
185 { wxCMD_LINE_OPTION, "w", "width", "", wxCMD_LINE_VAL_NUMBER },
186 { wxCMD_LINE_OPTION, "h", "height", "", wxCMD_LINE_VAL_NUMBER },
187 { wxCMD_LINE_OPTION, "L", "lines", "", wxCMD_LINE_VAL_NUMBER },
188 { wxCMD_LINE_NONE },
189 };
190
191 parser.SetDesc(desc);
192 }
193
194 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
195 {
196 if ( parser.Found("m", &opts.mapMode) &&
197 (opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) )
198 return false;
199 if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 )
200 return false;
201 if ( parser.Found("w", &opts.width) && opts.width < 1 )
202 return false;
203 if ( parser.Found("h", &opts.height) && opts.height < 1 )
204 return false;
205 if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 )
206 return false;
207
208 return true;
209 }
210
211 virtual bool OnInit()
212 {
213 if ( !wxApp::OnInit() )
214 return false;
215
216 new GraphicsBenchmarkFrame;
217
218 return true;
219 }
220 };
221
222 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)