]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: graphics.cpp | |
3 | // Purpose: Some benchmarks for measuring graphics operations performance | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2008-04-13 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/app.h" | |
12 | #include "wx/frame.h" | |
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" | |
18 | #include "wx/crt.h" | |
19 | ||
20 | struct GraphicsBenchmarkOptions | |
21 | { | |
22 | GraphicsBenchmarkOptions() | |
23 | { | |
24 | mapMode = 0; | |
25 | penWidth = 0; | |
26 | ||
27 | width = 800; | |
28 | height = 600; | |
29 | ||
30 | numLines = 10000; | |
31 | } | |
32 | ||
33 | long mapMode, | |
34 | penWidth, | |
35 | width, | |
36 | height, | |
37 | numLines; | |
38 | } opts; | |
39 | ||
40 | class GraphicsBenchmarkFrame : public wxFrame | |
41 | { | |
42 | public: | |
43 | GraphicsBenchmarkFrame() | |
44 | : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark") | |
45 | { | |
46 | Connect(wxEVT_CREATE, | |
47 | wxWindowCreateEventHandler(GraphicsBenchmarkFrame::OnCreate)); | |
48 | Connect(wxEVT_PAINT, | |
49 | wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint)); | |
50 | ||
51 | m_bitmap.Create(64, 64, 32); | |
52 | ||
53 | Show(); | |
54 | SetClientSize(opts.width, opts.height); | |
55 | } | |
56 | ||
57 | private: | |
58 | void OnCreate(wxWindowCreateEvent&) | |
59 | { | |
60 | wxClientDC dc(this); | |
61 | BenchmarkLines("dc client", dc); | |
62 | BenchmarkRectangles("dc client", dc); | |
63 | BenchmarkBitmaps("dc client", dc); | |
64 | ||
65 | wxGCDC gcdc( dc ); | |
66 | BenchmarkLines("gcdc client", gcdc); | |
67 | BenchmarkRectangles("gcdc client", gcdc); | |
68 | BenchmarkBitmaps("gcdc client", gcdc); | |
69 | ||
70 | wxBitmap bmp(opts.width, opts.height); | |
71 | wxMemoryDC dc2(bmp); | |
72 | BenchmarkLines("dc memory", dc2); | |
73 | BenchmarkRectangles("dc memory", dc2); | |
74 | BenchmarkBitmaps("dc memory", dc2); | |
75 | ||
76 | wxGCDC gcdc2( dc2 ); | |
77 | BenchmarkLines("gcdc memory", gcdc2); | |
78 | BenchmarkRectangles("gcdc memory", gcdc2); | |
79 | BenchmarkBitmaps("gcdc memory", gcdc2); | |
80 | } | |
81 | ||
82 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
83 | { | |
84 | wxPaintDC dc(this); | |
85 | BenchmarkLines("dc paint", dc); | |
86 | BenchmarkRectangles("dc paint", dc); | |
87 | BenchmarkBitmaps("dc paint", dc); | |
88 | ||
89 | wxGCDC gcdc( dc ); | |
90 | BenchmarkLines("gcdc paint", gcdc); | |
91 | BenchmarkRectangles("gcdc paint", gcdc); | |
92 | BenchmarkBitmaps("gcdc paint", gcdc); | |
93 | ||
94 | wxTheApp->ExitMainLoop(); | |
95 | } | |
96 | ||
97 | ||
98 | void BenchmarkLines(const char *msg, wxDC& dc) | |
99 | { | |
100 | if ( opts.mapMode != 0 ) | |
101 | dc.SetMapMode((wxMappingMode)opts.mapMode); | |
102 | if ( opts.penWidth != 0 ) | |
103 | dc.SetPen(wxPen(*wxWHITE, opts.penWidth)); | |
104 | ||
105 | wxPrintf("Benchmarking %s DC: ", msg); | |
106 | ||
107 | wxStopWatch sw; | |
108 | int x = 0, | |
109 | y = 0; | |
110 | for ( int n = 0; n < opts.numLines; n++ ) | |
111 | { | |
112 | int x1 = rand() % opts.width, | |
113 | y1 = rand() % opts.height; | |
114 | ||
115 | dc.DrawLine(x, y, x1, y1); | |
116 | ||
117 | x = x1; | |
118 | y = y1; | |
119 | } | |
120 | ||
121 | const long t = sw.Time(); | |
122 | ||
123 | wxPrintf("%ld lines done in %ldms = %gus/line\n", | |
124 | opts.numLines, t, (1000. * t)/opts.numLines); | |
125 | } | |
126 | ||
127 | ||
128 | void BenchmarkRectangles(const char *msg, wxDC& dc) | |
129 | { | |
130 | if ( opts.mapMode != 0 ) | |
131 | dc.SetMapMode((wxMappingMode)opts.mapMode); | |
132 | if ( opts.penWidth != 0 ) | |
133 | dc.SetPen(wxPen(*wxWHITE, opts.penWidth)); | |
134 | ||
135 | dc.SetBrush( *wxRED_BRUSH ); | |
136 | ||
137 | wxPrintf("Benchmarking %s DC: ", msg); | |
138 | ||
139 | wxStopWatch sw; | |
140 | for ( int n = 0; n < opts.numLines; n++ ) | |
141 | { | |
142 | int x = rand() % opts.width, | |
143 | y = rand() % opts.height; | |
144 | ||
145 | dc.DrawRectangle(x, y, 32, 32); | |
146 | } | |
147 | ||
148 | const long t = sw.Time(); | |
149 | ||
150 | wxPrintf("%ld rects done in %ldms = %gus/rect\n", | |
151 | opts.numLines, t, (1000. * t)/opts.numLines); | |
152 | } | |
153 | ||
154 | void BenchmarkBitmaps(const char *msg, wxDC& dc) | |
155 | { | |
156 | if ( opts.mapMode != 0 ) | |
157 | dc.SetMapMode((wxMappingMode)opts.mapMode); | |
158 | if ( opts.penWidth != 0 ) | |
159 | dc.SetPen(wxPen(*wxWHITE, opts.penWidth)); | |
160 | ||
161 | wxPrintf("Benchmarking %s DC: ", msg); | |
162 | ||
163 | wxStopWatch sw; | |
164 | for ( int n = 0; n < opts.numLines; n++ ) | |
165 | { | |
166 | int x = rand() % opts.width, | |
167 | y = rand() % opts.height; | |
168 | ||
169 | dc.DrawBitmap(m_bitmap, x, y, true); | |
170 | } | |
171 | ||
172 | const long t = sw.Time(); | |
173 | ||
174 | wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n", | |
175 | opts.numLines, t, (1000. * t)/opts.numLines); | |
176 | } | |
177 | ||
178 | ||
179 | wxBitmap m_bitmap; | |
180 | }; | |
181 | ||
182 | class GraphicsBenchmarkApp : public wxApp | |
183 | { | |
184 | public: | |
185 | virtual void OnInitCmdLine(wxCmdLineParser& parser) | |
186 | { | |
187 | static const wxCmdLineEntryDesc desc[] = | |
188 | { | |
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 }, | |
194 | { wxCMD_LINE_NONE }, | |
195 | }; | |
196 | ||
197 | parser.SetDesc(desc); | |
198 | } | |
199 | ||
200 | virtual bool OnCmdLineParsed(wxCmdLineParser& parser) | |
201 | { | |
202 | if ( parser.Found("m", &opts.mapMode) && | |
203 | (opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) ) | |
204 | return false; | |
205 | if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 ) | |
206 | return false; | |
207 | if ( parser.Found("w", &opts.width) && opts.width < 1 ) | |
208 | return false; | |
209 | if ( parser.Found("h", &opts.height) && opts.height < 1 ) | |
210 | return false; | |
211 | if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 ) | |
212 | return false; | |
213 | ||
214 | return true; | |
215 | } | |
216 | ||
217 | virtual bool OnInit() | |
218 | { | |
219 | if ( !wxApp::OnInit() ) | |
220 | return false; | |
221 | ||
222 | new GraphicsBenchmarkFrame; | |
223 | ||
224 | return true; | |
225 | } | |
226 | }; | |
227 | ||
228 | IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp) |