]> git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
5ee262844dca9ad0bba434ea64be3ca05030c797
[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 #include "wx/crt.h"
19
20 struct GraphicsBenchmarkOptions
21 {
22 GraphicsBenchmarkOptions()
23 {
24 mapMode = 0;
25 penWidth = 0;
26
27 width = 800;
28 height = 600;
29
30 numLines = 10000;
31
32 testBitmaps =
33 testLines =
34 testRectangles = false;
35
36 usePaint =
37 useClient =
38 useMemory = false;
39
40 useDC =
41 useGC = false;
42 }
43
44 long mapMode,
45 penWidth,
46 width,
47 height,
48 numLines;
49
50 bool testBitmaps,
51 testLines,
52 testRectangles;
53
54 bool usePaint,
55 useClient,
56 useMemory;
57
58 bool useDC,
59 useGC;
60 } opts;
61
62 class GraphicsBenchmarkFrame : public wxFrame
63 {
64 public:
65 GraphicsBenchmarkFrame()
66 : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
67 {
68 Connect(wxEVT_PAINT,
69 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
70
71 m_bitmap.Create(64, 64, 32);
72
73 Show();
74 SetClientSize(opts.width, opts.height);
75 }
76
77 private:
78 void OnPaint(wxPaintEvent& WXUNUSED(event))
79 {
80 if ( opts.usePaint )
81 {
82 wxPaintDC dc(this);
83 wxGCDC gcdc(dc);
84 BenchmarkDCAndGC("paint", dc, gcdc);
85 }
86
87 if ( opts.useClient )
88 {
89 wxClientDC dc(this);
90 wxGCDC gcdc(dc);
91 BenchmarkDCAndGC("client", dc, gcdc);
92 }
93
94 if ( opts.useMemory )
95 {
96 wxBitmap bmp(opts.width, opts.height);
97 wxMemoryDC dc(bmp);
98 wxGCDC gcdc(dc);
99 BenchmarkDCAndGC("memory", dc, gcdc);
100 }
101
102 wxTheApp->ExitMainLoop();
103 }
104
105 void BenchmarkDCAndGC(const char* dckind, wxDC& dc, wxGCDC& gcdc)
106 {
107 if ( opts.useDC )
108 BenchmarkAll(wxString::Format("%6s DC", dckind), dc);
109 if ( opts.useGC )
110 BenchmarkAll(wxString::Format("%6s GC", dckind), gcdc);
111 }
112
113 void BenchmarkAll(const wxString& msg, wxDC& dc)
114 {
115 BenchmarkLines(msg, dc);
116 BenchmarkRectangles(msg, dc);
117 BenchmarkBitmaps(msg, dc);
118 }
119
120 void BenchmarkLines(const wxString& msg, wxDC& dc)
121 {
122 if ( !opts.testLines )
123 return;
124
125 if ( opts.mapMode != 0 )
126 dc.SetMapMode((wxMappingMode)opts.mapMode);
127 if ( opts.penWidth != 0 )
128 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
129
130 wxPrintf("Benchmarking %s: ", msg);
131 fflush(stdout);
132
133 wxStopWatch sw;
134 int x = 0,
135 y = 0;
136 for ( int n = 0; n < opts.numLines; n++ )
137 {
138 int x1 = rand() % opts.width,
139 y1 = rand() % opts.height;
140
141 dc.DrawLine(x, y, x1, y1);
142
143 x = x1;
144 y = y1;
145 }
146
147 const long t = sw.Time();
148
149 wxPrintf("%ld lines done in %ldms = %gus/line\n",
150 opts.numLines, t, (1000. * t)/opts.numLines);
151 }
152
153
154 void BenchmarkRectangles(const wxString& msg, wxDC& dc)
155 {
156 if ( !opts.testRectangles )
157 return;
158
159 if ( opts.mapMode != 0 )
160 dc.SetMapMode((wxMappingMode)opts.mapMode);
161 if ( opts.penWidth != 0 )
162 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
163
164 dc.SetBrush( *wxRED_BRUSH );
165
166 wxPrintf("Benchmarking %s: ", msg);
167 fflush(stdout);
168
169 wxStopWatch sw;
170 for ( int n = 0; n < opts.numLines; n++ )
171 {
172 int x = rand() % opts.width,
173 y = rand() % opts.height;
174
175 dc.DrawRectangle(x, y, 32, 32);
176 }
177
178 const long t = sw.Time();
179
180 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
181 opts.numLines, t, (1000. * t)/opts.numLines);
182 }
183
184 void BenchmarkBitmaps(const wxString& msg, wxDC& dc)
185 {
186 if ( !opts.testBitmaps )
187 return;
188
189 if ( opts.mapMode != 0 )
190 dc.SetMapMode((wxMappingMode)opts.mapMode);
191 if ( opts.penWidth != 0 )
192 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
193
194 wxPrintf("Benchmarking %s: ", msg);
195 fflush(stdout);
196
197 wxStopWatch sw;
198 for ( int n = 0; n < opts.numLines; n++ )
199 {
200 int x = rand() % opts.width,
201 y = rand() % opts.height;
202
203 dc.DrawBitmap(m_bitmap, x, y, true);
204 }
205
206 const long t = sw.Time();
207
208 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
209 opts.numLines, t, (1000. * t)/opts.numLines);
210 }
211
212
213 wxBitmap m_bitmap;
214 };
215
216 class GraphicsBenchmarkApp : public wxApp
217 {
218 public:
219 virtual void OnInitCmdLine(wxCmdLineParser& parser)
220 {
221 static const wxCmdLineEntryDesc desc[] =
222 {
223 { wxCMD_LINE_SWITCH, "", "bitmaps" },
224 { wxCMD_LINE_SWITCH, "", "lines" },
225 { wxCMD_LINE_SWITCH, "", "rectangles" },
226 { wxCMD_LINE_SWITCH, "", "paint" },
227 { wxCMD_LINE_SWITCH, "", "client" },
228 { wxCMD_LINE_SWITCH, "", "memory" },
229 { wxCMD_LINE_SWITCH, "", "dc" },
230 { wxCMD_LINE_SWITCH, "", "gc" },
231 { wxCMD_LINE_OPTION, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER },
232 { wxCMD_LINE_OPTION, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER },
233 { wxCMD_LINE_OPTION, "w", "width", "", wxCMD_LINE_VAL_NUMBER },
234 { wxCMD_LINE_OPTION, "h", "height", "", wxCMD_LINE_VAL_NUMBER },
235 { wxCMD_LINE_OPTION, "L", "lines", "", wxCMD_LINE_VAL_NUMBER },
236 { wxCMD_LINE_NONE },
237 };
238
239 parser.SetDesc(desc);
240 }
241
242 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
243 {
244 if ( parser.Found("m", &opts.mapMode) &&
245 (opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) )
246 return false;
247 if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 )
248 return false;
249 if ( parser.Found("w", &opts.width) && opts.width < 1 )
250 return false;
251 if ( parser.Found("h", &opts.height) && opts.height < 1 )
252 return false;
253 if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 )
254 return false;
255
256 opts.testBitmaps = parser.Found("bitmaps");
257 opts.testLines = parser.Found("lines");
258 opts.testRectangles = parser.Found("rectangles");
259 if ( !(opts.testBitmaps || opts.testLines || opts.testRectangles) )
260 {
261 // Do everything by default.
262 opts.testBitmaps =
263 opts.testLines =
264 opts.testRectangles = true;
265 }
266
267 opts.usePaint = parser.Found("paint");
268 opts.useClient = parser.Found("client");
269 opts.useMemory = parser.Found("memory");
270 if ( !(opts.usePaint || opts.useClient || opts.useMemory) )
271 {
272 opts.usePaint =
273 opts.useClient =
274 opts.useMemory = true;
275 }
276
277 opts.useDC = parser.Found("dc");
278 opts.useGC = parser.Found("gc");
279 if ( !(opts.useDC || opts.useGC) )
280 {
281 opts.useDC =
282 opts.useGC = true;
283 }
284
285 return true;
286 }
287
288 virtual bool OnInit()
289 {
290 if ( !wxApp::OnInit() )
291 return false;
292
293 new GraphicsBenchmarkFrame;
294
295 return true;
296 }
297 };
298
299 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)