PDA

View Full Version : Fastest way to clear a bitmap that painter draws on



PhilippM
22nd February 2011, 12:09
Hi folks,

I'm using a QPainter to draw a 2D-Scene on a QBitmap, and this bitmap is later used by openGL as a texture. The re-drawing occurs about 20 times/second and performance matters, because it is a soft-realtime system.

What is the correct way for the rendering function to clear the entire image and start drawing?

Simply overpaint?


void SceneRenderer::render()
{
painter.fillRect(bitmap.rect(), brush_background);
doRendering();
}


cancel painting at the end in re-initialize everytime?


void SceneRenderer::render()
{
painter.begin(&bitmap);
painter.fillRect(bitmap.rect(), brush_background);
doRendering();
painter.end();
}


clear the bitmap?


void SceneRenderer::render()
{
bitmap.fill(0);
painter.fillRect(bitmap.rect(), brush_background);
doRendering();
}


First solution seems obvious, but I fear that all the painting from the previous calls is still stored "underneath" the visible sceen and costs time and memory despite being invisible.
Second solution seems to not have this problem, but I fear the cost of resource releasing and allocation every cycle.
Third solution is probably nonsense.

Any insight is appreciated.

Philipp

high_flyer
22nd February 2011, 13:17
What about QBitmap::clear() ?

PhilippM
22nd February 2011, 13:39
Sorry,
the variable bitmap is of type QImage.
Unfortunate nameing by me I guess.
QImage doesn't have a clear() function.

But actually I'm more worried about the painter.
Can I use the painter to draw on and on for millions of cycles, whithout ever having to reset it in any way?

Philipp

high_flyer
22nd February 2011, 13:53
Unfortunate nameing by me I guess.
its not the naming - you explicitly said:


I'm using a QPainter to draw a 2D-Scene on a QBitmap


QImage doesn't have a clear() function.
Make sure you really need to clear it, maybe you don't.
If you do, you can always do :


quint32 iSize = pImage->depth()*pImage->width()*pImage->height(); //maybe byteCount () does this too, I am not sure.
memset(pImage->bits(),0,iSize);



But actually I'm more worried about the painter.
Can I use the painter to draw on and on for millions of cycles, whithout ever having to reset it in any way?

Not sure what do you mean by that.
You can save and restore the painter status with save() and restore().

PhilippM
21st March 2011, 14:51
If you do, you can always do :


quint32 iSize = pImage->depth()*pImage->width()*pImage->height(); //maybe byteCount () does this too, I am not sure.
memset(pImage->bits(),0,iSize);



This is exactly what i need and it works marvelous - after i changed
pImage->depth() to pImage->depth()/8
because depth() returns bits, memset expects bytes.


Not sure what do you mean by that.
You can save and restore the painter status with save() and restore().

Of course I figured that out already. It was just that I feared that using the same painter to draw millions of squares and ellipses over time would sometime lead to performance issue because the painter "remembers" all the path he has gone through already.

Regards,
Philipp