blob: 738367e1830c394f2808ab8a234355dd8de8e48a [file] [log] [blame]
Zack Rusin3acde362011-04-06 01:11:55 -04001#include "retracer.h"
2
Zack Rusinf389ae82011-04-10 19:27:28 -04003#include "apitracecall.h"
José Fonseca3f456402012-03-25 20:59:24 +01004#include "thumbnail.h"
Zack Rusinf389ae82011-04-10 19:27:28 -04005
José Fonsecae7102bf2012-12-07 07:33:05 +00006#include "image/image.hpp"
Dan McCabe66dfdda2012-03-05 17:20:39 -08007
James Bentonfc4f55a2012-08-08 17:09:07 +01008#include "trace_profiler.hpp"
9
Zack Rusin3acde362011-04-06 01:11:55 -040010#include <QDebug>
Zack Rusinf389ae82011-04-10 19:27:28 -040011#include <QVariant>
Dan McCabe66dfdda2012-03-05 17:20:39 -080012#include <QList>
13#include <QImage>
Zack Rusinf389ae82011-04-10 19:27:28 -040014
15#include <qjson/parser.h>
Zack Rusin3acde362011-04-06 01:11:55 -040016
José Fonseca6ea8dee2012-03-25 17:25:24 +010017/**
18 * Wrapper around a QProcess which enforces IO to block .
19 *
20 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
21 * they expect that QIODevice::read() will blocked until the requested ammount
22 * of bytes is read or end of file is reached. But by default QProcess, does
23 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
24 * address the problem either.
25 *
26 * This class wraps around QProcess, providing QIODevice interface, while
27 * ensuring that all reads block.
28 *
29 * This class also works around a bug in QProcess::atEnd() implementation.
30 *
31 * See also:
32 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
33 * - http://qt-project.org/wiki/Custom_IO_Device
34 */
35class BlockingIODevice : public QIODevice
36{
37 /* We don't use the Q_OBJECT in this class given we don't declare any
38 * signals and slots or use any other services provided by Qt's meta-object
39 * system. */
40public:
41 BlockingIODevice(QProcess * io);
42 bool isSequential() const;
43 bool atEnd() const;
44 bool waitForReadyRead(int msecs = -1);
45
46protected:
47 qint64 readData(char * data, qint64 maxSize);
48 qint64 writeData(const char * data, qint64 maxSize);
49
50private:
51 QProcess *m_device;
52};
53
54BlockingIODevice::BlockingIODevice(QProcess * io) :
55 m_device(io)
56{
57 /*
58 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
59 * its own buffering on top of the overridden readData() method.
60 *
61 * The only buffering used will be to satisfy QIODevice::peek() and
62 * QIODevice::ungetChar().
63 */
64 setOpenMode(ReadOnly | Unbuffered);
65}
66
67bool BlockingIODevice::isSequential() const
68{
69 return true;
70}
71
72bool BlockingIODevice::atEnd() const
73{
74 /*
75 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
76 * even when the process is running --, so we try to workaround that here.
77 */
78 if (m_device->atEnd()) {
79 if (m_device->state() == QProcess::Running) {
80 if (!m_device->waitForReadyRead(-1)) {
81 return true;
82 }
83 }
84 }
85 return false;
86}
87
88bool BlockingIODevice::waitForReadyRead(int msecs)
89{
90 Q_UNUSED(msecs);
91 return true;
92}
93
94qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
95{
96 qint64 bytesToRead = maxSize;
97 qint64 readSoFar = 0;
98 do {
99 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
100 if (chunkSize < 0) {
101 if (readSoFar) {
102 return readSoFar;
103 } else {
104 return chunkSize;
105 }
106 }
107 Q_ASSERT(chunkSize <= bytesToRead);
108 bytesToRead -= chunkSize;
109 readSoFar += chunkSize;
110 if (bytesToRead) {
111 if (!m_device->waitForReadyRead(-1)) {
112 qDebug() << "waitForReadyRead failed\n";
113 break;
114 }
115 }
116 } while(bytesToRead);
117
118 return readSoFar;
119}
120
121qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
122{
123 Q_ASSERT(false);
124 return -1;
125}
126
José Fonseca5bba4772012-03-25 12:46:04 +0100127Q_DECLARE_METATYPE(QList<ApiTraceError>);
128
Zack Rusin3acde362011-04-06 01:11:55 -0400129Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400130 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400131 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400132 m_doubleBuffered(true),
133 m_captureState(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100134 m_captureCall(0),
135 m_profileGpu(false),
136 m_profileCpu(false),
137 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400138{
José Fonseca5bba4772012-03-25 12:46:04 +0100139 qRegisterMetaType<QList<ApiTraceError> >();
140
Zack Rusinf389ae82011-04-10 19:27:28 -0400141#ifdef Q_OS_WIN
142 QString format = QLatin1String("%1;");
143#else
144 QString format = QLatin1String("%1:");
145#endif
José Fonseca27440922011-11-01 08:27:12 +0000146 QString buildPath = format.arg(APITRACE_BINARY_DIR);
Zack Rusinf389ae82011-04-10 19:27:28 -0400147 m_processEnvironment = QProcessEnvironment::systemEnvironment();
148 m_processEnvironment.insert("PATH", buildPath +
149 m_processEnvironment.value("PATH"));
150
151 qputenv("PATH",
152 m_processEnvironment.value("PATH").toLatin1());
Zack Rusin3acde362011-04-06 01:11:55 -0400153}
154
155QString Retracer::fileName() const
156{
157 return m_fileName;
158}
159
160void Retracer::setFileName(const QString &name)
161{
162 m_fileName = name;
163}
164
José Fonseca62997b42011-11-27 15:16:34 +0000165void Retracer::setAPI(trace::API api)
166{
167 m_api = api;
168}
169
Zack Rusin3acde362011-04-06 01:11:55 -0400170bool Retracer::isBenchmarking() const
171{
172 return m_benchmarking;
173}
174
175void Retracer::setBenchmarking(bool bench)
176{
177 m_benchmarking = bench;
178}
179
180bool Retracer::isDoubleBuffered() const
181{
182 return m_doubleBuffered;
183}
184
185void Retracer::setDoubleBuffered(bool db)
186{
187 m_doubleBuffered = db;
188}
189
James Bentonfc4f55a2012-08-08 17:09:07 +0100190bool Retracer::isProfilingGpu() const
191{
192 return m_profileGpu;
193}
194
195bool Retracer::isProfilingCpu() const
196{
197 return m_profileCpu;
198}
199
200bool Retracer::isProfilingPixels() const
201{
202 return m_profilePixels;
203}
204
205bool Retracer::isProfiling() const
206{
207 return m_profileGpu || m_profileCpu || m_profilePixels;
208}
209
210void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
211{
212 m_profileGpu = gpu;
213 m_profileCpu = cpu;
214 m_profilePixels = pixels;
215}
216
Zack Rusin3acde362011-04-06 01:11:55 -0400217void Retracer::setCaptureAtCallNumber(qlonglong num)
218{
219 m_captureCall = num;
220}
221
222qlonglong Retracer::captureAtCallNumber() const
223{
224 return m_captureCall;
225}
226
227bool Retracer::captureState() const
228{
229 return m_captureState;
230}
231
232void Retracer::setCaptureState(bool enable)
233{
234 m_captureState = enable;
235}
236
Dan McCabe66dfdda2012-03-05 17:20:39 -0800237bool Retracer::captureThumbnails() const
238{
239 return m_captureThumbnails;
240}
241
242void Retracer::setCaptureThumbnails(bool enable)
243{
244 m_captureThumbnails = enable;
245}
246
José Fonseca5bba4772012-03-25 12:46:04 +0100247/**
248 * Starting point for the retracing thread.
249 *
250 * Overrides QThread::run().
251 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400252void Retracer::run()
253{
José Fonseca126f64b2012-03-28 00:13:55 +0100254 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400255
José Fonseca5bba4772012-03-25 12:46:04 +0100256 /*
257 * Construct command line
258 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400259
José Fonseca62997b42011-11-27 15:16:34 +0000260 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400261 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400262
José Fonseca889d32c2012-04-23 10:18:28 +0100263 switch (m_api) {
264 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000265 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100266 break;
267 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000268 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100269 break;
270 case trace::API_DX:
271 case trace::API_D3D7:
272 case trace::API_D3D8:
273 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000274 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100275#ifdef Q_OS_WIN
276 prog = QLatin1String("d3dretrace");
277#else
278 prog = QLatin1String("wine");
279 arguments << QLatin1String("d3dretrace.exe");
280#endif
281 break;
282 default:
José Fonseca67964382012-03-27 23:54:30 +0100283 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000284 return;
285 }
286
José Fonseca5bba4772012-03-25 12:46:04 +0100287 if (m_captureState) {
288 arguments << QLatin1String("-D");
289 arguments << QString::number(m_captureCall);
290 } else if (m_captureThumbnails) {
291 arguments << QLatin1String("-s"); // emit snapshots
292 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100293 } else if (isProfiling()) {
294 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000295 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100296 }
297
298 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000299 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100300 }
301
302 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000303 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100304 }
305 } else {
306 if (m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000307 arguments << QLatin1String("--db");
James Bentonfc4f55a2012-08-08 17:09:07 +0100308 } else {
José Fonsecabce31f62012-11-14 07:21:01 +0000309 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100310 }
311
312 if (m_benchmarking) {
313 arguments << QLatin1String("-b");
314 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400315 }
316
317 arguments << m_fileName;
318
José Fonseca5bba4772012-03-25 12:46:04 +0100319 /*
320 * Start the process.
321 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400322
José Fonseca5bba4772012-03-25 12:46:04 +0100323 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400324
José Fonseca6ea8dee2012-03-25 17:25:24 +0100325 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100326 if (!process.waitForStarted(-1)) {
327 emit finished(QLatin1String("Could not start process"));
328 return;
329 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000330
José Fonseca5bba4772012-03-25 12:46:04 +0100331 /*
332 * Process standard output
333 */
334
335 QList<QImage> thumbnails;
336 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100337 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100338
339 process.setReadChannel(QProcess::StandardOutput);
340 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100341 BlockingIODevice io(&process);
342
José Fonseca5bba4772012-03-25 12:46:04 +0100343 if (m_captureState) {
344 /*
345 * Parse JSON from the output.
346 *
José Fonseca5bba4772012-03-25 12:46:04 +0100347 * XXX: QJSON's scanner is inneficient as it abuses single
348 * character QIODevice::peek (not cheap), instead of maintaining a
349 * lookahead character on its own.
350 */
351
José Fonseca5bba4772012-03-25 12:46:04 +0100352 bool ok = false;
353 QJson::Parser jsonParser;
José Fonseca74936bb2012-10-27 10:13:58 +0100354
355 // Allow Nan/Infinity
356 jsonParser.allowSpecialNumbers(true);
José Fonseca95b40562012-04-05 20:06:42 +0100357#if 0
José Fonseca6ea8dee2012-03-25 17:25:24 +0100358 parsedJson = jsonParser.parse(&io, &ok).toMap();
José Fonseca95b40562012-04-05 20:06:42 +0100359#else
360 /*
361 * XXX: QJSON expects blocking IO, and it looks like
362 * BlockingIODevice does not work reliably in all cases.
363 */
364 process.waitForFinished(-1);
365 parsedJson = jsonParser.parse(&process, &ok).toMap();
366#endif
José Fonseca5bba4772012-03-25 12:46:04 +0100367 if (!ok) {
368 msg = QLatin1String("failed to parse JSON");
369 }
370 } else if (m_captureThumbnails) {
371 /*
372 * Parse concatenated PNM images from output.
373 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800374
José Fonseca6ea8dee2012-03-25 17:25:24 +0100375 while (!io.atEnd()) {
José Fonseca5bba4772012-03-25 12:46:04 +0100376 unsigned channels = 0;
377 unsigned width = 0;
378 unsigned height = 0;
379
380 char header[512];
381 qint64 headerSize = 0;
382 int headerLines = 3; // assume no optional comment line
383
384 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100385 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100386
387 // if header actually contains optional comment line, ...
388 if (headerLine == 1 && header[headerSize] == '#') {
389 ++headerLines;
390 }
391
392 headerSize += headerRead;
393 }
394
395 const char *headerEnd = image::readPNMHeader(header, headerSize, &channels, &width, &height);
396
397 // if invalid PNM header was encountered, ...
398 if (header == headerEnd) {
399 qDebug() << "error: invalid snapshot stream encountered";
400 break;
401 }
402
403 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
404
405 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
406
407 int rowBytes = channels * width;
408 for (int y = 0; y < height; ++y) {
409 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100410 qint64 readBytes = io.read((char *) scanLine, rowBytes);
411 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000412 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100413 }
414
José Fonsecadc9e9c62012-03-26 10:29:32 +0100415 QImage thumb = thumbnail(snapshot);
416 thumbnails.append(thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800417 }
José Fonseca5bba4772012-03-25 12:46:04 +0100418
419 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100420 } else if (isProfiling()) {
421 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100422
James Bentonfc4f55a2012-08-08 17:09:07 +0100423 while (!io.atEnd()) {
424 char line[256];
425 qint64 lineLength;
426
427 lineLength = io.readLine(line, 256);
428
429 if (lineLength == -1)
430 break;
431
432 trace::Profiler::parseLine(line, profile);
433 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000434 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000435 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100436 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100437 if (output.length() < 80) {
438 msg = QString::fromUtf8(output);
439 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000440 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400441 }
442
José Fonseca5bba4772012-03-25 12:46:04 +0100443 /*
444 * Wait for process termination
445 */
446
447 process.waitForFinished(-1);
448
449 if (process.exitStatus() != QProcess::NormalExit) {
450 msg = QLatin1String("Process crashed");
451 } else if (process.exitCode() != 0) {
452 msg = QLatin1String("Process exited with non zero exit code");
453 }
454
455 /*
456 * Parse errors.
457 */
458
Zack Rusin10fd4772011-09-14 01:45:12 -0400459 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100460 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000461 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100462 while (!process.atEnd()) {
463 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400464 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400465 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400466 error.callIndex = regexp.cap(1).toInt();
467 error.type = regexp.cap(2);
468 error.message = regexp.cap(3);
469 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200470 } else if (!errors.isEmpty()) {
471 // Probably a multiligne message
472 ApiTraceError &previous = errors.last();
473 if (line.endsWith("\n")) {
474 line.chop(1);
475 }
476 previous.message.append('\n');
477 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400478 }
479 }
José Fonseca5bba4772012-03-25 12:46:04 +0100480
481 /*
482 * Emit signals
483 */
484
485 if (m_captureState) {
486 ApiTraceState *state = new ApiTraceState(parsedJson);
487 emit foundState(state);
488 msg = QLatin1String("State fetched.");
489 }
490
491 if (m_captureThumbnails && !thumbnails.isEmpty()) {
492 emit foundThumbnails(thumbnails);
493 }
494
James Bentonfc4f55a2012-08-08 17:09:07 +0100495 if (isProfiling() && profile) {
496 emit foundProfile(profile);
497 }
498
Zack Rusinb39e1c62011-04-19 23:09:26 -0400499 if (!errors.isEmpty()) {
500 emit retraceErrors(errors);
501 }
José Fonseca5bba4772012-03-25 12:46:04 +0100502
Zack Rusinf389ae82011-04-10 19:27:28 -0400503 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400504}
505
Zack Rusin3acde362011-04-06 01:11:55 -0400506#include "retracer.moc"