]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
95965dc3fd65b415667f9725a8764c912ab8adec
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
.numLines
; 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
.numLines
, t
, (1000. * t
)/opts
.numLines
);
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
.numLines
; 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
.numLines
, t
, (1000. * t
)/opts
.numLines
);
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
.numLines
; 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
.numLines
, t
, (1000. * t
)/opts
.numLines
);
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 const int numImages
= opts
.numLines
;
235 for ( int n
= 0; n
< numImages
; n
++ )
237 image
.Clear(n
% 256);
238 dc
.DrawBitmap(image
, 0, 0);
241 const long t
= sw
.Time();
243 wxPrintf("%ld images done in %ldms = %gus/image or %d FPS\n",
244 numImages
, t
, (1000. * t
)/numImages
,
245 (1000*numImages
+ t
- 1)/t
);
248 void BenchmarkRawBitmaps(const wxString
& msg
, wxDC
& dc
)
250 if ( !opts
.testRawBitmaps
)
253 if ( opts
.mapMode
!= 0 )
254 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
256 wxPrintf("Benchmarking %s: ", msg
);
259 wxBitmap
bitmap(opts
.width
, opts
.height
, 24);
260 wxNativePixelData
data(bitmap
);
263 const int numImages
= opts
.numLines
;
264 for ( int n
= 0; n
< numImages
; n
++ )
266 const unsigned char c
= n
% 256;
268 wxNativePixelData::Iterator
p(data
);
269 for ( int y
= 0; y
< opts
.height
; ++y
)
271 wxNativePixelData::Iterator rowStart
= p
;
273 for ( int x
= 0; x
< opts
.width
; ++x
)
286 dc
.DrawBitmap(bitmap
, 0, 0);
289 const long t
= sw
.Time();
291 wxPrintf("%ld raw bitmaps done in %ldms = %gus/bitmap or %d FPS\n",
292 numImages
, t
, (1000. * t
)/numImages
,
293 (1000*numImages
+ t
- 1)/t
);
300 class GraphicsBenchmarkApp
: public wxApp
303 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
305 static const wxCmdLineEntryDesc desc
[] =
307 { wxCMD_LINE_SWITCH
, "", "bitmaps" },
308 { wxCMD_LINE_SWITCH
, "", "images" },
309 { wxCMD_LINE_SWITCH
, "", "lines" },
310 { wxCMD_LINE_SWITCH
, "", "rawbmp" },
311 { wxCMD_LINE_SWITCH
, "", "rectangles" },
312 { wxCMD_LINE_SWITCH
, "", "paint" },
313 { wxCMD_LINE_SWITCH
, "", "client" },
314 { wxCMD_LINE_SWITCH
, "", "memory" },
315 { wxCMD_LINE_SWITCH
, "", "dc" },
316 { wxCMD_LINE_SWITCH
, "", "gc" },
317 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
318 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
319 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
320 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
321 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
325 parser
.SetDesc(desc
);
328 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
330 if ( parser
.Found("m", &opts
.mapMode
) &&
331 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
333 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
335 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
337 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
339 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
342 opts
.testBitmaps
= parser
.Found("bitmaps");
343 opts
.testImages
= parser
.Found("images");
344 opts
.testLines
= parser
.Found("lines");
345 opts
.testRawBitmaps
= parser
.Found("rawbmp");
346 opts
.testRectangles
= parser
.Found("rectangles");
347 if ( !(opts
.testBitmaps
|| opts
.testImages
|| opts
.testLines
348 || opts
.testRawBitmaps
|| opts
.testRectangles
) )
350 // Do everything by default.
354 opts
.testRawBitmaps
=
355 opts
.testRectangles
= true;
358 opts
.usePaint
= parser
.Found("paint");
359 opts
.useClient
= parser
.Found("client");
360 opts
.useMemory
= parser
.Found("memory");
361 if ( !(opts
.usePaint
|| opts
.useClient
|| opts
.useMemory
) )
365 opts
.useMemory
= true;
368 opts
.useDC
= parser
.Found("dc");
369 opts
.useGC
= parser
.Found("gc");
370 if ( !(opts
.useDC
|| opts
.useGC
) )
379 virtual bool OnInit()
381 if ( !wxApp::OnInit() )
384 new GraphicsBenchmarkFrame
;
390 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)