1 #include "apitracecall.h"
4 #include "trace_model.hpp"
9 #define QT_USE_FAST_OPERATOR_PLUS
10 #include <QStringBuilder>
11 #include <QTextDocument>
13 const char * const styleSheet =
15 " font-weight:bold;\n"
16 // text shadow looks great but doesn't work well in qtwebkit 4.7
17 " /*text-shadow: 0px 2px 3px #555;*/\n"
18 " font-size: 1.2em;\n"
21 " border: 1px solid rgb(238,206,0);\n"
22 " border-radius: 4px;\n"
23 " background: yellow;\n"
25 " box-shadow: 0px 1px 3px dimgrey;\n"
26 " -webkit-transition: background 1s linear;\n"
29 " background: white;\n"
35 " border: 1px solid rgb(255,0,0);\n"
38 " border-radius: 4px;\n"
39 // also looks great but qtwebkit doesn't support it
40 //" background: #6fb2e5;\n"
41 //" box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
42 //" -o-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
43 //" -webkit-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
44 //" -moz-box-shadow: 0 1px 5px #0061aa, inset 0 10px 20px #b6f9ff;\n"
48 // Qt::convertFromPlainText doesn't do precisely what we want
50 plainTextToHTML(const QString & plain, bool multiLine)
55 for (int i = 0; i < plain.length(); ++i) {
56 if (plain[i] == QLatin1Char('\n')){
58 rich += QLatin1String("<br>\n");
60 rich += QLatin1String("\\n");
65 if (plain[i] == QLatin1Char('\t')){
67 rich += QChar(0x00a0U);
70 rich += QChar(0x00a0U);
74 rich += QLatin1String("\\t");
77 } else if (plain[i].isSpace()) {
78 rich += QChar(0x00a0U);
80 } else if (plain[i] == QLatin1Char('<')) {
81 rich += QLatin1String("<");
82 } else if (plain[i] == QLatin1Char('>')) {
83 rich += QLatin1String(">");
84 } else if (plain[i] == QLatin1Char('&')) {
85 rich += QLatin1String("&");
94 return QLatin1Literal("\"") + rich + QLatin1Literal("\"");
101 apiVariantToString(const QVariant &variant, bool multiLine)
103 if (variant.userType() == QVariant::Double) {
104 return QString::number(variant.toFloat());
106 if (variant.userType() == QVariant::ByteArray) {
107 if (variant.toByteArray().size() < 1024) {
108 int bytes = variant.toByteArray().size();
109 return QObject::tr("[binary data, size = %1 bytes]").arg(bytes);
111 float kb = variant.toByteArray().size()/1024.;
112 return QObject::tr("[binary data, size = %1 kb]").arg(kb);
116 if (variant.userType() == QVariant::String) {
117 return plainTextToHTML(variant.toString(), multiLine);
120 if (variant.userType() < QVariant::UserType) {
121 return variant.toString();
124 if (variant.canConvert<ApiPointer>()) {
125 return variant.value<ApiPointer>().toString();
127 if (variant.canConvert<ApiBitmask>()) {
128 return variant.value<ApiBitmask>().toString();
130 if (variant.canConvert<ApiStruct>()) {
131 return variant.value<ApiStruct>().toString();
133 if (variant.canConvert<ApiArray>()) {
134 return variant.value<ApiArray>().toString();
136 if (variant.canConvert<ApiEnum>()) {
137 return variant.value<ApiEnum>().toString();
144 void VariantVisitor::visit(Trace::Null *)
146 m_variant = QVariant::fromValue(ApiPointer(0));
149 void VariantVisitor::visit(Trace::Bool *node)
151 m_variant = QVariant(node->value);
154 void VariantVisitor::visit(Trace::SInt *node)
156 m_variant = QVariant(node->value);
159 void VariantVisitor::visit(Trace::UInt *node)
161 m_variant = QVariant(node->value);
164 void VariantVisitor::visit(Trace::Float *node)
166 m_variant = QVariant(node->value);
169 void VariantVisitor::visit(Trace::String *node)
171 m_variant = QVariant(QString::fromStdString(node->value));
174 void VariantVisitor::visit(Trace::Enum *e)
176 ApiTraceEnumSignature *sig = 0;
179 sig = m_trace->enumSignature(e->sig->id);
182 sig = new ApiTraceEnumSignature(
183 QString::fromStdString(e->sig->name),
184 QVariant(e->sig->value));
186 m_trace->addEnumSignature(e->sig->id, sig);
190 m_variant = QVariant::fromValue(ApiEnum(sig));
193 void VariantVisitor::visit(Trace::Bitmask *bitmask)
195 m_variant = QVariant::fromValue(ApiBitmask(bitmask));
198 void VariantVisitor::visit(Trace::Struct *str)
200 m_variant = QVariant::fromValue(ApiStruct(str));
203 void VariantVisitor::visit(Trace::Array *array)
205 m_variant = QVariant::fromValue(ApiArray(array));
208 void VariantVisitor::visit(Trace::Blob *blob)
211 //FIXME: this is a nasty hack. Trace::Blob's can't
212 // delete the contents in the destructor because
213 // the data is being used by other calls. We piggy back
214 // on that assumption and don't deep copy the data. If
215 // Blob's will start deleting the data we will need to
216 // start deep copying it or switch to using something like
217 // Boost's shared_ptr or Qt's QSharedPointer to handle it
218 QByteArray barray = QByteArray::fromRawData(blob->buf, blob->size);
219 m_variant = QVariant(barray);
222 void VariantVisitor::visit(Trace::Pointer *ptr)
224 m_variant = QVariant::fromValue(ApiPointer(ptr->value));
228 ApiEnum::ApiEnum(ApiTraceEnumSignature *sig)
233 QString ApiEnum::toString() const
236 return m_sig->name();
238 Q_ASSERT(!"should never happen");
242 QVariant ApiEnum::value() const
245 return m_sig->value();
247 Q_ASSERT(!"should never happen");
251 QString ApiEnum::name() const
254 return m_sig->name();
256 Q_ASSERT(!"should never happen");
260 unsigned long long ApiBitmask::value() const
265 ApiBitmask::Signature ApiBitmask::signature() const
270 ApiStruct::Signature ApiStruct::signature() const
275 QList<QVariant> ApiStruct::values() const
280 ApiPointer::ApiPointer(unsigned long long val)
286 unsigned long long ApiPointer::value() const
291 QString ApiPointer::toString() const
294 return QString("0x%1").arg(m_value, 0, 16);
296 return QLatin1String("NULL");
299 ApiBitmask::ApiBitmask(const Trace::Bitmask *bitmask)
305 void ApiBitmask::init(const Trace::Bitmask *bitmask)
310 m_value = bitmask->value;
311 for (const Trace::BitmaskFlag *it = bitmask->sig->flags;
312 it != bitmask->sig->flags + bitmask->sig->num_flags; ++it) {
314 QPair<QString, unsigned long long> pair;
316 pair.first = QString::fromStdString(it->name);
317 pair.second = it->value;
323 QString ApiBitmask::toString() const
326 unsigned long long value = m_value;
328 for (Signature::const_iterator it = m_sig.begin();
329 value != 0 && it != m_sig.end(); ++it) {
330 Q_ASSERT(it->second);
331 if ((value & it->second) == it->second) {
333 str += QLatin1String(" | ");
336 value &= ~it->second;
340 if (value || first) {
342 str += QLatin1String(" | ");
344 str += QString::fromLatin1("0x%1").arg(value, 0, 16);
349 ApiStruct::ApiStruct(const Trace::Struct *s)
354 QString ApiStruct::toString() const
358 str += QLatin1String("{");
359 for (unsigned i = 0; i < m_members.count(); ++i) {
360 str += m_sig.memberNames[i] %
361 QLatin1Literal(" = ") %
362 apiVariantToString(m_members[i]);
363 if (i < m_members.count() - 1)
364 str += QLatin1String(", ");
366 str += QLatin1String("}");
371 void ApiStruct::init(const Trace::Struct *s)
376 m_sig.name = QString::fromStdString(s->sig->name);
377 for (unsigned i = 0; i < s->sig->num_members; ++i) {
378 VariantVisitor vis(0);
379 m_sig.memberNames.append(
380 QString::fromStdString(s->sig->member_names[i]));
381 s->members[i]->visit(vis);
382 m_members.append(vis.variant());
386 ApiArray::ApiArray(const Trace::Array *arr)
391 ApiArray::ApiArray(const QList<QVariant> &vals)
396 QList<QVariant> ApiArray::values() const
401 QString ApiArray::toString() const
404 str += QLatin1String("[");
405 for(int i = 0; i < m_array.count(); ++i) {
406 const QVariant &var = m_array[i];
407 str += apiVariantToString(var);
408 if (i < m_array.count() - 1)
409 str += QLatin1String(", ");
411 str += QLatin1String("]");
416 void ApiArray::init(const Trace::Array *arr)
421 m_array.reserve(arr->values.size());
422 for (int i = 0; i < arr->values.size(); ++i) {
423 VariantVisitor vis(0);
424 arr->values[i]->visit(vis);
426 m_array.append(vis.variant());
430 ApiTraceState::ApiTraceState()
434 ApiTraceState::ApiTraceState(const QVariantMap &parsedJson)
436 m_parameters = parsedJson[QLatin1String("parameters")].toMap();
437 QVariantMap attachedShaders =
438 parsedJson[QLatin1String("shaders")].toMap();
439 QVariantMap::const_iterator itr;
442 for (itr = attachedShaders.constBegin(); itr != attachedShaders.constEnd();
444 QString type = itr.key();
445 QString source = itr.value().toString();
446 m_shaderSources[type] = source;
449 m_uniforms = parsedJson[QLatin1String("uniforms")].toMap();
451 QVariantMap textures =
452 parsedJson[QLatin1String("textures")].toMap();
453 for (itr = textures.constBegin(); itr != textures.constEnd(); ++itr) {
454 QVariantMap image = itr.value().toMap();
455 QSize size(image[QLatin1String("__width__")].toInt(),
456 image[QLatin1String("__height__")].toInt());
457 QString cls = image[QLatin1String("__class__")].toString();
458 QString type = image[QLatin1String("__type__")].toString();
460 image[QLatin1String("__normalized__")].toBool();
462 image[QLatin1String("__channels__")].toInt();
464 Q_ASSERT(type == QLatin1String("uint8"));
465 Q_ASSERT(normalized == true);
466 Q_UNUSED(normalized);
468 QByteArray dataArray =
469 image[QLatin1String("__data__")].toByteArray();
473 tex.setNumChannels(numChannels);
474 tex.setLabel(itr.key());
475 tex.contentsFromBase64(dataArray);
477 m_textures.append(tex);
481 parsedJson[QLatin1String("framebuffer")].toMap();
482 for (itr = fbos.constBegin(); itr != fbos.constEnd(); ++itr) {
483 QVariantMap buffer = itr.value().toMap();
484 QSize size(buffer[QLatin1String("__width__")].toInt(),
485 buffer[QLatin1String("__height__")].toInt());
486 QString cls = buffer[QLatin1String("__class__")].toString();
487 QString type = buffer[QLatin1String("__type__")].toString();
488 bool normalized = buffer[QLatin1String("__normalized__")].toBool();
489 int numChannels = buffer[QLatin1String("__channels__")].toInt();
491 Q_ASSERT(type == QLatin1String("uint8"));
492 Q_ASSERT(normalized == true);
493 Q_UNUSED(normalized);
495 QByteArray dataArray =
496 buffer[QLatin1String("__data__")].toByteArray();
500 fbo.setNumChannels(numChannels);
501 fbo.setType(itr.key());
502 fbo.contentsFromBase64(dataArray);
503 m_framebuffers.append(fbo);
507 const QVariantMap & ApiTraceState::parameters() const
512 const QMap<QString, QString> & ApiTraceState::shaderSources() const
514 return m_shaderSources;
517 const QVariantMap & ApiTraceState::uniforms() const
522 bool ApiTraceState::isEmpty() const
524 return m_parameters.isEmpty();
527 const QList<ApiTexture> & ApiTraceState::textures() const
532 const QList<ApiFramebuffer> & ApiTraceState::framebuffers() const
534 return m_framebuffers;
537 ApiTraceCallSignature::ApiTraceCallSignature(const QString &name,
538 const QStringList &argNames)
544 ApiTraceCallSignature::~ApiTraceCallSignature()
548 QUrl ApiTraceCallSignature::helpUrl() const
553 void ApiTraceCallSignature::setHelpUrl(const QUrl &url)
558 ApiTraceEvent::ApiTraceEvent()
559 : m_type(ApiTraceEvent::None),
560 m_hasBinaryData(false),
561 m_binaryDataIndex(0),
567 ApiTraceEvent::ApiTraceEvent(Type t)
569 m_hasBinaryData(false),
570 m_binaryDataIndex(0),
576 ApiTraceEvent::~ApiTraceEvent()
582 QVariantMap ApiTraceEvent::stateParameters() const
585 return m_state->parameters();
587 return QVariantMap();
591 ApiTraceState *ApiTraceEvent::state() const
596 void ApiTraceEvent::setState(ApiTraceState *state)
601 ApiTraceCall::ApiTraceCall(ApiTraceFrame *parentFrame, const Trace::Call *call)
602 : ApiTraceEvent(ApiTraceEvent::Call),
603 m_parentFrame(parentFrame)
605 ApiTrace *trace = parentTrace();
611 m_signature = trace->signature(call->sig->id);
614 QString name = QString::fromStdString(call->sig->name);
615 QStringList argNames;
616 argNames.reserve(call->sig->num_args);
617 for (int i = 0; i < call->sig->num_args; ++i) {
618 argNames += QString::fromStdString(call->sig->arg_names[i]);
620 m_signature = new ApiTraceCallSignature(name, argNames);
621 trace->addSignature(call->sig->id, m_signature);
624 VariantVisitor retVisitor(trace);
625 call->ret->visit(retVisitor);
626 m_returnValue = retVisitor.variant();
628 m_argValues.reserve(call->args.size());
629 for (int i = 0; i < call->args.size(); ++i) {
630 VariantVisitor argVisitor(trace);
631 call->args[i]->visit(argVisitor);
632 m_argValues.append(argVisitor.variant());
633 if (m_argValues[i].type() == QVariant::ByteArray) {
634 m_hasBinaryData = true;
635 m_binaryDataIndex = i;
638 m_argValues.squeeze();
641 ApiTraceCall::~ApiTraceCall()
646 bool ApiTraceCall::hasError() const
648 return !m_error.isEmpty();
651 QString ApiTraceCall::error() const
656 void ApiTraceCall::setError(const QString &msg)
658 if (m_error != msg) {
659 ApiTrace *trace = parentTrace();
661 m_richText = QString();
663 trace->callError(this);
667 ApiTrace * ApiTraceCall::parentTrace() const
670 return m_parentFrame->parentTrace();
674 QVector<QVariant> ApiTraceCall::originalValues() const
679 void ApiTraceCall::setEditedValues(const QVector<QVariant> &lst)
681 ApiTrace *trace = parentTrace();
683 m_editedValues = lst;
684 //lets regenerate data
685 m_richText = QString();
686 m_searchText = QString();
691 if (!lst.isEmpty()) {
692 trace->callEdited(this);
694 trace->callReverted(this);
699 QVector<QVariant> ApiTraceCall::editedValues() const
701 return m_editedValues;
704 bool ApiTraceCall::edited() const
706 return !m_editedValues.isEmpty();
709 void ApiTraceCall::revert()
711 setEditedValues(QVector<QVariant>());
714 void ApiTraceCall::setHelpUrl(const QUrl &url)
716 m_signature->setHelpUrl(url);
719 void ApiTraceCall::setParentFrame(ApiTraceFrame *frame)
721 m_parentFrame = frame;
724 ApiTraceFrame * ApiTraceCall::parentFrame()const
726 return m_parentFrame;
729 int ApiTraceCall::index() const
734 QString ApiTraceCall::name() const
736 return m_signature->name();
739 QStringList ApiTraceCall::argNames() const
741 return m_signature->argNames();
744 QVector<QVariant> ApiTraceCall::arguments() const
746 if (m_editedValues.isEmpty())
749 return m_editedValues;
752 QVariant ApiTraceCall::returnValue() const
754 return m_returnValue;
757 QUrl ApiTraceCall::helpUrl() const
759 return m_signature->helpUrl();
762 bool ApiTraceCall::hasBinaryData() const
764 return m_hasBinaryData;
767 int ApiTraceCall::binaryDataIndex() const
769 return m_binaryDataIndex;
772 QStaticText ApiTraceCall::staticText() const
774 if (m_staticText && !m_staticText->text().isEmpty())
775 return *m_staticText;
777 QVector<QVariant> argValues = arguments();
779 QString richText = QString::fromLatin1(
780 "<span style=\"font-weight:bold\">%1</span>(").arg(
781 m_signature->name());
782 QStringList argNames = m_signature->argNames();
783 for (int i = 0; i < argNames.count(); ++i) {
784 richText += QLatin1String("<span style=\"color:#0000ff\">");
785 QString argText = apiVariantToString(argValues[i]);
787 //if arguments are really long (e.g. shader text), cut them
789 if (argText.length() > 40) {
790 QString shortened = argText.mid(0, 40);
791 shortened[argText.length() - 5] = '.';
792 shortened[argText.length() - 4] = '.';
793 shortened[argText.length() - 3] = '.';
794 shortened[argText.length() - 2] = argText.at(argText.length() - 2);
795 shortened[argText.length() - 1] = argText.at(argText.length() - 1);
796 richText += shortened;
800 richText += QLatin1String("</span>");
801 if (i < argNames.count() - 1)
802 richText += QLatin1String(", ");
804 richText += QLatin1String(")");
805 if (m_returnValue.isValid()) {
807 QLatin1Literal(" = ") %
808 QLatin1Literal("<span style=\"color:#0000ff\">") %
809 apiVariantToString(m_returnValue) %
810 QLatin1Literal("</span>");
814 m_staticText = new QStaticText(richText);
816 m_staticText->setText(richText);
818 opt.setWrapMode(QTextOption::NoWrap);
819 m_staticText->setTextOption(opt);
820 m_staticText->prepare();
822 return *m_staticText;
825 QString ApiTraceCall::toHtml() const
827 if (!m_richText.isEmpty())
830 m_richText = QLatin1String("<div class=\"call\">");
832 QUrl helpUrl = m_signature->helpUrl();
833 if (helpUrl.isEmpty()) {
834 m_richText += QString::fromLatin1(
835 "%1) <span class=\"callName\">%2</span>(")
837 .arg(m_signature->name());
839 m_richText += QString::fromLatin1(
840 "%1) <span class=\"callName\"><a href=\"%2\">%3</a></span>(")
842 .arg(helpUrl.toString())
843 .arg(m_signature->name());
846 QVector<QVariant> argValues = arguments();
847 QStringList argNames = m_signature->argNames();
848 for (int i = 0; i < argNames.count(); ++i) {
850 QLatin1String("<span class=\"arg-name\">") +
852 QLatin1String("</span>") +
853 QLatin1Literal(" = ") +
854 QLatin1Literal("<span class=\"arg-value\">") +
855 apiVariantToString(argValues[i], true) +
856 QLatin1Literal("</span>");
857 if (i < argNames.count() - 1)
858 m_richText += QLatin1String(", ");
860 m_richText += QLatin1String(")");
862 if (m_returnValue.isValid()) {
864 QLatin1String(" = ") +
865 QLatin1String("<span style=\"color:#0000ff\">") +
866 apiVariantToString(m_returnValue, true) +
867 QLatin1String("</span>");
869 m_richText += QLatin1String("</div>");
874 "<div class=\"error\">%1</div>")
876 m_richText += errorStr;
881 "<html><head><style type=\"text/css\" media=\"all\">"
882 "%1</style></head><body>%2</body></html>")
885 m_richText.squeeze();
887 //qDebug()<<m_richText;
891 QString ApiTraceCall::searchText() const
893 if (!m_searchText.isEmpty())
896 QVector<QVariant> argValues = arguments();
897 m_searchText = m_signature->name() + QLatin1Literal("(");
898 QStringList argNames = m_signature->argNames();
899 for (int i = 0; i < argNames.count(); ++i) {
900 m_searchText += argNames[i] +
901 QLatin1Literal(" = ") +
902 apiVariantToString(argValues[i]);
903 if (i < argNames.count() - 1)
904 m_searchText += QLatin1String(", ");
906 m_searchText += QLatin1String(")");
908 if (m_returnValue.isValid()) {
909 m_searchText += QLatin1Literal(" = ") +
910 apiVariantToString(m_returnValue);
912 m_searchText.squeeze();
916 int ApiTraceCall::numChildren() const
921 ApiTraceFrame::ApiTraceFrame(ApiTrace *parentTrace)
922 : ApiTraceEvent(ApiTraceEvent::Frame),
923 m_parentTrace(parentTrace),
928 QStaticText ApiTraceFrame::staticText() const
930 if (m_staticText && !m_staticText->text().isEmpty())
931 return *m_staticText;
935 //mark the frame if it uploads more than a meg a frame
936 if (m_binaryDataSize > (1024*1024)) {
939 "<span style=\"font-weight:bold;\">"
940 "Frame %1</span>"
941 "<span style=\"font-style:italic;\">"
942 " (%2MB)</span>")
944 .arg(double(m_binaryDataSize / (1024.*1024.)), 0, 'g', 2);
948 "<span style=\"font-weight:bold\">Frame %1</span>")
953 m_staticText = new QStaticText(richText);
956 opt.setWrapMode(QTextOption::NoWrap);
957 m_staticText->setTextOption(opt);
958 m_staticText->prepare();
960 return *m_staticText;
963 int ApiTraceFrame::numChildren() const
965 return m_calls.count();
968 ApiTrace * ApiTraceFrame::parentTrace() const
970 return m_parentTrace;
973 void ApiTraceFrame::addCall(ApiTraceCall *call)
975 m_calls.append(call);
976 if (call->hasBinaryData()) {
978 call->arguments()[call->binaryDataIndex()].toByteArray();
979 m_binaryDataSize += data.size();
983 QVector<ApiTraceCall*> ApiTraceFrame::calls() const
988 ApiTraceCall * ApiTraceFrame::call(int idx) const
990 return m_calls.value(idx);
993 int ApiTraceFrame::callIndex(ApiTraceCall *call) const
995 return m_calls.indexOf(call);
998 bool ApiTraceFrame::isEmpty() const
1000 return m_calls.isEmpty();
1003 int ApiTraceFrame::binaryDataSize() const
1005 return m_binaryDataSize;
1008 void ApiTraceFrame::setCalls(const QVector<ApiTraceCall*> &calls,
1009 quint64 binaryDataSize)
1012 m_binaryDataSize = binaryDataSize;