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