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 | |
Dan McCabe | 8893885 | 2012-06-01 13:40:04 -0700 | [diff] [blame] | 266 | void Retracer::addThumbnailToCapture(qlonglong num) |
| 267 | { |
| 268 | if (!m_thumbnailsToCapture.contains(num)) { |
| 269 | m_thumbnailsToCapture.append(num); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void Retracer::resetThumbnailsToCapture() |
| 274 | { |
| 275 | m_thumbnailsToCapture.clear(); |
| 276 | } |
| 277 | |
| 278 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 279 | /** |
| 280 | * Starting point for the retracing thread. |
| 281 | * |
| 282 | * Overrides QThread::run(). |
| 283 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 284 | void Retracer::run() |
| 285 | { |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 286 | QString msg = QLatin1String("Replay finished!"); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 287 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 288 | /* |
| 289 | * Construct command line |
| 290 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 291 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 292 | QString prog; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 293 | QStringList arguments; |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 294 | |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 295 | switch (m_api) { |
| 296 | case trace::API_GL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 297 | prog = QLatin1String("glretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 298 | break; |
| 299 | case trace::API_EGL: |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 300 | prog = QLatin1String("eglretrace"); |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 301 | break; |
| 302 | case trace::API_DX: |
| 303 | case trace::API_D3D7: |
| 304 | case trace::API_D3D8: |
| 305 | case trace::API_D3D9: |
José Fonseca | e51e22f | 2012-12-07 07:48:10 +0000 | [diff] [blame] | 306 | case trace::API_DXGI: |
José Fonseca | 889d32c | 2012-04-23 10:18:28 +0100 | [diff] [blame] | 307 | #ifdef Q_OS_WIN |
| 308 | prog = QLatin1String("d3dretrace"); |
| 309 | #else |
| 310 | prog = QLatin1String("wine"); |
| 311 | arguments << QLatin1String("d3dretrace.exe"); |
| 312 | #endif |
| 313 | break; |
| 314 | default: |
José Fonseca | 6796438 | 2012-03-27 23:54:30 +0100 | [diff] [blame] | 315 | emit finished(QLatin1String("Unsupported API")); |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Peter Lohrmann | b34c675 | 2013-07-10 11:08:14 -0400 | [diff] [blame] | 319 | if (m_singlethread) { |
| 320 | arguments << QLatin1String("--singlethread"); |
| 321 | } |
| 322 | |
Corey Richardson | f300646 | 2014-01-26 17:15:42 -0500 | [diff] [blame] | 323 | if (m_useCoreProfile) { |
| 324 | arguments << QLatin1String("--core"); |
| 325 | } |
| 326 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 327 | if (m_captureState) { |
| 328 | arguments << QLatin1String("-D"); |
| 329 | arguments << QString::number(m_captureCall); |
| 330 | } else if (m_captureThumbnails) { |
| 331 | arguments << QLatin1String("-s"); // emit snapshots |
| 332 | arguments << QLatin1String("-"); // emit to stdout |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 333 | } else if (isProfiling()) { |
| 334 | if (m_profileGpu) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 335 | arguments << QLatin1String("--pgpu"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (m_profileCpu) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 339 | arguments << QLatin1String("--pcpu"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (m_profilePixels) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 343 | arguments << QLatin1String("--ppd"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 344 | } |
| 345 | } else { |
Jose Fonseca | c4f9daa | 2015-01-25 17:09:13 +0000 | [diff] [blame] | 346 | if (!m_doubleBuffered) { |
José Fonseca | bce31f6 | 2012-11-14 07:21:01 +0000 | [diff] [blame] | 347 | arguments << QLatin1String("--sb"); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (m_benchmarking) { |
| 351 | arguments << QLatin1String("-b"); |
| 352 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | arguments << m_fileName; |
| 356 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 357 | /* |
Carl Worth | 7257dfc | 2012-08-09 08:21:42 -0700 | [diff] [blame] | 358 | * Support remote execution on a separate target. |
| 359 | */ |
| 360 | |
| 361 | if (m_remoteTarget.length() != 0) { |
| 362 | arguments.prepend(prog); |
| 363 | arguments.prepend(m_remoteTarget); |
| 364 | prog = QLatin1String("ssh"); |
| 365 | } |
| 366 | |
| 367 | /* |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 368 | * Start the process. |
| 369 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 370 | |
José Fonseca | 11ad0ea | 2014-11-15 09:58:44 +0000 | [diff] [blame] | 371 | { |
| 372 | QDebug debug(QtDebugMsg); |
| 373 | debug << "Running:"; |
| 374 | debug << prog; |
| 375 | foreach (const QString &argument, arguments) { |
| 376 | debug << argument; |
| 377 | } |
| 378 | } |
| 379 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 380 | QProcess process; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 381 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 382 | process.start(prog, arguments, QIODevice::ReadOnly); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 383 | if (!process.waitForStarted(-1)) { |
| 384 | emit finished(QLatin1String("Could not start process")); |
| 385 | return; |
| 386 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 387 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 388 | /* |
| 389 | * Process standard output |
| 390 | */ |
| 391 | |
Dan McCabe | c6f924e | 2012-06-01 13:40:05 -0700 | [diff] [blame^] | 392 | ImageHash thumbnails; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 393 | QVariantMap parsedJson; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 394 | trace::Profile* profile = NULL; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 395 | |
| 396 | process.setReadChannel(QProcess::StandardOutput); |
| 397 | if (process.waitForReadyRead(-1)) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 398 | BlockingIODevice io(&process); |
| 399 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 400 | if (m_captureState) { |
José Fonseca | 95b4056 | 2012-04-05 20:06:42 +0100 | [diff] [blame] | 401 | process.waitForFinished(-1); |
José Fonseca | 3f4cd30 | 2015-01-14 12:02:06 +0000 | [diff] [blame] | 402 | QByteArray data = process.readAll(); |
| 403 | QJsonParseError error; |
| 404 | QJsonDocument jsonDoc = |
| 405 | QJsonDocument::fromJson(data, &error); |
| 406 | |
| 407 | if (error.error != QJsonParseError::NoError) { |
| 408 | //qDebug()<<"Error is "<<error.errorString(); |
| 409 | msg = error.errorString(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 410 | } |
José Fonseca | 3f4cd30 | 2015-01-14 12:02:06 +0000 | [diff] [blame] | 411 | parsedJson = jsonDoc.toVariant().toMap(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 412 | } else if (m_captureThumbnails) { |
| 413 | /* |
| 414 | * Parse concatenated PNM images from output. |
| 415 | */ |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 416 | |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 417 | while (!io.atEnd()) { |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 418 | image::PNMInfo info; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 419 | |
| 420 | char header[512]; |
| 421 | qint64 headerSize = 0; |
| 422 | int headerLines = 3; // assume no optional comment line |
| 423 | |
| 424 | for (int headerLine = 0; headerLine < headerLines; ++headerLine) { |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 425 | qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 426 | |
| 427 | // if header actually contains optional comment line, ... |
| 428 | if (headerLine == 1 && header[headerSize] == '#') { |
| 429 | ++headerLines; |
| 430 | } |
| 431 | |
| 432 | headerSize += headerRead; |
| 433 | } |
| 434 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 435 | const char *headerEnd = image::readPNMHeader(header, headerSize, info); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 436 | |
| 437 | // if invalid PNM header was encountered, ... |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 438 | if (headerEnd == NULL || |
| 439 | info.channelType != image::TYPE_UNORM8) { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 440 | qDebug() << "error: invalid snapshot stream encountered"; |
| 441 | break; |
| 442 | } |
| 443 | |
José Fonseca | beda444 | 2013-09-12 17:25:04 +0100 | [diff] [blame] | 444 | unsigned channels = info.channels; |
| 445 | unsigned width = info.width; |
| 446 | unsigned height = info.height; |
| 447 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 448 | // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height"; |
| 449 | |
| 450 | QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888); |
| 451 | |
| 452 | int rowBytes = channels * width; |
| 453 | for (int y = 0; y < height; ++y) { |
| 454 | unsigned char *scanLine = snapshot.scanLine(y); |
José Fonseca | 6ea8dee | 2012-03-25 17:25:24 +0100 | [diff] [blame] | 455 | qint64 readBytes = io.read((char *) scanLine, rowBytes); |
| 456 | Q_ASSERT(readBytes == rowBytes); |
José Fonseca | a2bf287 | 2012-11-15 13:35:22 +0000 | [diff] [blame] | 457 | (void)readBytes; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 458 | } |
| 459 | |
José Fonseca | dc9e9c6 | 2012-03-26 10:29:32 +0100 | [diff] [blame] | 460 | QImage thumb = thumbnail(snapshot); |
Dan McCabe | c6f924e | 2012-06-01 13:40:05 -0700 | [diff] [blame^] | 461 | thumbnails.insert(info.commentNumber, thumb); |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 462 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 463 | |
| 464 | Q_ASSERT(process.state() != QProcess::Running); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 465 | } else if (isProfiling()) { |
| 466 | profile = new trace::Profile(); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 467 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 468 | while (!io.atEnd()) { |
| 469 | char line[256]; |
| 470 | qint64 lineLength; |
| 471 | |
| 472 | lineLength = io.readLine(line, 256); |
| 473 | |
| 474 | if (lineLength == -1) |
| 475 | break; |
| 476 | |
| 477 | trace::Profiler::parseLine(line, profile); |
| 478 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 479 | } else { |
José Fonseca | 676cd17 | 2012-03-24 09:46:24 +0000 | [diff] [blame] | 480 | QByteArray output; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 481 | output = process.readAllStandardOutput(); |
José Fonseca | 126f64b | 2012-03-28 00:13:55 +0100 | [diff] [blame] | 482 | if (output.length() < 80) { |
| 483 | msg = QString::fromUtf8(output); |
| 484 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 485 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 486 | } |
| 487 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 488 | /* |
| 489 | * Wait for process termination |
| 490 | */ |
| 491 | |
| 492 | process.waitForFinished(-1); |
| 493 | |
| 494 | if (process.exitStatus() != QProcess::NormalExit) { |
| 495 | msg = QLatin1String("Process crashed"); |
| 496 | } else if (process.exitCode() != 0) { |
| 497 | msg = QLatin1String("Process exited with non zero exit code"); |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Parse errors. |
| 502 | */ |
| 503 | |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 504 | QList<ApiTraceError> errors; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 505 | process.setReadChannel(QProcess::StandardError); |
José Fonseca | 8fdf56c | 2012-03-24 10:06:56 +0000 | [diff] [blame] | 506 | QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$"); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 507 | while (!process.atEnd()) { |
| 508 | QString line = process.readLine(); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 509 | if (regexp.indexIn(line) != -1) { |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 510 | ApiTraceError error; |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 511 | error.callIndex = regexp.cap(1).toInt(); |
| 512 | error.type = regexp.cap(2); |
| 513 | error.message = regexp.cap(3); |
| 514 | errors.append(error); |
gregory | f2329b6 | 2012-07-06 21:48:59 +0200 | [diff] [blame] | 515 | } else if (!errors.isEmpty()) { |
| 516 | // Probably a multiligne message |
| 517 | ApiTraceError &previous = errors.last(); |
| 518 | if (line.endsWith("\n")) { |
| 519 | line.chop(1); |
| 520 | } |
| 521 | previous.message.append('\n'); |
| 522 | previous.message.append(line); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 523 | } |
| 524 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * Emit signals |
| 528 | */ |
| 529 | |
| 530 | if (m_captureState) { |
| 531 | ApiTraceState *state = new ApiTraceState(parsedJson); |
| 532 | emit foundState(state); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if (m_captureThumbnails && !thumbnails.isEmpty()) { |
| 536 | emit foundThumbnails(thumbnails); |
| 537 | } |
| 538 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 539 | if (isProfiling() && profile) { |
| 540 | emit foundProfile(profile); |
| 541 | } |
| 542 | |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 543 | if (!errors.isEmpty()) { |
| 544 | emit retraceErrors(errors); |
| 545 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame] | 546 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 547 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 550 | #include "retracer.moc" |