QGLWidget和硬件加速?

发布于 2024-09-08 07:02:47 字数 243 浏览 2 评论 0原文

大家好,

简单地子类化 QGLWidget 并重新实现 PaintEvent() 是否可以利用 OpenGL 和硬件加速? 我创建了一个 QPainter 并在此 PaintEvent() 中绘制 QImage。

QGLWidget的paintEvent()方法内部发生了什么?它是否将图像(QImage、QPixmap)转换为 OpenGL 纹理?

它是否使用硬件加速来进行图像缩放?

提前致谢, 乌曼加

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-09-15 07:02:47

看看 http://doc.qt.io/archives/4.6/ opengl-2dpainting.html 是一个指导性示例,您还可以在其中找到以下引用:“可以重新实现其 [QGLWidget] PaintEvent() 并使用 QPainter 在设备上绘图,就像您一样唯一的区别是,如果系统的 OpenGL 驱动程序支持的话,绘画操作将在硬件中加速。”

所以,你的第一个问题的答案是肯定的。

为了弄清楚实现的具体细节,让我们快速浏览一下 QOpenGLPaintEngine 的一段源代码(可以通过搜索互联网找到):

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);
    }
}

这回答了您关于 QImages 的问题,它们确实是使用纹理绘制的。

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.

年华零落成诗 2024-09-15 07:02:47

是的,如果您在 QGLWidget 内、paintGL、resizeGL 和initializeGL 方法内使用GL 命令,您将获得完整的硬件加速(如果可用)。

另外,似乎在 QGLWidget 中使用 QPainter 也可以获得硬件加速,因为有一个 OpenGL QPainEngine 实现,您可以阅读有关 此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文