Efficient use of wxGCDC on a Raspberry PI

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
robbiegregg
In need of some credit
In need of some credit
Posts: 9
Joined: Wed Mar 22, 2017 9:27 pm

Efficient use of wxGCDC on a Raspberry PI

Post by robbiegregg »

Hi all,

I would like to be able to draw a large number (100x100) of filled circles on a grid using a Raspberry PI 2 model B using wxWidgets 3.0 (Raspbian). The OnPaint function I currently have is shown below:

Code: Select all

void temp2Dialog::OnPaint(wxPaintEvent& event)
{
    int borderSize = 4;
    wxBufferedPaintDC pdc(Panel1);
    wxGCDC dc(pdc);
    PrepareDC(dc);
    wxSize totalSize = dc.GetSize();

    // Define where picture is to start (top left corner), noting presence of border
    double g1Position[2] = {0};
    g1Position[0] += borderSize;
    g1Position[1] += borderSize;

    // Define how big the picture can be (i.e. size of wxPanel Panel1), noting presence of border all around
    double g1Size[2] = {0};
    g1Size[0] = totalSize.GetWidth();
    g1Size[1] = totalSize.GetHeight();

    g1Size[0] -= borderSize*2;
    g1Size[1] -= borderSize*2;
    
    // Draw underlying black rectangle
    dc.DrawRectangle(g1Position[0],g1Position[1],g1Size[0],g1Size[1]);

    // Matrix of circles will be square. Work out the maximum length of the square
    double picSize = 0;
    if(g1Size[0] > g1Size[1]) picSize = g1Size[1];
    else picSize = g1Size[0];

    // Work out size of circles
    int matrixSize = 100;
    double deltaX = picSize / matrixSize;
    int ideltaX = (int)deltaX;
    double Radius = deltaX /2.0;
    int iRadius = (int)Radius;
    
    // Loop through the matrix of circles
    for(int i=0;i<matrixSize;i++)
    {
        for(int j=0;j<matrixSize;j++)
        {
            // Define a random colour, and then draw a filled circle
            wxColour randomColour(rand()%255,rand()%255,rand()%255);
            dc.SetBrush(wxBrush(randomColour));
            dc.SetPen(wxPen(randomColour));
            int X = ideltaX*(i+1);
            int Y = ideltaX*(j+1);
            dc.DrawCircle(X,Y,iRadius);
        }
    }
}
I have tried increasing the GPU graphics from 64 mb up to 256 mb and 512 mb (and overclocking the RPI-B CPU), but this does not help. Before deciding the computing power from a RPI is insufficient for my needs, can anyone think of a better way of coding this (retaining the double buffering), resulting in a function that takes a fraction of a second to complete? On my setup this function takes roughly 3-5 seconds when I maximise the window on a 1080p screen.

Ideally, I would also like to retain the use of wxGCDC as this seems to be the only way to ensure the circles are drawn using anti-aliasing.

Thanks very much for reading.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Efficient use of wxGCDC on a Raspberry PI

Post by doublemax »

In your code the only optimization you could make is to use a static buffer bitmap for wxBufferedPaintDC, but that won't make much of a difference.

Realistically i think your only option to achieve the required speed is to use OpenGL for drawing. If it's hardware-accelerated on the Raspberry (and i think it is), then is should be fast enough. And if you only need to draw rectangles and circles, it shouldn't be that much work to implement it.
Use the source, Luke!
Post Reply