2008-04-13 01:30:14 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: graphics.cpp
|
|
|
|
// Purpose: Some benchmarks for measuring graphics operations performance
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-04-13
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
|
2010-07-13 13:29:13 +00:00
|
|
|
// Licence: wxWindows licence
|
2008-04-13 01:30:14 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
|
|
|
#include "wx/cmdline.h"
|
|
|
|
#include "wx/dcclient.h"
|
|
|
|
#include "wx/dcmemory.h"
|
2012-03-01 01:05:28 +00:00
|
|
|
#include "wx/dcgraph.h"
|
2008-04-13 01:30:14 +00:00
|
|
|
#include "wx/stopwatch.h"
|
2012-03-01 07:09:43 +00:00
|
|
|
#include "wx/crt.h"
|
2008-04-13 01:30:14 +00:00
|
|
|
|
|
|
|
struct GraphicsBenchmarkOptions
|
|
|
|
{
|
|
|
|
GraphicsBenchmarkOptions()
|
|
|
|
{
|
|
|
|
mapMode = 0;
|
|
|
|
penWidth = 0;
|
|
|
|
|
|
|
|
width = 800;
|
|
|
|
height = 600;
|
|
|
|
|
|
|
|
numLines = 10000;
|
2013-02-05 20:46:53 +00:00
|
|
|
|
|
|
|
testBitmaps =
|
|
|
|
testLines =
|
|
|
|
testRectangles = false;
|
2008-04-13 01:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long mapMode,
|
|
|
|
penWidth,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
numLines;
|
2013-02-05 20:46:53 +00:00
|
|
|
|
|
|
|
bool testBitmaps,
|
|
|
|
testLines,
|
|
|
|
testRectangles;
|
2008-04-13 01:30:14 +00:00
|
|
|
} opts;
|
|
|
|
|
|
|
|
class GraphicsBenchmarkFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GraphicsBenchmarkFrame()
|
|
|
|
: wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
|
|
|
|
{
|
|
|
|
Connect(wxEVT_PAINT,
|
|
|
|
wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
|
|
|
|
|
2012-03-01 07:09:43 +00:00
|
|
|
m_bitmap.Create(64, 64, 32);
|
2012-03-01 01:05:28 +00:00
|
|
|
|
2008-04-13 01:30:14 +00:00
|
|
|
Show();
|
|
|
|
SetClientSize(opts.width, opts.height);
|
2012-03-01 07:09:43 +00:00
|
|
|
}
|
2012-03-01 01:05:28 +00:00
|
|
|
|
2012-03-01 07:09:43 +00:00
|
|
|
private:
|
2008-04-13 01:30:14 +00:00
|
|
|
void OnPaint(wxPaintEvent& WXUNUSED(event))
|
|
|
|
{
|
2013-02-05 20:46:55 +00:00
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
wxGCDC gcdc(dc);
|
|
|
|
BenchmarkDCAndGC("paint", dc, gcdc);
|
|
|
|
}
|
2008-04-13 01:30:14 +00:00
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
{
|
|
|
|
wxClientDC dc(this);
|
|
|
|
wxGCDC gcdc(dc);
|
|
|
|
BenchmarkDCAndGC("client", dc, gcdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
wxBitmap bmp(opts.width, opts.height);
|
|
|
|
wxMemoryDC dc(bmp);
|
|
|
|
wxGCDC gcdc(dc);
|
|
|
|
BenchmarkDCAndGC("memory", dc, gcdc);
|
|
|
|
}
|
2008-04-13 01:30:14 +00:00
|
|
|
|
|
|
|
wxTheApp->ExitMainLoop();
|
|
|
|
}
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
void BenchmarkDCAndGC(const char* dckind, wxDC& dc, wxGCDC& gcdc)
|
|
|
|
{
|
|
|
|
BenchmarkAll(wxString::Format("%6s DC", dckind), dc);
|
|
|
|
BenchmarkAll(wxString::Format("%6s GC", dckind), gcdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BenchmarkAll(const wxString& msg, wxDC& dc)
|
|
|
|
{
|
|
|
|
BenchmarkLines(msg, dc);
|
|
|
|
BenchmarkRectangles(msg, dc);
|
|
|
|
BenchmarkBitmaps(msg, dc);
|
|
|
|
}
|
2012-03-01 01:05:28 +00:00
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
void BenchmarkLines(const wxString& msg, wxDC& dc)
|
2008-04-13 01:30:14 +00:00
|
|
|
{
|
2013-02-05 20:46:53 +00:00
|
|
|
if ( !opts.testLines )
|
|
|
|
return;
|
|
|
|
|
2008-04-13 01:30:14 +00:00
|
|
|
if ( opts.mapMode != 0 )
|
2012-03-01 01:05:28 +00:00
|
|
|
dc.SetMapMode((wxMappingMode)opts.mapMode);
|
2008-04-13 01:30:14 +00:00
|
|
|
if ( opts.penWidth != 0 )
|
|
|
|
dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
wxPrintf("Benchmarking %s: ", msg);
|
2013-02-05 20:46:57 +00:00
|
|
|
fflush(stdout);
|
2008-04-13 01:30:14 +00:00
|
|
|
|
|
|
|
wxStopWatch sw;
|
|
|
|
int x = 0,
|
|
|
|
y = 0;
|
|
|
|
for ( int n = 0; n < opts.numLines; n++ )
|
|
|
|
{
|
|
|
|
int x1 = rand() % opts.width,
|
|
|
|
y1 = rand() % opts.height;
|
|
|
|
|
|
|
|
dc.DrawLine(x, y, x1, y1);
|
|
|
|
|
|
|
|
x = x1;
|
|
|
|
y = y1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const long t = sw.Time();
|
|
|
|
|
2012-03-01 07:09:43 +00:00
|
|
|
wxPrintf("%ld lines done in %ldms = %gus/line\n",
|
2008-04-13 01:30:14 +00:00
|
|
|
opts.numLines, t, (1000. * t)/opts.numLines);
|
|
|
|
}
|
2012-03-01 01:05:28 +00:00
|
|
|
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
void BenchmarkRectangles(const wxString& msg, wxDC& dc)
|
2012-03-01 01:05:28 +00:00
|
|
|
{
|
2013-02-05 20:46:53 +00:00
|
|
|
if ( !opts.testRectangles )
|
|
|
|
return;
|
|
|
|
|
2012-03-01 01:05:28 +00:00
|
|
|
if ( opts.mapMode != 0 )
|
|
|
|
dc.SetMapMode((wxMappingMode)opts.mapMode);
|
|
|
|
if ( opts.penWidth != 0 )
|
|
|
|
dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
|
|
|
|
|
|
|
|
dc.SetBrush( *wxRED_BRUSH );
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
wxPrintf("Benchmarking %s: ", msg);
|
2013-02-05 20:46:57 +00:00
|
|
|
fflush(stdout);
|
2012-03-01 01:05:28 +00:00
|
|
|
|
|
|
|
wxStopWatch sw;
|
|
|
|
for ( int n = 0; n < opts.numLines; n++ )
|
|
|
|
{
|
|
|
|
int x = rand() % opts.width,
|
|
|
|
y = rand() % opts.height;
|
|
|
|
|
|
|
|
dc.DrawRectangle(x, y, 32, 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
const long t = sw.Time();
|
|
|
|
|
2012-03-01 07:09:43 +00:00
|
|
|
wxPrintf("%ld rects done in %ldms = %gus/rect\n",
|
2012-03-01 01:05:28 +00:00
|
|
|
opts.numLines, t, (1000. * t)/opts.numLines);
|
|
|
|
}
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
void BenchmarkBitmaps(const wxString& msg, wxDC& dc)
|
2012-03-01 01:05:28 +00:00
|
|
|
{
|
2013-02-05 20:46:53 +00:00
|
|
|
if ( !opts.testBitmaps )
|
|
|
|
return;
|
|
|
|
|
2012-03-01 01:05:28 +00:00
|
|
|
if ( opts.mapMode != 0 )
|
|
|
|
dc.SetMapMode((wxMappingMode)opts.mapMode);
|
|
|
|
if ( opts.penWidth != 0 )
|
|
|
|
dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
|
|
|
|
|
2013-02-05 20:46:55 +00:00
|
|
|
wxPrintf("Benchmarking %s: ", msg);
|
2013-02-05 20:46:57 +00:00
|
|
|
fflush(stdout);
|
2012-03-01 01:05:28 +00:00
|
|
|
|
|
|
|
wxStopWatch sw;
|
|
|
|
for ( int n = 0; n < opts.numLines; n++ )
|
|
|
|
{
|
|
|
|
int x = rand() % opts.width,
|
|
|
|
y = rand() % opts.height;
|
|
|
|
|
|
|
|
dc.DrawBitmap(m_bitmap, x, y, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const long t = sw.Time();
|
|
|
|
|
2012-03-01 07:09:43 +00:00
|
|
|
wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
|
2012-03-01 01:05:28 +00:00
|
|
|
opts.numLines, t, (1000. * t)/opts.numLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxBitmap m_bitmap;
|
2008-04-13 01:30:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class GraphicsBenchmarkApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void OnInitCmdLine(wxCmdLineParser& parser)
|
|
|
|
{
|
|
|
|
static const wxCmdLineEntryDesc desc[] =
|
|
|
|
{
|
2013-02-05 20:46:53 +00:00
|
|
|
{ wxCMD_LINE_SWITCH, "", "bitmaps" },
|
|
|
|
{ wxCMD_LINE_SWITCH, "", "lines" },
|
|
|
|
{ wxCMD_LINE_SWITCH, "", "rectangles" },
|
2008-04-13 01:30:14 +00:00
|
|
|
{ wxCMD_LINE_OPTION, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER },
|
|
|
|
{ wxCMD_LINE_OPTION, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER },
|
|
|
|
{ wxCMD_LINE_OPTION, "w", "width", "", wxCMD_LINE_VAL_NUMBER },
|
|
|
|
{ wxCMD_LINE_OPTION, "h", "height", "", wxCMD_LINE_VAL_NUMBER },
|
|
|
|
{ wxCMD_LINE_OPTION, "L", "lines", "", wxCMD_LINE_VAL_NUMBER },
|
2012-03-01 01:05:28 +00:00
|
|
|
{ wxCMD_LINE_NONE },
|
2008-04-13 01:30:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
parser.SetDesc(desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
|
|
|
|
{
|
|
|
|
if ( parser.Found("m", &opts.mapMode) &&
|
|
|
|
(opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) )
|
|
|
|
return false;
|
|
|
|
if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 )
|
|
|
|
return false;
|
|
|
|
if ( parser.Found("w", &opts.width) && opts.width < 1 )
|
|
|
|
return false;
|
|
|
|
if ( parser.Found("h", &opts.height) && opts.height < 1 )
|
|
|
|
return false;
|
|
|
|
if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 )
|
|
|
|
return false;
|
|
|
|
|
2013-02-05 20:46:53 +00:00
|
|
|
opts.testBitmaps = parser.Found("bitmaps");
|
|
|
|
opts.testLines = parser.Found("lines");
|
|
|
|
opts.testRectangles = parser.Found("rectangles");
|
|
|
|
if ( !(opts.testBitmaps || opts.testLines || opts.testRectangles) )
|
|
|
|
{
|
|
|
|
// Do everything by default.
|
|
|
|
opts.testBitmaps =
|
|
|
|
opts.testLines =
|
|
|
|
opts.testRectangles = true;
|
|
|
|
}
|
|
|
|
|
2008-04-13 01:30:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
new GraphicsBenchmarkFrame;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)
|