QGLWidget and hardware acceleration?
Asked Answered
L

2

9

Greetings all,

Does simply subclassing QGLWidget and reimplementing paintEvent() make use of OpenGL and hardware acceleration? I create a QPainter and draw QImages in this paintEvent().

What happen inside the paintEvent() method of QGLWidget? Does it convert the images(QImage,QPixmap) into OpenGL textures?

Does it use hardware acceleration for image scaling?

Thanks in advance, umanga

Lagoon answered 1/7, 2010 at 8:56 Comment(3)
Looks very unlikely. From the docs - except that instead of drawing the widget's contents using QPainter etc. you use the standard OpenGL rendering commands.Curarize
@tathagata: unfortunately you're wrong, despite the upvote; please read my answer to the question.Practice
Thanks Greg, for pointing that out! I was just making a logical guess. Guess I was wrong.Curarize
P
13

Take a look at http://doc.qt.io/archives/4.6/opengl-2dpainting.html for an instructive example, where you can also find the following quote: "it is possible to re-implement its [QGLWidget] paintEvent() and use QPainter to draw on the device, just as you would with a QWidget. The only difference is that the painting operations will be accelerated in hardware if it is supported by your system's OpenGL drivers."

So, the answer to your first question is yes.

For figuring out the exact details of the implementation, let's take a quick peek at a piece of source-code from QOpenGLPaintEngine (which can be found by searching the internet) :

void QOpenGLPaintEngine::drawImage(const QRectF &r, const QImage &image, 
                              const QRectF &sr, Qt::ImageConversionFlags)
{
    Q_D(QOpenGLPaintEngine);
    if (d->composition_mode > QPainter::CompositionMode_Plus 
         || d->high_quality_antialiasing && !d->isFastRect(r))
        d->drawImageAsPath(r, image, sr);
    else {
        GLenum target = (QGLExtensions::glExtensions 
                         & QGLExtensions::TextureRectangle)
                        ? GL_TEXTURE_RECTANGLE_NV
                        : GL_TEXTURE_2D;
        if (r.size() != image.size())
            target = GL_TEXTURE_2D;
        d->flushDrawQueue();
        d->drawable.bindTexture(image, target);
        drawTextureRect(image.width(), image.height(), r, sr, target);
    }
}

This answers your question regarding QImages, they are indeed drawn using textures.

Practice answered 1/7, 2010 at 12:46 Comment(0)
M
6

Yes, if you use GL commands inside a QGLWidget, inside the paintGL, resizeGL and initializeGL methods, you will get full hardware acceleration (if available).

Also seems that using QPainter in a QGLWidget also gets HW acceleration, since there's a OpenGL QPainEngine implementation, you can read about that here.

Maui answered 1/7, 2010 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.