qmlprofilerpainteventsmodelproxy.cpp 10.1 KB
Newer Older
Christiaan Janssen's avatar
Christiaan Janssen committed
1
2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
Christiaan Janssen's avatar
Christiaan Janssen committed
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
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "qmlprofilerpainteventsmodelproxy.h"
#include "qmlprofilermodelmanager.h"
32
#include "qmlprofilerdatamodel.h"
33
#include "sortedtimelinemodel.h"
34
#include "abstracttimelinemodel_p.h"
35
#include <utils/qtcassert.h>
Christiaan Janssen's avatar
Christiaan Janssen committed
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <QCoreApplication>

#include <QVector>
#include <QHash>
#include <QUrl>
#include <QString>
#include <QStack>

#include <QDebug>

namespace QmlProfiler {
namespace Internal {

49
50
class PaintEventsModelProxy::PaintEventsModelProxyPrivate :
        public SortedTimelineModel<QmlPaintEventData,
51
                                   AbstractTimelineModel::AbstractTimelineModelPrivate>
Christiaan Janssen's avatar
Christiaan Janssen committed
52
53
54
{
public:

55
56
    int maxGuiThreadAnimations;
    int maxRenderThreadAnimations;
57
    bool seenForeignPaintEvent;
58
    int rowFromThreadId(QmlDebug::AnimationThread threadId) const;
Christiaan Janssen's avatar
Christiaan Janssen committed
59

60
61
private:
    Q_DECLARE_PUBLIC(PaintEventsModelProxy)
Christiaan Janssen's avatar
Christiaan Janssen committed
62
63
64
};

PaintEventsModelProxy::PaintEventsModelProxy(QObject *parent)
65
    : AbstractTimelineModel(new PaintEventsModelProxyPrivate,
66
                                  QLatin1String("PaintEventsModelProxy"), tr("Painting"),
67
                                  QmlDebug::Event, QmlDebug::MaximumRangeType, parent)
Christiaan Janssen's avatar
Christiaan Janssen committed
68
{
69
70
    Q_D(PaintEventsModelProxy);
    d->seenForeignPaintEvent = false;
71
    d->maxGuiThreadAnimations = d->maxRenderThreadAnimations = 0;
Christiaan Janssen's avatar
Christiaan Janssen committed
72
73
74
75
76
}


void PaintEventsModelProxy::clear()
{
77
    Q_D(PaintEventsModelProxy);
78
    d->clear();
79
    d->maxGuiThreadAnimations = d->maxRenderThreadAnimations = 0;
Christiaan Janssen's avatar
Christiaan Janssen committed
80
    d->expanded = false;
81
    d->seenForeignPaintEvent = false;
82
    d->modelManager->modelProxyCountUpdated(d->modelId, 0, 1);
Christiaan Janssen's avatar
Christiaan Janssen committed
83
84
}

85
bool PaintEventsModelProxy::eventAccepted(const QmlProfilerDataModel::QmlEventTypeData &event) const
Christiaan Janssen's avatar
Christiaan Janssen committed
86
{
87
    return AbstractTimelineModel::eventAccepted(event) &&
88
            event.detailType== QmlDebug::AnimationFrame;
Christiaan Janssen's avatar
Christiaan Janssen committed
89
90
91
92
}

void PaintEventsModelProxy::loadData()
{
93
    Q_D(PaintEventsModelProxy);
Christiaan Janssen's avatar
Christiaan Janssen committed
94
    clear();
95
    QmlProfilerDataModel *simpleModel = d->modelManager->qmlModel();
Christiaan Janssen's avatar
Christiaan Janssen committed
96
97
98
99
    if (simpleModel->isEmpty())
        return;

    // collect events
100
101
    const QVector<QmlProfilerDataModel::QmlEventData> &referenceList = simpleModel->getEvents();
    const QVector<QmlProfilerDataModel::QmlEventTypeData> &typeList = simpleModel->getEventTypes();
102
103

    QmlPaintEventData lastEvent;
104
    qint64 minNextStartTimes[] = {0, 0};
105

106
    foreach (const QmlProfilerDataModel::QmlEventData &event, referenceList) {
107
108
109
        const QmlProfilerDataModel::QmlEventTypeData &type = typeList[event.typeIndex];
        if (!eventAccepted(type)) {
            if (type.rangeType == QmlDebug::Painting)
110
                d->seenForeignPaintEvent = true;
Christiaan Janssen's avatar
Christiaan Janssen committed
111
            continue;
112
        }
Christiaan Janssen's avatar
Christiaan Janssen committed
113

114
115
        lastEvent.threadId = (QmlDebug::AnimationThread)event.numericData3;

Christiaan Janssen's avatar
Christiaan Janssen committed
116
        // initial estimation of the event duration: 1/framerate
117
        qint64 estimatedDuration = event.numericData1 > 0 ? 1e9/event.numericData1 : 1;
Christiaan Janssen's avatar
Christiaan Janssen committed
118
119

        // the profiler registers the animation events at the end of them
120
        qint64 realEndTime = event.startTime;
Christiaan Janssen's avatar
Christiaan Janssen committed
121

122
        // ranges should not overlap. If they do, our estimate wasn't accurate enough
123
        qint64 realStartTime = qMax(event.startTime - estimatedDuration, minNextStartTimes[lastEvent.threadId]);
Christiaan Janssen's avatar
Christiaan Janssen committed
124

125
126
127
        // Sometimes our estimate is far off or the server has miscalculated the frame rate
        if (realStartTime >= realEndTime)
            realEndTime = realStartTime + 1;
Christiaan Janssen's avatar
Christiaan Janssen committed
128

129
130
        // Don't "fix" the framerate even if we've fixed the duration.
        // The server should know better after all and if it doesn't we want to see that.
131
132
        lastEvent.framerate = (int)event.numericData1;
        lastEvent.animationcount = (int)event.numericData2;
133
134
        QTC_ASSERT(lastEvent.animationcount > 0, continue);

135
136
        d->insert(realStartTime, realEndTime - realStartTime, lastEvent);

137
138
139
140
141
142
143
        if (lastEvent.threadId == QmlDebug::GuiThread)
            d->maxGuiThreadAnimations = qMax(lastEvent.animationcount, d->maxGuiThreadAnimations);
        else
            d->maxRenderThreadAnimations =
                    qMax(lastEvent.animationcount, d->maxRenderThreadAnimations);

        minNextStartTimes[lastEvent.threadId] = event.startTime + 1;
Christiaan Janssen's avatar
Christiaan Janssen committed
144

145
        d->modelManager->modelProxyCountUpdated(d->modelId, d->count(), referenceList.count());
Christiaan Janssen's avatar
Christiaan Janssen committed
146
147
    }

148
    d->computeNesting();
Christiaan Janssen's avatar
Christiaan Janssen committed
149

150
    d->modelManager->modelProxyCountUpdated(d->modelId, 1, 1);
Christiaan Janssen's avatar
Christiaan Janssen committed
151
152
153
154
}

/////////////////// QML interface

155
int PaintEventsModelProxy::rowCount() const
Christiaan Janssen's avatar
Christiaan Janssen committed
156
{
157
    Q_D(const PaintEventsModelProxy);
Christiaan Janssen's avatar
Christiaan Janssen committed
158
    if (isEmpty())
159
        return d->seenForeignPaintEvent ? 0 : 1;
Christiaan Janssen's avatar
Christiaan Janssen committed
160
    else
161
        return (d->maxGuiThreadAnimations == 0 || d->maxRenderThreadAnimations == 0) ? 2 : 3;
Christiaan Janssen's avatar
Christiaan Janssen committed
162
163
}

164
165
166
167
168
169
int PaintEventsModelProxy::PaintEventsModelProxyPrivate::rowFromThreadId(
        QmlDebug::AnimationThread threadId) const
{
    return (threadId == QmlDebug::GuiThread || maxGuiThreadAnimations == 0) ? 1 : 2;
}

Christiaan Janssen's avatar
Christiaan Janssen committed
170
171
int PaintEventsModelProxy::getEventRow(int index) const
{
172
    Q_D(const PaintEventsModelProxy);
173
    return d->rowFromThreadId(d->range(index).threadId);
Christiaan Janssen's avatar
Christiaan Janssen committed
174
175
}

176
177
178
179
180
181
182
183
184
185
186
187
188
189
int PaintEventsModelProxy::rowMaxValue(int rowNumber) const
{
    Q_D(const PaintEventsModelProxy);
    switch (rowNumber) {
    case 1:
        return d->maxGuiThreadAnimations > 0 ? d->maxGuiThreadAnimations :
                                               d->maxRenderThreadAnimations;
    case 2:
        return d->maxRenderThreadAnimations;
    default:
        return AbstractTimelineModel::rowMaxValue(rowNumber);
    }
}

Christiaan Janssen's avatar
Christiaan Janssen committed
190
191
int PaintEventsModelProxy::getEventId(int index) const
{
192
193
    Q_D(const PaintEventsModelProxy);
    return d->range(index).threadId;
Christiaan Janssen's avatar
Christiaan Janssen committed
194
195
196
197
}

QColor PaintEventsModelProxy::getColor(int index) const
{
198
    Q_D(const PaintEventsModelProxy);
199
    double fpsFraction = d->range(index).framerate / 60.0;
Christiaan Janssen's avatar
Christiaan Janssen committed
200
201
202
203
    if (fpsFraction > 1.0)
        fpsFraction = 1.0;
    if (fpsFraction < 0.0)
        fpsFraction = 0.0;
204
    return getFractionColor(fpsFraction);
Christiaan Janssen's avatar
Christiaan Janssen committed
205
206
207
208
}

float PaintEventsModelProxy::getHeight(int index) const
{
209
    Q_D(const PaintEventsModelProxy);
210
    const PaintEventsModelProxyPrivate::Range &range = d->range(index);
211
212
213
214
215
216
217
218
219

    // Add some height to the events if we're far from the scale threshold of 2 * DefaultRowHeight.
    // Like that you can see the smaller events more easily.
    int scaleThreshold = 2 * DefaultRowHeight - rowHeight(d->rowFromThreadId(range.threadId));
    float boost = scaleThreshold > 0 ? (0.15 * scaleThreshold / DefaultRowHeight) : 0;

    return boost + (1.0 - boost) * (float)range.animationcount /
            (float)(range.threadId == QmlDebug::GuiThread ? d->maxGuiThreadAnimations :
                                                            d->maxRenderThreadAnimations);
Christiaan Janssen's avatar
Christiaan Janssen committed
220
221
}

222
const QVariantList PaintEventsModelProxy::getLabels() const
Christiaan Janssen's avatar
Christiaan Janssen committed
223
{
224
    Q_D(const PaintEventsModelProxy);
Christiaan Janssen's avatar
Christiaan Janssen committed
225
226
    QVariantList result;

227
    if (d->maxGuiThreadAnimations > 0) {
Christiaan Janssen's avatar
Christiaan Janssen committed
228
        QVariantMap element;
229
230
231
        element.insert(QLatin1String("displayName"), QVariant(tr("Animations")));
        element.insert(QLatin1String("description"), QVariant(tr("GUI Thread")));
        element.insert(QLatin1String("id"), QVariant(QmlDebug::GuiThread));
Christiaan Janssen's avatar
Christiaan Janssen committed
232
233
234
        result << element;
    }

235
236
237
238
239
240
    if (d->maxRenderThreadAnimations > 0) {
        QVariantMap element;
        element.insert(QLatin1String("displayName"), QVariant(tr("Animations")));
        element.insert(QLatin1String("description"), QVariant(tr("Render Thread")));
        element.insert(QLatin1String("id"), QVariant(QmlDebug::RenderThread));
        result << element;
Christiaan Janssen's avatar
Christiaan Janssen committed
241
    }
242
243

    return result;
Christiaan Janssen's avatar
Christiaan Janssen committed
244
245
246
247
}

const QVariantList PaintEventsModelProxy::getEventDetails(int index) const
{
248
    Q_D(const PaintEventsModelProxy);
Christiaan Janssen's avatar
Christiaan Janssen committed
249
250
251
252
253
    QVariantList result;

    static const char trContext[] = "RangeDetails";
    {
        QVariantMap valuePair;
254
        valuePair.insert(QLatin1String("title"), QVariant(title()));
Christiaan Janssen's avatar
Christiaan Janssen committed
255
256
257
258
259
260
        result << valuePair;
    }

    // duration
    {
        QVariantMap valuePair;
261
        valuePair.insert(QCoreApplication::translate(trContext, "Duration:"),
262
                         QVariant(QmlProfilerBaseModel::formatTime(d->range(index).duration)));
Christiaan Janssen's avatar
Christiaan Janssen committed
263
264
265
266
267
268
        result << valuePair;
    }

    // duration
    {
        QVariantMap valuePair;
269
        valuePair.insert(QCoreApplication::translate(trContext, "Framerate:"), QVariant(QString::fromLatin1("%1 FPS").arg(d->range(index).framerate)));
Christiaan Janssen's avatar
Christiaan Janssen committed
270
271
272
273
274
275
        result << valuePair;
    }

    // duration
    {
        QVariantMap valuePair;
276
        valuePair.insert(QCoreApplication::translate(trContext, "Animations:"), QVariant(QString::fromLatin1("%1").arg(d->range(index).animationcount)));
Christiaan Janssen's avatar
Christiaan Janssen committed
277
278
279
        result << valuePair;
    }

280
281
282
283
284
285
286
287
288
    {
        QVariantMap valuePair;
        valuePair.insert(QCoreApplication::translate(trContext, "Context:"),
                         QCoreApplication::translate(trContext,
                                d->range(index).threadId == QmlDebug::GuiThread ?
                                "GUI Thread" : "Render Thread"));
        result << valuePair;
    }

Christiaan Janssen's avatar
Christiaan Janssen committed
289
290
291
292
293
    return result;
}

}
}