High CPU usage when using QOpenGLWidget to draw YUV data

93 Views Asked by At

I implemented a glRender class which subclassed QOpenGLWidget to display YUV data from a H264 decoder (1920x1080@60fps). The problem is that the OpenGL drawing process takes about 60% of processing power of a single core (observed from output of htop), this is quite high as my program runs on an embedded platform (4 x [email protected], Mali-G31) and other process also requires a lot of CPU usage.

Here is my paintGL function:

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

My questions are:

  1. Why the drawing process needs so much CPU time? I found that it's the setData call that make CPU usage high after some trial and error. If setDate is comment out, the CPU usage would drop about 60%.
  2. I printed the timestamp when entering paintGL and exiting paintGL, and the delta is always 4ms. Calculating 4ms * 60fps / 1000ms and I get 24% of CPU processing time which means 24% of CPU usage at most. But this is different from what I observe (60% of CPU usage), why?
  3. Is there any improvement I can make to make the drawing process less CPU consuming?

I have Googled around and some answer suggested that it's still software rendering. I have checked the GPU usage of my system, and it went up to around 20% when rendering.

0

There are 0 best solutions below