blob: 0595b310f908cdf93012088ed4ee24c964c0acd2 [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
Jose Fonsecabceafec2016-05-05 11:09:52 +01006#include "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>
Jose Fonsecac296a3e2015-05-01 17:43:54 +010014
15#include "qubjson.h"
16
Zack Rusin3acde362011-04-06 01:11:55 -040017
José Fonseca6ea8dee2012-03-25 17:25:24 +010018/**
19 * Wrapper around a QProcess which enforces IO to block .
20 *
21 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
22 * they expect that QIODevice::read() will blocked until the requested ammount
23 * of bytes is read or end of file is reached. But by default QProcess, does
24 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
25 * address the problem either.
26 *
27 * This class wraps around QProcess, providing QIODevice interface, while
28 * ensuring that all reads block.
29 *
30 * This class also works around a bug in QProcess::atEnd() implementation.
31 *
32 * See also:
33 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
34 * - http://qt-project.org/wiki/Custom_IO_Device
35 */
36class BlockingIODevice : public QIODevice
37{
38 /* We don't use the Q_OBJECT in this class given we don't declare any
39 * signals and slots or use any other services provided by Qt's meta-object
40 * system. */
41public:
42 BlockingIODevice(QProcess * io);
Jose Fonseca010f9962016-03-05 14:45:41 +000043 bool isSequential() const override;
44 bool atEnd() const override;
45 bool waitForReadyRead(int msecs = -1) override;
José Fonseca6ea8dee2012-03-25 17:25:24 +010046
47protected:
Jose Fonseca010f9962016-03-05 14:45:41 +000048 qint64 readData(char * data, qint64 maxSize) override;
49 qint64 writeData(const char * data, qint64 maxSize) override;
José Fonseca6ea8dee2012-03-25 17:25:24 +010050
51private:
52 QProcess *m_device;
53};
54
55BlockingIODevice::BlockingIODevice(QProcess * io) :
56 m_device(io)
57{
58 /*
59 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
60 * its own buffering on top of the overridden readData() method.
61 *
62 * The only buffering used will be to satisfy QIODevice::peek() and
63 * QIODevice::ungetChar().
64 */
65 setOpenMode(ReadOnly | Unbuffered);
66}
67
68bool BlockingIODevice::isSequential() const
69{
70 return true;
71}
72
73bool BlockingIODevice::atEnd() const
74{
75 /*
76 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
77 * even when the process is running --, so we try to workaround that here.
78 */
79 if (m_device->atEnd()) {
80 if (m_device->state() == QProcess::Running) {
81 if (!m_device->waitForReadyRead(-1)) {
82 return true;
83 }
84 }
85 }
86 return false;
87}
88
89bool BlockingIODevice::waitForReadyRead(int msecs)
90{
91 Q_UNUSED(msecs);
92 return true;
93}
94
95qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
96{
97 qint64 bytesToRead = maxSize;
98 qint64 readSoFar = 0;
99 do {
100 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
101 if (chunkSize < 0) {
102 if (readSoFar) {
103 return readSoFar;
104 } else {
105 return chunkSize;
106 }
107 }
108 Q_ASSERT(chunkSize <= bytesToRead);
109 bytesToRead -= chunkSize;
110 readSoFar += chunkSize;
111 if (bytesToRead) {
112 if (!m_device->waitForReadyRead(-1)) {
113 qDebug() << "waitForReadyRead failed\n";
114 break;
115 }
116 }
117 } while(bytesToRead);
118
119 return readSoFar;
120}
121
122qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
123{
124 Q_ASSERT(false);
125 return -1;
126}
127
José Fonseca5bba4772012-03-25 12:46:04 +0100128Q_DECLARE_METATYPE(QList<ApiTraceError>);
129
Zack Rusin3acde362011-04-06 01:11:55 -0400130Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400131 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400132 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400133 m_doubleBuffered(true),
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400134 m_singlethread(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100135 m_useCoreProfile(false),
Gabe Dagani9ff44052017-11-28 09:30:15 -0600136 m_msaaResolve(true),
Zack Rusin3acde362011-04-06 01:11:55 -0400137 m_captureState(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100138 m_captureThumbnails(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100139 m_captureCall(0),
140 m_profileGpu(false),
141 m_profileCpu(false),
Jose Fonseca65d26052017-07-30 13:48:40 +0100142 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400143{
José Fonseca5bba4772012-03-25 12:46:04 +0100144 qRegisterMetaType<QList<ApiTraceError> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400145}
146
147QString Retracer::fileName() const
148{
149 return m_fileName;
150}
151
152void Retracer::setFileName(const QString &name)
153{
154 m_fileName = name;
155}
156
Carl Worth7257dfc2012-08-09 08:21:42 -0700157QString Retracer::remoteTarget() const
158{
159 return m_remoteTarget;
160}
161
162void Retracer::setRemoteTarget(const QString &host)
163{
164 m_remoteTarget = host;
165}
166
José Fonseca62997b42011-11-27 15:16:34 +0000167void Retracer::setAPI(trace::API api)
168{
169 m_api = api;
170}
171
Zack Rusin3acde362011-04-06 01:11:55 -0400172bool Retracer::isBenchmarking() const
173{
174 return m_benchmarking;
175}
176
177void Retracer::setBenchmarking(bool bench)
178{
179 m_benchmarking = bench;
180}
181
182bool Retracer::isDoubleBuffered() const
183{
184 return m_doubleBuffered;
185}
186
187void Retracer::setDoubleBuffered(bool db)
188{
189 m_doubleBuffered = db;
190}
191
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400192bool Retracer::isSinglethread() const
193{
194 return m_singlethread;
195}
196
197void Retracer::setSinglethread(bool singlethread)
198{
199 m_singlethread = singlethread;
200}
201
Corey Richardsonf3006462014-01-26 17:15:42 -0500202bool Retracer::isCoreProfile() const
203{
204 return m_useCoreProfile;
205}
206
207void Retracer::setCoreProfile(bool coreprofile)
208{
209 m_useCoreProfile = coreprofile;
210}
211
James Bentonfc4f55a2012-08-08 17:09:07 +0100212bool Retracer::isProfilingGpu() const
213{
214 return m_profileGpu;
215}
216
217bool Retracer::isProfilingCpu() const
218{
219 return m_profileCpu;
220}
221
222bool Retracer::isProfilingPixels() const
223{
224 return m_profilePixels;
225}
226
BogDan Vatraa9f9e642015-02-10 14:31:17 +0200227bool Retracer::isProfiling() const
228{
Jose Fonseca65d26052017-07-30 13:48:40 +0100229 return m_profileGpu || m_profileCpu || m_profilePixels;
BogDan Vatraa9f9e642015-02-10 14:31:17 +0200230}
231
Jose Fonseca65d26052017-07-30 13:48:40 +0100232void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
James Bentonfc4f55a2012-08-08 17:09:07 +0100233{
234 m_profileGpu = gpu;
235 m_profileCpu = cpu;
236 m_profilePixels = pixels;
237}
238
Gabe Dagani9ff44052017-11-28 09:30:15 -0600239bool Retracer::isMsaaResolve() const
240{
241 return m_msaaResolve;
242}
243
244void Retracer::setMsaaResolve(bool resolve)
245{
246 m_msaaResolve = resolve;
247}
248
Zack Rusin3acde362011-04-06 01:11:55 -0400249void Retracer::setCaptureAtCallNumber(qlonglong num)
250{
251 m_captureCall = num;
252}
253
254qlonglong Retracer::captureAtCallNumber() const
255{
256 return m_captureCall;
257}
258
259bool Retracer::captureState() const
260{
261 return m_captureState;
262}
263
264void Retracer::setCaptureState(bool enable)
265{
266 m_captureState = enable;
267}
268
Dan McCabe66dfdda2012-03-05 17:20:39 -0800269bool Retracer::captureThumbnails() const
270{
271 return m_captureThumbnails;
272}
273
274void Retracer::setCaptureThumbnails(bool enable)
275{
276 m_captureThumbnails = enable;
277}
278
Dan McCabe88938852012-06-01 13:40:04 -0700279void Retracer::addThumbnailToCapture(qlonglong num)
280{
281 if (!m_thumbnailsToCapture.contains(num)) {
282 m_thumbnailsToCapture.append(num);
283 }
284}
285
286void Retracer::resetThumbnailsToCapture()
287{
288 m_thumbnailsToCapture.clear();
289}
290
Jose Fonseca65d26052017-07-30 13:48:40 +0100291QString Retracer::thumbnailCallSet()
Dan McCabeb14bda22012-06-01 13:40:06 -0700292{
293 QString callSet;
294
295 bool isFirst = true;
296
297 foreach (qlonglong callIndex, m_thumbnailsToCapture) {
298 // TODO: detect contiguous ranges
299 if (!isFirst) {
300 callSet.append(QLatin1String(","));
301 } else {
302 isFirst = false;
303 }
304
305 //emit "callIndex"
306 callSet.append(QString::number(callIndex));
307 }
308
309 //qDebug() << QLatin1String("debug: call set to capture: ") << callSet;
310 return callSet;
311}
Dan McCabe88938852012-06-01 13:40:04 -0700312
José Fonseca5bba4772012-03-25 12:46:04 +0100313/**
314 * Starting point for the retracing thread.
315 *
316 * Overrides QThread::run().
317 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400318void Retracer::run()
319{
José Fonseca126f64b2012-03-28 00:13:55 +0100320 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400321
José Fonseca5bba4772012-03-25 12:46:04 +0100322 /*
323 * Construct command line
324 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400325
José Fonseca62997b42011-11-27 15:16:34 +0000326 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400327 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400328
José Fonseca889d32c2012-04-23 10:18:28 +0100329 switch (m_api) {
330 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000331 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100332 break;
333 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000334 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100335 break;
336 case trace::API_DX:
337 case trace::API_D3D7:
338 case trace::API_D3D8:
339 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000340 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100341#ifdef Q_OS_WIN
342 prog = QLatin1String("d3dretrace");
343#else
344 prog = QLatin1String("wine");
345 arguments << QLatin1String("d3dretrace.exe");
346#endif
347 break;
348 default:
José Fonseca67964382012-03-27 23:54:30 +0100349 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000350 return;
351 }
352
Jose Fonseca65d26052017-07-30 13:48:40 +0100353 if (m_singlethread) {
354 arguments << QLatin1String("--singlethread");
355 }
356
357 if (m_useCoreProfile) {
358 arguments << QLatin1String("--core");
359 }
360
Gabe Dagani9ff44052017-11-28 09:30:15 -0600361 if (!m_msaaResolve) {
362 arguments << QLatin1String("--msaa-no-resolve");
363 }
364
Jose Fonseca65d26052017-07-30 13:48:40 +0100365 if (m_captureState) {
366 arguments << QLatin1String("-D");
367 arguments << QString::number(m_captureCall);
368 arguments << QLatin1String("--dump-format");
369 arguments << QLatin1String("ubjson");
370 } else if (m_captureThumbnails) {
371 if (!m_thumbnailsToCapture.isEmpty()) {
372 arguments << QLatin1String("-S");
373 arguments << thumbnailCallSet();
374 }
375 arguments << QLatin1String("-s"); // emit snapshots
376 arguments << QLatin1String("-"); // emit to stdout
377 } else if (isProfiling()) {
378 if (m_profileGpu) {
379 arguments << QLatin1String("--pgpu");
380 }
381
382 if (m_profileCpu) {
383 arguments << QLatin1String("--pcpu");
384 }
385
386 if (m_profilePixels) {
387 arguments << QLatin1String("--ppd");
388 }
389 } else {
390 if (!m_doubleBuffered) {
391 arguments << QLatin1String("--sb");
392 }
393
394 if (m_benchmarking) {
395 arguments << QLatin1String("-b");
396 }
397 }
398
399 arguments << m_fileName;
Zack Rusinf389ae82011-04-10 19:27:28 -0400400
José Fonseca5bba4772012-03-25 12:46:04 +0100401 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700402 * Support remote execution on a separate target.
403 */
404
405 if (m_remoteTarget.length() != 0) {
406 arguments.prepend(prog);
407 arguments.prepend(m_remoteTarget);
408 prog = QLatin1String("ssh");
409 }
410
411 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100412 * Start the process.
413 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400414
José Fonseca11ad0ea2014-11-15 09:58:44 +0000415 {
416 QDebug debug(QtDebugMsg);
417 debug << "Running:";
418 debug << prog;
419 foreach (const QString &argument, arguments) {
420 debug << argument;
421 }
422 }
423
José Fonseca5bba4772012-03-25 12:46:04 +0100424 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400425
José Fonseca6ea8dee2012-03-25 17:25:24 +0100426 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100427 if (!process.waitForStarted(-1)) {
428 emit finished(QLatin1String("Could not start process"));
429 return;
430 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000431
José Fonseca5bba4772012-03-25 12:46:04 +0100432 /*
433 * Process standard output
434 */
435
Dan McCabec6f924e2012-06-01 13:40:05 -0700436 ImageHash thumbnails;
José Fonseca5bba4772012-03-25 12:46:04 +0100437 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100438 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100439
440 process.setReadChannel(QProcess::StandardOutput);
441 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100442 BlockingIODevice io(&process);
443
José Fonseca5bba4772012-03-25 12:46:04 +0100444 if (m_captureState) {
Jose Fonseca08fdb7b2015-05-21 21:41:50 +0100445 parsedJson = decodeUBJSONObject(&io).toMap();
José Fonseca95b40562012-04-05 20:06:42 +0100446 process.waitForFinished(-1);
José Fonseca5bba4772012-03-25 12:46:04 +0100447 } else if (m_captureThumbnails) {
448 /*
449 * Parse concatenated PNM images from output.
450 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800451
José Fonseca6ea8dee2012-03-25 17:25:24 +0100452 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100453 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100454
455 char header[512];
456 qint64 headerSize = 0;
457 int headerLines = 3; // assume no optional comment line
458
459 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100460 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100461
462 // if header actually contains optional comment line, ...
463 if (headerLine == 1 && header[headerSize] == '#') {
464 ++headerLines;
465 }
466
467 headerSize += headerRead;
468 }
469
José Fonsecabeda4442013-09-12 17:25:04 +0100470 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100471
472 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100473 if (headerEnd == NULL ||
474 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100475 qDebug() << "error: invalid snapshot stream encountered";
476 break;
477 }
478
José Fonsecabeda4442013-09-12 17:25:04 +0100479 unsigned channels = info.channels;
480 unsigned width = info.width;
481 unsigned height = info.height;
482
José Fonseca5bba4772012-03-25 12:46:04 +0100483 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
484
485 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
486
487 int rowBytes = channels * width;
488 for (int y = 0; y < height; ++y) {
489 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100490 qint64 readBytes = io.read((char *) scanLine, rowBytes);
491 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000492 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100493 }
494
José Fonsecadc9e9c62012-03-26 10:29:32 +0100495 QImage thumb = thumbnail(snapshot);
Dan McCabec6f924e2012-06-01 13:40:05 -0700496 thumbnails.insert(info.commentNumber, thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800497 }
José Fonseca5bba4772012-03-25 12:46:04 +0100498
499 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100500 } else if (isProfiling()) {
501 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100502
James Bentonfc4f55a2012-08-08 17:09:07 +0100503 while (!io.atEnd()) {
504 char line[256];
505 qint64 lineLength;
506
507 lineLength = io.readLine(line, 256);
508
509 if (lineLength == -1)
510 break;
511
512 trace::Profiler::parseLine(line, profile);
513 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000514 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000515 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100516 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100517 if (output.length() < 80) {
518 msg = QString::fromUtf8(output);
519 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000520 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400521 }
522
José Fonseca5bba4772012-03-25 12:46:04 +0100523 /*
524 * Wait for process termination
525 */
526
527 process.waitForFinished(-1);
528
529 if (process.exitStatus() != QProcess::NormalExit) {
530 msg = QLatin1String("Process crashed");
531 } else if (process.exitCode() != 0) {
532 msg = QLatin1String("Process exited with non zero exit code");
533 }
534
535 /*
536 * Parse errors.
537 */
538
Zack Rusin10fd4772011-09-14 01:45:12 -0400539 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100540 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000541 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100542 while (!process.atEnd()) {
543 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400544 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400545 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400546 error.callIndex = regexp.cap(1).toInt();
547 error.type = regexp.cap(2);
548 error.message = regexp.cap(3);
549 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200550 } else if (!errors.isEmpty()) {
551 // Probably a multiligne message
552 ApiTraceError &previous = errors.last();
553 if (line.endsWith("\n")) {
554 line.chop(1);
555 }
556 previous.message.append('\n');
557 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400558 }
559 }
José Fonseca5bba4772012-03-25 12:46:04 +0100560
561 /*
562 * Emit signals
563 */
564
565 if (m_captureState) {
566 ApiTraceState *state = new ApiTraceState(parsedJson);
567 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100568 }
569
570 if (m_captureThumbnails && !thumbnails.isEmpty()) {
571 emit foundThumbnails(thumbnails);
572 }
573
James Bentonfc4f55a2012-08-08 17:09:07 +0100574 if (isProfiling() && profile) {
575 emit foundProfile(profile);
576 }
577
Zack Rusinb39e1c62011-04-19 23:09:26 -0400578 if (!errors.isEmpty()) {
579 emit retraceErrors(errors);
580 }
José Fonseca5bba4772012-03-25 12:46:04 +0100581
Zack Rusinf389ae82011-04-10 19:27:28 -0400582 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400583}
584
Zack Rusin3acde362011-04-06 01:11:55 -0400585#include "retracer.moc"