]> git.saurik.com Git - wxWidgets.git/blame - tests/benchmarks/graphics.cpp
Update of OpenVMS compile support
[wxWidgets.git] / tests / benchmarks / graphics.cpp
CommitLineData
e0257cc5
VZ
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>
526954c5 8// Licence: wxWindows licence
e0257cc5
VZ
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"
2a408a4b 16#include "wx/dcgraph.h"
e0257cc5 17#include "wx/stopwatch.h"
f8497215 18#include "wx/crt.h"
e0257cc5
VZ
19
20struct 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
40class GraphicsBenchmarkFrame : public wxFrame
41{
42public:
43 GraphicsBenchmarkFrame()
44 : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
45 {
f8497215
PC
46 Connect(wxEVT_CREATE,
47 wxWindowCreateEventHandler(GraphicsBenchmarkFrame::OnCreate));
e0257cc5
VZ
48 Connect(wxEVT_PAINT,
49 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
50
f8497215 51 m_bitmap.Create(64, 64, 32);
2a408a4b 52
e0257cc5
VZ
53 Show();
54 SetClientSize(opts.width, opts.height);
f8497215 55 }
2a408a4b 56
f8497215
PC
57private:
58 void OnCreate(wxWindowCreateEvent&)
59 {
e0257cc5 60 wxClientDC dc(this);
2a408a4b
VZ
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);
e0257cc5
VZ
69
70 wxBitmap bmp(opts.width, opts.height);
71 wxMemoryDC dc2(bmp);
2a408a4b
VZ
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);
e0257cc5
VZ
80 }
81
e0257cc5
VZ
82 void OnPaint(wxPaintEvent& WXUNUSED(event))
83 {
84 wxPaintDC dc(this);
2a408a4b
VZ
85 BenchmarkLines("dc paint", dc);
86 BenchmarkRectangles("dc paint", dc);
87 BenchmarkBitmaps("dc paint", dc);
e0257cc5 88
2a408a4b
VZ
89 wxGCDC gcdc( dc );
90 BenchmarkLines("gcdc paint", gcdc);
91 BenchmarkRectangles("gcdc paint", gcdc);
92 BenchmarkBitmaps("gcdc paint", gcdc);
e0257cc5
VZ
93
94 wxTheApp->ExitMainLoop();
95 }
96
2a408a4b 97
e0257cc5
VZ
98 void BenchmarkLines(const char *msg, wxDC& dc)
99 {
100 if ( opts.mapMode != 0 )
2a408a4b 101 dc.SetMapMode((wxMappingMode)opts.mapMode);
e0257cc5
VZ
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
f8497215 123 wxPrintf("%ld lines done in %ldms = %gus/line\n",
e0257cc5
VZ
124 opts.numLines, t, (1000. * t)/opts.numLines);
125 }
2a408a4b
VZ
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
f8497215 150 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
2a408a4b
VZ
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
f8497215 174 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
2a408a4b
VZ
175 opts.numLines, t, (1000. * t)/opts.numLines);
176 }
177
178
179 wxBitmap m_bitmap;
e0257cc5
VZ
180};
181
182class GraphicsBenchmarkApp : public wxApp
183{
184public:
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 },
2a408a4b 194 { wxCMD_LINE_NONE },
e0257cc5
VZ
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
228IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)