画布使用大量CPU [英] Canvas uses a lot of CPU

查看:152
本文介绍了画布使用大量CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在QML中使用Canvas来用OpenGL绘制旋转的Rectangle.这是代码:

I am using Canvas in QML to draw rotating Rectangle with OpenGL. Here is the code:

//...
property variant points: []

onPointsChanged:{
    canvas.requestPaint();
}

//...

Canvas{
    //...

    onPaint:{
        var ctx = canvas.getContext('2d')

        ctx.clearRect(0,0, width, height);
        ctx.beginPath()
        ctx.strokeStyle = 'red'
        ctx.lineWidth = 3

        for(var i = 0; i < points.length; i++){
            var p1 = convertPoint(points[i])
            if(i == 0){
                ctx.moveTo(p1.x, p1.y)
                continue
            }

            ctx.lineTo(p1.x, p1.y)
        }
        ctx.stroke()
        ctx.restore()
    }

    function convertPoint(p){
        var x = p.x * width;
        var y = p.y * height;
        return Qt.point(x,y);
    }
}

c ++代码中有4个点,每30毫秒发送到qml.问题在于,在MinGW下进行编译时,此绘画操作占用50%的CPU使用率;在MSVC2010下进行编译时,占用17%的CPU使用率,这仍然很多.是一些错误还是什么不好?

There are 4 points counted in c++ code and sent to qml every 30ms. Problem is that this paint operation takes 50% of CPU usage when compile under MinGW and when compile under MSVC2010 operation takes 17% of CPU, which is still a lot. It is some bug or what is bad?

推荐答案

考虑使用新场景图类而不是Canvas.特别是,您会对 QSGGeometryNode 类感兴趣.如果您喜欢Canvas API的简单性,则必须了解如何最好地使用它. 本文让您对此有所了解.

Consider using the new scene graph classes instead of Canvas if performance is critical. In particular, you'd be interested in the QSGGeometryNode class. If you prefer the simplicity of the Canvas API, you have to be aware of how it's best used. This article gives you some insight into that.

我还发现使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆