]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
a5f9a49705efc6864cdeb3c1349e8372aa8c2142
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Some benchmarks for measuring graphics operations performance
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/cmdline.h"
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include "wx/dcgraph.h"
18 #include "wx/rawbmp.h"
19 #include "wx/stopwatch.h"
22 struct GraphicsBenchmarkOptions
24 GraphicsBenchmarkOptions()
38 testRectangles
= false;
68 class GraphicsBenchmarkFrame
: public wxFrame
71 GraphicsBenchmarkFrame()
72 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Graphics Benchmark")
75 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint
));
77 m_bitmap
.Create(64, 64, 32);
80 SetClientSize(opts
.width
, opts
.height
);
84 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
90 BenchmarkDCAndGC("paint", dc
, gcdc
);
97 BenchmarkDCAndGC("client", dc
, gcdc
);
100 if ( opts
.useMemory
)
102 wxBitmap
bmp(opts
.width
, opts
.height
);
105 BenchmarkDCAndGC("memory", dc
, gcdc
);
108 wxTheApp
->ExitMainLoop();
111 void BenchmarkDCAndGC(const char* dckind
, wxDC
& dc
, wxGCDC
& gcdc
)
114 BenchmarkAll(wxString::Format("%6s DC", dckind
), dc
);
116 BenchmarkAll(wxString::Format("%6s GC", dckind
), gcdc
);
119 void BenchmarkAll(const wxString
& msg
, wxDC
& dc
)
121 BenchmarkBitmaps(msg
, dc
);
122 BenchmarkImages(msg
, dc
);
123 BenchmarkLines(msg
, dc
);
124 BenchmarkRawBitmaps(msg
, dc
);
125 BenchmarkRectangles(msg
, dc
);
128 void BenchmarkLines(const wxString
& msg
, wxDC
& dc
)
130 if ( !opts
.testLines
)
133 if ( opts
.mapMode
!= 0 )
134 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
135 if ( opts
.penWidth
!= 0 )
136 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
138 wxPrintf("Benchmarking %s: ", msg
);
144 for ( int n
= 0; n
< opts
.numIters
; n
++ )
146 int x1
= rand() % opts
.width
,
147 y1
= rand() % opts
.height
;
149 dc
.DrawLine(x
, y
, x1
, y1
);
155 const long t
= sw
.Time();
157 wxPrintf("%ld lines done in %ldms = %gus/line\n",
158 opts
.numIters
, t
, (1000. * t
)/opts
.numIters
);
162 void BenchmarkRectangles(const wxString
& msg
, wxDC
& dc
)
164 if ( !opts
.testRectangles
)
167 if ( opts
.mapMode
!= 0 )
168 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
169 if ( opts
.penWidth
!= 0 )
170 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
172 dc
.SetBrush( *wxRED_BRUSH
);
174 wxPrintf("Benchmarking %s: ", msg
);
178 for ( int n
= 0; n
< opts
.numIters
; n
++ )
180 int x
= rand() % opts
.width
,
181 y
= rand() % opts
.height
;
183 dc
.DrawRectangle(x
, y
, 32, 32);
186 const long t
= sw
.Time();
188 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
189 opts
.numIters
, t
, (1000. * t
)/opts
.numIters
);
192 void BenchmarkBitmaps(const wxString
& msg
, wxDC
& dc
)
194 if ( !opts
.testBitmaps
)
197 if ( opts
.mapMode
!= 0 )
198 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
199 if ( opts
.penWidth
!= 0 )
200 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
202 wxPrintf("Benchmarking %s: ", msg
);
206 for ( int n
= 0; n
< opts
.numIters
; n
++ )
208 int x
= rand() % opts
.width
,
209 y
= rand() % opts
.height
;
211 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
214 const long t
= sw
.Time();
216 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
217 opts
.numIters
, t
, (1000. * t
)/opts
.numIters
);
220 void BenchmarkImages(const wxString
& msg
, wxDC
& dc
)
222 if ( !opts
.testImages
)
225 if ( opts
.mapMode
!= 0 )
226 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
228 wxPrintf("Benchmarking %s: ", msg
);
231 wxImage
image(wxSize(opts
.width
, opts
.height
), false /* don't clear */);
234 for ( int n
= 0; n
< opts
.numIters
; n
++ )
236 image
.Clear(n
% 256);
237 dc
.DrawBitmap(image
, 0, 0);
240 const long t
= sw
.Time();
242 wxPrintf("%ld images done in %ldms = %gus/image or %d FPS\n",
243 opts
.numIters
, t
, (1000. * t
)/opts
.numIters
,
244 (1000*opts
.numIters
+ t
- 1)/t
);
247 void BenchmarkRawBitmaps(const wxString
& msg
, wxDC
& dc
)
249 if ( !opts
.testRawBitmaps
)
252 if ( opts
.mapMode
!= 0 )
253 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
255 wxPrintf("Benchmarking %s: ", msg
);
258 wxBitmap
bitmap(opts
.width
, opts
.height
, 24);
259 wxNativePixelData
data(bitmap
);
262 for ( int n
= 0; n
< opts
.numIters
; n
++ )
264 const unsigned char c
= n
% 256;
266 wxNativePixelData::Iterator
p(data
);
267 for ( int y
= 0; y
< opts
.height
; ++y
)
269 wxNativePixelData::Iterator rowStart
= p
;
271 for ( int x
= 0; x
< opts
.width
; ++x
)
284 dc
.DrawBitmap(bitmap
, 0, 0);
287 const long t
= sw
.Time();
289 wxPrintf("%ld raw bitmaps done in %ldms = %gus/bitmap or %d FPS\n",
290 opts
.numIters
, t
, (1000. * t
)/opts
.numIters
,
291 (1000*opts
.numIters
+ t
- 1)/t
);
298 class GraphicsBenchmarkApp
: public wxApp
301 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
303 static const wxCmdLineEntryDesc desc
[] =
305 { wxCMD_LINE_SWITCH
, "", "bitmaps" },
306 { wxCMD_LINE_SWITCH
, "", "images" },
307 { wxCMD_LINE_SWITCH
, "", "lines" },
308 { wxCMD_LINE_SWITCH
, "", "rawbmp" },
309 { wxCMD_LINE_SWITCH
, "", "rectangles" },
310 { wxCMD_LINE_SWITCH
, "", "paint" },
311 { wxCMD_LINE_SWITCH
, "", "client" },
312 { wxCMD_LINE_SWITCH
, "", "memory" },
313 { wxCMD_LINE_SWITCH
, "", "dc" },
314 { wxCMD_LINE_SWITCH
, "", "gc" },
315 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
316 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
317 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
318 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
319 { wxCMD_LINE_OPTION
, "I", "images", "", wxCMD_LINE_VAL_NUMBER
},
320 { wxCMD_LINE_OPTION
, "N", "number-of-iterations", "", wxCMD_LINE_VAL_NUMBER
},
324 parser
.SetDesc(desc
);
327 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
329 if ( parser
.Found("m", &opts
.mapMode
) &&
330 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
332 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
334 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
336 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
338 if ( parser
.Found("N", &opts
.numIters
) && opts
.numIters
< 1 )
341 opts
.testBitmaps
= parser
.Found("bitmaps");
342 opts
.testImages
= parser
.Found("images");
343 opts
.testLines
= parser
.Found("lines");
344 opts
.testRawBitmaps
= parser
.Found("rawbmp");
345 opts
.testRectangles
= parser
.Found("rectangles");
346 if ( !(opts
.testBitmaps
|| opts
.testImages
|| opts
.testLines
347 || opts
.testRawBitmaps
|| opts
.testRectangles
) )
349 // Do everything by default.
353 opts
.testRawBitmaps
=
354 opts
.testRectangles
= true;
357 opts
.usePaint
= parser
.Found("paint");
358 opts
.useClient
= parser
.Found("client");
359 opts
.useMemory
= parser
.Found("memory");
360 if ( !(opts
.usePaint
|| opts
.useClient
|| opts
.useMemory
) )
364 opts
.useMemory
= true;
367 opts
.useDC
= parser
.Found("dc");
368 opts
.useGC
= parser
.Found("gc");
369 if ( !(opts
.useDC
|| opts
.useGC
) )
378 virtual bool OnInit()
380 if ( !wxApp::OnInit() )
383 new GraphicsBenchmarkFrame
;
389 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)