Skip to content

Instantly share code, notes, and snippets.

@alexpana
Last active May 18, 2017 00:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpana/feb882b5b0fa87892d37 to your computer and use it in GitHub Desktop.
Save alexpana/feb882b5b0fa87892d37 to your computer and use it in GitHub Desktop.
Profiling Direc2D vs GDI+ wxWidgets implementations
struct ProfileResult
{
long double d2dResultNoAA;
long double d2dResultAA;
long double gdiResultNoAA;
long double gdiResultAA;
};
ProfileResult ProfileDrawFunction(wxWindow* window, std::function<void(wxGraphicsContext*)> drawFunction)
{
static const int testCount = 8;
long double elapsedSeconds;
LARGE_INTEGER frequency, counterBegin, counterEnd;
QueryPerformanceFrequency(&frequency);
long double accumulator = 0;
std::function<wxGraphicsContext* ()> contextFactories[2] = {
[&window]() -> wxGraphicsContext* { return wxGraphicsRenderer::GetDirect2DRenderer()->CreateContext(window); },
[&window]() -> wxGraphicsContext* { return wxGraphicsRenderer::GetGDIPlusRenderer()->CreateContext(window); }
};
long double results[4] = {0, 0};
for (int c = 0; c < 2; ++c)
{
for (int antialiasing = 0; antialiasing <= 1; ++antialiasing)
{
for (int i = 0; i < testCount; ++i)
{
QueryPerformanceCounter(&counterBegin);
wxGraphicsContext* gc = contextFactories[c]();
gc->SetAntialiasMode(antialiasing ? wxANTIALIAS_DEFAULT : wxANTIALIAS_NONE);
drawFunction(gc);
delete gc;
QueryPerformanceCounter(&counterEnd);
results[c * 2 + antialiasing] += (long double)(counterEnd.QuadPart - counterBegin.QuadPart) / (long double)frequency.QuadPart;
}
}
results[c] /= (float)testCount;
}
ProfileResult result = {
results[0], results[1], results[2], results[3]
};
return result;
}
void ReportResult(std::string testName, ProfileResult result)
{
std::ofstream fout("profiling.txt", std::ios_base::app);
fout << testName << ", " << result.d2dResultNoAA <<
", " << result.d2dResultAA <<
", " << result.gdiResultNoAA <<
", " << result.gdiResultAA << std::endl;
fout.close();
}
void ReportResultHeaders()
{
std::ofstream fout("profiling.txt", std::ios_base::app);
fout << "Test Name, Direct2D no antialiasing, Direct2D antialiasing, GDI+ no antialiasing, GDI+ antialiasing" << std::endl;
fout.close();
}
void ProfileDrawPrimitives(wxGraphicsContext* gc)
{
wxGraphicsBrush redBrush = gc->CreateBrush(*wxRED);
wxGraphicsPen blackPen = gc->CreatePen(*wxBLACK);
gc->SetPen(blackPen);
gc->SetBrush(redBrush);
for (int i = 0; i < 5000; ++i)
gc->DrawRectangle((i * 10) % 800, (i * 10 + 30) % 800, 200, 100);
for (int i = 0; i < 5000; ++i)
gc->DrawEllipse((i * 10) % 800, (i * 10 + 30) % 800, 200, 100);
}
void ProfileDrawPaths(wxGraphicsContext* gc)
{
gc->SetPen(*wxBLACK_PEN);
gc->SetBrush(*wxRED_BRUSH);
wxGraphicsPath path = gc->CreatePath();
path.AddQuadCurveToPoint(100, 100, 50, 32);
path.AddArc(wxPoint2DDouble(300, 300), 100, 30, 12, false);
path.AddLineToPoint(wxPoint2DDouble(100, 300));
path.AddLineToPoint(wxPoint2DDouble(400, 100));
path.AddLineToPoint(wxPoint2DDouble(200, 200));
path.AddCircle(300, 300, 200);
path.AddRectangle(30, 30, 400, 400);
for (int i = 0; i < 1000; ++i)
{
gc->DrawPath(path);
}
}
void ProfileDrawBitmaps(wxGraphicsContext* gc)
{
wxGraphicsBitmap bitmap01 = gc->CreateBitmap(*gs_bmpMask);
wxGraphicsBitmap bitmap02 = gc->CreateBitmap(*gs_bmpNoMask);
wxGraphicsBitmap bitmap03 = gc->CreateBitmap(*gs_bmpWithColMask);
wxGraphicsBitmap bitmap04 = gc->CreateBitmap(*gs_bmpWithMask);
for (int i = 0; i < 250; ++i)
{
gc->DrawBitmap(bitmap01, 100, 200, gs_bmpMask->GetWidth(), gs_bmpMask->GetHeight());
gc->DrawBitmap(bitmap01, 200, 300, gs_bmpNoMask->GetWidth(), gs_bmpNoMask->GetHeight());
gc->DrawBitmap(bitmap01, 300, 400, gs_bmpWithColMask->GetWidth(), gs_bmpWithColMask->GetHeight());
gc->DrawBitmap(bitmap01, 400, 500, gs_bmpWithMask->GetWidth(), gs_bmpWithMask->GetHeight());
}
}
void ProfileDrawText(wxGraphicsContext* gc)
{
gc->SetAntialiasMode(wxANTIALIAS_DEFAULT);
wxString string01 = "I don't want to go home from MasterChef!";
wxString string02 = "Fried Chicken Brest with crispy shell and crunchy sounds.";
wxString string03 = "Stuffed chicken breast cooked perfectly but without a lot of stuffing";
wxGraphicsFont font01 = gc->CreateFont(14, "Tahoma", 0, *wxRED);
wxGraphicsFont font02 = gc->CreateFont(24, "Arial", 0, *wxBLACK);
wxGraphicsFont font03 = gc->CreateFont(34, "Consolas", 0, *wxRED);
for (int i = 0; i < 800; ++i)
{
gc->SetFont(font01);
gc->DrawText(string01, 100, 100);
gc->SetFont(font02);
gc->DrawText(string02, 200, 200);
gc->SetFont(font03);
gc->DrawText(string03, 400, 400);
}
}
void DrawMixed(wxGraphicsContext* gc)
{
}
void MyCanvas::Draw(wxDC& pdc)
{
ReportResultHeaders();
ReportResult("DrawPrimitives", ProfileDrawFunction(this, ProfileDrawPrimitives));
ReportResult("DrawPaths", ProfileDrawFunction(this, ProfileDrawPaths));
ReportResult("DrawBitmaps", ProfileDrawFunction(this, ProfileDrawBitmaps));
ReportResult("DrawText", ProfileDrawFunction(this, ProfileDrawText));
wxMessageBox("Done!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment