summaryrefslogtreecommitdiff
path: root/src/plugins/qmlprofiler/qmlprofilerbindingloopsrenderpass.cpp
blob: c41ecffb83a8a57347fc4b1d08b40b36c262563d (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
/****************************************************************************
**
** 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 "qmlprofilerbindingloopsrenderpass.h"
#include <utils/qtcassert.h>

#include <utils/theme/theme.h>

namespace QmlProfiler {
namespace Internal {

class BindingLoopMaterial : public QSGMaterial {
public:
    QSGMaterialType *type() const;
    QSGMaterialShader *createShader() const;
    BindingLoopMaterial();
};

class BindingLoopsRenderPassState : public Timeline::TimelineRenderPass::State {
public:
    BindingLoopsRenderPassState(const QmlProfilerRangeModel *model);
    ~BindingLoopsRenderPassState();

    BindingLoopMaterial *material() { return &m_material; }
    void updateIndexes(int from, int to);

    int indexFrom() const { return m_indexFrom; }
    int indexTo() const { return m_indexTo; }

    QSGNode *expandedRow(int row) const { return m_expandedRows[row]; }
    const QVector<QSGNode *> &expandedRows() const { return m_expandedRows; }
    QSGNode *collapsedOverlay() const { return m_collapsedOverlay; }

private:
    QVector<QSGNode *> m_expandedRows;
    QSGNode *m_collapsedOverlay;
    BindingLoopMaterial m_material;
    int m_indexFrom;
    int m_indexTo;
};

struct Point2DWithOffset {
    float x, y, x2, y2;
    void set(float nx, float ny, float nx2, float ny2);
};

struct BindlingLoopsGeometry {
    static const QSGGeometry::AttributeSet &point2DWithOffset();
    static const int maxEventsPerNode = 0xffff / 18;

    BindlingLoopsGeometry() : allocatedVertices(0), usedVertices(0), currentY(-1), node(nullptr) {}
    uint allocatedVertices;
    uint usedVertices;
    float currentY;

    QSGGeometryNode *node;
    Point2DWithOffset *vertexData();

    void allocate(QSGMaterial *material);
    void addExpandedEvent(float itemCenter);
    void addCollapsedEvent(float horizontalCenterSource, float horizontalCenterTarget,
                           float verticalCenterSource, float verticalCenterTarget);
};

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

QmlProfilerBindingLoopsRenderPass::QmlProfilerBindingLoopsRenderPass()
{
}

static inline bool eventOutsideRange(const QmlProfilerRangeModel *model,
                                  const Timeline::TimelineRenderState *parentState, int i)
{
    const qint64 start = qMax(parentState->start(), model->startTime(i));
    const qint64 end = qMin(parentState->end(), model->endTime(i));
    return start > end;
}

void updateNodes(const QmlProfilerRangeModel *model, int from, int to,
                 const Timeline::TimelineRenderState *parentState,
                 BindingLoopsRenderPassState *state)
{
    QVector<BindlingLoopsGeometry> expandedPerRow(model->expandedRowCount());
    BindlingLoopsGeometry collapsed;

    for (int i = from; i < to; ++i) {
        int bindingLoopDest = model->bindingLoopDest(i);
        if (bindingLoopDest == -1 || eventOutsideRange(model, parentState, i))
            continue;

        expandedPerRow[model->expandedRow(i)].usedVertices += 4;
        collapsed.usedVertices += 18;
    }

    for (int i = 0; i < model->expandedRowCount(); ++i) {
        BindlingLoopsGeometry &row = expandedPerRow[i];
        if (row.usedVertices > 0) {
            row.allocate(state->material());
            state->expandedRow(i)->appendChildNode(row.node);
        }
    }

    if (collapsed.usedVertices > 0) {
        collapsed.allocate(state->material());
        state->collapsedOverlay()->appendChildNode(collapsed.node);
    }

    int rowHeight = Timeline::TimelineModel::defaultRowHeight();
    for (int i = from; i < to; ++i) {
        int bindingLoopDest = model->bindingLoopDest(i);
        if (bindingLoopDest == -1 || eventOutsideRange(model, parentState, i))
            continue;

        qint64 center = qMax(parentState->start(), qMin(parentState->end(),
                                                        (model->startTime(i) + model->endTime(i)) /
                                                        (qint64)2));

        float itemCenter = (center - parentState->start()) * parentState->scale();
        expandedPerRow[model->expandedRow(i)].addExpandedEvent(itemCenter);

        center = qMax(parentState->start(), qMin(parentState->end(),
                                                 (model->startTime(bindingLoopDest) +
                                                  model->endTime(bindingLoopDest)) / (qint64)2));

        float itemCenterTarget = (center - parentState->start()) * parentState->scale();
        collapsed.addCollapsedEvent(itemCenter, itemCenterTarget,
                                    (model->collapsedRow(i) + 0.5) * rowHeight,
                                    (model->collapsedRow(bindingLoopDest) + 0.5) * rowHeight);
    }
}

Timeline::TimelineRenderPass::State *QmlProfilerBindingLoopsRenderPass::update(
        const Timeline::TimelineAbstractRenderer *renderer,
        const Timeline::TimelineRenderState *parentState, State *oldState,
        int indexFrom, int indexTo, bool stateChanged, float spacing) const
{
    Q_UNUSED(stateChanged);
    Q_UNUSED(spacing);

    const QmlProfilerRangeModel *model = qobject_cast<const QmlProfilerRangeModel *>(
                renderer->model());

    if (!model || indexFrom < 0 || indexTo > model->count() || indexFrom >= indexTo)
        return oldState;

    BindingLoopsRenderPassState *state;
    if (oldState == nullptr)
        state = new BindingLoopsRenderPassState(model);
    else
        state = static_cast<BindingLoopsRenderPassState *>(oldState);

    if (state->indexFrom() < state->indexTo()) {
        if (indexFrom < state->indexFrom()) {
            for (int i = indexFrom; i < state->indexFrom();
                 i += BindlingLoopsGeometry::maxEventsPerNode)
                updateNodes(model, i, qMin(i + BindlingLoopsGeometry::maxEventsPerNode,
                                           state->indexFrom()), parentState, state);
        }
        if (indexTo > state->indexTo()) {
            for (int i = state->indexTo(); i < indexTo; i+= BindlingLoopsGeometry::maxEventsPerNode)
                updateNodes(model, i, qMin(i + BindlingLoopsGeometry::maxEventsPerNode, indexTo),
                            parentState, state);
        }
    } else {
        for (int i = indexFrom; i < indexTo; i+= BindlingLoopsGeometry::maxEventsPerNode)
            updateNodes(model, i, qMin(i + BindlingLoopsGeometry::maxEventsPerNode, indexTo),
                        parentState, state);
    }

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

const QSGGeometry::AttributeSet &BindlingLoopsGeometry::point2DWithOffset()
{
    static QSGGeometry::Attribute data[] = {
        QSGGeometry::Attribute::create(0, 2, GL_FLOAT, true),
        QSGGeometry::Attribute::create(1, 2, GL_FLOAT),
    };
    static QSGGeometry::AttributeSet attrs = {
        2,
        sizeof(Point2DWithOffset),
        data
    };
    return attrs;
}

Point2DWithOffset *BindlingLoopsGeometry::vertexData()
{
    QSGGeometry *geometry = node->geometry();
    Q_ASSERT(geometry->attributeCount() == 2);
    Q_ASSERT(geometry->sizeOfVertex() == sizeof(Point2DWithOffset));
    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_UNUSED(attributes);
    return static_cast<Point2DWithOffset *>(geometry->vertexData());
}

void BindlingLoopsGeometry::allocate(QSGMaterial *material)
{
    QSGGeometry *geometry = new QSGGeometry(BindlingLoopsGeometry::point2DWithOffset(),
                                            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);
    allocatedVertices = usedVertices;
    usedVertices = 0;
}

void BindlingLoopsGeometry::addExpandedEvent(float itemCenter)
{
    float verticalCenter = Timeline::TimelineModel::defaultRowHeight() / 2.0f;
    Point2DWithOffset *v = vertexData() + usedVertices;
    v[0].set(itemCenter, verticalCenter, -1.0f, currentY);
    v[1].set(itemCenter, verticalCenter, +1.0f, currentY);
    currentY = -currentY;
    v[2].set(itemCenter, verticalCenter, -1.0f, currentY);
    v[3].set(itemCenter, verticalCenter, +1.0f, currentY);
    usedVertices += 4;
}

void BindlingLoopsGeometry::addCollapsedEvent(float horizontalCenterSource,
                                              float horizontalCenterTarget,
                                              float verticalCenterSource,
                                              float verticalCenterTarget)
{
    // The source event should always be above the parent event because ranges are perfectly nested
    // and events are ordered by start time.
    QTC_ASSERT(verticalCenterSource > verticalCenterTarget, /**/);

    float tilt = horizontalCenterSource < horizontalCenterTarget ? +0.3f : -0.3f;

    Point2DWithOffset *v = vertexData() + usedVertices;
    v[0].set(horizontalCenterSource, verticalCenterSource, -0.3f, tilt);
    v[1].set(horizontalCenterSource, verticalCenterSource, -0.3f, tilt);
    v[2].set(horizontalCenterSource, verticalCenterSource, +0.3f, -tilt);

    v[3].set(horizontalCenterTarget, verticalCenterTarget, -0.3f, tilt);
    v[4].set(horizontalCenterTarget, verticalCenterTarget, +0.3f, -tilt);
    v[5].set(horizontalCenterTarget, verticalCenterTarget, -1.0f, -1.0f);
    v[6].set(horizontalCenterTarget, verticalCenterTarget, +1.0f, -1.0f);
    v[7].set(horizontalCenterTarget, verticalCenterTarget, -1.0f, +1.0f);
    v[8].set(horizontalCenterTarget, verticalCenterTarget, +1.0f, +1.0f);
    v[9].set(horizontalCenterTarget, verticalCenterTarget, -0.3f, tilt);
    v[10].set(horizontalCenterTarget, verticalCenterTarget, +0.3f, -tilt);

    v[11].set(horizontalCenterSource, verticalCenterSource, -0.3f, tilt);
    v[12].set(horizontalCenterSource, verticalCenterSource, +0.3f, -tilt);
    v[13].set(horizontalCenterSource, verticalCenterSource, -1.0f, +1.0f);
    v[14].set(horizontalCenterSource, verticalCenterSource, +1.0f, +1.0f);
    v[15].set(horizontalCenterSource, verticalCenterSource, -1.0f, -1.0f);
    v[16].set(horizontalCenterSource, verticalCenterSource, +1.0f, -1.0f);
    v[17].set(horizontalCenterSource, verticalCenterSource, +1.0f, -1.0f);

    usedVertices += 18;
}

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

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

private:
    virtual void initialize();

    int m_matrix_id = 0;
    int m_z_range_id = 0;
    int m_color_id = 0;
};

BindingLoopMaterialShader::BindingLoopMaterialShader()
    : QSGMaterialShader()
{
    setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/qmlprofiler/bindingloops.vert"));
    setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/qmlprofiler/bindingloops.frag"));
}

void BindingLoopMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *)
{
    if (state.isMatrixDirty()) {
        program()->setUniformValue(m_matrix_id, state.combinedMatrix());
        program()->setUniformValue(m_z_range_id, GLfloat(1.0));
        program()->setUniformValue(
                    m_color_id,
                    Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor));
    }
}

char const *const *BindingLoopMaterialShader::attributeNames() const
{
    static const char *const attr[] = {"vertexCoord", "postScaleOffset", nullptr};
    return attr;
}

void BindingLoopMaterialShader::initialize()
{
    m_matrix_id = program()->uniformLocation("matrix");
    m_z_range_id = program()->uniformLocation("_qt_zRange");
    m_color_id = program()->uniformLocation("bindingLoopsColor");
}


BindingLoopMaterial::BindingLoopMaterial()
{
    setFlag(QSGMaterial::Blending, false);
}

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

QSGMaterialShader *BindingLoopMaterial::createShader() const
{
    return new BindingLoopMaterialShader;
}

void Point2DWithOffset::set(float nx, float ny, float nx2, float ny2)
{
    x = nx; y = ny; x2 = nx2; y2 = ny2;
}

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

BindingLoopsRenderPassState::~BindingLoopsRenderPassState()
{
    delete m_collapsedOverlay;
    qDeleteAll(m_expandedRows);
}

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

} // namespace Internal
} // namespace QmlProfiler