Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 1 | #include "retracer.h" |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 2 | #include <iostream> |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 3 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 4 | #include "apitracecall.h" |
| 5 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 6 | #include "image.hpp" |
| 7 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 8 | #include <QDebug> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 9 | #include <QVariant> |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 10 | #include <QList> |
| 11 | #include <QImage> |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 12 | |
| 13 | #include <qjson/parser.h> |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 14 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 15 | Q_DECLARE_METATYPE(QList<ApiTraceError>); |
| 16 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 17 | Retracer::Retracer(QObject *parent) |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 18 | : QThread(parent), |
Zack Rusin | 404a1ef | 2011-04-19 23:49:56 -0400 | [diff] [blame] | 19 | m_benchmarking(false), |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 20 | m_doubleBuffered(true), |
| 21 | m_captureState(false), |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 22 | m_captureCall(0) |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 23 | { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 24 | qRegisterMetaType<QList<ApiTraceError> >(); |
| 25 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 26 | #ifdef Q_OS_WIN |
| 27 | QString format = QLatin1String("%1;"); |
| 28 | #else |
| 29 | QString format = QLatin1String("%1:"); |
| 30 | #endif |
José Fonseca | 2744092 | 2011-11-01 08:27:12 +0000 | [diff] [blame] | 31 | QString buildPath = format.arg(APITRACE_BINARY_DIR); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 32 | m_processEnvironment = QProcessEnvironment::systemEnvironment(); |
| 33 | m_processEnvironment.insert("PATH", buildPath + |
| 34 | m_processEnvironment.value("PATH")); |
| 35 | |
| 36 | qputenv("PATH", |
| 37 | m_processEnvironment.value("PATH").toLatin1()); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | QString Retracer::fileName() const |
| 41 | { |
| 42 | return m_fileName; |
| 43 | } |
| 44 | |
| 45 | void Retracer::setFileName(const QString &name) |
| 46 | { |
| 47 | m_fileName = name; |
| 48 | } |
| 49 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 50 | void Retracer::setAPI(trace::API api) |
| 51 | { |
| 52 | m_api = api; |
| 53 | } |
| 54 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 55 | bool Retracer::isBenchmarking() const |
| 56 | { |
| 57 | return m_benchmarking; |
| 58 | } |
| 59 | |
| 60 | void Retracer::setBenchmarking(bool bench) |
| 61 | { |
| 62 | m_benchmarking = bench; |
| 63 | } |
| 64 | |
| 65 | bool Retracer::isDoubleBuffered() const |
| 66 | { |
| 67 | return m_doubleBuffered; |
| 68 | } |
| 69 | |
| 70 | void Retracer::setDoubleBuffered(bool db) |
| 71 | { |
| 72 | m_doubleBuffered = db; |
| 73 | } |
| 74 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 75 | void Retracer::setCaptureAtCallNumber(qlonglong num) |
| 76 | { |
| 77 | m_captureCall = num; |
| 78 | } |
| 79 | |
| 80 | qlonglong Retracer::captureAtCallNumber() const |
| 81 | { |
| 82 | return m_captureCall; |
| 83 | } |
| 84 | |
| 85 | bool Retracer::captureState() const |
| 86 | { |
| 87 | return m_captureState; |
| 88 | } |
| 89 | |
| 90 | void Retracer::setCaptureState(bool enable) |
| 91 | { |
| 92 | m_captureState = enable; |
| 93 | } |
| 94 | |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 95 | bool Retracer::captureThumbnails() const |
| 96 | { |
| 97 | return m_captureThumbnails; |
| 98 | } |
| 99 | |
| 100 | void Retracer::setCaptureThumbnails(bool enable) |
| 101 | { |
| 102 | m_captureThumbnails = enable; |
| 103 | } |
| 104 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 105 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 106 | /** |
| 107 | * Starting point for the retracing thread. |
| 108 | * |
| 109 | * Overrides QThread::run(). |
| 110 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 111 | void Retracer::run() |
| 112 | { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 113 | QString msg; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 114 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 115 | /* |
| 116 | * Construct command line |
| 117 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 118 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 119 | QString prog; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 120 | QStringList arguments; |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 121 | |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 122 | if (m_api == trace::API_GL) { |
| 123 | prog = QLatin1String("glretrace"); |
| 124 | } else if (m_api == trace::API_EGL) { |
| 125 | prog = QLatin1String("eglretrace"); |
| 126 | } else { |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 127 | Q_ASSERT(0); |
José Fonseca | 62997b4 | 2011-11-27 15:16:34 +0000 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 131 | if (m_doubleBuffered) { |
| 132 | arguments << QLatin1String("-db"); |
José Fonseca | 1872d96 | 2011-06-01 19:49:13 +0100 | [diff] [blame] | 133 | } else { |
| 134 | arguments << QLatin1String("-sb"); |
Zack Rusin | 16ae036 | 2011-04-11 21:30:04 -0400 | [diff] [blame] | 135 | } |
| 136 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 137 | if (m_captureState) { |
| 138 | arguments << QLatin1String("-D"); |
| 139 | arguments << QString::number(m_captureCall); |
| 140 | } else if (m_captureThumbnails) { |
| 141 | arguments << QLatin1String("-s"); // emit snapshots |
| 142 | arguments << QLatin1String("-"); // emit to stdout |
| 143 | } else if (m_benchmarking) { |
| 144 | arguments << QLatin1String("-b"); |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | arguments << m_fileName; |
| 148 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 149 | /* |
| 150 | * Start the process. |
| 151 | */ |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 152 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 153 | QProcess process; |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 154 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 155 | process.start(prog, arguments); |
| 156 | if (!process.waitForStarted(-1)) { |
| 157 | emit finished(QLatin1String("Could not start process")); |
| 158 | return; |
| 159 | } |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 160 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 161 | /* |
| 162 | * Process standard output |
| 163 | */ |
| 164 | |
| 165 | QList<QImage> thumbnails; |
| 166 | QVariantMap parsedJson; |
| 167 | |
| 168 | process.setReadChannel(QProcess::StandardOutput); |
| 169 | if (process.waitForReadyRead(-1)) { |
| 170 | if (m_captureState) { |
| 171 | /* |
| 172 | * Parse JSON from the output. |
| 173 | * |
| 174 | * XXX: QJSON does not wait for QIODevice::waitForReadyRead so we |
| 175 | * need to buffer all stdout. |
| 176 | * |
| 177 | * XXX: QJSON's scanner is inneficient as it abuses single |
| 178 | * character QIODevice::peek (not cheap), instead of maintaining a |
| 179 | * lookahead character on its own. |
| 180 | */ |
| 181 | |
| 182 | if (!process.waitForFinished(-1)) { |
| 183 | return; |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 184 | } |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 185 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 186 | bool ok = false; |
| 187 | QJson::Parser jsonParser; |
| 188 | parsedJson = jsonParser.parse(&process, &ok).toMap(); |
| 189 | if (!ok) { |
| 190 | msg = QLatin1String("failed to parse JSON"); |
| 191 | } |
| 192 | } else if (m_captureThumbnails) { |
| 193 | /* |
| 194 | * Parse concatenated PNM images from output. |
| 195 | */ |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 196 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 197 | while (true) { |
| 198 | /* |
| 199 | * QProcess::atEnd() documentation is wrong -- it will return |
| 200 | * true even when the process is running --, so try to handle |
| 201 | * that here. |
| 202 | */ |
| 203 | if (process.atEnd()) { |
| 204 | if (process.state() == QProcess::Running) { |
| 205 | if (!process.waitForReadyRead(-1)) { |
| 206 | break; |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 207 | } |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 208 | } |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 209 | } |
| 210 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 211 | unsigned channels = 0; |
| 212 | unsigned width = 0; |
| 213 | unsigned height = 0; |
| 214 | |
| 215 | char header[512]; |
| 216 | qint64 headerSize = 0; |
| 217 | int headerLines = 3; // assume no optional comment line |
| 218 | |
| 219 | for (int headerLine = 0; headerLine < headerLines; ++headerLine) { |
| 220 | while (!process.canReadLine()) { |
| 221 | if (!process.waitForReadyRead(-1)) { |
| 222 | qDebug() << "QProcess::waitForReadyRead failed"; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | qint64 headerRead = process.readLine(&header[headerSize], sizeof(header) - headerSize); |
| 228 | |
| 229 | // if header actually contains optional comment line, ... |
| 230 | if (headerLine == 1 && header[headerSize] == '#') { |
| 231 | ++headerLines; |
| 232 | } |
| 233 | |
| 234 | headerSize += headerRead; |
| 235 | } |
| 236 | |
| 237 | const char *headerEnd = image::readPNMHeader(header, headerSize, &channels, &width, &height); |
| 238 | |
| 239 | // if invalid PNM header was encountered, ... |
| 240 | if (header == headerEnd) { |
| 241 | qDebug() << "error: invalid snapshot stream encountered"; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height"; |
| 246 | |
| 247 | QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888); |
| 248 | |
| 249 | int rowBytes = channels * width; |
| 250 | for (int y = 0; y < height; ++y) { |
| 251 | unsigned char *scanLine = snapshot.scanLine(y); |
| 252 | |
| 253 | while (process.bytesAvailable() < rowBytes) { |
| 254 | if (!process.waitForReadyRead(-1)) { |
| 255 | qDebug() << "QProcess::waitForReadyRead failed"; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | qint64 read = process.read((char *) scanLine, rowBytes); |
| 261 | Q_ASSERT(read == rowBytes); |
| 262 | } |
| 263 | |
| 264 | QImage thumbnail = snapshot.scaled(16, 16, Qt::KeepAspectRatio, Qt::FastTransformation); |
| 265 | thumbnails.append(thumbnail); |
Dan McCabe | 66dfdda | 2012-03-05 17:20:39 -0800 | [diff] [blame] | 266 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 267 | |
| 268 | Q_ASSERT(process.state() != QProcess::Running); |
| 269 | |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 270 | } else { |
José Fonseca | 676cd17 | 2012-03-24 09:46:24 +0000 | [diff] [blame] | 271 | QByteArray output; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 272 | output = process.readAllStandardOutput(); |
José Fonseca | 56cd8ac | 2011-11-24 16:30:49 +0000 | [diff] [blame] | 273 | msg = QString::fromUtf8(output); |
| 274 | } |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 275 | } |
| 276 | |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 277 | /* |
| 278 | * Wait for process termination |
| 279 | */ |
| 280 | |
| 281 | process.waitForFinished(-1); |
| 282 | |
| 283 | if (process.exitStatus() != QProcess::NormalExit) { |
| 284 | msg = QLatin1String("Process crashed"); |
| 285 | } else if (process.exitCode() != 0) { |
| 286 | msg = QLatin1String("Process exited with non zero exit code"); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Parse errors. |
| 291 | */ |
| 292 | |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 293 | QList<ApiTraceError> errors; |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 294 | process.setReadChannel(QProcess::StandardError); |
José Fonseca | 8fdf56c | 2012-03-24 10:06:56 +0000 | [diff] [blame] | 295 | QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$"); |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 296 | while (!process.atEnd()) { |
| 297 | QString line = process.readLine(); |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 298 | if (regexp.indexIn(line) != -1) { |
Zack Rusin | 10fd477 | 2011-09-14 01:45:12 -0400 | [diff] [blame] | 299 | ApiTraceError error; |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 300 | error.callIndex = regexp.cap(1).toInt(); |
| 301 | error.type = regexp.cap(2); |
| 302 | error.message = regexp.cap(3); |
| 303 | errors.append(error); |
| 304 | } |
| 305 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 306 | |
| 307 | /* |
| 308 | * Emit signals |
| 309 | */ |
| 310 | |
| 311 | if (m_captureState) { |
| 312 | ApiTraceState *state = new ApiTraceState(parsedJson); |
| 313 | emit foundState(state); |
| 314 | msg = QLatin1String("State fetched."); |
| 315 | } |
| 316 | |
| 317 | if (m_captureThumbnails && !thumbnails.isEmpty()) { |
| 318 | emit foundThumbnails(thumbnails); |
| 319 | } |
| 320 | |
Zack Rusin | b39e1c6 | 2011-04-19 23:09:26 -0400 | [diff] [blame] | 321 | if (!errors.isEmpty()) { |
| 322 | emit retraceErrors(errors); |
| 323 | } |
José Fonseca | 5bba477 | 2012-03-25 12:46:04 +0100 | [diff] [blame^] | 324 | |
Zack Rusin | f389ae8 | 2011-04-10 19:27:28 -0400 | [diff] [blame] | 325 | emit finished(msg); |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Zack Rusin | 3acde36 | 2011-04-06 01:11:55 -0400 | [diff] [blame] | 328 | #include "retracer.moc" |