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

查看:32
本文介绍了画布使用大量 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个点,每30ms发送到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.

我还发现使用 QQuickPaintedItem 类在某些情况下.

I've also found improvements on embedded hardware (specifically the Raspberry Pi) using the QQuickPaintedItem class in certain cases.

这篇关于画布使用大量 CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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