Results 1 to 5 of 5

Thread: Fastest way to clear a bitmap that painter draws on

  1. #1
    Join Date
    Feb 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Fastest way to clear a bitmap that painter draws on

    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?
    Qt Code:
    1. void SceneRenderer::render()
    2. {
    3. painter.fillRect(bitmap.rect(), brush_background);
    4. doRendering();
    5. }
    To copy to clipboard, switch view to plain text mode 

    cancel painting at the end in re-initialize everytime?
    Qt Code:
    1. void SceneRenderer::render()
    2. {
    3. painter.begin(&bitmap);
    4. painter.fillRect(bitmap.rect(), brush_background);
    5. doRendering();
    6. painter.end();
    7. }
    To copy to clipboard, switch view to plain text mode 

    clear the bitmap?
    Qt Code:
    1. void SceneRenderer::render()
    2. {
    3. bitmap.fill(0);
    4. painter.fillRect(bitmap.rect(), brush_background);
    5. doRendering();
    6. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fastest way to clear a bitmap that painter draws on

    What about QBitmap::clear() ?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Fastest way to clear a bitmap that painter draws on

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Fastest way to clear a bitmap that painter draws on

    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 :
    Qt Code:
    1. quint32 iSize = pImage->depth()*pImage->width()*pImage->height(); //maybe byteCount () does this too, I am not sure.
    2. memset(pImage->bits(),0,iSize);
    To copy to clipboard, switch view to plain text mode 

    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().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Fastest way to clear a bitmap that painter draws on

    Quote Originally Posted by high_flyer View Post
    If you do, you can always do :
    Qt Code:
    1. quint32 iSize = pImage->depth()*pImage->width()*pImage->height(); //maybe byteCount () does this too, I am not sure.
    2. memset(pImage->bits(),0,iSize);
    To copy to clipboard, switch view to plain text mode 
    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

Similar Threads

  1. QTextLayout draws text outside the widget
    By alpinista in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2009, 06:25
  2. Fastest PaintDevice?
    By rage in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 8th November 2007, 15:46
  3. What is the fastest way to draw a circle ?
    By Vladimir in forum Qt Programming
    Replies: 18
    Last Post: 6th September 2007, 17:26
  4. Fastest way to clear a QList
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 09:26
  5. atomicy in gui draws
    By elcuco in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2006, 20:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.