]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
41083c3463811c455f5d6dac760066ee75921894
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
);
116 for ( int n
= 0; n
< opts
.numLines
; n
++ )
118 int x1
= rand() % opts
.width
,
119 y1
= rand() % opts
.height
;
121 dc
.DrawLine(x
, y
, x1
, y1
);
127 const long t
= sw
.Time();
129 wxPrintf("%ld lines done in %ldms = %gus/line\n",
130 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
134 void BenchmarkRectangles(const wxString
& msg
, wxDC
& dc
)
136 if ( !opts
.testRectangles
)
139 if ( opts
.mapMode
!= 0 )
140 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
141 if ( opts
.penWidth
!= 0 )
142 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
144 dc
.SetBrush( *wxRED_BRUSH
);
146 wxPrintf("Benchmarking %s: ", msg
);
149 for ( int n
= 0; n
< opts
.numLines
; n
++ )
151 int x
= rand() % opts
.width
,
152 y
= rand() % opts
.height
;
154 dc
.DrawRectangle(x
, y
, 32, 32);
157 const long t
= sw
.Time();
159 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
160 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
163 void BenchmarkBitmaps(const wxString
& msg
, wxDC
& dc
)
165 if ( !opts
.testBitmaps
)
168 if ( opts
.mapMode
!= 0 )
169 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
170 if ( opts
.penWidth
!= 0 )
171 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
173 wxPrintf("Benchmarking %s: ", msg
);
176 for ( int n
= 0; n
< opts
.numLines
; n
++ )
178 int x
= rand() % opts
.width
,
179 y
= rand() % opts
.height
;
181 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
184 const long t
= sw
.Time();
186 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
187 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
194 class GraphicsBenchmarkApp
: public wxApp
197 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
199 static const wxCmdLineEntryDesc desc
[] =
201 { wxCMD_LINE_SWITCH
, "", "bitmaps" },
202 { wxCMD_LINE_SWITCH
, "", "lines" },
203 { wxCMD_LINE_SWITCH
, "", "rectangles" },
204 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
205 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
206 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
207 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
208 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
212 parser
.SetDesc(desc
);
215 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
217 if ( parser
.Found("m", &opts
.mapMode
) &&
218 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
220 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
222 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
224 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
226 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
229 opts
.testBitmaps
= parser
.Found("bitmaps");
230 opts
.testLines
= parser
.Found("lines");
231 opts
.testRectangles
= parser
.Found("rectangles");
232 if ( !(opts
.testBitmaps
|| opts
.testLines
|| opts
.testRectangles
) )
234 // Do everything by default.
237 opts
.testRectangles
= true;
243 virtual bool OnInit()
245 if ( !wxApp::OnInit() )
248 new GraphicsBenchmarkFrame
;
254 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)