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