summaryrefslogtreecommitdiff
path: root/plugins/qmlprofilerextension/scenegraphtimelinemodel.cpp
blob: 86066261a6c3966344b4bf1bb8bc68570e7b26db (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com <http://qt.digia.com/>
**
** This file is part of the Qt Enterprise Qt Quick Profiler Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com <http://qt.digia.com/>
**
****************************************************************************/

#include "scenegraphtimelinemodel.h"
#include "qmldebug/qmlprofilereventtypes.h"
#include "qmlprofiler/qmlprofilermodelmanager.h"

#include <QCoreApplication>
#include <QDebug>

namespace QmlProfilerExtension {
namespace Internal {

using namespace QmlProfiler::Internal;

enum SceneGraphEventType {
    SceneGraphRendererFrame,
    SceneGraphAdaptationLayerFrame,
    SceneGraphContextFrame,
    SceneGraphRenderLoopFrame,
    SceneGraphTexturePrepare,
    SceneGraphTextureDeletion,
    SceneGraphPolishAndSync,
    SceneGraphWindowsRenderShow,
    SceneGraphWindowsAnimations,
    SceneGraphWindowsPolishFrame,

    MaximumSceneGraphFrameType
};

enum SceneGraphCategoryType {
    SceneGraphRenderThread,
    SceneGraphGUIThread,

    MaximumSceneGraphCategoryType
};

class SceneGraphTimelineModel::SceneGraphTimelineModelPrivate {
public:
    SceneGraphTimelineModelPrivate(SceneGraphTimelineModel *qq):q(qq) {}
    ~SceneGraphTimelineModelPrivate();

    SceneGraphTimelineModel *q;

    QVector < SceneGraphTimelineModel::SceneGraphEvent > eventList;
    bool isExpanded;
    QString displayTime(double time);
    void addVP(QVariantList &l, QString label, qint64 time);
};

SceneGraphTimelineModel::SceneGraphTimelineModel(QObject *parent)
    : AbstractTimelineModel(parent), d(new SceneGraphTimelineModelPrivate(this))
{
}

SceneGraphTimelineModel::~SceneGraphTimelineModel()
{
}

int SceneGraphTimelineModel::categories() const
{
    return 1;
}

QStringList SceneGraphTimelineModel::categoryTitles() const
{
    QStringList retString;
    retString << categoryLabel(0);
    return retString;
}

QString SceneGraphTimelineModel::name() const
{
    return QLatin1String("SceneGraphTimeLineModel");
}

int SceneGraphTimelineModel::count() const
{
    return d->eventList.count();
}

bool SceneGraphTimelineModel::isEmpty() const
{
    return d->eventList.isEmpty();
}

bool SceneGraphTimelineModel::eventAccepted(const QmlProfiler::Internal::QmlProfilerSimpleModel::QmlEventData &event) const
{
    return (event.eventType == QmlDebug::SceneGraphFrameEvent);
}

qint64 SceneGraphTimelineModel::lastTimeMark() const
{
    return d->eventList.last().startTime;
}

bool SceneGraphTimelineModel::expanded(int ) const
{
    return d->isExpanded;
}

void SceneGraphTimelineModel::setExpanded(int category, bool expanded)
{
    Q_UNUSED(category);
    d->isExpanded = expanded;
}

int SceneGraphTimelineModel::categoryDepth(int categoryIndex) const
{
    Q_UNUSED(categoryIndex);
    if (isEmpty())
        return 1;
    return 3;
}

int SceneGraphTimelineModel::categoryCount() const
{
    return 1;
}

const QString SceneGraphTimelineModel::categoryLabel(int categoryIndex) const
{
    Q_UNUSED(categoryIndex);
    return tr("Scene Graph");
}

int SceneGraphTimelineModel::findFirstIndex(qint64 startTime) const
{
    int candidate = findFirstIndexNoParents(startTime);
    // because there's two asynchronous threads in the display,
    // check the former event in same thread for false positives
    int i = candidate - 1;
    while (i >= 0) {
        if (d->eventList[candidate].sgEventType == d->eventList[i].sgEventType) {
            if (d->eventList[i].startTime + d->eventList[i].duration >= startTime)
                candidate = i;
            else
                break;
        }
        i--;
    }

    return candidate;
}

int SceneGraphTimelineModel::findFirstIndexNoParents(qint64 startTime) const
{
    if (d->eventList.isEmpty())
        return -1;
    if (d->eventList.count() == 1 || d->eventList.first().startTime+d->eventList.first().duration >= startTime)
        return 0;
    else
        if (d->eventList.last().startTime+d->eventList.last().duration <= startTime)
            return -1;

    int fromIndex = 0;
    int toIndex = d->eventList.count()-1;
    while (toIndex - fromIndex > 1) {
        int midIndex = (fromIndex + toIndex)/2;
        if (d->eventList[midIndex].startTime + d->eventList[midIndex].duration < startTime)
            fromIndex = midIndex;
        else
            toIndex = midIndex;
    }
    return toIndex;
}

int SceneGraphTimelineModel::findLastIndex(qint64 endTime) const
{
    if (d->eventList.isEmpty())
        return -1;
    if (d->eventList.first().startTime >= endTime)
        return -1;
    if (d->eventList.count() == 1)
        return 0;
    if (d->eventList.last().startTime <= endTime)
        return d->eventList.count()-1;

    int fromIndex = 0;
    int toIndex = d->eventList.count()-1;
    while (toIndex - fromIndex > 1) {
        int midIndex = (fromIndex + toIndex)/2;
        if (d->eventList[midIndex].startTime < endTime)
            fromIndex = midIndex;
        else
            toIndex = midIndex;
    }

    return fromIndex;
}

int SceneGraphTimelineModel::getEventType(int index) const
{
    Q_UNUSED(index);
    return QmlDebug::SceneGraphFrameEvent;
}

int SceneGraphTimelineModel::getEventCategory(int index) const
{
    Q_UNUSED(index);
    return 0;
}

int SceneGraphTimelineModel::getEventRow(int index) const
{
    return d->eventList[index].sgEventType + 1;
    if (d->isExpanded)
        return d->eventList[index].sgEventType + 1;
    else
        return 0;
}

qint64 SceneGraphTimelineModel::getDuration(int index) const
{
    return d->eventList[index].duration;
}

qint64 SceneGraphTimelineModel::getStartTime(int index) const
{
    return d->eventList[index].startTime;
}

qint64 SceneGraphTimelineModel::getEndTime(int index) const
{
    return getStartTime(index)+getDuration(index);
}

int SceneGraphTimelineModel::getEventId(int index) const
{
    return d->eventList[index].sgEventType;
}

QColor SceneGraphTimelineModel::getColor(int index) const
{
    // get duration in seconds
    double eventDuration = getDuration(index) / 1e9;

    // supposedly never above 60 frames per second
    // limit it in that case
    if (eventDuration < 1/60.0)
        eventDuration = 1/60.0;

    // generate hue based on fraction of the 60fps
    double fpsFraction = 1 / (eventDuration * 60.0);
    if (fpsFraction > 1.0)
        fpsFraction = 1.0;
    return QColor::fromHsl((fpsFraction*96)+10, 76, 166);
}

float SceneGraphTimelineModel::getHeight(int index) const
{
    Q_UNUSED(index);
    return 1.0f;
}

QString labelForSGType(int t)
{
    switch ((SceneGraphCategoryType)t) {
    case SceneGraphRenderThread:
        return QCoreApplication::translate("SceneGraphTimelineModel", "Renderer Thread");
    case SceneGraphGUIThread:
        return QCoreApplication::translate("SceneGraphTimelineModel", "GUI Thread");
    default: return QString();
    }
}

const QVariantList SceneGraphTimelineModel::getLabelsForCategory(int category) const
{
    Q_UNUSED(category);
    QVariantList result;

    if (d->isExpanded && !isEmpty()) {
        for (int i = 0; i < MaximumSceneGraphCategoryType; i++) {
            QVariantMap element;

            element.insert(QLatin1String("displayName"), QVariant(labelForSGType(i)));
            element.insert(QLatin1String("description"), QVariant(labelForSGType(i)));
            element.insert(QLatin1String("id"), QVariant(i));
            result << element;
        }
    }

    return result;
}



QString SceneGraphTimelineModel::SceneGraphTimelineModelPrivate::displayTime(double time)
{
    if (time < 1e6)
        return QString::number(time/1e3,'f',3) + trUtf8(" \xc2\xb5s");
    if (time < 1e9)
        return QString::number(time/1e6,'f',3) + tr(" ms");

    return QString::number(time/1e9,'f',3) + tr(" s");
}

void SceneGraphTimelineModel::SceneGraphTimelineModelPrivate::addVP(QVariantList &l, QString label, qint64 time)
{
    if (time > 0) {
        QVariantMap res;
        res.insert(label, QVariant(displayTime(time)));
        l << res;
    }
}


const QVariantList SceneGraphTimelineModel::getEventDetails(int index) const
{
    QVariantList result;
    SceneGraphEvent *ev = &d->eventList[index];

    {
        QVariantMap res;
        res.insert(QLatin1String("title"), QVariant(labelForSGType(ev->sgEventType)));
        result << res;
    }

    d->addVP(result, tr("Duration"), ev->duration );

    if (ev->sgEventType == SceneGraphRenderThread) {
        d->addVP(result, tr("Polish"), ev->timing[14]);
        d->addVP(result, tr("Sync"), ev->timing[0]);
        d->addVP(result, tr("Preprocess"), ev->timing[1]);
        d->addVP(result, tr("Upload"), ev->timing[2]);
        d->addVP(result, tr("Swizzle"), ev->timing[3]);
        d->addVP(result, tr("Convert"), ev->timing[4]);
        d->addVP(result, tr("Mipmap"), ev->timing[5]);
        d->addVP(result, tr("Bind"), ev->timing[6]);
        d->addVP(result, tr("Material"), ev->timing[7]);
        d->addVP(result, tr("Glyph Render"), ev->timing[8]);
        d->addVP(result, tr("Glyph Store"), ev->timing[9]);
        d->addVP(result, tr("Update"), ev->timing[10]);
        d->addVP(result, tr("Binding"), ev->timing[11]);
        d->addVP(result, tr("Render"), ev->timing[12]);
        d->addVP(result, tr("Swap"), ev->timing[13]);
        d->addVP(result, tr("Animations"), ev->timing[15]);
    }
    if (ev->sgEventType == SceneGraphGUIThread) {
        d->addVP(result, tr("Polish"), ev->timing[0]);
        d->addVP(result, tr("Wait"), ev->timing[1]);
        d->addVP(result, tr("Sync"), ev->timing[2]);
        d->addVP(result, tr("Animations"), ev->timing[3]);
    }

    return result;
}

const QVariantMap SceneGraphTimelineModel::getEventLocation(int /*index*/) const
{
    QVariantMap map;
    return map;
}

int SceneGraphTimelineModel::getEventIdForHash(const QString &) const
{
    return -1;
}

int SceneGraphTimelineModel::getEventIdForLocation(const QString &/*filename*/, int /*line*/, int /*column*/) const
{
    return -1;
}

bool compareStartTimes(const SceneGraphTimelineModel::SceneGraphEvent&t1, const SceneGraphTimelineModel::SceneGraphEvent &t2)
{
    return t1.startTime < t2.startTime;
}

void SceneGraphTimelineModel::loadData()
{
    clear();
    QmlProfilerSimpleModel *simpleModel = m_modelManager->simpleModel();
    if (simpleModel->isEmpty())
        return;

    int lastRenderEvent = -1;

    // combine the data of several eventtypes into two rows
    foreach (const QmlProfilerSimpleModel::QmlEventData &event, simpleModel->getEvents()) {
        if (!eventAccepted(event))
            continue;

        if (event.bindingType == SceneGraphRenderLoopFrame) {
            SceneGraphEvent newEvent;
            newEvent.sgEventType = SceneGraphRenderThread;
            newEvent.duration = event.numericData1 + event.numericData2 + event.numericData3;
            newEvent.startTime = event.startTime - newEvent.duration;
            for (int i=0; i < timingFieldCount; i++)
                newEvent.timing[i] = 0;

            // Filter out events with incorrect timings due to interrupted thread on server side
            if (newEvent.duration > 0 && newEvent.startTime > 0)
                d->eventList << newEvent;
            lastRenderEvent = d->eventList.count()-1;
        }

        if (lastRenderEvent >= 0) {
            switch ((SceneGraphEventType)event.bindingType) {
            case SceneGraphRendererFrame: {
                d->eventList[lastRenderEvent].timing[1] = event.numericData1;
                d->eventList[lastRenderEvent].timing[10] = event.numericData2;
                d->eventList[lastRenderEvent].timing[11] = event.numericData3;
                d->eventList[lastRenderEvent].timing[12] = event.numericData4;
                break;
            }
            case SceneGraphAdaptationLayerFrame: {
                d->eventList[lastRenderEvent].timing[8] = event.numericData2;
                d->eventList[lastRenderEvent].timing[9] = event.numericData3;
                break;
            }
            case SceneGraphContextFrame: {
                d->eventList[lastRenderEvent].timing[7] = event.numericData1;
                break;
            }
            case SceneGraphRenderLoopFrame: {
                d->eventList[lastRenderEvent].timing[0] = event.numericData1;
                d->eventList[lastRenderEvent].timing[13] = event.numericData3;

                break;
            }
            case SceneGraphTexturePrepare: {
                d->eventList[lastRenderEvent].timing[2] = event.numericData4;
                d->eventList[lastRenderEvent].timing[3] = event.numericData3;
                d->eventList[lastRenderEvent].timing[4] = event.numericData2;
                d->eventList[lastRenderEvent].timing[5] = event.numericData5;
                d->eventList[lastRenderEvent].timing[6] = event.numericData1;
                break;
            }
            case SceneGraphPolishAndSync: {
                // GUI thread
                SceneGraphEvent newEvent;
                newEvent.sgEventType = SceneGraphGUIThread;
                newEvent.duration = event.numericData1 + event.numericData2 + event.numericData3 + event.numericData4;
                newEvent.startTime = event.startTime - newEvent.duration;
                for (int i=0; i < timingFieldCount; i++)
                    newEvent.timing[i] = 0;

                newEvent.timing[0] = event.numericData1;
                newEvent.timing[1] = event.numericData2;
                newEvent.timing[2] = event.numericData3;
                newEvent.timing[3] = event.numericData4;

                // Filter out events with incorrect timings due to interrupted thread on server side
                if (newEvent.duration > 0 && newEvent.startTime > 0)
                    d->eventList << newEvent;

                break;
            }
            case SceneGraphWindowsAnimations: {
                d->eventList[lastRenderEvent].timing[14] = event.numericData1;
                break;
            }
            case SceneGraphWindowsPolishFrame: {
                d->eventList[lastRenderEvent].timing[15] = event.numericData1;
                break;
            }
            default: break;
            }
        }

        m_modelManager->modelProxyCountUpdated(m_modelId, d->eventList.count(), simpleModel->getEvents().count());
    }

    qSort(d->eventList.begin(), d->eventList.end(), compareStartTimes);
    m_modelManager->modelProxyCountUpdated(m_modelId, 1, 1);
}

void SceneGraphTimelineModel::clear()
{
    d->eventList.clear();
    d->isExpanded = false;
    m_modelManager->modelProxyCountUpdated(m_modelId, 0, 1);
}

void SceneGraphTimelineModel::dataChanged()
{
    if (m_modelManager->state() == QmlProfilerDataState::ProcessingData)
        loadData();

    if (m_modelManager->state() == QmlProfilerDataState::Empty)
        clear();

    emit stateChanged();
    emit dataAvailable();
    emit emptyChanged();
    emit expandedChanged();
}




} // namespace Internal
} // namespace QmlProfilerExtension