]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
5ee262844dca9ad0bba434ea64be3ca05030c797
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;
62 class GraphicsBenchmarkFrame
: public wxFrame
65 GraphicsBenchmarkFrame()
66 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Graphics Benchmark")
69 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint
));
71 m_bitmap
.Create(64, 64, 32);
74 SetClientSize(opts
.width
, opts
.height
);
78 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
84 BenchmarkDCAndGC("paint", dc
, gcdc
);
91 BenchmarkDCAndGC("client", dc
, gcdc
);
96 wxBitmap
bmp(opts
.width
, opts
.height
);
99 BenchmarkDCAndGC("memory", dc
, gcdc
);
102 wxTheApp
->ExitMainLoop();
105 void BenchmarkDCAndGC(const char* dckind
, wxDC
& dc
, wxGCDC
& gcdc
)
108 BenchmarkAll(wxString::Format("%6s DC", dckind
), dc
);
110 BenchmarkAll(wxString::Format("%6s GC", dckind
), gcdc
);
113 void BenchmarkAll(const wxString
& msg
, wxDC
& dc
)
115 BenchmarkLines(msg
, dc
);
116 BenchmarkRectangles(msg
, dc
);
117 BenchmarkBitmaps(msg
, dc
);
120 void BenchmarkLines(const wxString
& msg
, wxDC
& dc
)
122 if ( !opts
.testLines
)
125 if ( opts
.mapMode
!= 0 )
126 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
127 if ( opts
.penWidth
!= 0 )
128 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
130 wxPrintf("Benchmarking %s: ", msg
);
136 for ( int n
= 0; n
< opts
.numLines
; n
++ )
138 int x1
= rand() % opts
.width
,
139 y1
= rand() % opts
.height
;
141 dc
.DrawLine(x
, y
, x1
, y1
);
147 const long t
= sw
.Time();
149 wxPrintf("%ld lines done in %ldms = %gus/line\n",
150 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
154 void BenchmarkRectangles(const wxString
& msg
, wxDC
& dc
)
156 if ( !opts
.testRectangles
)
159 if ( opts
.mapMode
!= 0 )
160 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
161 if ( opts
.penWidth
!= 0 )
162 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
164 dc
.SetBrush( *wxRED_BRUSH
);
166 wxPrintf("Benchmarking %s: ", msg
);
170 for ( int n
= 0; n
< opts
.numLines
; n
++ )
172 int x
= rand() % opts
.width
,
173 y
= rand() % opts
.height
;
175 dc
.DrawRectangle(x
, y
, 32, 32);
178 const long t
= sw
.Time();
180 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
181 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
184 void BenchmarkBitmaps(const wxString
& msg
, wxDC
& dc
)
186 if ( !opts
.testBitmaps
)
189 if ( opts
.mapMode
!= 0 )
190 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
191 if ( opts
.penWidth
!= 0 )
192 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
194 wxPrintf("Benchmarking %s: ", msg
);
198 for ( int n
= 0; n
< opts
.numLines
; n
++ )
200 int x
= rand() % opts
.width
,
201 y
= rand() % opts
.height
;
203 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
206 const long t
= sw
.Time();
208 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
209 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
216 class GraphicsBenchmarkApp
: public wxApp
219 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
221 static const wxCmdLineEntryDesc desc
[] =
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
},
239 parser
.SetDesc(desc
);
242 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
244 if ( parser
.Found("m", &opts
.mapMode
) &&
245 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
247 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
249 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
251 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
253 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
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
) )
261 // Do everything by default.
264 opts
.testRectangles
= true;
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
) )
274 opts
.useMemory
= true;
277 opts
.useDC
= parser
.Found("dc");
278 opts
.useGC
= parser
.Found("gc");
279 if ( !(opts
.useDC
|| opts
.useGC
) )
288 virtual bool OnInit()
290 if ( !wxApp::OnInit() )
293 new GraphicsBenchmarkFrame
;
299 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)