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 | |
Jose Fonseca | bceafec | 2016-05-05 11:09:52 +0100 | [diff] [blame] | 6 | #include "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); |
Jose Fonseca | 010f996 | 2016-03-05 14:45:41 +0000 | [diff] [blame] | 43 | bool isSequential() const override; |
| 44 | bool atEnd() const override; |
| 45 | bool waitForReadyRead(int msecs = -1) override; |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 46 | |
| 47 | protected: |
Jose Fonseca | 010f996 | 2016-03-05 14:45:41 +0000 | [diff] [blame] | 48 | qint64 readData(char * data, qint64 maxSize) override; |
| 49 | qint64 writeData(const char * data, qint64 maxSize) override; |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 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), |
Gabe Dagani | 9ff4405 | 2017-11-28 09:30:15 -0600 | [diff] [blame] | 136 | m_msaaResolve(true), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 137 | m_captureState(false), |
José Fonseca | c03e4b0 | 2014-05-30 17:24:42 +0100 | [diff] [blame] | 138 | m_captureThumbnails(false), |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 139 | m_captureCall(0), |
| 140 | m_profileGpu(false), |
| 141 | m_profileCpu(false), |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 142 | m_profilePixels(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::isProfiling() const |
| 228 | { |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 229 | return m_profileGpu || m_profileCpu || m_profilePixels; |
BogDan Vatra | a9f9e64 | 2015-02-10 14:31:17 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 232 | void Retracer::setProfiling(bool gpu, bool cpu, bool pixels) |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 233 | { |
| 234 | m_profileGpu = gpu; |
| 235 | m_profileCpu = cpu; |
| 236 | m_profilePixels = pixels; |
| 237 | } |
| 238 | |
Gabe Dagani | 9ff4405 | 2017-11-28 09:30:15 -0600 | [diff] [blame] | 239 | bool Retracer::isMsaaResolve() const |
| 240 | { |
| 241 | return m_msaaResolve; |
| 242 | } |
| 243 | |
| 244 | void Retracer::setMsaaResolve(bool resolve) |
| 245 | { |
| 246 | m_msaaResolve = resolve; |
| 247 | } |
| 248 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 249 | void Retracer::setCaptureAtCallNumber(qlonglong num) |
| 250 | { |
| 251 | m_captureCall = num; |
| 252 | } |
| 253 | |
| 254 | qlonglong Retracer::captureAtCallNumber() const |
| 255 | { |
| 256 | return m_captureCall; |
| 257 | } |
| 258 | |
| 259 | bool Retracer::captureState() const |
| 260 | { |
| 261 | return m_captureState; |
| 262 | } |
| 263 | |
| 264 | void Retracer::setCaptureState(bool enable) |
| 265 | { |
| 266 | m_captureState = enable; |
| 267 | } |
| 268 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 269 | bool Retracer::captureThumbnails() const |
| 270 | { |
| 271 | return m_captureThumbnails; |
| 272 | } |
| 273 | |
| 274 | void Retracer::setCaptureThumbnails(bool enable) |
| 275 | { |
| 276 | m_captureThumbnails = enable; |
| 277 | } |
| 278 | |
Dan McCabe | 8893885 | 2012-06-01 13:40:04 -0700 | [diff] [blame] | 279 | void Retracer::addThumbnailToCapture(qlonglong num) |
| 280 | { |
| 281 | if (!m_thumbnailsToCapture.contains(num)) { |
| 282 | m_thumbnailsToCapture.append(num); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void Retracer::resetThumbnailsToCapture() |
| 287 | { |
| 288 | m_thumbnailsToCapture.clear(); |
| 289 | } |
| 290 | |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 291 | QString Retracer::thumbnailCallSet() |
Dan McCabe | b14bda2 | 2012-06-01 13:40:06 -0700 | [diff] [blame] | 292 | { |
| 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 McCabe | 8893885 | 2012-06-01 13:40:04 -0700 | [diff] [blame] | 312 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 313 | /** |
| 314 | * Starting point for the retracing thread. |
| 315 | * |
| 316 | * Overrides QThread::run(). |
| 317 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 318 | void Retracer::run() |
| 319 | { |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 320 | QString msg = QLatin1String("Replay finished!"); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 321 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 322 | /* |
| 323 | * Construct command line |
| 324 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 325 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 326 | QString prog; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 327 | QStringList arguments; |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 328 | |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 329 | switch (m_api) { |
| 330 | case trace::API_GL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 331 | prog = QLatin1String("glretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 332 | break; |
| 333 | case trace::API_EGL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 334 | prog = QLatin1String("eglretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 335 | break; |
| 336 | case trace::API_DX: |
| 337 | case trace::API_D3D7: |
| 338 | case trace::API_D3D8: |
| 339 | case trace::API_D3D9: |
José Fonseca | e51e22f | 2012-12-07 07:48:10 +0000 | [diff] [blame] | 340 | case trace::API_DXGI: |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 341 | #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é Fonseca | 6796438 | 2012-03-27 23:54:30 +0100 | [diff] [blame] | 349 | emit finished(QLatin1String("Unsupported API")); |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 353 | if (m_singlethread) { |
| 354 | arguments << QLatin1String("--singlethread"); |
| 355 | } |
| 356 | |
| 357 | if (m_useCoreProfile) { |
| 358 | arguments << QLatin1String("--core"); |
| 359 | } |
| 360 | |
Gabe Dagani | 9ff4405 | 2017-11-28 09:30:15 -0600 | [diff] [blame] | 361 | if (!m_msaaResolve) { |
| 362 | arguments << QLatin1String("--msaa-no-resolve"); |
| 363 | } |
| 364 | |
Jose Fonseca | 65d2605 | 2017-07-30 13:48:40 +0100 | [diff] [blame] | 365 | 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 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) { |
Jose Fonseca | 08fdb7b | 2015-05-21 21:41:50 +0100 | [diff] [blame] | 445 | parsedJson = decodeUBJSONObject(&io).toMap(); |
José Fonseca | 95b4056 | 2012-04-05 20:06:42 +0100 | [diff] [blame] | 446 | process.waitForFinished(-1); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 447 | } else if (m_captureThumbnails) { |
| 448 | /* |
| 449 | * Parse concatenated PNM images from output. |
| 450 | */ |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 451 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 452 | while (!io.atEnd()) { |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 453 | image::PNMInfo info; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 454 | |
| 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é Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 460 | qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 461 | |
| 462 | // if header actually contains optional comment line, ... |
| 463 | if (headerLine == 1 && header[headerSize] == '#') { |
| 464 | ++headerLines; |
| 465 | } |
| 466 | |
| 467 | headerSize += headerRead; |
| 468 | } |
| 469 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 470 | const char *headerEnd = image::readPNMHeader(header, headerSize, info); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 471 | |
| 472 | // if invalid PNM header was encountered, ... |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 473 | if (headerEnd == NULL || |
| 474 | info.channelType != image::TYPE_UNORM8) { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 475 | qDebug() << "error: invalid snapshot stream encountered"; |
| 476 | break; |
| 477 | } |
| 478 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 479 | unsigned channels = info.channels; |
| 480 | unsigned width = info.width; |
| 481 | unsigned height = info.height; |
| 482 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 483 | // 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é Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 490 | qint64 readBytes = io.read((char *) scanLine, rowBytes); |
| 491 | Q_ASSERT(readBytes == rowBytes); |
José Fonseca | a2bf287 | 2012-11-15 13:35:22 +0000 | [diff] [blame] | 492 | (void)readBytes; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 493 | } |
| 494 | |
José Fonseca | dc9e9c6 | 2012-03-26 10:29:32 +0100 | [diff] [blame] | 495 | QImage thumb = thumbnail(snapshot); |
Dan McCabe | c6f924e | 2012-06-01 13:40:05 -0700 | [diff] [blame] | 496 | thumbnails.insert(info.commentNumber, thumb); |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 497 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 498 | |
| 499 | Q_ASSERT(process.state() != QProcess::Running); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 500 | } else if (isProfiling()) { |
| 501 | profile = new trace::Profile(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 502 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 503 | 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é Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 514 | } else { |
José Fonseca | 676cd17 | 2012-03-24 09:46:24 +0000 | [diff] [blame] | 515 | QByteArray output; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 516 | output = process.readAllStandardOutput(); |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 517 | if (output.length() < 80) { |
| 518 | msg = QString::fromUtf8(output); |
| 519 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 520 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 521 | } |
| 522 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 523 | /* |
| 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 Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 539 | QList<ApiTraceError> errors; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 540 | process.setReadChannel(QProcess::StandardError); |
José Fonseca | 8fdf56c | 2012-03-24 10:06:56 +0000 | [diff] [blame] | 541 | QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$"); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 542 | while (!process.atEnd()) { |
| 543 | QString line = process.readLine(); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 544 | if (regexp.indexIn(line) != -1) { |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 545 | ApiTraceError error; |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 546 | error.callIndex = regexp.cap(1).toInt(); |
| 547 | error.type = regexp.cap(2); |
| 548 | error.message = regexp.cap(3); |
| 549 | errors.append(error); |
gregory | f2329b6 | 2012-07-06 21:48:59 +0200 | [diff] [blame] | 550 | } 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 Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 558 | } |
| 559 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * Emit signals |
| 563 | */ |
| 564 | |
| 565 | if (m_captureState) { |
| 566 | ApiTraceState *state = new ApiTraceState(parsedJson); |
| 567 | emit foundState(state); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | if (m_captureThumbnails && !thumbnails.isEmpty()) { |
| 571 | emit foundThumbnails(thumbnails); |
| 572 | } |
| 573 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 574 | if (isProfiling() && profile) { |
| 575 | emit foundProfile(profile); |
| 576 | } |
| 577 | |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 578 | if (!errors.isEmpty()) { |
| 579 | emit retraceErrors(errors); |
| 580 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 581 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 582 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 583 | } |
| 584 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 585 | #include "retracer.moc" |