blob: 6531f4c1cdcf5a995304b77bd446e1cda32f2e88 [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>
José Fonseca3f4cd302015-01-14 12:02:06 +000014#include <QJsonDocument>
Zack Rusin3acde362011-04-06 01:11:55 -040015
José Fonseca6ea8dee2012-03-25 17:25:24 +010016/**
17 * Wrapper around a QProcess which enforces IO to block .
18 *
19 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
20 * they expect that QIODevice::read() will blocked until the requested ammount
21 * of bytes is read or end of file is reached. But by default QProcess, does
22 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
23 * address the problem either.
24 *
25 * This class wraps around QProcess, providing QIODevice interface, while
26 * ensuring that all reads block.
27 *
28 * This class also works around a bug in QProcess::atEnd() implementation.
29 *
30 * See also:
31 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
32 * - http://qt-project.org/wiki/Custom_IO_Device
33 */
34class BlockingIODevice : public QIODevice
35{
36 /* We don't use the Q_OBJECT in this class given we don't declare any
37 * signals and slots or use any other services provided by Qt's meta-object
38 * system. */
39public:
40 BlockingIODevice(QProcess * io);
41 bool isSequential() const;
42 bool atEnd() const;
43 bool waitForReadyRead(int msecs = -1);
44
45protected:
46 qint64 readData(char * data, qint64 maxSize);
47 qint64 writeData(const char * data, qint64 maxSize);
48
49private:
50 QProcess *m_device;
51};
52
53BlockingIODevice::BlockingIODevice(QProcess * io) :
54 m_device(io)
55{
56 /*
57 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
58 * its own buffering on top of the overridden readData() method.
59 *
60 * The only buffering used will be to satisfy QIODevice::peek() and
61 * QIODevice::ungetChar().
62 */
63 setOpenMode(ReadOnly | Unbuffered);
64}
65
66bool BlockingIODevice::isSequential() const
67{
68 return true;
69}
70
71bool BlockingIODevice::atEnd() const
72{
73 /*
74 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
75 * even when the process is running --, so we try to workaround that here.
76 */
77 if (m_device->atEnd()) {
78 if (m_device->state() == QProcess::Running) {
79 if (!m_device->waitForReadyRead(-1)) {
80 return true;
81 }
82 }
83 }
84 return false;
85}
86
87bool BlockingIODevice::waitForReadyRead(int msecs)
88{
89 Q_UNUSED(msecs);
90 return true;
91}
92
93qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
94{
95 qint64 bytesToRead = maxSize;
96 qint64 readSoFar = 0;
97 do {
98 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
99 if (chunkSize < 0) {
100 if (readSoFar) {
101 return readSoFar;
102 } else {
103 return chunkSize;
104 }
105 }
106 Q_ASSERT(chunkSize <= bytesToRead);
107 bytesToRead -= chunkSize;
108 readSoFar += chunkSize;
109 if (bytesToRead) {
110 if (!m_device->waitForReadyRead(-1)) {
111 qDebug() << "waitForReadyRead failed\n";
112 break;
113 }
114 }
115 } while(bytesToRead);
116
117 return readSoFar;
118}
119
120qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
121{
122 Q_ASSERT(false);
123 return -1;
124}
125
José Fonseca5bba4772012-03-25 12:46:04 +0100126Q_DECLARE_METATYPE(QList<ApiTraceError>);
127
Zack Rusin3acde362011-04-06 01:11:55 -0400128Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400129 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400130 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400131 m_doubleBuffered(true),
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400132 m_singlethread(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100133 m_useCoreProfile(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400134 m_captureState(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100135 m_captureThumbnails(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100136 m_captureCall(0),
137 m_profileGpu(false),
138 m_profileCpu(false),
139 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400140{
José Fonseca5bba4772012-03-25 12:46:04 +0100141 qRegisterMetaType<QList<ApiTraceError> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400142}
143
144QString Retracer::fileName() const
145{
146 return m_fileName;
147}
148
149void Retracer::setFileName(const QString &name)
150{
151 m_fileName = name;
152}
153
Carl Worth7257dfc2012-08-09 08:21:42 -0700154QString Retracer::remoteTarget() const
155{
156 return m_remoteTarget;
157}
158
159void Retracer::setRemoteTarget(const QString &host)
160{
161 m_remoteTarget = host;
162}
163
José Fonseca62997b42011-11-27 15:16:34 +0000164void Retracer::setAPI(trace::API api)
165{
166 m_api = api;
167}
168
Zack Rusin3acde362011-04-06 01:11:55 -0400169bool Retracer::isBenchmarking() const
170{
171 return m_benchmarking;
172}
173
174void Retracer::setBenchmarking(bool bench)
175{
176 m_benchmarking = bench;
177}
178
179bool Retracer::isDoubleBuffered() const
180{
181 return m_doubleBuffered;
182}
183
184void Retracer::setDoubleBuffered(bool db)
185{
186 m_doubleBuffered = db;
187}
188
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400189bool Retracer::isSinglethread() const
190{
191 return m_singlethread;
192}
193
194void Retracer::setSinglethread(bool singlethread)
195{
196 m_singlethread = singlethread;
197}
198
Corey Richardsonf3006462014-01-26 17:15:42 -0500199bool Retracer::isCoreProfile() const
200{
201 return m_useCoreProfile;
202}
203
204void Retracer::setCoreProfile(bool coreprofile)
205{
206 m_useCoreProfile = coreprofile;
207}
208
James Bentonfc4f55a2012-08-08 17:09:07 +0100209bool Retracer::isProfilingGpu() const
210{
211 return m_profileGpu;
212}
213
214bool Retracer::isProfilingCpu() const
215{
216 return m_profileCpu;
217}
218
219bool Retracer::isProfilingPixels() const
220{
221 return m_profilePixels;
222}
223
224bool Retracer::isProfiling() const
225{
226 return m_profileGpu || m_profileCpu || m_profilePixels;
227}
228
229void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
230{
231 m_profileGpu = gpu;
232 m_profileCpu = cpu;
233 m_profilePixels = pixels;
234}
235
Zack Rusin3acde362011-04-06 01:11:55 -0400236void Retracer::setCaptureAtCallNumber(qlonglong num)
237{
238 m_captureCall = num;
239}
240
241qlonglong Retracer::captureAtCallNumber() const
242{
243 return m_captureCall;
244}
245
246bool Retracer::captureState() const
247{
248 return m_captureState;
249}
250
251void Retracer::setCaptureState(bool enable)
252{
253 m_captureState = enable;
254}
255
Dan McCabe66dfdda2012-03-05 17:20:39 -0800256bool Retracer::captureThumbnails() const
257{
258 return m_captureThumbnails;
259}
260
261void Retracer::setCaptureThumbnails(bool enable)
262{
263 m_captureThumbnails = enable;
264}
265
Dan McCabe88938852012-06-01 13:40:04 -0700266void Retracer::addThumbnailToCapture(qlonglong num)
267{
268 if (!m_thumbnailsToCapture.contains(num)) {
269 m_thumbnailsToCapture.append(num);
270 }
271}
272
273void Retracer::resetThumbnailsToCapture()
274{
275 m_thumbnailsToCapture.clear();
276}
277
278
José Fonseca5bba4772012-03-25 12:46:04 +0100279/**
280 * Starting point for the retracing thread.
281 *
282 * Overrides QThread::run().
283 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400284void Retracer::run()
285{
José Fonseca126f64b2012-03-28 00:13:55 +0100286 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400287
José Fonseca5bba4772012-03-25 12:46:04 +0100288 /*
289 * Construct command line
290 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400291
José Fonseca62997b42011-11-27 15:16:34 +0000292 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400293 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400294
José Fonseca889d32c2012-04-23 10:18:28 +0100295 switch (m_api) {
296 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000297 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100298 break;
299 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000300 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100301 break;
302 case trace::API_DX:
303 case trace::API_D3D7:
304 case trace::API_D3D8:
305 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000306 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100307#ifdef Q_OS_WIN
308 prog = QLatin1String("d3dretrace");
309#else
310 prog = QLatin1String("wine");
311 arguments << QLatin1String("d3dretrace.exe");
312#endif
313 break;
314 default:
José Fonseca67964382012-03-27 23:54:30 +0100315 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000316 return;
317 }
318
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400319 if (m_singlethread) {
320 arguments << QLatin1String("--singlethread");
321 }
322
Corey Richardsonf3006462014-01-26 17:15:42 -0500323 if (m_useCoreProfile) {
324 arguments << QLatin1String("--core");
325 }
326
José Fonseca5bba4772012-03-25 12:46:04 +0100327 if (m_captureState) {
328 arguments << QLatin1String("-D");
329 arguments << QString::number(m_captureCall);
330 } else if (m_captureThumbnails) {
331 arguments << QLatin1String("-s"); // emit snapshots
332 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100333 } else if (isProfiling()) {
334 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000335 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100336 }
337
338 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000339 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100340 }
341
342 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000343 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100344 }
345 } else {
Jose Fonsecac4f9daa2015-01-25 17:09:13 +0000346 if (!m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000347 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100348 }
349
350 if (m_benchmarking) {
351 arguments << QLatin1String("-b");
352 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400353 }
354
355 arguments << m_fileName;
356
José Fonseca5bba4772012-03-25 12:46:04 +0100357 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700358 * Support remote execution on a separate target.
359 */
360
361 if (m_remoteTarget.length() != 0) {
362 arguments.prepend(prog);
363 arguments.prepend(m_remoteTarget);
364 prog = QLatin1String("ssh");
365 }
366
367 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100368 * Start the process.
369 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400370
José Fonseca11ad0ea2014-11-15 09:58:44 +0000371 {
372 QDebug debug(QtDebugMsg);
373 debug << "Running:";
374 debug << prog;
375 foreach (const QString &argument, arguments) {
376 debug << argument;
377 }
378 }
379
José Fonseca5bba4772012-03-25 12:46:04 +0100380 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400381
José Fonseca6ea8dee2012-03-25 17:25:24 +0100382 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100383 if (!process.waitForStarted(-1)) {
384 emit finished(QLatin1String("Could not start process"));
385 return;
386 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000387
José Fonseca5bba4772012-03-25 12:46:04 +0100388 /*
389 * Process standard output
390 */
391
392 QList<QImage> thumbnails;
393 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100394 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100395
396 process.setReadChannel(QProcess::StandardOutput);
397 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100398 BlockingIODevice io(&process);
399
José Fonseca5bba4772012-03-25 12:46:04 +0100400 if (m_captureState) {
José Fonseca95b40562012-04-05 20:06:42 +0100401 process.waitForFinished(-1);
José Fonseca3f4cd302015-01-14 12:02:06 +0000402 QByteArray data = process.readAll();
403 QJsonParseError error;
404 QJsonDocument jsonDoc =
405 QJsonDocument::fromJson(data, &error);
406
407 if (error.error != QJsonParseError::NoError) {
408 //qDebug()<<"Error is "<<error.errorString();
409 msg = error.errorString();
José Fonseca5bba4772012-03-25 12:46:04 +0100410 }
José Fonseca3f4cd302015-01-14 12:02:06 +0000411 parsedJson = jsonDoc.toVariant().toMap();
José Fonseca5bba4772012-03-25 12:46:04 +0100412 } else if (m_captureThumbnails) {
413 /*
414 * Parse concatenated PNM images from output.
415 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800416
José Fonseca6ea8dee2012-03-25 17:25:24 +0100417 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100418 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100419
420 char header[512];
421 qint64 headerSize = 0;
422 int headerLines = 3; // assume no optional comment line
423
424 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100425 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100426
427 // if header actually contains optional comment line, ...
428 if (headerLine == 1 && header[headerSize] == '#') {
429 ++headerLines;
430 }
431
432 headerSize += headerRead;
433 }
434
José Fonsecabeda4442013-09-12 17:25:04 +0100435 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100436
437 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100438 if (headerEnd == NULL ||
439 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100440 qDebug() << "error: invalid snapshot stream encountered";
441 break;
442 }
443
José Fonsecabeda4442013-09-12 17:25:04 +0100444 unsigned channels = info.channels;
445 unsigned width = info.width;
446 unsigned height = info.height;
447
José Fonseca5bba4772012-03-25 12:46:04 +0100448 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
449
450 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
451
452 int rowBytes = channels * width;
453 for (int y = 0; y < height; ++y) {
454 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100455 qint64 readBytes = io.read((char *) scanLine, rowBytes);
456 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000457 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100458 }
459
José Fonsecadc9e9c62012-03-26 10:29:32 +0100460 QImage thumb = thumbnail(snapshot);
461 thumbnails.append(thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800462 }
José Fonseca5bba4772012-03-25 12:46:04 +0100463
464 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100465 } else if (isProfiling()) {
466 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100467
James Bentonfc4f55a2012-08-08 17:09:07 +0100468 while (!io.atEnd()) {
469 char line[256];
470 qint64 lineLength;
471
472 lineLength = io.readLine(line, 256);
473
474 if (lineLength == -1)
475 break;
476
477 trace::Profiler::parseLine(line, profile);
478 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000479 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000480 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100481 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100482 if (output.length() < 80) {
483 msg = QString::fromUtf8(output);
484 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000485 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400486 }
487
José Fonseca5bba4772012-03-25 12:46:04 +0100488 /*
489 * Wait for process termination
490 */
491
492 process.waitForFinished(-1);
493
494 if (process.exitStatus() != QProcess::NormalExit) {
495 msg = QLatin1String("Process crashed");
496 } else if (process.exitCode() != 0) {
497 msg = QLatin1String("Process exited with non zero exit code");
498 }
499
500 /*
501 * Parse errors.
502 */
503
Zack Rusin10fd4772011-09-14 01:45:12 -0400504 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100505 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000506 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100507 while (!process.atEnd()) {
508 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400509 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400510 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400511 error.callIndex = regexp.cap(1).toInt();
512 error.type = regexp.cap(2);
513 error.message = regexp.cap(3);
514 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200515 } else if (!errors.isEmpty()) {
516 // Probably a multiligne message
517 ApiTraceError &previous = errors.last();
518 if (line.endsWith("\n")) {
519 line.chop(1);
520 }
521 previous.message.append('\n');
522 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400523 }
524 }
José Fonseca5bba4772012-03-25 12:46:04 +0100525
526 /*
527 * Emit signals
528 */
529
530 if (m_captureState) {
531 ApiTraceState *state = new ApiTraceState(parsedJson);
532 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100533 }
534
535 if (m_captureThumbnails && !thumbnails.isEmpty()) {
536 emit foundThumbnails(thumbnails);
537 }
538
James Bentonfc4f55a2012-08-08 17:09:07 +0100539 if (isProfiling() && profile) {
540 emit foundProfile(profile);
541 }
542
Zack Rusinb39e1c62011-04-19 23:09:26 -0400543 if (!errors.isEmpty()) {
544 emit retraceErrors(errors);
545 }
José Fonseca5bba4772012-03-25 12:46:04 +0100546
Zack Rusinf389ae82011-04-10 19:27:28 -0400547 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400548}
549
Zack Rusin3acde362011-04-06 01:11:55 -0400550#include "retracer.moc"