File: paintanalyzer.cpp

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (186 lines) | stat: -rw-r--r-- 5,856 bytes parent folder | download | duplicates (2)
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
/*
  paintanalyzer.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include <config-gammaray.h>

#include "paintanalyzer.h"
#include "paintbuffer.h"
#include "paintbuffermodel.h"
#include "painterprofilingreplayer.h"

#include <core/aggregatedpropertymodel.h>
#include <core/probe.h>
#include <core/remoteviewserver.h>
#include <core/stacktracemodel.h>
#include <core/remote/serverproxymodel.h>

#include <common/metatypedeclarations.h>
#include <common/objectbroker.h>
#include <common/remoteviewframe.h>
#include <common/paintbuffermodelroles.h>

#include <QItemSelectionModel>
#include <QSortFilterProxyModel>

using namespace GammaRay;

class PaintBufferModelFilterProxy : public QSortFilterProxyModel
{
    Q_OBJECT
public:
    explicit PaintBufferModelFilterProxy(QObject *parent = nullptr)
        : QSortFilterProxyModel(parent)
    {
    }
    void sort(int, Qt::SortOrder) override
    {
    } // never sort, that has no semantics here
};

PaintAnalyzer::PaintAnalyzer(const QString &name, QObject *parent)
    : PaintAnalyzerInterface(name, parent)
    , m_paintBufferModel(nullptr)
    , m_paintBufferFilter(nullptr)
    , m_selectionModel(nullptr)
    , m_paintBuffer(nullptr)
    , m_remoteView(new RemoteViewServer(name + QStringLiteral(".remoteView"), this))
    , m_argumentModel(new AggregatedPropertyModel(this))
    , m_stackTraceModel(new StackTraceModel(this))
{
    m_paintBufferModel = new PaintBufferModel(this);
    auto proxy = new ServerProxyModel<PaintBufferModelFilterProxy>(this);
    proxy->addRole(PaintBufferModelRoles::MaxCostRole);
    m_paintBufferFilter = proxy;
    m_paintBufferFilter->setSourceModel(m_paintBufferModel);
    Probe::instance()->registerModel(name + QStringLiteral(".paintBufferModel"), m_paintBufferFilter);

    m_selectionModel = ObjectBroker::selectionModel(m_paintBufferFilter);
    connect(m_selectionModel, &QItemSelectionModel::currentChanged, m_remoteView,
            &RemoteViewServer::sourceChanged);

    m_argumentModel->setReadOnly(true);
    Probe::instance()->registerModel(name + QStringLiteral(".argumentProperties"), m_argumentModel);
    Probe::instance()->registerModel(name + QStringLiteral(".stackTrace"), m_stackTraceModel);

    connect(m_remoteView, &RemoteViewServer::requestUpdate, this, &PaintAnalyzer::repaint);
}

PaintAnalyzer::~PaintAnalyzer() = default;

void PaintAnalyzer::reset()
{
    m_remoteView->sourceChanged();
    m_paintBufferModel->setPaintBuffer(PaintBuffer());
}

void PaintAnalyzer::repaint()
{
    if (!m_remoteView->isActive())
        return;

    if (m_paintBufferModel->rowCount() == 0) {
        emit requestUpdate();
        return;
    }

    const QSize sourceSize = m_paintBufferModel->buffer().boundingRect().size().toSize();
    const qreal ratio = m_paintBufferModel->buffer().devicePixelRatioF();
    QImage image(sourceSize * ratio, QImage::Format_ARGB32);
    image.setDevicePixelRatio(ratio);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    const auto start = m_paintBufferModel->buffer().frameStartIndex(0);

    auto index = m_paintBufferFilter->mapToSource(m_selectionModel->currentIndex());
    m_currentArgument = index.data(PaintBufferModelRoles::ValueRole);
    m_argumentModel->setObject(m_currentArgument);
    setHasArgumentDetails(m_argumentModel->rowCount());

    // include selected row or paint all if nothing is selected
    if (index.parent().isValid()) {
        index = index.parent();
    }
    const auto end = index.isValid() ? index.row() + 1 : m_paintBufferModel->rowCount();
    auto depth = m_paintBufferModel->buffer().processCommands(&painter, start, start + end);
    for (; depth > 0; --depth) {
        painter.restore();
    }
    painter.end();

    PaintAnalyzerFrameData data;
    if (index.isValid()) {
        data.clipPath = index.data(PaintBufferModelRoles::ClipPathRole).value<QPainterPath>();
    }
    RemoteViewFrame frame;
    frame.setImage(image);
    frame.data = QVariant::fromValue(data);
    m_remoteView->sendFrame(frame);

    if (index.isValid()) {
        m_stackTraceModel->setStackTrace(m_paintBufferModel->buffer().stackTrace(index.row()));
        setHasStackTrace(m_stackTraceModel->rowCount() > 0);
    } else {
        setHasStackTrace(false);
    }
}

void PaintAnalyzer::beginAnalyzePainting()
{
    Q_ASSERT(!m_paintBuffer);
    m_paintBuffer = new PaintBuffer;
}

void PaintAnalyzer::setBoundingRect(const QRectF &boundingBox)
{
    Q_ASSERT(m_paintBuffer);
    m_paintBuffer->setBoundingRect(boundingBox);
}

QPaintDevice *PaintAnalyzer::paintDevice() const
{
    Q_ASSERT(m_paintBuffer);
    return m_paintBuffer;
}

void PaintAnalyzer::endAnalyzePainting()
{
    Q_ASSERT(m_paintBuffer);
    Q_ASSERT(m_paintBufferModel);
    m_paintBufferModel->setPaintBuffer(*m_paintBuffer);
    delete m_paintBuffer;
    m_paintBuffer = nullptr;
    m_remoteView->resetView();
    m_remoteView->sourceChanged();

    if (auto rowCount = m_paintBufferFilter->rowCount()) {
        const auto idx = m_paintBufferFilter->index(rowCount - 1, 0);
        m_selectionModel->select(idx,
                                 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows | QItemSelectionModel::Current);
    }

    PainterProfilingReplayer profiler;
    profiler.profile(m_paintBufferModel->buffer());
    m_paintBufferModel->setCosts(profiler.costs());
}

void GammaRay::PaintAnalyzer::setOrigin(const ObjectId &obj)
{
    m_paintBuffer->setOrigin(obj);
}

bool PaintAnalyzer::isAvailable()
{
    return true;
}

#include "paintanalyzer.moc"