当MouseArea也存在时,不断重绘8000个不可见项的场景会导致较高的CPU使用率 [英] Constantly repainting a scene of 8000 invisible items, when a MouseArea also exists, causes high CPU usage

查看:101
本文介绍了当MouseArea也存在时,不断重绘8000个不可见项的场景会导致较高的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何复制:


  • 运行我的代码

  • 将鼠标放在窗口上方出现

您会看到CPU使用率很高,尽管它取决于您的硬件。在我的PC 上,它是20%(4个虚拟内核中的5%) )。

You'll see that CPU usage is fairly high, although it will depend on your hardware. On my PC it's 20% (5% in each of the 4 virtual cores).

此测试用例的动机:在我的真实应用中,我有很多看不见的内容()项目,尽管该选择对CPU使用率有很大帮助,但并没有达到我的预期。

My motivation for this testcase: in my real app I have a lot of invisible (culled) items, and while that the culling helps a lot with the CPU usage, it doesn't help as much as I'd expect.

我想知道为什么CPU使用率如此之高以及如何减少它。

I'd like ideas on why the CPU usage is so high, and how to reduce it.

我的代码

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 800
    height: 500

    MouseArea {
        width: 1
        height: 1
        hoverEnabled: true
    }
    AnimatedItem {
        anchors.centerIn: parent
        width: 100
        height: 100
    }

    Repeater {
        model: 8000
        Item {
            opacity: 0
            layer.enabled: true
            width: 1
            height: 1
        }
    }
}

AnimatedItem.qml

import QtQuick 2.0

Rectangle {
    id: root
    color: "black"
    property real rotAngle: 0
    NumberAnimation on rotAngle {
        from: 0
        to: 360
        loops: Animation.Infinite
        running: true
        duration: 500
    }
    transform: Rotation {
        origin.x: root.width / 2
        origin.y: root.height / 2
        angle: root.rotAngle
    }
}

我已经用QML探查器,它表明在QML中花费了很少的时间。因此,我还使用C ++探查器(CodeXL)进行了探查。它报告大部分时间都花在 QSGRootNode ::〜QSGRootNode()中,这是因为它调用了 QSGNodeUpdater :: isNodeBlocked(QSGNode *,QSGNode *)const 。我已经看过Qt的源代码,但是无法弄清为什么它甚至调用了前者。

I've profiled it with the QML profiler, which has shown that insignificant time is spent in QML. So I've also profiled with a C++ profiler (CodeXL). It reports that the majority of time is spent in QSGRootNode::~QSGRootNode(), due to it calling QSGNodeUpdater::isNodeBlocked(QSGNode*, QSGNode*) const. I've looked at the Qt source but haven't been able to figure out why it's even calling the former.

推荐答案

保存具有可见性的GPU:false



您似乎对可见性感到困惑, opacity:0 不一样为 visible:false 。只需将 visible:false 设置为将不会渲染项目,但是通过将不透明度保持为零,该项目不仅仍会渲染,而且会导致混合操作。将可见性设置为 false ,您将节省GPU内存和周期。

Save the GPU with visibility:false

Seems you are confused about the visibility thing, opacity:0 is not the same as visible: false. Just set visible: false and items won't be rendered, but by leaving opacity at zero, the item not only still renders, but causes blending operations. Make visibility false and you'll save GPU memory and cycles.

另一方面,如果看不到项目,最好停止动画,这样也可以节省CPU周期。

On the other hand, if items are not visible, a good idea is to stop animations, then you save CPU cycles too.

另一方面,如果必须对它们进行动画处理,则更喜欢使用 Animators 而不是 Animation s。动画会通知每项更改,而动画师则不会,因此,如果您不需要跟踪状态,则可以通过使用动画师节省一些CPU。甚至还有特定的 RotationAnimator

On the other hand, if you must animate them, then prefer to use Animators and not Animations. Animations inform about every change, and Animators do not, so if you do not need to track status, then you save some CPU by using Animators. There is even a specific RotationAnimator.

最后,如果您有很多相同的项目并且必须设置动画它们确实是一个优化迷(或者您正在嵌入式系统上运行),那么您应该实现自定义QQuickItem并在几何着色器中进行动画处理。

Finally, if you have lots of identical items and you must animate them and you are really an optimization junkie (or you are running on embedded) then you should implement a custom QQuickItem and do the animation in a geometry shader.

其他更奇特的想法,但您可以使用上面的列表进行很多改进。

There are some other more exotic ideas, but you could improve a lot with the above list.

这篇关于当MouseArea也存在时,不断重绘8000个不可见项的场景会导致较高的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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