使用“cached:true”时,使用静态源的DropShadow更快

时间:2016-10-13 18:26:57

标签: performance qt qml qtquick2 dropshadow

我将从我的测试用例开始。它创造了21个不变的阴影蓝色矩形。它还创建了一个1x1px Canvas3D不断重新绘制,因此我可以检查它是否经常重新绘制所有其他内容的重新绘制(Canvas3D具有内置的fps属性)。在DropShadow项目上设置cached: true时,我获得60 FPS。如果没有,我得到30 FPS。但我所期望的是在两种情况下获得相同的FPS,因为我不认为阴影的模糊会被重新计算,因为源代码永远不会更新。

main.cpp :(琐碎)

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
import QtCanvas3D 1.1

Window {
    visible: true
    width: 800
    height: 600
    id: window

    Column {
        Text {
            text: canvas3d.fps + " FPS"
            font.pointSize: 18
        }
        Flow {
            width: window.width
            spacing: 10
            Repeater {
                model: 21

                ShadowedItem {
                }
            }
        }
        Canvas3D {
            id: canvas3d
            width: 1; height: 1 // nonzero size so it can be redrawn
            property var gl;

            onInitializeGL: {
                // should get and save context, otherwise FPS isn't measured for some reason
                gl = canvas3d.getContext("canvas3d", {depth:true, antialias:true, alpha:true});
            }
        }
    }
}

ShadowedItem.qml:

import QtQuick 2.0
import QtGraphicalEffects 1.0

Item {
    width: 100
    height: 100

    Rectangle {
        anchors.fill: parent
        id: rect
        visible: false
        color: "blue"
    }

    DropShadow {
        source: rect
        anchors.fill: rect
        cached: true // !
        radius: 8
    }
}

有关性能差异的任何想法吗?

1 个答案:

答案 0 :(得分:1)

我发布了一个follow-up question。在评论中,我了解到当场景中的1个项目(例如本例中的Canvas3D)需要重绘时,整个场景会被重绘。这意味着每次我的Canvas3D被重绘(这是不断的)时,所有阴影都会重新绘制。如果cachedfalse,则表示模糊会重新计算,从而导致速度减慢。