aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprofiler/quick3dmodel.cpp
blob: 75bfd619d76a22eb4ebf29ce57dff2d3c21e2d79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "quick3dmodel.h"
#include "qmlprofilerconstants.h"
#include <tracing/timelineformattime.h>


namespace QmlProfiler {
namespace Internal {

Quick3DModel::Quick3DModel(QmlProfilerModelManager *manager,
                                       Timeline::TimelineModelAggregator *parent) :
    QmlProfilerTimelineModel(manager, Quick3DEvent, MaximumRangeType, ProfileQuick3D, parent),
    m_maximumMsgType(-1)
{
}

QRgb Quick3DModel::color(int index) const
{
    return colorBySelectionId(index);
}

static const char *messageTypes[] = {
    QT_TRANSLATE_NOOP("Quick3DModel", "Render Frame"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Synchronize Frame"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Prepare Frame"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Mesh Load"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Custom Mesh Load"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Texture Load"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Generate Shader"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Load Shader"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Particle Update"),

    QT_TRANSLATE_NOOP("Quick3DModel", "Mesh Memory consumption"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Texture Memory consumption"),
};

static const char *unloadMessageTypes[] = {
    QT_TRANSLATE_NOOP("Quick3DModel", "Mesh Unload"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Custom Mesh Unload"),
    QT_TRANSLATE_NOOP("Quick3DModel", "Texture Unload"),
};

QString Quick3DModel::messageType(uint i)
{
    return i < sizeof(messageTypes) / sizeof(char *) ? tr(messageTypes[i]) :
                                                       tr("Unknown Message %1").arg(i);
}

QString Quick3DModel::unloadMessageType(uint i)
{
    switch (i) {
    case MeshLoad:
        return tr(unloadMessageTypes[0]);
    case CustomMeshLoad:
        return tr(unloadMessageTypes[1]);
    case TextureLoad:
        return tr(unloadMessageTypes[2]);
    }
    return tr("Unknown Unload Message %1").arg(i);
}

QVariantList Quick3DModel::labels() const
{
    QVariantList result;
    for (int i = 0; i <= m_maximumMsgType; ++i) {
        QVariantMap element;
        element.insert(QLatin1String("displayName"),
                       i != ParticleUpdate ? tr("Render Thread") : tr("GUI Thread"));
        element.insert(QLatin1String("description"), messageType(i));
        element.insert(QLatin1String("id"), i);
        result << element;
    }
    return result;
}

float Quick3DModel::relativeHeight(int index) const
{
    auto detailType = m_data[index].additionalType;
    if (detailType == TextureMemoryConsumption)
        return qMin(1.0f, (float)m_data[index].data / (float)m_maxTextureSize);
    if (detailType == MeshMemoryConsumption)
        return qMin(1.0f, (float)m_data[index].data / (float)m_maxMeshSize);
    return 1.0f;
}

qint64 Quick3DModel::rowMaxValue(int rowNumber) const
{
    int index = rowNumber - 1;
    if (index == TextureMemoryConsumption)
        return m_maxTextureSize;
    if (index == MeshMemoryConsumption)
        return m_maxMeshSize;
    return 0;
}

QVariantMap Quick3DModel::details(int index) const
{
    auto detailType = m_data[index].additionalType;
    bool unload = m_data[index].unload;
    QVariantMap result;
    result.insert(QLatin1String("displayName"),
                  detailType != ParticleUpdate ? tr("Render Thread") : tr("GUI Thread"));
    result.insert(tr("Description"),
                  !unload ? messageType(detailType) : unloadMessageType(detailType));
    if (detailType < MeshMemoryConsumption)
        result.insert(tr("Duration"), Timeline::formatTime(duration(index)));
    if (detailType == ParticleUpdate)
        result.insert(tr("Count"), m_data[index].data);
    if (detailType == RenderFrame) {
        quint32 calls = m_data[index].data & 0xffffffff;
        quint32 passes = m_data[index].data >> 32;
        result.insert(tr("Draw Calls"), calls);
        result.insert(tr("Render Passes"), passes);
    }
    if ((detailType >= MeshLoad && detailType <= TextureLoad)
            || (detailType >= MeshMemoryConsumption && detailType <= TextureMemoryConsumption)) {
        result.insert(tr("Total Memory Usage"), m_data[index].data);
    }
    return result;
}

int Quick3DModel::expandedRow(int index) const
{
    return selectionId(index) + 1;
}

int Quick3DModel::collapsedRow(int index) const
{
    Q_UNUSED(index)
    return Constants::QML_MIN_LEVEL;
}

void Quick3DModel::loadEvent(const QmlEvent &event, const QmlEventType &type)
{
    auto detailType = type.detailType();
    qint64 eventDuration = event.number<qint64>(0);
    qint64 eventTime = event.timestamp() - eventDuration;
    QVector<quint64> numbers = event.numbers<QVector<quint64>>();
    quint64 data = numbers.size() > 1 ? event.number<quint64>(1) : 0;

    int typeCount = detailType;
    if (detailType == MeshLoad || detailType == CustomMeshLoad) {
        bool updatePrevValues = true;
        if (m_prevMeshStartTime != -1) {
            bool unload = m_prevMeshData > data;
            m_data.insert(insert(eventTime, eventDuration, detailType),
                          Item(detailType, data, unload));
            if (m_prevMeshData != data) {
                m_data.insert(insert(m_prevMeshStartTime,
                                     eventTime - m_prevMeshStartTime, MeshMemoryConsumption),
                              Item(MeshMemoryConsumption, m_prevMeshData));
            } else {
                updatePrevValues = false;
            }
        } else {
            m_data.insert(insert(eventTime, eventDuration, detailType),
                          Item(detailType, data));
        }
        m_maxMeshSize = qMax(m_maxMeshSize, data);
        if (updatePrevValues) {
            m_prevMeshStartTime = eventTime;
            m_prevMeshData = data;
        }
        typeCount = MeshMemoryConsumption;
    } else if (detailType == TextureLoad) {
        bool updatePrevValues = true;
        if (m_prevTexStartTime != -1) {
            bool unload = m_prevTexData > data;
            m_data.insert(insert(eventTime, eventDuration, detailType),
                          Item(detailType, data, unload));
            if (m_prevTexData != data) {
                m_data.insert(insert(m_prevTexStartTime,
                                     eventTime - m_prevTexStartTime, TextureMemoryConsumption),
                              Item(TextureMemoryConsumption, m_prevTexData));
            } else {
                updatePrevValues = false;
            }
        } else {
            m_data.insert(insert(eventTime, eventDuration, detailType),
                          Item(detailType, data));
        }
        m_maxTextureSize = qMax(m_maxTextureSize, data);
        if (updatePrevValues) {
            m_prevTexData = data;
            m_prevTexStartTime = eventTime;
        }
        typeCount = TextureMemoryConsumption;
    } else {
        m_data.insert(insert(eventTime, eventDuration, detailType),
                      Item(detailType, data));
    }
    if (typeCount > m_maximumMsgType)
        m_maximumMsgType = typeCount;
}

void Quick3DModel::finalize()
{
    if (m_prevMeshStartTime != -1) {
        m_data.insert(insert(m_prevMeshStartTime, modelManager()->traceEnd() - m_prevMeshStartTime,
                             MeshMemoryConsumption),
                      Item(MeshMemoryConsumption, m_prevMeshData));
    }
    if (m_prevTexStartTime != -1) {
        m_data.insert(insert(m_prevTexStartTime, modelManager()->traceEnd() - m_prevTexStartTime,
                             TextureMemoryConsumption),
                      Item(TextureMemoryConsumption, m_prevTexData));
    }
    computeNesting();
    setCollapsedRowCount(Constants::QML_MIN_LEVEL + 1);
    setExpandedRowCount(m_maximumMsgType + 2);
    QmlProfilerTimelineModel::finalize();
}

void Quick3DModel::clear()
{
    m_data.clear();
    m_maximumMsgType = -1;
    m_prevTexStartTime = -1;
    m_prevMeshStartTime = -1;
    m_maxMeshSize = 0;
    m_maxTextureSize = 0;
    QmlProfilerTimelineModel::clear();
}

QVariantMap Quick3DModel::location(int index) const
{
    return locationFromTypeId(index);
}

} // namespace Internal
} // namespace QmlProfiler