aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/tracing/timelineitemsrenderpass.cpp
blob: a4f89dc64d0de9f550f81eb4da3e8124b0caf404 (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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "timelineitemsrenderpass.h"
#include "timelinerenderstate.h"
#include <QSGSimpleRectNode>
#include <QSGVertexColorMaterial>
#include <QtAlgorithms>

namespace Timeline {

class TimelineItemsRenderPassState : public TimelineRenderPass::State {
public:
    TimelineItemsRenderPassState(const TimelineModel *model);
    ~TimelineItemsRenderPassState();

    QSGNode *expandedRow(int row) const { return m_expandedRows[row]; }
    QSGNode *collapsedRow(int row) const { return m_collapsedRows[row]; }

    const QVector<QSGNode *> &expandedRows() const { return m_expandedRows; }
    const QVector<QSGNode *> &collapsedRows() const { return m_collapsedRows; }
    TimelineItemsMaterial *collapsedRowMaterial() { return &m_collapsedRowMaterial; }

    int indexFrom() const { return m_indexFrom; }
    int indexTo() const { return m_indexTo; }
    void updateIndexes(int from, int to);
    void updateCollapsedRowMaterial(float xScale, int selectedItem, QColor selectionColor);

private:
    int m_indexFrom;
    int m_indexTo;
    TimelineItemsMaterial m_collapsedRowMaterial;

    QVector<QSGNode *> m_expandedRows;
    QVector<QSGNode *> m_collapsedRows;
};

struct TimelineItemsGeometry {
    // Vertex indices are 16bit
    static const int maxVerticesPerNode = 0xffff;

    enum VerticesPerEvent {
        NoVertices = 0,
        VerticesForSameHeight = 4,
        VerticesForDifferentHeight = 6
    };

    TimelineItemsGeometry();
    uint usedVertices;

    OpaqueColoredPoint2DWithSize prevNode;
    OpaqueColoredPoint2DWithSize currentNode;

    QSGGeometryNode *node;

    void initNodes();

    bool isEmpty() const;

    void allocate(QSGMaterial *material);
    VerticesPerEvent addVertices();
    void addEvent();

    void nextNode(float itemLeft, float itemTop, float itemWidth = 0, float selectionId = 0,
                  uchar red = 0, uchar green = 0, uchar blue = 0);
    void updateCurrentNode(float itemRight, float itemTop);
};

class NodeUpdater {
public:
    NodeUpdater(const TimelineModel *model, const TimelineRenderState *parentState,
                TimelineItemsRenderPassState *state, int indexFrom, int indexTo);
    void run();

private:
    static const int s_maxNumItems = 1 << 20;
    static const qint64 s_invalidTimestamp = 0xffffffffffffffffLL;

    struct ItemDescription
    {
        uchar red;
        uchar green;
        uchar blue;

        float width;
        float left;
        float right;

        float top;
        float selectionId;
    };

    void calculateDistances();
    int updateVertices(TimelineItemsGeometry &geometry, const QVarLengthArray<qint64> &distances,
                       qint64 minDistance, float itemTop, int i) const;
    void addEvent(TimelineItemsGeometry &geometry, const QVarLengthArray<qint64> &distances,
                  qint64 minDistance, const ItemDescription &item, int i) const;
    int updateNodes(const int from, const int to) const;

    const TimelineModel *m_model;
    const TimelineRenderState *m_parentState;
    const int m_indexFrom;
    const int m_indexTo;

    TimelineItemsRenderPassState *m_state;
    QVarLengthArray<qint64> m_collapsedDistances;
    QVarLengthArray<qint64> m_expandedDistances;
    qint64 m_minCollapsedDistance;
    qint64 m_minExpandedDistance;
};

void OpaqueColoredPoint2DWithSize::set(float nx, float ny, float nw, float nh, float nid,
                                       uchar nr, uchar ng, uchar nb, uchar d) {
    x = nx; y = ny; w = nw; h = nh; id = nid;
    r = nr; g = ng, b = nb; a = d;
}

float OpaqueColoredPoint2DWithSize::top() const
{
    return id < 0 ? (y / -id) : y;
}

void OpaqueColoredPoint2DWithSize::update(float nr, float ny)
{
    if (a <= MaximumDirection) {
        a += MaximumDirection;
        id = -2;
    } else {
        --id;
    }

    y += ny;
    w = nr - x;
}

OpaqueColoredPoint2DWithSize::Direction OpaqueColoredPoint2DWithSize::direction() const
{
    return static_cast<Direction>(a > MaximumDirection ? a - MaximumDirection : a);
}

void OpaqueColoredPoint2DWithSize::setCommon(const OpaqueColoredPoint2DWithSize *master)
{
    a = 255;
    if (master->a > MaximumDirection) {
        id = std::numeric_limits<float>::lowest();
        r = g = b = 128;
    } else {
        id = master->id;
        r = master->r;
        g = master->g;
        b = master->b;
    }
}

void OpaqueColoredPoint2DWithSize::setLeft(const OpaqueColoredPoint2DWithSize *master)
{
    w = -master->w;
    x = master->x;
}

void OpaqueColoredPoint2DWithSize::setRight(const OpaqueColoredPoint2DWithSize *master)
{
    w = master->w;
    x = master->x + master->w;
}

void OpaqueColoredPoint2DWithSize::setTop(const OpaqueColoredPoint2DWithSize *master)
{
    y = master->id < 0 ? master->y / -master->id : master->y;
    h = TimelineModel::defaultRowHeight() - y;
}

void OpaqueColoredPoint2DWithSize::setBottom(const OpaqueColoredPoint2DWithSize *master)
{
    y = TimelineModel::defaultRowHeight();
    h = (master->id < 0 ? master->y / -master->id : master->y) - TimelineModel::defaultRowHeight();
}

void OpaqueColoredPoint2DWithSize::setBottomLeft(const OpaqueColoredPoint2DWithSize *master)
{
    setCommon(master);
    setLeft(master);
    setBottom(master);
}

void OpaqueColoredPoint2DWithSize::setBottomRight(const OpaqueColoredPoint2DWithSize *master)
{
    setCommon(master);
    setRight(master);
    setBottom(master);
}

void OpaqueColoredPoint2DWithSize::setTopLeft(const OpaqueColoredPoint2DWithSize *master)
{
    setCommon(master);
    setLeft(master);
    setTop(master);
}

void OpaqueColoredPoint2DWithSize::setTopRight(const OpaqueColoredPoint2DWithSize *master)
{
    setCommon(master);
    setRight(master);
    setTop(master);
}

void TimelineItemsGeometry::addEvent()
{
    OpaqueColoredPoint2DWithSize *v =
            OpaqueColoredPoint2DWithSize::fromVertexData(node->geometry());
    switch (currentNode.direction()) {
    case OpaqueColoredPoint2DWithSize::BottomToTop:
        v[usedVertices++].setBottomLeft(&currentNode);
        v[usedVertices++].setBottomRight(&currentNode);
        v[usedVertices++].setTopLeft(&currentNode);
        v[usedVertices++].setTopRight(&currentNode);
        break;
    case OpaqueColoredPoint2DWithSize::TopToBottom:
        if (prevNode.top() != currentNode.top()) {
            v[usedVertices++].setTopRight(&prevNode);
            v[usedVertices++].setTopLeft(&currentNode);
        }
        v[usedVertices++].setTopLeft((&currentNode));
        v[usedVertices++].setTopRight((&currentNode));
        v[usedVertices++].setBottomLeft((&currentNode));
        v[usedVertices++].setBottomRight((&currentNode));
        break;
    default:
        break;
    }
}

OpaqueColoredPoint2DWithSize *OpaqueColoredPoint2DWithSize::fromVertexData(QSGGeometry *geometry)
{
    Q_ASSERT(geometry->attributeCount() == 4);
    Q_ASSERT(geometry->sizeOfVertex() == sizeof(OpaqueColoredPoint2DWithSize));
    const QSGGeometry::Attribute *attributes = geometry->attributes();
    Q_ASSERT(attributes[0].position == 0);
    Q_ASSERT(attributes[0].tupleSize == 2);
    Q_ASSERT(attributes[0].type == GL_FLOAT);
    Q_ASSERT(attributes[1].position == 1);
    Q_ASSERT(attributes[1].tupleSize == 2);
    Q_ASSERT(attributes[1].type == GL_FLOAT);
    Q_ASSERT(attributes[2].position == 2);
    Q_ASSERT(attributes[2].tupleSize == 1);
    Q_ASSERT(attributes[2].type == GL_FLOAT);
    Q_ASSERT(attributes[3].position == 3);
    Q_ASSERT(attributes[3].tupleSize == 4);
    Q_ASSERT(attributes[3].type == GL_UNSIGNED_BYTE);
    Q_UNUSED(attributes);
    return static_cast<OpaqueColoredPoint2DWithSize *>(geometry->vertexData());
}

TimelineItemsGeometry::TimelineItemsGeometry() : usedVertices(0), node(0)
{
    initNodes();
}

void TimelineItemsGeometry::initNodes()
{
    currentNode.set(0, TimelineModel::defaultRowHeight(), 0, 0, 0, 0, 0, 0,
                    OpaqueColoredPoint2DWithSize::InvalidDirection);
    prevNode.set(0, TimelineModel::defaultRowHeight(), 0, 0, 0, 0, 0, 0,
                 OpaqueColoredPoint2DWithSize::InvalidDirection);
}

bool TimelineItemsGeometry::isEmpty() const
{
    return usedVertices == 0 &&
            currentNode.direction() == OpaqueColoredPoint2DWithSize::InvalidDirection;
}

void TimelineItemsGeometry::allocate(QSGMaterial *material)
{
    QSGGeometry *geometry = new QSGGeometry(OpaqueColoredPoint2DWithSize::attributes(),
                                            usedVertices);
    Q_ASSERT(geometry->vertexData());
    geometry->setIndexDataPattern(QSGGeometry::StaticPattern);
    geometry->setVertexDataPattern(QSGGeometry::StaticPattern);
    node = new QSGGeometryNode;
    node->setGeometry(geometry);
    node->setFlag(QSGNode::OwnsGeometry, true);
    node->setMaterial(material);
    usedVertices = 0;
    initNodes();
}

TimelineItemsGeometry::VerticesPerEvent TimelineItemsGeometry::addVertices()
{
    switch (currentNode.direction()) {
    case OpaqueColoredPoint2DWithSize::BottomToTop:
        usedVertices += VerticesForSameHeight;
        return VerticesForSameHeight;
    case OpaqueColoredPoint2DWithSize::TopToBottom: {
        VerticesPerEvent vertices = (prevNode.top() != currentNode.top() ?
                    VerticesForDifferentHeight : VerticesForSameHeight);
        usedVertices += vertices;
        return vertices;
    } default:
        return NoVertices;
    }
}

void TimelineItemsGeometry::nextNode(float itemLeft, float itemTop, float itemWidth,
                                     float selectionId, uchar red, uchar green, uchar blue)
{
    prevNode = currentNode;
    currentNode.set(itemLeft, itemTop, itemWidth, TimelineModel::defaultRowHeight() - itemTop,
                    selectionId, red, green, blue,
                    currentNode.direction() == OpaqueColoredPoint2DWithSize::BottomToTop ?
                        OpaqueColoredPoint2DWithSize::TopToBottom :
                        OpaqueColoredPoint2DWithSize::BottomToTop);
}

void TimelineItemsGeometry::updateCurrentNode(float itemRight, float itemTop)
{
    currentNode.update(itemRight, itemTop);
}

class TimelineExpandedRowNode : public QSGNode {
public:
    TimelineItemsMaterial material;
    ~TimelineExpandedRowNode() override {}
};

static qint64 startTime(const TimelineModel *model, const TimelineRenderState *parentState, int i)
{
    return qMax(parentState->start(), model->startTime(i));
}

static qint64 endTime(const TimelineModel *model, const TimelineRenderState *parentState, int i)
{
    return qMin(parentState->end(), model->startTime(i) + model->duration(i));
}

const QSGGeometry::AttributeSet &OpaqueColoredPoint2DWithSize::attributes()
{
    static QSGGeometry::Attribute data[] = {
        QSGGeometry::Attribute::create(0, 2, GL_FLOAT, true),
        QSGGeometry::Attribute::create(1, 2, GL_FLOAT),
        QSGGeometry::Attribute::create(2, 1, GL_FLOAT),
        QSGGeometry::Attribute::create(3, 4, GL_UNSIGNED_BYTE)
    };
    static QSGGeometry::AttributeSet attrs = {
        4,
        sizeof(OpaqueColoredPoint2DWithSize),
        data
    };
    return attrs;
}

const TimelineItemsRenderPass *TimelineItemsRenderPass::instance()
{
    static const TimelineItemsRenderPass pass;
    return &pass;
}

TimelineRenderPass::State *TimelineItemsRenderPass::update(const TimelineAbstractRenderer *renderer,
                                                           const TimelineRenderState *parentState,
                                                           State *oldState, int indexFrom,
                                                           int indexTo, bool stateChanged,
                                                           float spacing) const
{
    Q_UNUSED(stateChanged);
    const TimelineModel *model = renderer->model();
    if (!model || indexFrom < 0 || indexTo > model->count() || indexFrom >= indexTo)
        return oldState;

    QColor selectionColor = (renderer->selectionLocked() ? QColor(96,0,255) :
                                                           QColor(Qt::blue)).lighter(130);

    TimelineItemsRenderPassState *state;
    if (oldState == 0)
        state = new TimelineItemsRenderPassState(model);
    else
        state = static_cast<TimelineItemsRenderPassState *>(oldState);


    int selectedItem = renderer->selectedItem() == -1 ? -1 :
            model->selectionId(renderer->selectedItem());

    state->updateCollapsedRowMaterial(spacing / parentState->scale(), selectedItem, selectionColor);

    if (state->indexFrom() < state->indexTo()) {
        if (indexFrom < state->indexFrom() || indexTo > state->indexTo())
            NodeUpdater(model, parentState, state, indexFrom, indexTo).run();
    } else if (indexFrom < indexTo) {
        NodeUpdater(model, parentState, state, indexFrom, indexTo).run();
    }

    if (model->expanded()) {
        for (int row = 0; row < model->expandedRowCount(); ++row) {
            TimelineExpandedRowNode *rowNode = static_cast<TimelineExpandedRowNode *>(
                        state->expandedRow(row));
            rowNode->material.setScale(
                        QVector2D(spacing / parentState->scale(),
                                  static_cast<float>(model->expandedRowHeight(row))) /
                                  static_cast<float>(TimelineModel::defaultRowHeight()));
            rowNode->material.setSelectedItem(selectedItem);
            rowNode->material.setSelectionColor(selectionColor);
        }
    }

    state->updateIndexes(indexFrom, indexTo);
    return state;
}

TimelineItemsRenderPass::TimelineItemsRenderPass()
{
}

class TimelineItemsMaterialShader : public QSGMaterialShader
{
public:
    TimelineItemsMaterialShader();

    void updateState(const RenderState &state, QSGMaterial *newEffect,
                     QSGMaterial *oldEffect) override;
    char const *const *attributeNames() const override;

private:
    void initialize() override;

    int m_matrix_id;
    int m_scale_id;
    int m_selection_color_id;
    int m_selected_item_id;
    int m_z_range_id;
};

TimelineItemsMaterialShader::TimelineItemsMaterialShader()
    : QSGMaterialShader()
{
    setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/tracing/timelineitems.vert"));
    setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/tracing/timelineitems.frag"));
}

void TimelineItemsMaterialShader::updateState(const RenderState &state, QSGMaterial *newMaterial,
                                              QSGMaterial *)
{
    if (state.isMatrixDirty()) {
        TimelineItemsMaterial *material = static_cast<TimelineItemsMaterial *>(newMaterial);
        program()->setUniformValue(m_matrix_id, state.combinedMatrix());
        program()->setUniformValue(m_scale_id, material->scale());
        program()->setUniformValue(m_selection_color_id, material->selectionColor());
        program()->setUniformValue(m_selected_item_id, material->selectedItem());
        program()->setUniformValue(m_z_range_id, GLfloat(1.0));
    }
}

char const *const *TimelineItemsMaterialShader::attributeNames() const
{
    static const char *const attr[] = {"vertexCoord", "rectSize", "selectionId", "vertexColor", 0};
    return attr;
}

void TimelineItemsMaterialShader::initialize()
{
    m_matrix_id = program()->uniformLocation("matrix");
    m_scale_id = program()->uniformLocation("scale");
    m_selection_color_id = program()->uniformLocation("selectionColor");
    m_selected_item_id = program()->uniformLocation("selectedItem");
    m_z_range_id = program()->uniformLocation("_qt_zRange");
}


TimelineItemsMaterial::TimelineItemsMaterial() : m_selectedItem(-1)
{
    setFlag(QSGMaterial::Blending, false);
}

QVector2D TimelineItemsMaterial::scale() const
{
    return m_scale;
}

void TimelineItemsMaterial::setScale(QVector2D scale)
{
    m_scale = scale;
}

float TimelineItemsMaterial::selectedItem() const
{
    return m_selectedItem;
}

void TimelineItemsMaterial::setSelectedItem(float selectedItem)
{
    m_selectedItem = selectedItem;
}

QColor TimelineItemsMaterial::selectionColor() const
{
    return m_selectionColor;
}

void TimelineItemsMaterial::setSelectionColor(QColor selectionColor)
{
    m_selectionColor = selectionColor;
}

QSGMaterialType *TimelineItemsMaterial::type() const
{
    static QSGMaterialType type;
    return &type;
}

QSGMaterialShader *TimelineItemsMaterial::createShader() const
{
    return new TimelineItemsMaterialShader;
}

TimelineItemsRenderPassState::TimelineItemsRenderPassState(const TimelineModel *model) :
    m_indexFrom(std::numeric_limits<int>::max()), m_indexTo(-1)
{
    m_expandedRows.reserve(model->expandedRowCount());
    m_collapsedRows.reserve(model->collapsedRowCount());
    for (int i = 0; i < model->expandedRowCount(); ++i) {
        TimelineExpandedRowNode *node = new TimelineExpandedRowNode;
        node->setFlag(QSGNode::OwnedByParent, false);
        m_expandedRows << node;
    }
    for (int i = 0; i < model->collapsedRowCount(); ++i) {
        QSGNode *node = new QSGNode;
        node->setFlag(QSGNode::OwnedByParent, false);
        m_collapsedRows << node;
    }
}

TimelineItemsRenderPassState::~TimelineItemsRenderPassState()
{
    qDeleteAll(m_collapsedRows);
    qDeleteAll(m_expandedRows);
}

void TimelineItemsRenderPassState::updateIndexes(int from, int to)
{
    if (from < m_indexFrom)
        m_indexFrom = from;
    if (to > m_indexTo)
        m_indexTo = to;
}

void TimelineItemsRenderPassState::updateCollapsedRowMaterial(float xScale, int selectedItem,
                                                              QColor selectionColor)
{
    m_collapsedRowMaterial.setScale(QVector2D(xScale, 1));
    m_collapsedRowMaterial.setSelectedItem(selectedItem);
    m_collapsedRowMaterial.setSelectionColor(selectionColor);
}

NodeUpdater::NodeUpdater(const TimelineModel *model, const TimelineRenderState *parentState,
                         TimelineItemsRenderPassState *state, int indexFrom, int indexTo) :
    m_model(model), m_parentState(parentState), m_indexFrom(indexFrom), m_indexTo(indexTo),
    m_state(state), m_minCollapsedDistance(0), m_minExpandedDistance(0)
{
}

void NodeUpdater::calculateDistances()
{
    int numItems = m_indexTo - m_indexFrom;

    m_collapsedDistances.resize(numItems);
    m_expandedDistances.resize(numItems);
    QVarLengthArray<qint64> startsPerExpandedRow(m_model->expandedRowCount());
    QVarLengthArray<qint64> startsPerCollapsedRow(m_model->collapsedRowCount());
    memset(startsPerCollapsedRow.data(), 0xff, startsPerCollapsedRow.size());
    memset(startsPerExpandedRow.data(), 0xff, startsPerExpandedRow.size());
    for (int i = m_indexFrom; i < m_indexTo; ++i) {
        // Add some "random" factor. Distances below 256ns cannot be properly displayed
        // anyway and if all events have the same distance from one another, then we'd merge
        // them all together otherwise.
        qint64 start = startTime(m_model, m_parentState, i) + (i % 256);
        qint64 end = endTime(m_model, m_parentState, i) + (i % 256);
        if (start > end) {
            m_collapsedDistances[i - m_indexFrom] = m_expandedDistances[i - m_indexFrom] = 0;
            continue;
        }

        qint64 &collapsedStart = startsPerCollapsedRow[m_model->collapsedRow(i)];
        m_collapsedDistances[i - m_indexFrom] = (collapsedStart != s_invalidTimestamp) ?
                    end - collapsedStart : std::numeric_limits<qint64>::max();
        collapsedStart = start;

        qint64 &expandedStart = startsPerExpandedRow[m_model->expandedRow(i)];
        m_expandedDistances[i - m_indexFrom] = (expandedStart != s_invalidTimestamp) ?
                    end - expandedStart : std::numeric_limits<qint64>::max();
        expandedStart = start;
    }

    QVarLengthArray<qint64> sorted = m_collapsedDistances;
    std::sort(sorted.begin(), sorted.end());
    m_minCollapsedDistance = sorted[numItems - s_maxNumItems];
    sorted = m_expandedDistances;
    std::sort(sorted.begin(), sorted.end());
    m_minExpandedDistance = sorted[numItems - s_maxNumItems];
}

int NodeUpdater::updateVertices(TimelineItemsGeometry &geometry,
                                const QVarLengthArray<qint64> &distances, qint64 minDistance,
                                float itemTop, int i) const
{
    int vertices = 0;
    if (geometry.isEmpty()) {
        // We'll run another addVertices() on each row with content after the loop.
        // Reserve some space for that.
        vertices = TimelineItemsGeometry::VerticesForDifferentHeight;
        geometry.nextNode(0, itemTop);
    } else if (distances.isEmpty() || distances[i - m_indexFrom] > minDistance) {
        vertices = geometry.addVertices();
        geometry.nextNode(0, itemTop);
    } else {
        geometry.updateCurrentNode(0, itemTop);
    }
    return vertices;
}

void NodeUpdater::addEvent(TimelineItemsGeometry &geometry,
                           const QVarLengthArray<qint64> &distances, qint64 minDistance,
                           const NodeUpdater::ItemDescription &item, int i) const
{
    if (geometry.isEmpty()) {
        geometry.nextNode(item.left, item.top, item.width, item.selectionId, item.red,
                          item.green, item.blue);
    } else if (distances.isEmpty() || distances[i - m_indexFrom] > minDistance) {
        geometry.addEvent();
        geometry.nextNode(item.left, item.top, item.width, item.selectionId, item.red,
                          item.green, item.blue);
    } else {
        geometry.updateCurrentNode(item.right, item.top);
    }
}

int NodeUpdater::updateNodes(const int from, const int to) const
{
    float defaultRowHeight = TimelineModel::defaultRowHeight();

    QVector<TimelineItemsGeometry> expandedPerRow(m_model->expandedRowCount());
    QVector<TimelineItemsGeometry> collapsedPerRow(m_model->collapsedRowCount());

    int collapsedVertices = 0;
    int expandedVertices = 0;
    int lastEvent = from;
    for (;lastEvent < to
         && collapsedVertices < TimelineItemsGeometry::maxVerticesPerNode
         && expandedVertices < TimelineItemsGeometry::maxVerticesPerNode;
         ++lastEvent) {
        qint64 start = startTime(m_model, m_parentState, lastEvent);
        qint64 end = endTime(m_model, m_parentState, lastEvent);
        if (start > end)
            continue;

        float itemTop = (1.0 - m_model->relativeHeight(lastEvent)) * defaultRowHeight;

        expandedVertices += updateVertices(expandedPerRow[m_model->expandedRow(lastEvent)],
                m_expandedDistances, m_minExpandedDistance, itemTop, lastEvent);
        collapsedVertices += updateVertices(collapsedPerRow[m_model->collapsedRow(lastEvent)],
                m_collapsedDistances, m_minCollapsedDistance, itemTop, lastEvent);
    }

    for (int i = 0, end = m_model->expandedRowCount(); i < end; ++i) {
        TimelineItemsGeometry &row = expandedPerRow[i];
        if (row.currentNode.direction() != OpaqueColoredPoint2DWithSize::InvalidDirection)
            row.addVertices();
        if (row.usedVertices > 0) {
            row.allocate(&static_cast<TimelineExpandedRowNode *>(
                             m_state->expandedRow(i))->material);
            m_state->expandedRow(i)->appendChildNode(row.node);
        }
    }

    for (int i = 0; i < m_model->collapsedRowCount(); ++i) {
        TimelineItemsGeometry &row = collapsedPerRow[i];
        if (row.currentNode.direction() != OpaqueColoredPoint2DWithSize::InvalidDirection)
            row.addVertices();
        if (row.usedVertices > 0) {
            row.allocate(m_state->collapsedRowMaterial());
            m_state->collapsedRow(i)->appendChildNode(row.node);
        }
    }

    ItemDescription item;
    for (int i = from; i < lastEvent; ++i) {
        qint64 start = startTime(m_model, m_parentState, i);
        qint64 end = endTime(m_model, m_parentState, i);
        if (start > end)
            continue;

        QRgb color = m_model->color(i);
        item.red = qRed(color);
        item.green = qGreen(color);
        item.blue = qBlue(color);

        item.width = end > start ? (end - start) * m_parentState->scale() :
                                   std::numeric_limits<float>::min();
        item.left = (start - m_parentState->start()) * m_parentState->scale();
        item.right = (end - m_parentState->start()) * m_parentState->scale();

        // This has to be the exact same expression as above, to guarantee determinism.
        item.top = (1.0 - m_model->relativeHeight(i)) * defaultRowHeight;
        item.selectionId = m_model->selectionId(i);

        addEvent(expandedPerRow[m_model->expandedRow(i)], m_expandedDistances,
                m_minExpandedDistance, item, i);
        addEvent(collapsedPerRow[m_model->collapsedRow(i)], m_collapsedDistances,
                m_minCollapsedDistance, item, i);
    }

    for (int i = 0, end = m_model->expandedRowCount(); i < end; ++i) {
        TimelineItemsGeometry &row = expandedPerRow[i];
        if (row.currentNode.direction() != OpaqueColoredPoint2DWithSize::InvalidDirection)
            row.addEvent();
    }

    for (int i = 0, end = m_model->collapsedRowCount(); i < end; ++i) {
        TimelineItemsGeometry &row = collapsedPerRow[i];
        if (row.currentNode.direction() != OpaqueColoredPoint2DWithSize::InvalidDirection)
            row.addEvent();
    }

    return lastEvent;
}

void NodeUpdater::run()
{
    if (m_indexTo - m_indexFrom > s_maxNumItems)
        calculateDistances();

    if (m_state->indexFrom() < m_state->indexTo()) {
        if (m_indexFrom < m_state->indexFrom()) {
            for (int i = m_indexFrom; i < m_state->indexFrom();)
                i = updateNodes(i, m_state->indexFrom());
        }
        if (m_indexTo > m_state->indexTo()) {
            for (int i = m_state->indexTo(); i < m_indexTo;)
                i = updateNodes(i, m_indexTo);
        }
    } else {
        for (int i = m_indexFrom; i < m_indexTo;)
            i = updateNodes(i, m_indexTo);
    }
}

} // namespace Timeline