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"
17 #include "wx/stopwatch.h"
20 struct GraphicsBenchmarkOptions
22 GraphicsBenchmarkOptions()
34 testRectangles
= false;
48 class GraphicsBenchmarkFrame
: public wxFrame
51 GraphicsBenchmarkFrame()
52 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Graphics Benchmark")
55 wxPaintEventHandler(GraphicsBenchmarkFrame
::OnPaint
));
57 m_bitmap
.Create(64, 64, 32);
60 SetClientSize(opts
.width
, opts
.height
);
64 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
69 BenchmarkDCAndGC("paint", dc
, gcdc
);
75 BenchmarkDCAndGC("client", dc
, gcdc
);
79 wxBitmap
bmp(opts
.width
, opts
.height
);
82 BenchmarkDCAndGC("memory", dc
, gcdc
);
85 wxTheApp
->ExitMainLoop();
88 void BenchmarkDCAndGC(const char* dckind
, wxDC
& dc
, wxGCDC
& gcdc
)
90 BenchmarkAll(wxString
::Format("%6s DC", dckind
), dc
);
91 BenchmarkAll(wxString
::Format("%6s GC", dckind
), gcdc
);
94 void BenchmarkAll(const wxString
& msg
, wxDC
& dc
)
96 BenchmarkLines(msg
, dc
);
97 BenchmarkRectangles(msg
, dc
);
98 BenchmarkBitmaps(msg
, dc
);
101 void BenchmarkLines(const wxString
& msg
, wxDC
& dc
)
103 if ( !opts
.testLines
)
106 if ( opts
.mapMode
!= 0 )
107 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
108 if ( opts
.penWidth
!= 0 )
109 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
111 wxPrintf("Benchmarking %s: ", msg
);
117 for ( int n
= 0; n
< opts
.numLines
; n
++ )
119 int x1
= rand() % opts
.width
,
120 y1
= rand() % opts
.height
;
122 dc
.DrawLine(x
, y
, x1
, y1
);
128 const long t
= sw
.Time();
130 wxPrintf("%ld lines done in %ldms = %gus/line\n",
131 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
135 void BenchmarkRectangles(const wxString
& msg
, wxDC
& dc
)
137 if ( !opts
.testRectangles
)
140 if ( opts
.mapMode
!= 0 )
141 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
142 if ( opts
.penWidth
!= 0 )
143 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
145 dc
.SetBrush( *wxRED_BRUSH
);
147 wxPrintf("Benchmarking %s: ", msg
);
151 for ( int n
= 0; n
< opts
.numLines
; n
++ )
153 int x
= rand() % opts
.width
,
154 y
= rand() % opts
.height
;
156 dc
.DrawRectangle(x
, y
, 32, 32);
159 const long t
= sw
.Time();
161 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
162 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
165 void BenchmarkBitmaps(const wxString
& msg
, wxDC
& dc
)
167 if ( !opts
.testBitmaps
)
170 if ( opts
.mapMode
!= 0 )
171 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
172 if ( opts
.penWidth
!= 0 )
173 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
175 wxPrintf("Benchmarking %s: ", msg
);
179 for ( int n
= 0; n
< opts
.numLines
; n
++ )
181 int x
= rand() % opts
.width
,
182 y
= rand() % opts
.height
;
184 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
187 const long t
= sw
.Time();
189 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
190 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
197 class GraphicsBenchmarkApp
: public wxApp
200 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
202 static const wxCmdLineEntryDesc desc
[] =
204 { wxCMD_LINE_SWITCH
, "", "bitmaps" },
205 { wxCMD_LINE_SWITCH
, "", "lines" },
206 { wxCMD_LINE_SWITCH
, "", "rectangles" },
207 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
208 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
209 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
210 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
211 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
215 parser
.SetDesc(desc
);
218 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
220 if ( parser
.Found("m", &opts
.mapMode
) &&
221 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
223 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
225 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
227 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
229 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
232 opts
.testBitmaps
= parser
.Found("bitmaps");
233 opts
.testLines
= parser
.Found("lines");
234 opts
.testRectangles
= parser
.Found("rectangles");
235 if ( !(opts
.testBitmaps
|| opts
.testLines
|| opts
.testRectangles
) )
237 // Do everything by default.
240 opts
.testRectangles
= true;
246 virtual bool OnInit()
248 if ( !wxApp
::OnInit() )
251 new GraphicsBenchmarkFrame
;
257 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)