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