使用QOpenGLWidget绘制YUV数据时CPU占用率高

问题描述 投票:0回答:0

我实现了一个 glRender 类,它是 QOpenGLWidget 的子类,用于显示来自 H264 解码器 (1920x1080@60fps) 的 YUV 数据。问题是 OpenGL 绘图过程占用了单核处理能力的大约 60%(从 htop 的输出观察),这相当高,因为我的程序运行在嵌入式平台上(4 x [email protected],马里) -G31)等进程也需要大量的CPU使用率。这是我的 paintGL 函数:

void glRender::paintGL()
{
    // 1. setup a rectangle and texture coordinates
    vao.bind();
    vertexBuffer->bind();
    indexBuffer->bind();

    quintptr offset = 0;
    int vertexLocation = program->attributeLocation("a_position");
    program->enableAttributeArray(vertexLocation);
    program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));

    offset += sizeof(QVector3D);
    int texcoordLocation = program->attributeLocation("a_texcoord");
    program->enableAttributeArray(texcoordLocation);
    program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData));

    // 2. setup texture
    y_tex->bind(0);
    y_tex->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, (const void *)y_data);

    u_tex->bind(1);
    u_tex->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, (const void *)u_data);

    v_tex->bind(2);
    v_tex->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, (const void *)v_data);

    program->setUniformValue("y_texture", 0);
    program->setUniformValue("u_texture", 1);
    program->setUniformValue("v_texture", 2);

    // 3. draw
    glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_SHORT, nullptr);

    // 4. release resources
    y_tex->release();
    u_tex->release();
    v_tex->release();

    vao.release();

    emit displayDone();
}

我的问题是:
1。为什么绘图过程需要这么多 CPU 时间?经过反复试验,我发现是 setData 调用使 CPU 使用率很高。如果将 setDate 注释掉,CPU 使用率将下降约 60%。
2。我在进入paintGL和退出paintGL时都打印了时间戳,delta一直是4ms。计算 4ms * 60fps / 1000ms,我得到 24% 的 CPU 处理时间,这意味着最多 24% 的 CPU 使用率。但这与我观察到的(60% 的 CPU 使用率)不同,为什么?
3。我可以做任何改进来减少绘图过程的 CPU 消耗吗?

我用谷歌搜索了一下,一些答案表明它仍然是软件渲染。我检查了我系统的 GPU 使用率,渲染时它上升了大约 20%。

qt opengl cpu-usage qopenglwidget
© www.soinside.com 2019 - 2024. All rights reserved.