Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bad paint performance on QImage with transparency
QtWS25 Last Chance

Bad paint performance on QImage with transparency

Scheduled Pinned Locked Moved Unsolved General and Desktop
qopenglwidgetqpainterqimage
10 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    closer_ex
    wrote on 17 May 2021, 16:35 last edited by
    #1

    I'm subclassing QOpenGLWidget and overriding paintGL() to render video frames with images composed by edges detected in each frame( consider it as some sort of edge sharpening ). QPainter takes only 1 - 3 ms on rendering video frame, but 60 - 80 ms on the edge image. Code is presented below:

    paintGL()

    void VideoGLWidget::paintGL()
    {
        if(!queue_frames.isEmpty() && tml.state() == QTimeLine::Running)
        {
            QElapsedTimer timer;
            timer.start();
            QPainter p(this);
            p.beginNativePainting();
            p.setRenderHint(QPainter::Antialiasing, true);
            p.setRenderHint(QPainter::SmoothPixmapTransform, true);
            p.drawImage(rect(), queue_frames.first().first);    //queue_frames stores QPair<QImage, qint64>
            qDebug() << "rendering video frame took" << timer.elapsed() << "ms";
            if(!queue_edges.isEmpty())
            {
                p.drawImage(rect(), queue_edges.first().first);     //queue_edges stores QPair<QImage, qint64>
            }
            p.endNativePainting();
            qDebug() << "paintGL took" << timer.elapsed() << "ms";
        }
    }
    

    Edge image is constructed this way:

    QImage img(buf_edge, width, height, QImage::Format_Mono);    //buf_edge is a XBM format array
    img.setColorCount(2);
    img.setColor(0, qRgba(0, 0, 0, 0));
    img.setColor(1, qRgba(255, 0, 0, 255));
    

    What is causing the second drawImage() to be so much slower than the first one?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 May 2021, 18:08 last edited by
      #2

      Hi,

      I wild guess: conversion from the current image format to something suitable for drawing.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • C Offline
        C Offline
        closer_ex
        wrote on 18 May 2021, 05:17 last edited by closer_ex
        #3

        Hi @SGaist ,
        Thanks for the reply! Turns out your guess is right, converting the QImage to QPixmap takes about 50 ms and painting it takes about 10 ms. I went through QImage doc again and found something I neglected for a long time:

        Note: Avoid most rendering directly to most of these formats using QPainter. Rendering is best optimized to the Format_RGB32 and Format_ARGB32_Premultiplied formats, and secondarily for rendering to the Format_RGB16, Format_RGBX8888, Format_RGBA8888_Premultiplied, Format_RGBX64 and Format_RGBA64_Premultiplied formats

        But now the question splits into two:

        • Why does image with transparency takes more time to paint?
        • How to improve efficiency (from construction to painting)?
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 18 May 2021, 21:39 last edited by
          #4

          I haven't check the implementation recently but there's likely a form of composition to do in order to properly respect the transparency.

          Did you change the image format ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply 19 May 2021, 04:58
          0
          • S SGaist
            18 May 2021, 21:39

            I haven't check the implementation recently but there's likely a form of composition to do in order to properly respect the transparency.

            Did you change the image format ?

            C Offline
            C Offline
            closer_ex
            wrote on 19 May 2021, 04:58 last edited by
            #5

            @SGaist Ops, this actually reminds me to use ARGB instead of XBM for output, since it's a parallel operation done by CUDA. Now Format_ARGB32_Premutiplied does the job well. Thank you!

            Though the topic can be marked solved now, I'm still wondering why QBitmap and mono format QImage can not be used directly for painting (looks like a bitmap is also converted to pixmap on drawPixmap()), sorry if it's a stupid basic question :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 19 May 2021, 19:35 last edited by
              #6

              AFAIK, only the QImage::Format_Indexed8 cannot be painted on. But maybe we are not talking about the same thing.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply 20 May 2021, 14:21
              0
              • S SGaist
                19 May 2021, 19:35

                AFAIK, only the QImage::Format_Indexed8 cannot be painted on. But maybe we are not talking about the same thing.

                C Offline
                C Offline
                closer_ex
                wrote on 20 May 2021, 14:21 last edited by
                #7

                @SGaist
                I mean, both QBitmap and QImage::Format_Mono QImage are converted on painting ( just as your first reply says ), and I'm curious about the reason. Is mask the only usage of bitmap?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 20 May 2021, 18:53 last edited by
                  #8

                  That's were I am not sure we are talking about the same thing. Are you talking about them as target for painting ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  C 1 Reply Last reply 21 May 2021, 06:11
                  0
                  • S SGaist
                    20 May 2021, 18:53

                    That's were I am not sure we are talking about the same thing. Are you talking about them as target for painting ?

                    C Offline
                    C Offline
                    closer_ex
                    wrote on 21 May 2021, 06:11 last edited by
                    #9

                    @SGaist Yes, I'm talking about conversion performed on calling QPainter::drawPixmap() and QPainter::drawImage() in QWidget::paint(). Sorry for the confusion!

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 21 May 2021, 20:06 last edited by
                      #10

                      One of the issue here is that the conversion must be done each time the draw method is called. If you know that your images will not change or not much then you should do the conversion upfront so that the painting can be optimized.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1

                      2/10

                      17 May 2021, 18:08

                      topic:navigator.unread, 8
                      • Login

                      • Login or register to search.
                      2 out of 10
                      • First post
                        2/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved