]>
git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/graphics.cpp
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()
40 class GraphicsBenchmarkFrame
: public wxFrame
43 GraphicsBenchmarkFrame()
44 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Graphics Benchmark")
47 wxWindowCreateEventHandler(GraphicsBenchmarkFrame::OnCreate
));
49 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint
));
51 m_bitmap
.Create(64, 64, 32);
54 SetClientSize(opts
.width
, opts
.height
);
58 void OnCreate(wxWindowCreateEvent
&)
61 BenchmarkLines("dc client", dc
);
62 BenchmarkRectangles("dc client", dc
);
63 BenchmarkBitmaps("dc client", dc
);
66 BenchmarkLines("gcdc client", gcdc
);
67 BenchmarkRectangles("gcdc client", gcdc
);
68 BenchmarkBitmaps("gcdc client", gcdc
);
70 wxBitmap
bmp(opts
.width
, opts
.height
);
72 BenchmarkLines("dc memory", dc2
);
73 BenchmarkRectangles("dc memory", dc2
);
74 BenchmarkBitmaps("dc memory", dc2
);
77 BenchmarkLines("gcdc memory", gcdc2
);
78 BenchmarkRectangles("gcdc memory", gcdc2
);
79 BenchmarkBitmaps("gcdc memory", gcdc2
);
82 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
85 BenchmarkLines("dc paint", dc
);
86 BenchmarkRectangles("dc paint", dc
);
87 BenchmarkBitmaps("dc paint", dc
);
90 BenchmarkLines("gcdc paint", gcdc
);
91 BenchmarkRectangles("gcdc paint", gcdc
);
92 BenchmarkBitmaps("gcdc paint", gcdc
);
94 wxTheApp
->ExitMainLoop();
98 void BenchmarkLines(const char *msg
, wxDC
& dc
)
100 if ( opts
.mapMode
!= 0 )
101 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
102 if ( opts
.penWidth
!= 0 )
103 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
105 wxPrintf("Benchmarking %s DC: ", msg
);
110 for ( int n
= 0; n
< opts
.numLines
; n
++ )
112 int x1
= rand() % opts
.width
,
113 y1
= rand() % opts
.height
;
115 dc
.DrawLine(x
, y
, x1
, y1
);
121 const long t
= sw
.Time();
123 wxPrintf("%ld lines done in %ldms = %gus/line\n",
124 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
128 void BenchmarkRectangles(const char *msg
, wxDC
& dc
)
130 if ( opts
.mapMode
!= 0 )
131 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
132 if ( opts
.penWidth
!= 0 )
133 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
135 dc
.SetBrush( *wxRED_BRUSH
);
137 wxPrintf("Benchmarking %s DC: ", msg
);
140 for ( int n
= 0; n
< opts
.numLines
; n
++ )
142 int x
= rand() % opts
.width
,
143 y
= rand() % opts
.height
;
145 dc
.DrawRectangle(x
, y
, 32, 32);
148 const long t
= sw
.Time();
150 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
151 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
154 void BenchmarkBitmaps(const char *msg
, wxDC
& dc
)
156 if ( opts
.mapMode
!= 0 )
157 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
158 if ( opts
.penWidth
!= 0 )
159 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
161 wxPrintf("Benchmarking %s DC: ", msg
);
164 for ( int n
= 0; n
< opts
.numLines
; n
++ )
166 int x
= rand() % opts
.width
,
167 y
= rand() % opts
.height
;
169 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
172 const long t
= sw
.Time();
174 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
175 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
182 class GraphicsBenchmarkApp
: public wxApp
185 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
187 static const wxCmdLineEntryDesc desc
[] =
189 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
190 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
191 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
192 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
193 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
197 parser
.SetDesc(desc
);
200 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
202 if ( parser
.Found("m", &opts
.mapMode
) &&
203 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
205 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
207 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
209 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
211 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
217 virtual bool OnInit()
219 if ( !wxApp::OnInit() )
222 new GraphicsBenchmarkFrame
;
228 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)