Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 1 | #include "retracer.h" |
| 2 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 3 | #include "apitracecall.h" |
José Fonseca | 3f45640 | 2012-03-25 20:59:24 +0100 | [diff] [blame] | 4 | #include "thumbnail.h" |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 5 | |
José Fonseca | e7102bf | 2012-12-07 07:33:05 +0000 | [diff] [blame] | 6 | #include "image/image.hpp" |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 7 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 8 | #include "trace_profiler.hpp" |
| 9 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 10 | #include <QDebug> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 11 | #include <QVariant> |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 12 | #include <QList> |
| 13 | #include <QImage> |
José Fonseca | 3f4cd30 | 2015-01-14 12:02:06 +0000 | [diff] [blame] | 14 | #include <QJsonDocument> |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 15 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 16 | /** |
| 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 | */ |
| 34 | class 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. */ |
| 39 | public: |
| 40 | BlockingIODevice(QProcess * io); |
| 41 | bool isSequential() const; |
| 42 | bool atEnd() const; |
| 43 | bool waitForReadyRead(int msecs = -1); |
| 44 | |
| 45 | protected: |
| 46 | qint64 readData(char * data, qint64 maxSize); |
| 47 | qint64 writeData(const char * data, qint64 maxSize); |
| 48 | |
| 49 | private: |
| 50 | QProcess *m_device; |
| 51 | }; |
| 52 | |
| 53 | BlockingIODevice::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 | |
| 66 | bool BlockingIODevice::isSequential() const |
| 67 | { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | bool 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 | |
| 87 | bool BlockingIODevice::waitForReadyRead(int msecs) |
| 88 | { |
| 89 | Q_UNUSED(msecs); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | qint64 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 | |
| 120 | qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize) |
| 121 | { |
| 122 | Q_ASSERT(false); |
| 123 | return -1; |
| 124 | } |
| 125 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 126 | Q_DECLARE_METATYPE(QList<ApiTraceError>); |
| 127 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 128 | Retracer::Retracer(QObject *parent) |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 129 | : QThread(parent), |
Zack Rusin | 404a1ef | 2011-04-19 23:49:56 -0400 | [diff] [blame] | 130 | m_benchmarking(false), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 131 | m_doubleBuffered(true), |
Peter Lohrmann | b34c675 | 2013-07-10 11:08:14 -0400 | [diff] [blame] | 132 | m_singlethread(false), |
José Fonseca | c03e4b0 | 2014-05-30 17:24:42 +0100 | [diff] [blame] | 133 | m_useCoreProfile(false), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 134 | m_captureState(false), |
José Fonseca | c03e4b0 | 2014-05-30 17:24:42 +0100 | [diff] [blame] | 135 | m_captureThumbnails(false), |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 136 | m_captureCall(0), |
| 137 | m_profileGpu(false), |
| 138 | m_profileCpu(false), |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 139 | m_profilePixels(false), |
| 140 | m_profileMemory(false) |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 141 | { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 142 | qRegisterMetaType<QList<ApiTraceError> >(); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | QString Retracer::fileName() const |
| 146 | { |
| 147 | return m_fileName; |
| 148 | } |
| 149 | |
| 150 | void Retracer::setFileName(const QString &name) |
| 151 | { |
| 152 | m_fileName = name; |
| 153 | } |
| 154 | |
Carl Worth | 7257dfc | 2012-08-09 08:21:42 -0700 | [diff] [blame] | 155 | QString Retracer::remoteTarget() const |
| 156 | { |
| 157 | return m_remoteTarget; |
| 158 | } |
| 159 | |
| 160 | void Retracer::setRemoteTarget(const QString &host) |
| 161 | { |
| 162 | m_remoteTarget = host; |
| 163 | } |
| 164 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 165 | void Retracer::setAPI(trace::API api) |
| 166 | { |
| 167 | m_api = api; |
| 168 | } |
| 169 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 170 | bool Retracer::isBenchmarking() const |
| 171 | { |
| 172 | return m_benchmarking; |
| 173 | } |
| 174 | |
| 175 | void Retracer::setBenchmarking(bool bench) |
| 176 | { |
| 177 | m_benchmarking = bench; |
| 178 | } |
| 179 | |
| 180 | bool Retracer::isDoubleBuffered() const |
| 181 | { |
| 182 | return m_doubleBuffered; |
| 183 | } |
| 184 | |
| 185 | void Retracer::setDoubleBuffered(bool db) |
| 186 | { |
| 187 | m_doubleBuffered = db; |
| 188 | } |
| 189 | |
Peter Lohrmann | b34c675 | 2013-07-10 11:08:14 -0400 | [diff] [blame] | 190 | bool Retracer::isSinglethread() const |
| 191 | { |
| 192 | return m_singlethread; |
| 193 | } |
| 194 | |
| 195 | void Retracer::setSinglethread(bool singlethread) |
| 196 | { |
| 197 | m_singlethread = singlethread; |
| 198 | } |
| 199 | |
Corey Richardson | f300646 | 2014-01-26 17:15:42 -0500 | [diff] [blame] | 200 | bool Retracer::isCoreProfile() const |
| 201 | { |
| 202 | return m_useCoreProfile; |
| 203 | } |
| 204 | |
| 205 | void Retracer::setCoreProfile(bool coreprofile) |
| 206 | { |
| 207 | m_useCoreProfile = coreprofile; |
| 208 | } |
| 209 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 210 | bool Retracer::isProfilingGpu() const |
| 211 | { |
| 212 | return m_profileGpu; |
| 213 | } |
| 214 | |
| 215 | bool Retracer::isProfilingCpu() const |
| 216 | { |
| 217 | return m_profileCpu; |
| 218 | } |
| 219 | |
| 220 | bool Retracer::isProfilingPixels() const |
| 221 | { |
| 222 | return m_profilePixels; |
| 223 | } |
| 224 | |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 225 | bool Retracer::isProfilingMemory() const |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 226 | { |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 227 | return m_profileMemory; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 228 | } |
| 229 | |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 230 | bool Retracer::isProfiling() const |
| 231 | { |
| 232 | return m_profileGpu || m_profileCpu || m_profilePixels | m_profileMemory; |
| 233 | } |
| 234 | |
| 235 | void Retracer::setProfiling(bool gpu, bool cpu, bool pixels, bool memory) |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 236 | { |
| 237 | m_profileGpu = gpu; |
| 238 | m_profileCpu = cpu; |
| 239 | m_profilePixels = pixels; |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 240 | m_profileMemory = memory; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 243 | void Retracer::setCaptureAtCallNumber(qlonglong num) |
| 244 | { |
| 245 | m_captureCall = num; |
| 246 | } |
| 247 | |
| 248 | qlonglong Retracer::captureAtCallNumber() const |
| 249 | { |
| 250 | return m_captureCall; |
| 251 | } |
| 252 | |
| 253 | bool Retracer::captureState() const |
| 254 | { |
| 255 | return m_captureState; |
| 256 | } |
| 257 | |
| 258 | void Retracer::setCaptureState(bool enable) |
| 259 | { |
| 260 | m_captureState = enable; |
| 261 | } |
| 262 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 263 | bool Retracer::captureThumbnails() const |
| 264 | { |
| 265 | return m_captureThumbnails; |
| 266 | } |
| 267 | |
| 268 | void Retracer::setCaptureThumbnails(bool enable) |
| 269 | { |
| 270 | m_captureThumbnails = enable; |
| 271 | } |
| 272 | |
Dan McCabe | 8893885 | 2012-06-01 13:40:04 -0700 | [diff] [blame] | 273 | void Retracer::addThumbnailToCapture(qlonglong num) |
| 274 | { |
| 275 | if (!m_thumbnailsToCapture.contains(num)) { |
| 276 | m_thumbnailsToCapture.append(num); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void Retracer::resetThumbnailsToCapture() |
| 281 | { |
| 282 | m_thumbnailsToCapture.clear(); |
| 283 | } |
| 284 | |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 285 | QString Retracer::thumbnailCallSet() const |
Dan McCabe | b14bda2 | 2012-06-01 13:40:06 -0700 | [diff] [blame] | 286 | { |
| 287 | QString callSet; |
| 288 | |
| 289 | bool isFirst = true; |
| 290 | |
| 291 | foreach (qlonglong callIndex, m_thumbnailsToCapture) { |
| 292 | // TODO: detect contiguous ranges |
| 293 | if (!isFirst) { |
| 294 | callSet.append(QLatin1String(",")); |
| 295 | } else { |
| 296 | isFirst = false; |
| 297 | } |
| 298 | |
| 299 | //emit "callIndex" |
| 300 | callSet.append(QString::number(callIndex)); |
| 301 | } |
| 302 | |
| 303 | //qDebug() << QLatin1String("debug: call set to capture: ") << callSet; |
| 304 | return callSet; |
| 305 | } |
Dan McCabe | 8893885 | 2012-06-01 13:40:04 -0700 | [diff] [blame] | 306 | |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 307 | QStringList Retracer::retraceArguments() const |
| 308 | { |
| 309 | QStringList arguments; |
| 310 | |
| 311 | if (m_singlethread) { |
| 312 | arguments << QLatin1String("--singlethread"); |
| 313 | } |
| 314 | |
| 315 | if (m_useCoreProfile) { |
| 316 | arguments << QLatin1String("--core"); |
| 317 | } |
| 318 | |
| 319 | if (m_captureState) { |
| 320 | arguments << QLatin1String("-D"); |
| 321 | arguments << QString::number(m_captureCall); |
| 322 | } else if (m_captureThumbnails) { |
| 323 | if (!m_thumbnailsToCapture.isEmpty()) { |
| 324 | arguments << QLatin1String("-S"); |
| 325 | arguments << thumbnailCallSet(); |
| 326 | } |
| 327 | arguments << QLatin1String("-s"); // emit snapshots |
| 328 | arguments << QLatin1String("-"); // emit to stdout |
| 329 | } else if (isProfiling()) { |
| 330 | if (m_profileGpu) { |
| 331 | arguments << QLatin1String("--pgpu"); |
| 332 | } |
| 333 | |
| 334 | if (m_profileCpu) { |
| 335 | arguments << QLatin1String("--pcpu"); |
| 336 | } |
| 337 | |
| 338 | if (m_profilePixels) { |
| 339 | arguments << QLatin1String("--ppd"); |
| 340 | } |
| 341 | |
| 342 | if (m_profileMemory) { |
| 343 | arguments << QLatin1String("--pmem"); |
| 344 | } |
| 345 | } else { |
| 346 | if (!m_doubleBuffered) { |
| 347 | arguments << QLatin1String("--sb"); |
| 348 | } |
| 349 | |
| 350 | if (m_benchmarking) { |
| 351 | arguments << QLatin1String("-b"); |
| 352 | } else { |
| 353 | arguments << QLatin1String("-d"); |
| 354 | } |
| 355 | } |
| 356 | return arguments; |
| 357 | } |
| 358 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 359 | /** |
| 360 | * Starting point for the retracing thread. |
| 361 | * |
| 362 | * Overrides QThread::run(). |
| 363 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 364 | void Retracer::run() |
| 365 | { |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 366 | QString msg = QLatin1String("Replay finished!"); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 367 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 368 | /* |
| 369 | * Construct command line |
| 370 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 371 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 372 | QString prog; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 373 | QStringList arguments; |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 374 | |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 375 | switch (m_api) { |
| 376 | case trace::API_GL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 377 | prog = QLatin1String("glretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 378 | break; |
| 379 | case trace::API_EGL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 380 | prog = QLatin1String("eglretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 381 | break; |
| 382 | case trace::API_DX: |
| 383 | case trace::API_D3D7: |
| 384 | case trace::API_D3D8: |
| 385 | case trace::API_D3D9: |
José Fonseca | e51e22f | 2012-12-07 07:48:10 +0000 | [diff] [blame] | 386 | case trace::API_DXGI: |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 387 | #ifdef Q_OS_WIN |
| 388 | prog = QLatin1String("d3dretrace"); |
| 389 | #else |
| 390 | prog = QLatin1String("wine"); |
| 391 | arguments << QLatin1String("d3dretrace.exe"); |
| 392 | #endif |
| 393 | break; |
| 394 | default: |
José Fonseca | 6796438 | 2012-03-27 23:54:30 +0100 | [diff] [blame] | 395 | emit finished(QLatin1String("Unsupported API")); |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 399 | arguments << retraceArguments() << m_fileName; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 400 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 401 | /* |
Carl Worth | 7257dfc | 2012-08-09 08:21:42 -0700 | [diff] [blame] | 402 | * 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é Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 412 | * Start the process. |
| 413 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 414 | |
José Fonseca | 11ad0ea | 2014-11-15 09:58:44 +0000 | [diff] [blame] | 415 | { |
| 416 | QDebug debug(QtDebugMsg); |
| 417 | debug << "Running:"; |
| 418 | debug << prog; |
| 419 | foreach (const QString &argument, arguments) { |
| 420 | debug << argument; |
| 421 | } |
| 422 | } |
| 423 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 424 | QProcess process; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 425 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 426 | process.start(prog, arguments, QIODevice::ReadOnly); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 427 | if (!process.waitForStarted(-1)) { |
| 428 | emit finished(QLatin1String("Could not start process")); |
| 429 | return; |
| 430 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 431 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 432 | /* |
| 433 | * Process standard output |
| 434 | */ |
| 435 | |
Dan McCabe | c6f924e | 2012-06-01 13:40:05 -0700 | [diff] [blame] | 436 | ImageHash thumbnails; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 437 | QVariantMap parsedJson; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 438 | trace::Profile* profile = NULL; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 439 | |
| 440 | process.setReadChannel(QProcess::StandardOutput); |
| 441 | if (process.waitForReadyRead(-1)) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 442 | BlockingIODevice io(&process); |
| 443 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 444 | if (m_captureState) { |
José Fonseca | 95b4056 | 2012-04-05 20:06:42 +0100 | [diff] [blame] | 445 | process.waitForFinished(-1); |
José Fonseca | 3f4cd30 | 2015-01-14 12:02:06 +0000 | [diff] [blame] | 446 | QByteArray data = process.readAll(); |
| 447 | QJsonParseError error; |
| 448 | QJsonDocument jsonDoc = |
| 449 | QJsonDocument::fromJson(data, &error); |
| 450 | |
| 451 | if (error.error != QJsonParseError::NoError) { |
| 452 | //qDebug()<<"Error is "<<error.errorString(); |
| 453 | msg = error.errorString(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 454 | } |
José Fonseca | 3f4cd30 | 2015-01-14 12:02:06 +0000 | [diff] [blame] | 455 | parsedJson = jsonDoc.toVariant().toMap(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 456 | } else if (m_captureThumbnails) { |
| 457 | /* |
| 458 | * Parse concatenated PNM images from output. |
| 459 | */ |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 460 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 461 | while (!io.atEnd()) { |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 462 | image::PNMInfo info; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 463 | |
| 464 | char header[512]; |
| 465 | qint64 headerSize = 0; |
| 466 | int headerLines = 3; // assume no optional comment line |
| 467 | |
| 468 | for (int headerLine = 0; headerLine < headerLines; ++headerLine) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 469 | qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 470 | |
| 471 | // if header actually contains optional comment line, ... |
| 472 | if (headerLine == 1 && header[headerSize] == '#') { |
| 473 | ++headerLines; |
| 474 | } |
| 475 | |
| 476 | headerSize += headerRead; |
| 477 | } |
| 478 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 479 | const char *headerEnd = image::readPNMHeader(header, headerSize, info); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 480 | |
| 481 | // if invalid PNM header was encountered, ... |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 482 | if (headerEnd == NULL || |
| 483 | info.channelType != image::TYPE_UNORM8) { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 484 | qDebug() << "error: invalid snapshot stream encountered"; |
| 485 | break; |
| 486 | } |
| 487 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 488 | unsigned channels = info.channels; |
| 489 | unsigned width = info.width; |
| 490 | unsigned height = info.height; |
| 491 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 492 | // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height"; |
| 493 | |
| 494 | QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888); |
| 495 | |
| 496 | int rowBytes = channels * width; |
| 497 | for (int y = 0; y < height; ++y) { |
| 498 | unsigned char *scanLine = snapshot.scanLine(y); |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 499 | qint64 readBytes = io.read((char *) scanLine, rowBytes); |
| 500 | Q_ASSERT(readBytes == rowBytes); |
José Fonseca | a2bf287 | 2012-11-15 13:35:22 +0000 | [diff] [blame] | 501 | (void)readBytes; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 502 | } |
| 503 | |
José Fonseca | dc9e9c6 | 2012-03-26 10:29:32 +0100 | [diff] [blame] | 504 | QImage thumb = thumbnail(snapshot); |
Dan McCabe | c6f924e | 2012-06-01 13:40:05 -0700 | [diff] [blame] | 505 | thumbnails.insert(info.commentNumber, thumb); |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 506 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 507 | |
| 508 | Q_ASSERT(process.state() != QProcess::Running); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 509 | } else if (isProfiling()) { |
| 510 | profile = new trace::Profile(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 511 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 512 | while (!io.atEnd()) { |
| 513 | char line[256]; |
| 514 | qint64 lineLength; |
| 515 | |
| 516 | lineLength = io.readLine(line, 256); |
| 517 | |
| 518 | if (lineLength == -1) |
| 519 | break; |
| 520 | |
| 521 | trace::Profiler::parseLine(line, profile); |
| 522 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 523 | } else { |
José Fonseca | 676cd17 | 2012-03-24 09:46:24 +0000 | [diff] [blame] | 524 | QByteArray output; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 525 | output = process.readAllStandardOutput(); |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 526 | if (output.length() < 80) { |
| 527 | msg = QString::fromUtf8(output); |
| 528 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 529 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 530 | } |
| 531 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 532 | /* |
| 533 | * Wait for process termination |
| 534 | */ |
| 535 | |
| 536 | process.waitForFinished(-1); |
| 537 | |
| 538 | if (process.exitStatus() != QProcess::NormalExit) { |
| 539 | msg = QLatin1String("Process crashed"); |
| 540 | } else if (process.exitCode() != 0) { |
| 541 | msg = QLatin1String("Process exited with non zero exit code"); |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * Parse errors. |
| 546 | */ |
| 547 | |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 548 | QList<ApiTraceError> errors; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 549 | process.setReadChannel(QProcess::StandardError); |
José Fonseca | 8fdf56c | 2012-03-24 10:06:56 +0000 | [diff] [blame] | 550 | QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$"); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 551 | while (!process.atEnd()) { |
| 552 | QString line = process.readLine(); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 553 | if (regexp.indexIn(line) != -1) { |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 554 | ApiTraceError error; |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 555 | error.callIndex = regexp.cap(1).toInt(); |
| 556 | error.type = regexp.cap(2); |
| 557 | error.message = regexp.cap(3); |
| 558 | errors.append(error); |
gregory | f2329b6 | 2012-07-06 21:48:59 +0200 | [diff] [blame] | 559 | } else if (!errors.isEmpty()) { |
| 560 | // Probably a multiligne message |
| 561 | ApiTraceError &previous = errors.last(); |
| 562 | if (line.endsWith("\n")) { |
| 563 | line.chop(1); |
| 564 | } |
| 565 | previous.message.append('\n'); |
| 566 | previous.message.append(line); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 567 | } |
| 568 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 569 | |
| 570 | /* |
| 571 | * Emit signals |
| 572 | */ |
| 573 | |
| 574 | if (m_captureState) { |
| 575 | ApiTraceState *state = new ApiTraceState(parsedJson); |
| 576 | emit foundState(state); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (m_captureThumbnails && !thumbnails.isEmpty()) { |
| 580 | emit foundThumbnails(thumbnails); |
| 581 | } |
| 582 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 583 | if (isProfiling() && profile) { |
| 584 | emit foundProfile(profile); |
| 585 | } |
| 586 | |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 587 | if (!errors.isEmpty()) { |
| 588 | emit retraceErrors(errors); |
| 589 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 590 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 591 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 592 | } |
| 593 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 594 | #include "retracer.moc" |