Results 1 to 6 of 6

Thread: QPixmap Overhead

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QPixmap Overhead

    My organization has created some software that displays video by reading frames over UDP and converting the uchar* data into a QImage that gets converted into a QPixmap which we display using setPixmap().

    Machines with eight or so cores handle 30 Hz frames nicely, but they tend to get bogged down on conventional CPUs. A single player instance displaying 720x480 frames at 30 Hz ends up occupying 50% of the CPU (this is a dual-core MacBook Pro).

    After learning about the performance overhead in converting QImages to QPixmaps (and with some help from the folks here on QtCenter.org) I was able to drop my uchar* data into a QByteArray and convert to a QPixmap. This ends up using about 56% of the CPU –or about 6% more than the QPixmap->QImage conversion.

    Instead of creating a separate QByteArray for every frame, I added a QByteArray member to the class that I resize to the size of the header + data. This shaved off about 2% of the cpu expense, but it’s still 4% more costly than the QImage->QPixmap conversion. I’m not sure why that would be?
    I’m wondering if there is a way to optimize this process further … could the QByteArray be making a deep copy and slowing things up?
    Any guesses?

    This thread basically details how I did it:
    http://www.qtcentre.org/threads/2799...unsigned-char*
    Last edited by mhoover; 27th July 2010 at 22:24. Reason: formatting

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap Overhead

    Hi,

    I recommend you to use an QGLWidget and display the images using the GPU. I'm able to reach 30 fps with two cameras using less that 10% CPU, and there is a image processing that consumes some CPU also.
    Try seaching google how to display images using OpenGL and use the code into the QGLWidget.
    Òscar Llarch i Galán

  3. The following user says thank you to ^NyAw^ for this useful post:

    mhoover (27th July 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap Overhead

    Wow!

    Out of curiosity, how powerful is your CPU/GPU? How many cores?

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap Overhead

    Hi,

    The code was using a Intel P4 3.0GH with Hyperthreading CPU and a NVIDIA 6600GT AGP GPU.
    Òscar Llarch i Galán

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPixmap Overhead

    So a CPU is at 100% keeping up with displaying 10,368,000 bytes each second. Are you sure you are only doing the work you think you are? For example are you displaying only when you have a full frame to display and not every time a UDP packet arrives? Are you sure that receiving and assembling the UDP packets is not the bottleneck?

    The crude example below sits on 34% (Intel(R) Core(TM)2 CPU 6400 @ 2.13GHz Linux) unless I generate a new page of static every frame using the inefficient method you see. If I display the same static frame every time the CPU is about 7%.
    Qt Code:
    1. #include <cstdlib>
    2. #include <QtGui>
    3. #include <QDebug>
    4.  
    5. class mainwindow: public QMainWindow
    6. {
    7. Q_OBJECT
    8. public:
    9. mainwindow(QWidget *p = 0): QMainWindow(p) {
    10. l = new QLabel(this);
    11. setCentralWidget(l);
    12.  
    13. frame = 0;
    14. QTimer *t = new QTimer(this);
    15. connect(t, SIGNAL(timeout()), this, SLOT(update()));
    16. t->start(33); // 30 times per second
    17. }
    18. public slots:
    19. void update() {
    20. // create a fake frame of video every second frame
    21. if (frame % 2 == 0) {
    22. raw.clear();
    23. for (int i = 0; i < 720 * 480; i++)
    24. raw.append( rand() % 256 );
    25. }
    26. frame++;
    27.  
    28. pgm.clear();
    29. pgm.append("P5 720 480 255\n");
    30. pgm.append(raw);
    31.  
    32. bool ok = p.loadFromData(pgm, "ppm");
    33. l->setPixmap(p);
    34. };
    35. private:
    36. QLabel *l;
    37. int frame;
    38. };
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42. QApplication app(argc, argv);
    43.  
    44. mainwindow m;
    45. m.show();
    46.  
    47. return app.exec();
    48. }
    49. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to ChrisW67 for this useful post:

    mhoover (21st September 2010)

  8. #6
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPixmap Overhead

    I ended up changing my QLabel to a QGLWidget.

    Qt changes all the painting commands to OpenGL functions under the hood with minimal performance cost.

    Your suggestion would explain a lot. I should look at it anyway. I don't want to repaint for every packet ...

Similar Threads

  1. QObject overhead
    By sriky27 in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2009, 18:12
  2. Replies: 4
    Last Post: 28th August 2008, 13:13
  3. Replies: 1
    Last Post: 21st August 2008, 07:44
  4. Replies: 5
    Last Post: 9th April 2007, 14:26
  5. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11

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
  •  
Qt is a trademark of The Qt Company.