blob: 2928ed63f321f31e34667e2448e1b8f765f2fdbf [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> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400140}
141
142QString Retracer::fileName() const
143{
144 return m_fileName;
145}
146
147void Retracer::setFileName(const QString &name)
148{
149 m_fileName = name;
150}
151
José Fonseca62997b42011-11-27 15:16:34 +0000152void Retracer::setAPI(trace::API api)
153{
154 m_api = api;
155}
156
Zack Rusin3acde362011-04-06 01:11:55 -0400157bool Retracer::isBenchmarking() const
158{
159 return m_benchmarking;
160}
161
162void Retracer::setBenchmarking(bool bench)
163{
164 m_benchmarking = bench;
165}
166
167bool Retracer::isDoubleBuffered() const
168{
169 return m_doubleBuffered;
170}
171
172void Retracer::setDoubleBuffered(bool db)
173{
174 m_doubleBuffered = db;
175}
176
James Bentonfc4f55a2012-08-08 17:09:07 +0100177bool Retracer::isProfilingGpu() const
178{
179 return m_profileGpu;
180}
181
182bool Retracer::isProfilingCpu() const
183{
184 return m_profileCpu;
185}
186
187bool Retracer::isProfilingPixels() const
188{
189 return m_profilePixels;
190}
191
192bool Retracer::isProfiling() const
193{
194 return m_profileGpu || m_profileCpu || m_profilePixels;
195}
196
197void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
198{
199 m_profileGpu = gpu;
200 m_profileCpu = cpu;
201 m_profilePixels = pixels;
202}
203
Zack Rusin3acde362011-04-06 01:11:55 -0400204void Retracer::setCaptureAtCallNumber(qlonglong num)
205{
206 m_captureCall = num;
207}
208
209qlonglong Retracer::captureAtCallNumber() const
210{
211 return m_captureCall;
212}
213
214bool Retracer::captureState() const
215{
216 return m_captureState;
217}
218
219void Retracer::setCaptureState(bool enable)
220{
221 m_captureState = enable;
222}
223
Dan McCabe66dfdda2012-03-05 17:20:39 -0800224bool Retracer::captureThumbnails() const
225{
226 return m_captureThumbnails;
227}
228
229void Retracer::setCaptureThumbnails(bool enable)
230{
231 m_captureThumbnails = enable;
232}
233
José Fonseca5bba4772012-03-25 12:46:04 +0100234/**
235 * Starting point for the retracing thread.
236 *
237 * Overrides QThread::run().
238 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400239void Retracer::run()
240{
José Fonseca126f64b2012-03-28 00:13:55 +0100241 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400242
José Fonseca5bba4772012-03-25 12:46:04 +0100243 /*
244 * Construct command line
245 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400246
José Fonseca62997b42011-11-27 15:16:34 +0000247 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400248 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400249
José Fonseca889d32c2012-04-23 10:18:28 +0100250 switch (m_api) {
251 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000252 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100253 break;
254 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000255 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100256 break;
257 case trace::API_DX:
258 case trace::API_D3D7:
259 case trace::API_D3D8:
260 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000261 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100262#ifdef Q_OS_WIN
263 prog = QLatin1String("d3dretrace");
264#else
265 prog = QLatin1String("wine");
266 arguments << QLatin1String("d3dretrace.exe");
267#endif
268 break;
269 default:
José Fonseca67964382012-03-27 23:54:30 +0100270 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000271 return;
272 }
273
José Fonseca5bba4772012-03-25 12:46:04 +0100274 if (m_captureState) {
275 arguments << QLatin1String("-D");
276 arguments << QString::number(m_captureCall);
277 } else if (m_captureThumbnails) {
278 arguments << QLatin1String("-s"); // emit snapshots
279 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100280 } else if (isProfiling()) {
281 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000282 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100283 }
284
285 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000286 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100287 }
288
289 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000290 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100291 }
292 } else {
293 if (m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000294 arguments << QLatin1String("--db");
James Bentonfc4f55a2012-08-08 17:09:07 +0100295 } else {
José Fonsecabce31f62012-11-14 07:21:01 +0000296 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100297 }
298
299 if (m_benchmarking) {
300 arguments << QLatin1String("-b");
301 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400302 }
303
304 arguments << m_fileName;
305
José Fonseca5bba4772012-03-25 12:46:04 +0100306 /*
307 * Start the process.
308 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400309
José Fonseca5bba4772012-03-25 12:46:04 +0100310 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400311
José Fonseca6ea8dee2012-03-25 17:25:24 +0100312 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100313 if (!process.waitForStarted(-1)) {
314 emit finished(QLatin1String("Could not start process"));
315 return;
316 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000317
José Fonseca5bba4772012-03-25 12:46:04 +0100318 /*
319 * Process standard output
320 */
321
322 QList<QImage> thumbnails;
323 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100324 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100325
326 process.setReadChannel(QProcess::StandardOutput);
327 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100328 BlockingIODevice io(&process);
329
José Fonseca5bba4772012-03-25 12:46:04 +0100330 if (m_captureState) {
331 /*
332 * Parse JSON from the output.
333 *
José Fonseca5bba4772012-03-25 12:46:04 +0100334 * XXX: QJSON's scanner is inneficient as it abuses single
335 * character QIODevice::peek (not cheap), instead of maintaining a
336 * lookahead character on its own.
337 */
338
José Fonseca5bba4772012-03-25 12:46:04 +0100339 bool ok = false;
340 QJson::Parser jsonParser;
José Fonseca74936bb2012-10-27 10:13:58 +0100341
342 // Allow Nan/Infinity
343 jsonParser.allowSpecialNumbers(true);
José Fonseca95b40562012-04-05 20:06:42 +0100344#if 0
José Fonseca6ea8dee2012-03-25 17:25:24 +0100345 parsedJson = jsonParser.parse(&io, &ok).toMap();
José Fonseca95b40562012-04-05 20:06:42 +0100346#else
347 /*
348 * XXX: QJSON expects blocking IO, and it looks like
349 * BlockingIODevice does not work reliably in all cases.
350 */
351 process.waitForFinished(-1);
352 parsedJson = jsonParser.parse(&process, &ok).toMap();
353#endif
José Fonseca5bba4772012-03-25 12:46:04 +0100354 if (!ok) {
355 msg = QLatin1String("failed to parse JSON");
356 }
357 } else if (m_captureThumbnails) {
358 /*
359 * Parse concatenated PNM images from output.
360 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800361
José Fonseca6ea8dee2012-03-25 17:25:24 +0100362 while (!io.atEnd()) {
José Fonseca5bba4772012-03-25 12:46:04 +0100363 unsigned channels = 0;
364 unsigned width = 0;
365 unsigned height = 0;
366
367 char header[512];
368 qint64 headerSize = 0;
369 int headerLines = 3; // assume no optional comment line
370
371 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100372 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100373
374 // if header actually contains optional comment line, ...
375 if (headerLine == 1 && header[headerSize] == '#') {
376 ++headerLines;
377 }
378
379 headerSize += headerRead;
380 }
381
382 const char *headerEnd = image::readPNMHeader(header, headerSize, &channels, &width, &height);
383
384 // if invalid PNM header was encountered, ...
385 if (header == headerEnd) {
386 qDebug() << "error: invalid snapshot stream encountered";
387 break;
388 }
389
390 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
391
392 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
393
394 int rowBytes = channels * width;
395 for (int y = 0; y < height; ++y) {
396 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100397 qint64 readBytes = io.read((char *) scanLine, rowBytes);
398 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000399 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100400 }
401
José Fonsecadc9e9c62012-03-26 10:29:32 +0100402 QImage thumb = thumbnail(snapshot);
403 thumbnails.append(thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800404 }
José Fonseca5bba4772012-03-25 12:46:04 +0100405
406 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100407 } else if (isProfiling()) {
408 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100409
James Bentonfc4f55a2012-08-08 17:09:07 +0100410 while (!io.atEnd()) {
411 char line[256];
412 qint64 lineLength;
413
414 lineLength = io.readLine(line, 256);
415
416 if (lineLength == -1)
417 break;
418
419 trace::Profiler::parseLine(line, profile);
420 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000421 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000422 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100423 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100424 if (output.length() < 80) {
425 msg = QString::fromUtf8(output);
426 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000427 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400428 }
429
José Fonseca5bba4772012-03-25 12:46:04 +0100430 /*
431 * Wait for process termination
432 */
433
434 process.waitForFinished(-1);
435
436 if (process.exitStatus() != QProcess::NormalExit) {
437 msg = QLatin1String("Process crashed");
438 } else if (process.exitCode() != 0) {
439 msg = QLatin1String("Process exited with non zero exit code");
440 }
441
442 /*
443 * Parse errors.
444 */
445
Zack Rusin10fd4772011-09-14 01:45:12 -0400446 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100447 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000448 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100449 while (!process.atEnd()) {
450 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400451 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400452 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400453 error.callIndex = regexp.cap(1).toInt();
454 error.type = regexp.cap(2);
455 error.message = regexp.cap(3);
456 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200457 } else if (!errors.isEmpty()) {
458 // Probably a multiligne message
459 ApiTraceError &previous = errors.last();
460 if (line.endsWith("\n")) {
461 line.chop(1);
462 }
463 previous.message.append('\n');
464 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400465 }
466 }
José Fonseca5bba4772012-03-25 12:46:04 +0100467
468 /*
469 * Emit signals
470 */
471
472 if (m_captureState) {
473 ApiTraceState *state = new ApiTraceState(parsedJson);
474 emit foundState(state);
475 msg = QLatin1String("State fetched.");
476 }
477
478 if (m_captureThumbnails && !thumbnails.isEmpty()) {
479 emit foundThumbnails(thumbnails);
480 }
481
James Bentonfc4f55a2012-08-08 17:09:07 +0100482 if (isProfiling() && profile) {
483 emit foundProfile(profile);
484 }
485
Zack Rusinb39e1c62011-04-19 23:09:26 -0400486 if (!errors.isEmpty()) {
487 emit retraceErrors(errors);
488 }
José Fonseca5bba4772012-03-25 12:46:04 +0100489
Zack Rusinf389ae82011-04-10 19:27:28 -0400490 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400491}
492
Zack Rusin3acde362011-04-06 01:11:55 -0400493#include "retracer.moc"