]> git.cworth.org Git - apitrace/blob - gui/mainwindow.cpp
Show currently bound shaders.
[apitrace] / gui / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "apitrace.h"
4 #include "apitracecall.h"
5 #include "apicalldelegate.h"
6 #include "apitracemodel.h"
7 #include "apitracefilter.h"
8 #include "retracer.h"
9 #include "settingsdialog.h"
10 #include "shaderssourcewidget.h"
11 #include "ui_retracerdialog.h"
12 #include "vertexdatainterpreter.h"
13
14 #include <qjson/parser.h>
15
16 #include <QAction>
17 #include <QDebug>
18 #include <QDesktopServices>
19 #include <QDir>
20 #include <QFileDialog>
21 #include <QLineEdit>
22 #include <QMessageBox>
23 #include <QProgressBar>
24 #include <QToolBar>
25 #include <QUrl>
26 #include <QVBoxLayout>
27 #include <QWebPage>
28 #include <QWebView>
29
30
31 MainWindow::MainWindow()
32     : QMainWindow(),
33       m_selectedEvent(0),
34       m_stateEvent(0),
35       m_jsonParser(new QJson::Parser())
36 {
37     m_ui.setupUi(this);
38     m_ui.stateTreeWidget->sortByColumn(0, Qt::AscendingOrder);
39
40     m_sourcesWidget = new ShadersSourceWidget(m_ui.shadersTab);
41     QVBoxLayout *layout = new QVBoxLayout;
42     layout->addWidget(m_sourcesWidget);
43     m_ui.shadersTab->setLayout(layout);
44
45     m_trace = new ApiTrace();
46     connect(m_trace, SIGNAL(startedLoadingTrace()),
47             this, SLOT(startedLoadingTrace()));
48     connect(m_trace, SIGNAL(finishedLoadingTrace()),
49             this, SLOT(finishedLoadingTrace()));
50
51     m_retracer = new Retracer(this);
52     connect(m_retracer, SIGNAL(finished(const QByteArray&)),
53             this, SLOT(replayFinished(const QByteArray&)));
54     connect(m_retracer, SIGNAL(error(const QString&)),
55             this, SLOT(replayError(const QString&)));
56
57     m_vdataInterpreter = new VertexDataInterpreter(this);
58     m_vdataInterpreter->setListWidget(m_ui.vertexDataListWidget);
59     m_vdataInterpreter->setStride(
60         m_ui.vertexStrideSB->value());
61     m_vdataInterpreter->setComponents(
62         m_ui.vertexComponentsSB->value());
63     m_vdataInterpreter->setStartingOffset(
64         m_ui.startingOffsetSB->value());
65     m_vdataInterpreter->setTypeFromString(
66         m_ui.vertexTypeCB->currentText());
67
68     connect(m_ui.vertexInterpretButton, SIGNAL(clicked()),
69             m_vdataInterpreter, SLOT(interpretData()));
70     connect(m_ui.vertexTypeCB, SIGNAL(currentIndexChanged(const QString&)),
71             m_vdataInterpreter, SLOT(setTypeFromString(const QString&)));
72     connect(m_ui.vertexStrideSB, SIGNAL(valueChanged(int)),
73             m_vdataInterpreter, SLOT(setStride(int)));
74     connect(m_ui.vertexComponentsSB, SIGNAL(valueChanged(int)),
75             m_vdataInterpreter, SLOT(setComponents(int)));
76     connect(m_ui.startingOffsetSB, SIGNAL(valueChanged(int)),
77             m_vdataInterpreter, SLOT(setStartingOffset(int)));
78
79     m_model = new ApiTraceModel();
80     m_model->setApiTrace(m_trace);
81     m_proxyModel = new ApiTraceFilter();
82     m_proxyModel->setSourceModel(m_model);
83     m_ui.callView->setModel(m_proxyModel);
84     m_ui.callView->setItemDelegate(new ApiCallDelegate);
85     m_ui.callView->resizeColumnToContents(0);
86     m_ui.callView->header()->swapSections(0, 1);
87     m_ui.callView->setColumnWidth(1, 42);
88
89     QToolBar *toolBar = addToolBar(tr("Navigation"));
90     m_filterEdit = new QLineEdit(toolBar);
91     toolBar->addWidget(m_filterEdit);
92
93     m_progressBar = new QProgressBar();
94     m_progressBar->setRange(0, 0);
95     statusBar()->addPermanentWidget(m_progressBar);
96     m_progressBar->hide();
97
98     m_ui.detailsDock->hide();
99     m_ui.vertexDataDock->hide();
100     m_ui.stateDock->hide();
101     setDockOptions(dockOptions() | QMainWindow::ForceTabbedDocks);
102
103     tabifyDockWidget(m_ui.stateDock, m_ui.vertexDataDock);
104
105     connect(m_ui.actionOpen, SIGNAL(triggered()),
106             this, SLOT(openTrace()));
107     connect(m_ui.actionQuit, SIGNAL(triggered()),
108             this, SLOT(close()));
109
110     connect(m_ui.actionReplay, SIGNAL(triggered()),
111             this, SLOT(replayStart()));
112     connect(m_ui.actionStop, SIGNAL(triggered()),
113             this, SLOT(replayStop()));
114     connect(m_ui.actionLookupState, SIGNAL(triggered()),
115             this, SLOT(lookupState()));
116        connect(m_ui.actionOptions, SIGNAL(triggered()),
117             this, SLOT(showSettings()));
118
119     connect(m_ui.callView, SIGNAL(activated(const QModelIndex &)),
120             this, SLOT(callItemSelected(const QModelIndex &)));
121     connect(m_filterEdit, SIGNAL(returnPressed()),
122             this, SLOT(filterTrace()));
123
124     m_ui.detailsWebView->page()->setLinkDelegationPolicy(
125         QWebPage::DelegateExternalLinks);
126     connect(m_ui.detailsWebView, SIGNAL(linkClicked(const QUrl&)),
127             this, SLOT(openHelp(const QUrl&)));
128 }
129
130 void MainWindow::openTrace()
131 {
132     QString fileName =
133         QFileDialog::getOpenFileName(
134             this,
135             tr("Open Trace"),
136             QDir::homePath(),
137             tr("Trace Files (*.trace)"));
138
139     qDebug()<< "File name : " <<fileName;
140
141     newTraceFile(fileName);
142 }
143
144 void MainWindow::loadTrace(const QString &fileName)
145 {
146     if (!QFile::exists(fileName)) {
147         QMessageBox::warning(this, tr("File Missing"),
148                              tr("File '%1' doesn't exist.").arg(fileName));
149         return;
150     }
151     qDebug()<< "Loading  : " <<fileName;
152
153     m_progressBar->setValue(0);
154     newTraceFile(fileName);
155 }
156
157 void MainWindow::callItemSelected(const QModelIndex &index)
158 {
159     ApiTraceEvent *event =
160         index.data(ApiTraceModel::EventRole).value<ApiTraceEvent*>();
161
162     if (event && event->type() == ApiTraceEvent::Call) {
163         ApiTraceCall *call = static_cast<ApiTraceCall*>(event);
164         m_ui.detailsWebView->setHtml(call->toHtml());
165         m_ui.detailsDock->show();
166         if (call->hasBinaryData()) {
167             QByteArray data =
168                 call->argValues[call->binaryDataIndex()].toByteArray();
169             m_vdataInterpreter->setData(data);
170
171             for (int i = 0; i < call->argNames.count(); ++i) {
172                 QString name = call->argNames[i];
173                 if (name == QLatin1String("stride")) {
174                     int stride = call->argValues[i].toInt();
175                     m_ui.vertexStrideSB->setValue(stride);
176                 } else if (name == QLatin1String("size")) {
177                     int components = call->argValues[i].toInt();
178                     m_ui.vertexComponentsSB->setValue(components);
179                 } else if (name == QLatin1String("type")) {
180                     QString val = call->argValues[i].toString();
181                     int textIndex = m_ui.vertexTypeCB->findText(val);
182                     if (textIndex >= 0)
183                         m_ui.vertexTypeCB->setCurrentIndex(textIndex);
184                 }
185             }
186         }
187         m_ui.vertexDataDock->setVisible(call->hasBinaryData());
188         m_selectedEvent = call;
189     } else {
190         if (event && event->type() == ApiTraceEvent::Frame) {
191             m_selectedEvent = static_cast<ApiTraceFrame*>(event);
192         } else
193             m_selectedEvent = 0;
194         m_ui.detailsDock->hide();
195         m_ui.vertexDataDock->hide();
196     }
197     if (m_selectedEvent && !m_selectedEvent->state().isEmpty()) {
198         fillStateForFrame();
199     } else
200         m_ui.stateDock->hide();
201 }
202
203 void MainWindow::filterTrace()
204 {
205     m_proxyModel->setFilterString(m_filterEdit->text());
206 }
207
208 void MainWindow::replayStart()
209 {
210     QDialog dlg;
211     Ui_RetracerDialog dlgUi;
212     dlgUi.setupUi(&dlg);
213
214     dlgUi.doubleBufferingCB->setChecked(
215         m_retracer->isDoubleBuffered());
216     dlgUi.benchmarkCB->setChecked(
217         m_retracer->isBenchmarking());
218
219     if (dlg.exec() == QDialog::Accepted) {
220         m_retracer->setDoubleBuffered(
221             dlgUi.doubleBufferingCB->isChecked());
222         m_retracer->setBenchmarking(
223             dlgUi.benchmarkCB->isChecked());
224         replayTrace(false);
225     }
226 }
227
228 void MainWindow::replayStop()
229 {
230     m_retracer->terminate();
231     m_ui.actionStop->setEnabled(false);
232     m_ui.actionReplay->setEnabled(true);
233     m_ui.actionLookupState->setEnabled(true);
234 }
235
236 void MainWindow::newTraceFile(const QString &fileName)
237 {
238     m_traceFileName = fileName;
239     m_trace->setFileName(fileName);
240
241     if (m_traceFileName.isEmpty()) {
242         m_ui.actionReplay->setEnabled(false);
243         m_ui.actionLookupState->setEnabled(false);
244         setWindowTitle(tr("QApiTrace"));
245     } else {
246         QFileInfo info(fileName);
247         m_ui.actionReplay->setEnabled(true);
248         m_ui.actionLookupState->setEnabled(true);
249         setWindowTitle(
250             tr("QApiTrace - %1").arg(info.fileName()));
251     }
252 }
253
254 void MainWindow::replayFinished(const QByteArray &output)
255 {
256     m_ui.actionStop->setEnabled(false);
257     m_ui.actionReplay->setEnabled(true);
258     m_ui.actionLookupState->setEnabled(true);
259
260     if (m_retracer->captureState()) {
261         bool ok = false;
262         QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
263         parseState(parsedJson);
264     } else if (output.length() < 80) {
265         statusBar()->showMessage(output);
266     }
267     m_stateEvent = 0;
268 }
269
270 void MainWindow::replayError(const QString &message)
271 {
272     m_ui.actionStop->setEnabled(false);
273     m_ui.actionReplay->setEnabled(true);
274     m_ui.actionLookupState->setEnabled(true);
275     m_stateEvent = 0;
276
277     QMessageBox::warning(
278         this, tr("Replay Failed"), message);
279 }
280
281 void MainWindow::startedLoadingTrace()
282 {
283     Q_ASSERT(m_trace);
284     m_progressBar->show();
285     QFileInfo info(m_trace->fileName());
286     statusBar()->showMessage(
287         tr("Loading %1...").arg(info.fileName()));
288 }
289
290 void MainWindow::finishedLoadingTrace()
291 {
292     m_progressBar->hide();
293     if (!m_trace) {
294         return;
295     }
296     QFileInfo info(m_trace->fileName());
297     statusBar()->showMessage(
298         tr("Loaded %1").arg(info.fileName()), 3000);
299 }
300
301 void MainWindow::replayTrace(bool dumpState)
302 {
303     if (m_traceFileName.isEmpty())
304         return;
305
306     m_retracer->setFileName(m_traceFileName);
307     m_retracer->setCaptureState(dumpState);
308     if (m_retracer->captureState() && m_selectedEvent) {
309         int index = 0;
310         if (m_selectedEvent->type() == ApiTraceEvent::Call) {
311             index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
312         } else if (m_selectedEvent->type() == ApiTraceEvent::Frame) {
313             ApiTraceFrame *frame = static_cast<ApiTraceFrame*>(m_selectedEvent);
314             if (frame->calls.isEmpty()) {
315                 //XXX i guess we could still get the current state
316                 qDebug()<<"tried to get a state for an empty frame";
317                 return;
318             }
319             index = frame->calls.first()->index;
320         } else {
321             qDebug()<<"Unknown event type";
322             return;
323         }
324         m_retracer->setCaptureAtCallNumber(index);
325     }
326     m_retracer->start();
327
328     m_ui.actionStop->setEnabled(true);
329 }
330
331 void MainWindow::lookupState()
332 {
333     if (!m_selectedEvent) {
334         QMessageBox::warning(
335             this, tr("Unknown Event"),
336             tr("To inspect the state select an event in the event list."));
337         return;
338     }
339     m_stateEvent = m_selectedEvent;
340     replayTrace(true);
341 }
342
343 MainWindow::~MainWindow()
344 {
345     delete m_jsonParser;
346 }
347
348 void MainWindow::parseState(const QVariantMap &parsedJson)
349 {
350     m_stateEvent->setState(ApiTraceState(parsedJson));
351     m_model->stateSetOnEvent(m_stateEvent);
352     if (m_selectedEvent == m_stateEvent) {
353         fillStateForFrame();
354     } else {
355         m_ui.stateDock->hide();
356     }
357 }
358
359 static void
360 variantToString(const QVariant &var, QString &str)
361 {
362     if (var.type() == QVariant::List) {
363         QVariantList lst = var.toList();
364         str += QLatin1String("[");
365         for (int i = 0; i < lst.count(); ++i) {
366             QVariant val = lst[i];
367             variantToString(val, str);
368             if (i < lst.count() - 1)
369                 str += QLatin1String(", ");
370         }
371         str += QLatin1String("]");
372     } else if (var.type() == QVariant::Map) {
373         Q_ASSERT(!"unsupported state type");
374     } else if (var.type() == QVariant::Hash) {
375         Q_ASSERT(!"unsupported state type");
376     } else {
377         str += var.toString();
378     }
379 }
380
381 void MainWindow::fillStateForFrame()
382 {
383     QVariantMap::const_iterator itr;
384     QVariantMap params;
385
386     if (!m_selectedEvent || m_selectedEvent->state().isEmpty())
387         return;
388
389     const ApiTraceState &state = m_selectedEvent->state();
390     m_ui.stateTreeWidget->clear();
391     params = state.parameters();
392     QList<QTreeWidgetItem *> items;
393     for (itr = params.constBegin(); itr != params.constEnd(); ++itr) {
394         QString key = itr.key();
395         QString val;
396
397         variantToString(itr.value(), val);
398         //qDebug()<<"key = "<<key;
399         //qDebug()<<"val = "<<val;
400         QStringList lst;
401         lst += key;
402         lst += val;
403         items.append(new QTreeWidgetItem((QTreeWidget*)0, lst));
404     }
405     m_ui.stateTreeWidget->insertTopLevelItems(0, items);
406
407     QStringList shaderSources = state.shaderSources();
408     if (shaderSources.isEmpty()) {
409         m_sourcesWidget->setShaders(shaderSources);
410     } else {
411         m_sourcesWidget->setShaders(shaderSources);
412     }
413
414     m_ui.surfacesTab->setEnabled(false);
415     m_ui.stateDock->show();
416 }
417
418 void MainWindow::showSettings()
419 {
420     SettingsDialog dialog;
421     dialog.setFilterOptions(m_proxyModel->filterOptions());
422
423     if (dialog.exec() == QDialog::Accepted) {
424         m_proxyModel->setFilterOptions(dialog.filterOptions());
425     }
426 }
427
428 void MainWindow::openHelp(const QUrl &url)
429 {
430     QDesktopServices::openUrl(url);
431 }
432
433 #include "mainwindow.moc"