]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
7d4184fa4a402b7969a31713715f334847df60ee
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 wxWindowCreateEventHandler(GraphicsBenchmarkFrame::OnCreate
));
57 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint
));
59 m_bitmap
.Create(64, 64, 32);
62 SetClientSize(opts
.width
, opts
.height
);
66 void OnCreate(wxWindowCreateEvent
&)
69 BenchmarkLines("dc client", dc
);
70 BenchmarkRectangles("dc client", dc
);
71 BenchmarkBitmaps("dc client", dc
);
74 BenchmarkLines("gcdc client", gcdc
);
75 BenchmarkRectangles("gcdc client", gcdc
);
76 BenchmarkBitmaps("gcdc client", gcdc
);
78 wxBitmap
bmp(opts
.width
, opts
.height
);
80 BenchmarkLines("dc memory", dc2
);
81 BenchmarkRectangles("dc memory", dc2
);
82 BenchmarkBitmaps("dc memory", dc2
);
85 BenchmarkLines("gcdc memory", gcdc2
);
86 BenchmarkRectangles("gcdc memory", gcdc2
);
87 BenchmarkBitmaps("gcdc memory", gcdc2
);
90 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
93 BenchmarkLines("dc paint", dc
);
94 BenchmarkRectangles("dc paint", dc
);
95 BenchmarkBitmaps("dc paint", dc
);
98 BenchmarkLines("gcdc paint", gcdc
);
99 BenchmarkRectangles("gcdc paint", gcdc
);
100 BenchmarkBitmaps("gcdc paint", gcdc
);
102 wxTheApp
->ExitMainLoop();
106 void BenchmarkLines(const char *msg
, wxDC
& dc
)
108 if ( !opts
.testLines
)
111 if ( opts
.mapMode
!= 0 )
112 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
113 if ( opts
.penWidth
!= 0 )
114 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
116 wxPrintf("Benchmarking %s DC: ", msg
);
121 for ( int n
= 0; n
< opts
.numLines
; n
++ )
123 int x1
= rand() % opts
.width
,
124 y1
= rand() % opts
.height
;
126 dc
.DrawLine(x
, y
, x1
, y1
);
132 const long t
= sw
.Time();
134 wxPrintf("%ld lines done in %ldms = %gus/line\n",
135 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
139 void BenchmarkRectangles(const char *msg
, wxDC
& dc
)
141 if ( !opts
.testRectangles
)
144 if ( opts
.mapMode
!= 0 )
145 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
146 if ( opts
.penWidth
!= 0 )
147 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
149 dc
.SetBrush( *wxRED_BRUSH
);
151 wxPrintf("Benchmarking %s DC: ", msg
);
154 for ( int n
= 0; n
< opts
.numLines
; n
++ )
156 int x
= rand() % opts
.width
,
157 y
= rand() % opts
.height
;
159 dc
.DrawRectangle(x
, y
, 32, 32);
162 const long t
= sw
.Time();
164 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
165 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
168 void BenchmarkBitmaps(const char *msg
, wxDC
& dc
)
170 if ( !opts
.testBitmaps
)
173 if ( opts
.mapMode
!= 0 )
174 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
175 if ( opts
.penWidth
!= 0 )
176 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
178 wxPrintf("Benchmarking %s DC: ", msg
);
181 for ( int n
= 0; n
< opts
.numLines
; n
++ )
183 int x
= rand() % opts
.width
,
184 y
= rand() % opts
.height
;
186 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
189 const long t
= sw
.Time();
191 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
192 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
199 class GraphicsBenchmarkApp
: public wxApp
202 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
204 static const wxCmdLineEntryDesc desc
[] =
206 { wxCMD_LINE_SWITCH
, "", "bitmaps" },
207 { wxCMD_LINE_SWITCH
, "", "lines" },
208 { wxCMD_LINE_SWITCH
, "", "rectangles" },
209 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
210 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
211 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
212 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
213 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
217 parser
.SetDesc(desc
);
220 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
222 if ( parser
.Found("m", &opts
.mapMode
) &&
223 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
225 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
227 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
229 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
231 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
234 opts
.testBitmaps
= parser
.Found("bitmaps");
235 opts
.testLines
= parser
.Found("lines");
236 opts
.testRectangles
= parser
.Found("rectangles");
237 if ( !(opts
.testBitmaps
|| opts
.testLines
|| opts
.testRectangles
) )
239 // Do everything by default.
242 opts
.testRectangles
= true;
248 virtual bool OnInit()
250 if ( !wxApp::OnInit() )
253 new GraphicsBenchmarkFrame
;
259 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)