1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "timelinenotesrenderpass.h"
27 #include "timelinerenderstate.h"
28 #include "timelinenotesmodel.h"
29
30 #include <utils/theme/theme.h>
31
32 namespace Timeline {
33
34 struct Point2DWithDistanceFromTop {
35 float x, y; // vec4 vertexCoord
36 float d; // float distanceFromTop
37 void set(float nx, float ny, float nd);
38 };
39
40 class NotesMaterial : public QSGMaterial
41 {
42 public:
43 QSGMaterialType *type() const final;
44 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45 QSGMaterialShader *createShader() const final;
46 #else // < Qt 6
47 QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const final;
48 #endif // < Qt 6
49 };
50
51 struct NotesGeometry
52 {
53 static const int maxNotes;
54 static const QSGGeometry::AttributeSet &point2DWithDistanceFromTop();
55
56 static QSGGeometry *createGeometry(QVector<int> &ids, const TimelineModel *model,
57 const TimelineRenderState *parentState, bool collapsed);
58 };
59
60 const int NotesGeometry::maxNotes = 0xffff / 2;
61
62 class TimelineNotesRenderPassState : public TimelineRenderPass::State
63 {
64 public:
65 TimelineNotesRenderPassState(int expandedRows);
66 ~TimelineNotesRenderPassState();
67
expandedRow(int row) const68 QSGNode *expandedRow(int row) const { return m_expandedRows[row]; }
collapsedOverlay() const69 QSGNode *collapsedOverlay() const final { return m_collapsedOverlay; }
expandedRows() const70 const QVector<QSGNode *> &expandedRows() const final { return m_expandedRows; }
71
nullGeometry()72 QSGGeometry *nullGeometry() { return &m_nullGeometry; }
material()73 NotesMaterial *material() { return &m_material; }
74
75 private:
76 QSGGeometryNode *createNode();
77
78 NotesMaterial m_material;
79 QSGGeometry m_nullGeometry;
80 QSGGeometryNode *m_collapsedOverlay;
81 QVector<QSGNode *> m_expandedRows;
82 };
83
point2DWithDistanceFromTop()84 const QSGGeometry::AttributeSet &NotesGeometry::point2DWithDistanceFromTop()
85 {
86 static const QSGGeometry::Attribute data[] = {
87 // vec4 vertexCoord
88 QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType,
89 QSGGeometry::PositionAttribute),
90 // float distanceFromTop
91 QSGGeometry::Attribute::createWithAttributeType(1, 1, QSGGeometry::FloatType,
92 QSGGeometry::UnknownAttribute),
93 };
94 static const QSGGeometry::AttributeSet attrs = {
95 sizeof(data) / sizeof(data[0]),
96 sizeof(Point2DWithDistanceFromTop),
97 data
98 };
99 return attrs;
100 }
101
instance()102 const TimelineNotesRenderPass *TimelineNotesRenderPass::instance()
103 {
104 static const TimelineNotesRenderPass pass;
105 return &pass;
106 }
107
TimelineNotesRenderPass()108 TimelineNotesRenderPass::TimelineNotesRenderPass()
109 {
110 }
111
update(const TimelineAbstractRenderer * renderer,const TimelineRenderState * parentState,State * oldState,int firstIndex,int lastIndex,bool stateChanged,float spacing) const112 TimelineRenderPass::State *TimelineNotesRenderPass::update(const TimelineAbstractRenderer *renderer,
113 const TimelineRenderState *parentState,
114 State *oldState, int firstIndex,
115 int lastIndex, bool stateChanged,
116 float spacing) const
117 {
118 Q_UNUSED(firstIndex)
119 Q_UNUSED(lastIndex)
120 Q_UNUSED(spacing)
121
122 const TimelineNotesModel *notes = renderer->notes();
123 const TimelineModel *model = renderer->model();
124
125 if (!model || !notes)
126 return oldState;
127
128 TimelineNotesRenderPassState *state;
129 if (oldState == nullptr) {
130 state = new TimelineNotesRenderPassState(model->expandedRowCount());
131 } else {
132 if (!stateChanged && !renderer->notesDirty())
133 return oldState;
134 state = static_cast<TimelineNotesRenderPassState *>(oldState);
135 }
136
137 QVector<QVector<int> > expanded(model->expandedRowCount());
138 QVector<int> collapsed;
139
140 for (int i = 0; i < qMin(notes->count(), NotesGeometry::maxNotes); ++i) {
141 if (notes->timelineModel(i) != model->modelId())
142 continue;
143 int timelineIndex = notes->timelineIndex(i);
144 if (model->startTime(timelineIndex) > parentState->end() ||
145 model->endTime(timelineIndex) < parentState->start())
146 continue;
147 expanded[model->expandedRow(timelineIndex)] << timelineIndex;
148 collapsed << timelineIndex;
149 }
150
151 QSGGeometryNode *collapsedNode = static_cast<QSGGeometryNode *>(state->collapsedOverlay());
152
153 if (!collapsed.isEmpty()) {
154 collapsedNode->setGeometry(NotesGeometry::createGeometry(collapsed, model, parentState,
155 true));
156 collapsedNode->setFlag(QSGGeometryNode::OwnsGeometry, true);
157 } else {
158 collapsedNode->setGeometry(state->nullGeometry());
159 collapsedNode->setFlag(QSGGeometryNode::OwnsGeometry, false);
160 }
161
162 for (int row = 0; row < model->expandedRowCount(); ++row) {
163 QSGGeometryNode *rowNode = static_cast<QSGGeometryNode *>(state->expandedRow(row));
164 if (expanded[row].isEmpty()) {
165 rowNode->setGeometry(state->nullGeometry());
166 rowNode->setFlag(QSGGeometryNode::OwnsGeometry, false);
167 } else {
168 rowNode->setGeometry(NotesGeometry::createGeometry(expanded[row], model, parentState,
169 false));
170 collapsedNode->setFlag(QSGGeometryNode::OwnsGeometry, true);
171 }
172 }
173
174 return state;
175 }
176
TimelineNotesRenderPassState(int numExpandedRows)177 TimelineNotesRenderPassState::TimelineNotesRenderPassState(int numExpandedRows) :
178 m_nullGeometry(NotesGeometry::point2DWithDistanceFromTop(), 0)
179 {
180 m_material.setFlag(QSGMaterial::Blending, true);
181 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
182 m_material.setFlag(QSGMaterial::CustomCompileStep, true);
183 #endif // >= Qt 6
184 m_expandedRows.reserve(numExpandedRows);
185 for (int i = 0; i < numExpandedRows; ++i)
186 m_expandedRows << createNode();
187 m_collapsedOverlay = createNode();
188 }
189
~TimelineNotesRenderPassState()190 TimelineNotesRenderPassState::~TimelineNotesRenderPassState()
191 {
192 qDeleteAll(m_expandedRows);
193 delete m_collapsedOverlay;
194 }
195
createNode()196 QSGGeometryNode *TimelineNotesRenderPassState::createNode()
197 {
198 QSGGeometryNode *node = new QSGGeometryNode;
199 node->setGeometry(&m_nullGeometry);
200 node->setMaterial(&m_material);
201 node->setFlag(QSGNode::OwnedByParent, false);
202 return node;
203 }
204
createGeometry(QVector<int> & ids,const TimelineModel * model,const TimelineRenderState * parentState,bool collapsed)205 QSGGeometry *NotesGeometry::createGeometry(QVector<int> &ids, const TimelineModel *model,
206 const TimelineRenderState *parentState, bool collapsed)
207 {
208 float rowHeight = TimelineModel::defaultRowHeight();
209 QSGGeometry *geometry = new QSGGeometry(point2DWithDistanceFromTop(),
210 ids.count() * 2);
211 Q_ASSERT(geometry->vertexData());
212 geometry->setDrawingMode(QSGGeometry::DrawLines);
213 geometry->setLineWidth(3);
214 Point2DWithDistanceFromTop *v =
215 static_cast<Point2DWithDistanceFromTop *>(geometry->vertexData());
216 for (int i = 0; i < ids.count(); ++i) {
217 int timelineIndex = ids[i];
218 float horizontalCenter = ((model->startTime(timelineIndex) +
219 model->endTime(timelineIndex)) / (qint64)2 -
220 parentState->start()) * parentState->scale();
221 float verticalStart = (collapsed ? (model->collapsedRow(timelineIndex) + 0.1) : 0.1) *
222 rowHeight;
223 float verticalEnd = verticalStart + 0.8 * rowHeight;
224 v[i * 2].set(horizontalCenter, verticalStart, 0);
225 v[i * 2 + 1].set(horizontalCenter, verticalEnd, 1);
226 }
227 return geometry;
228 }
229
230 class NotesMaterialShader : public QSGMaterialShader
231 {
232 public:
233 NotesMaterialShader();
234
235 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
236 void updateState(const RenderState &state, QSGMaterial *newEffect,
237 QSGMaterial *oldEffect) override;
238 char const *const *attributeNames() const override;
239 #else // < Qt 6
240 bool updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *) override;
241 #endif // < Qt 6
242
243 private:
244 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
245 void initialize() override;
246
247 int m_matrix_id;
248 int m_z_range_id;
249 int m_color_id;
250 #endif // < Qt 6
251 };
252
NotesMaterialShader()253 NotesMaterialShader::NotesMaterialShader()
254 : QSGMaterialShader()
255 {
256 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
257 setShaderSourceFile(QOpenGLShader::Vertex, QStringLiteral(":/QtCreator/Tracing/notes.vert"));
258 setShaderSourceFile(QOpenGLShader::Fragment, QStringLiteral(":/QtCreator/Tracing/notes.frag"));
259 #else // < Qt 6
260 setShaderFileName(VertexStage, ":/QtCreator/Tracing/notes_qt6.vert.qsb");
261 setShaderFileName(FragmentStage, ":/QtCreator/Tracing/notes_qt6.frag.qsb");
262 #endif // < Qt 6
263 }
264
notesColor()265 static QColor notesColor()
266 {
267 return Utils::creatorTheme()
268 ? Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor)
269 : QColor(255, 165, 0);
270 }
271
272 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
updateState(const RenderState & state,QSGMaterial *,QSGMaterial *)273 void NotesMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *)
274 {
275 if (state.isMatrixDirty()) {
276 program()->setUniformValue(m_matrix_id, state.combinedMatrix());
277 program()->setUniformValue(m_z_range_id, GLfloat(1.0));
278 program()->setUniformValue(m_color_id, notesColor());
279 }
280 }
281 #else // < Qt 6
updateUniformData(RenderState & state,QSGMaterial *,QSGMaterial *)282 bool NotesMaterialShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
283 {
284 QByteArray *buf = state.uniformData();
285
286 // mat4 matrix
287 if (state.isMatrixDirty()) {
288 const QMatrix4x4 m = state.combinedMatrix();
289 memcpy(buf->data(), m.constData(), 64);
290 }
291
292 // vec4 notesColor
293 const QColor color = notesColor();
294 const float colorArray[] = { color.redF(), color.greenF(), color.blueF(), color.alphaF() };
295 memcpy(buf->data() + 64, colorArray, 16);
296
297 return true;
298 }
299 #endif // < Qt 6
300
301 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
attributeNames() const302 char const *const *NotesMaterialShader::attributeNames() const
303 {
304 static const char *const attr[] = {"vertexCoord", "distanceFromTop", nullptr};
305 return attr;
306 }
307
initialize()308 void NotesMaterialShader::initialize()
309 {
310 m_matrix_id = program()->uniformLocation("matrix");
311 m_z_range_id = program()->uniformLocation("_qt_zRange");
312 m_color_id = program()->uniformLocation("notesColor");
313 }
314 #endif // < Qt 6
315
type() const316 QSGMaterialType *NotesMaterial::type() const
317 {
318 static QSGMaterialType type;
319 return &type;
320 }
321
322 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
createShader() const323 QSGMaterialShader *NotesMaterial::createShader() const
324 #else // < Qt 6
325 QSGMaterialShader *NotesMaterial::createShader(QSGRendererInterface::RenderMode) const
326 #endif // < Qt 6
327 {
328 return new NotesMaterialShader;
329 }
330
set(float nx,float ny,float nd)331 void Point2DWithDistanceFromTop::set(float nx, float ny, float nd)
332 {
333 x = nx; y = ny; d = nd;
334 }
335
336 } // namespace Timeline
337