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 blob->toPointer(true);
219 QByteArray barray = QByteArray::fromRawData(blob->buf, blob->size);
220 m_variant = QVariant(barray);
223 void VariantVisitor::visit(Trace::Pointer *ptr)
225 m_variant = QVariant::fromValue(ApiPointer(ptr->value));
229 ApiEnum::ApiEnum(ApiTraceEnumSignature *sig)
234 QString ApiEnum::toString() const
237 return m_sig->name();
239 Q_ASSERT(!"should never happen");
243 QVariant ApiEnum::value() const
246 return m_sig->value();
248 Q_ASSERT(!"should never happen");
252 QString ApiEnum::name() const
255 return m_sig->name();
257 Q_ASSERT(!"should never happen");
261 unsigned long long ApiBitmask::value() const
266 ApiBitmask::Signature ApiBitmask::signature() const
271 ApiStruct::Signature ApiStruct::signature() const
276 QList<QVariant> ApiStruct::values() const
281 ApiPointer::ApiPointer(unsigned long long val)
287 unsigned long long ApiPointer::value() const
292 QString ApiPointer::toString() const
295 return QString("0x%1").arg(m_value, 0, 16);
297 return QLatin1String("NULL");
300 ApiBitmask::ApiBitmask(const Trace::Bitmask *bitmask)
306 void ApiBitmask::init(const Trace::Bitmask *bitmask)
311 m_value = bitmask->value;
312 for (const Trace::BitmaskFlag *it = bitmask->sig->flags;
313 it != bitmask->sig->flags + bitmask->sig->num_flags; ++it) {
315 QPair<QString, unsigned long long> pair;
317 pair.first = QString::fromStdString(it->name);
318 pair.second = it->value;
324 QString ApiBitmask::toString() const
327 unsigned long long value = m_value;
329 for (Signature::const_iterator it = m_sig.begin();
330 value != 0 && it != m_sig.end(); ++it) {
331 Q_ASSERT(it->second);
332 if ((value & it->second) == it->second) {
334 str += QLatin1String(" | ");
337 value &= ~it->second;
341 if (value || first) {
343 str += QLatin1String(" | ");
345 str += QString::fromLatin1("0x%1").arg(value, 0, 16);
350 ApiStruct::ApiStruct(const Trace::Struct *s)
355 QString ApiStruct::toString() const
359 str += QLatin1String("{");
360 for (unsigned i = 0; i < m_members.count(); ++i) {
361 str += m_sig.memberNames[i] %
362 QLatin1Literal(" = ") %
363 apiVariantToString(m_members[i]);
364 if (i < m_members.count() - 1)
365 str += QLatin1String(", ");
367 str += QLatin1String("}");
372 void ApiStruct::init(const Trace::Struct *s)
377 m_sig.name = QString::fromStdString(s->sig->name);
378 for (unsigned i = 0; i < s->sig->num_members; ++i) {
379 VariantVisitor vis(0);
380 m_sig.memberNames.append(
381 QString::fromStdString(s->sig->member_names[i]));
382 s->members[i]->visit(vis);
383 m_members.append(vis.variant());
387 ApiArray::ApiArray(const Trace::Array *arr)
392 ApiArray::ApiArray(const QVector<QVariant> &vals)
397 QVector<QVariant> ApiArray::values() const
402 QString ApiArray::toString() const
405 str += QLatin1String("[");
406 for(int i = 0; i < m_array.count(); ++i) {
407 const QVariant &var = m_array[i];
408 str += apiVariantToString(var);
409 if (i < m_array.count() - 1)
410 str += QLatin1String(", ");
412 str += QLatin1String("]");
417 void ApiArray::init(const Trace::Array *arr)
422 m_array.reserve(arr->values.size());
423 for (int i = 0; i < arr->values.size(); ++i) {
424 VariantVisitor vis(0);
425 arr->values[i]->visit(vis);
427 m_array.append(vis.variant());
432 ApiTraceState::ApiTraceState()
436 ApiTraceState::ApiTraceState(const QVariantMap &parsedJson)
438 m_parameters = parsedJson[QLatin1String("parameters")].toMap();
439 QVariantMap attachedShaders =
440 parsedJson[QLatin1String("shaders")].toMap();
441 QVariantMap::const_iterator itr;
444 for (itr = attachedShaders.constBegin(); itr != attachedShaders.constEnd();
446 QString type = itr.key();
447 QString source = itr.value().toString();
448 m_shaderSources[type] = source;
451 m_uniforms = parsedJson[QLatin1String("uniforms")].toMap();
453 QVariantMap textures =
454 parsedJson[QLatin1String("textures")].toMap();
455 for (itr = textures.constBegin(); itr != textures.constEnd(); ++itr) {
456 QVariantMap image = itr.value().toMap();
457 QSize size(image[QLatin1String("__width__")].toInt(),
458 image[QLatin1String("__height__")].toInt());
459 QString cls = image[QLatin1String("__class__")].toString();
460 QString type = image[QLatin1String("__type__")].toString();
462 image[QLatin1String("__normalized__")].toBool();
464 image[QLatin1String("__channels__")].toInt();
466 Q_ASSERT(type == QLatin1String("uint8"));
467 Q_ASSERT(normalized == true);
468 Q_UNUSED(normalized);
470 QByteArray dataArray =
471 image[QLatin1String("__data__")].toByteArray();
475 tex.setNumChannels(numChannels);
476 tex.setLabel(itr.key());
477 tex.contentsFromBase64(dataArray);
479 m_textures.append(tex);
483 parsedJson[QLatin1String("framebuffer")].toMap();
484 for (itr = fbos.constBegin(); itr != fbos.constEnd(); ++itr) {
485 QVariantMap buffer = itr.value().toMap();
486 QSize size(buffer[QLatin1String("__width__")].toInt(),
487 buffer[QLatin1String("__height__")].toInt());
488 QString cls = buffer[QLatin1String("__class__")].toString();
489 QString type = buffer[QLatin1String("__type__")].toString();
490 bool normalized = buffer[QLatin1String("__normalized__")].toBool();
491 int numChannels = buffer[QLatin1String("__channels__")].toInt();
493 Q_ASSERT(type == QLatin1String("uint8"));
494 Q_ASSERT(normalized == true);
495 Q_UNUSED(normalized);
497 QByteArray dataArray =
498 buffer[QLatin1String("__data__")].toByteArray();
502 fbo.setNumChannels(numChannels);
503 fbo.setType(itr.key());
504 fbo.contentsFromBase64(dataArray);
505 m_framebuffers.append(fbo);
509 const QVariantMap & ApiTraceState::parameters() const
514 const QMap<QString, QString> & ApiTraceState::shaderSources() const
516 return m_shaderSources;
519 const QVariantMap & ApiTraceState::uniforms() const
524 bool ApiTraceState::isEmpty() const
526 return m_parameters.isEmpty();
529 const QList<ApiTexture> & ApiTraceState::textures() const
534 const QList<ApiFramebuffer> & ApiTraceState::framebuffers() const
536 return m_framebuffers;
539 ApiTraceCallSignature::ApiTraceCallSignature(const QString &name,
540 const QStringList &argNames)
546 ApiTraceCallSignature::~ApiTraceCallSignature()
550 QUrl ApiTraceCallSignature::helpUrl() const
555 void ApiTraceCallSignature::setHelpUrl(const QUrl &url)
560 ApiTraceEvent::ApiTraceEvent()
561 : m_type(ApiTraceEvent::None),
562 m_hasBinaryData(false),
563 m_binaryDataIndex(0),
569 ApiTraceEvent::ApiTraceEvent(Type t)
571 m_hasBinaryData(false),
572 m_binaryDataIndex(0),
578 ApiTraceEvent::~ApiTraceEvent()
584 QVariantMap ApiTraceEvent::stateParameters() const
587 return m_state->parameters();
589 return QVariantMap();
593 ApiTraceState *ApiTraceEvent::state() const
598 void ApiTraceEvent::setState(ApiTraceState *state)
603 ApiTraceCall::ApiTraceCall(ApiTraceFrame *parentFrame, const Trace::Call *call)
604 : ApiTraceEvent(ApiTraceEvent::Call),
605 m_parentFrame(parentFrame)
607 ApiTrace *trace = parentTrace();
613 m_signature = trace->signature(call->sig->id);
616 QString name = QString::fromStdString(call->sig->name);
617 QStringList argNames;
618 argNames.reserve(call->sig->num_args);
619 for (int i = 0; i < call->sig->num_args; ++i) {
620 argNames += QString::fromStdString(call->sig->arg_names[i]);
622 m_signature = new ApiTraceCallSignature(name, argNames);
623 trace->addSignature(call->sig->id, m_signature);
626 VariantVisitor retVisitor(trace);
627 call->ret->visit(retVisitor);
628 m_returnValue = retVisitor.variant();
630 m_argValues.reserve(call->args.size());
631 for (int i = 0; i < call->args.size(); ++i) {
632 VariantVisitor argVisitor(trace);
633 call->args[i]->visit(argVisitor);
634 m_argValues.append(argVisitor.variant());
635 if (m_argValues[i].type() == QVariant::ByteArray) {
636 m_hasBinaryData = true;
637 m_binaryDataIndex = i;
640 m_argValues.squeeze();
643 ApiTraceCall::~ApiTraceCall()
648 bool ApiTraceCall::hasError() const
650 return !m_error.isEmpty();
653 QString ApiTraceCall::error() const
658 void ApiTraceCall::setError(const QString &msg)
660 if (m_error != msg) {
661 ApiTrace *trace = parentTrace();
663 m_richText = QString();
665 trace->callError(this);
669 ApiTrace * ApiTraceCall::parentTrace() const
672 return m_parentFrame->parentTrace();
676 QVector<QVariant> ApiTraceCall::originalValues() const
681 void ApiTraceCall::setEditedValues(const QVector<QVariant> &lst)
683 ApiTrace *trace = parentTrace();
685 m_editedValues = lst;
686 //lets regenerate data
687 m_richText = QString();
688 m_searchText = QString();
693 if (!lst.isEmpty()) {
694 trace->callEdited(this);
696 trace->callReverted(this);
701 QVector<QVariant> ApiTraceCall::editedValues() const
703 return m_editedValues;
706 bool ApiTraceCall::edited() const
708 return !m_editedValues.isEmpty();
711 void ApiTraceCall::revert()
713 setEditedValues(QVector<QVariant>());
716 void ApiTraceCall::setHelpUrl(const QUrl &url)
718 m_signature->setHelpUrl(url);
721 void ApiTraceCall::setParentFrame(ApiTraceFrame *frame)
723 m_parentFrame = frame;
726 ApiTraceFrame * ApiTraceCall::parentFrame()const
728 return m_parentFrame;
731 int ApiTraceCall::index() const
736 QString ApiTraceCall::name() const
738 return m_signature->name();
741 QStringList ApiTraceCall::argNames() const
743 return m_signature->argNames();
746 QVector<QVariant> ApiTraceCall::arguments() const
748 if (m_editedValues.isEmpty())
751 return m_editedValues;
754 QVariant ApiTraceCall::returnValue() const
756 return m_returnValue;
759 QUrl ApiTraceCall::helpUrl() const
761 return m_signature->helpUrl();
764 bool ApiTraceCall::hasBinaryData() const
766 return m_hasBinaryData;
769 int ApiTraceCall::binaryDataIndex() const
771 return m_binaryDataIndex;
774 QStaticText ApiTraceCall::staticText() const
776 if (m_staticText && !m_staticText->text().isEmpty())
777 return *m_staticText;
779 QVector<QVariant> argValues = arguments();
781 QString richText = QString::fromLatin1(
782 "<span style=\"font-weight:bold\">%1</span>(").arg(
783 m_signature->name());
784 QStringList argNames = m_signature->argNames();
785 for (int i = 0; i < argNames.count(); ++i) {
786 richText += QLatin1String("<span style=\"color:#0000ff\">");
787 QString argText = apiVariantToString(argValues[i]);
789 //if arguments are really long (e.g. shader text), cut them
791 if (argText.length() > 40) {
792 QString shortened = argText.mid(0, 40);
793 shortened[argText.length() - 5] = '.';
794 shortened[argText.length() - 4] = '.';
795 shortened[argText.length() - 3] = '.';
796 shortened[argText.length() - 2] = argText.at(argText.length() - 2);
797 shortened[argText.length() - 1] = argText.at(argText.length() - 1);
798 richText += shortened;
802 richText += QLatin1String("</span>");
803 if (i < argNames.count() - 1)
804 richText += QLatin1String(", ");
806 richText += QLatin1String(")");
807 if (m_returnValue.isValid()) {
809 QLatin1Literal(" = ") %
810 QLatin1Literal("<span style=\"color:#0000ff\">") %
811 apiVariantToString(m_returnValue) %
812 QLatin1Literal("</span>");
816 m_staticText = new QStaticText(richText);
818 m_staticText->setText(richText);
820 opt.setWrapMode(QTextOption::NoWrap);
821 m_staticText->setTextOption(opt);
822 m_staticText->prepare();
824 return *m_staticText;
827 QString ApiTraceCall::toHtml() const
829 if (!m_richText.isEmpty())
832 m_richText = QLatin1String("<div class=\"call\">");
834 QUrl helpUrl = m_signature->helpUrl();
835 if (helpUrl.isEmpty()) {
836 m_richText += QString::fromLatin1(
837 "%1) <span class=\"callName\">%2</span>(")
839 .arg(m_signature->name());
841 m_richText += QString::fromLatin1(
842 "%1) <span class=\"callName\"><a href=\"%2\">%3</a></span>(")
844 .arg(helpUrl.toString())
845 .arg(m_signature->name());
848 QVector<QVariant> argValues = arguments();
849 QStringList argNames = m_signature->argNames();
850 for (int i = 0; i < argNames.count(); ++i) {
852 QLatin1String("<span class=\"arg-name\">") +
854 QLatin1String("</span>") +
855 QLatin1Literal(" = ") +
856 QLatin1Literal("<span class=\"arg-value\">") +
857 apiVariantToString(argValues[i], true) +
858 QLatin1Literal("</span>");
859 if (i < argNames.count() - 1)
860 m_richText += QLatin1String(", ");
862 m_richText += QLatin1String(")");
864 if (m_returnValue.isValid()) {
866 QLatin1String(" = ") +
867 QLatin1String("<span style=\"color:#0000ff\">") +
868 apiVariantToString(m_returnValue, true) +
869 QLatin1String("</span>");
871 m_richText += QLatin1String("</div>");
876 "<div class=\"error\">%1</div>")
878 m_richText += errorStr;
883 "<html><head><style type=\"text/css\" media=\"all\">"
884 "%1</style></head><body>%2</body></html>")
887 m_richText.squeeze();
889 //qDebug()<<m_richText;
893 QString ApiTraceCall::searchText() const
895 if (!m_searchText.isEmpty())
898 QVector<QVariant> argValues = arguments();
899 m_searchText = m_signature->name() + QLatin1Literal("(");
900 QStringList argNames = m_signature->argNames();
901 for (int i = 0; i < argNames.count(); ++i) {
902 m_searchText += argNames[i] +
903 QLatin1Literal(" = ") +
904 apiVariantToString(argValues[i]);
905 if (i < argNames.count() - 1)
906 m_searchText += QLatin1String(", ");
908 m_searchText += QLatin1String(")");
910 if (m_returnValue.isValid()) {
911 m_searchText += QLatin1Literal(" = ") +
912 apiVariantToString(m_returnValue);
914 m_searchText.squeeze();
918 int ApiTraceCall::numChildren() const
923 ApiTraceFrame::ApiTraceFrame(ApiTrace *parentTrace)
924 : ApiTraceEvent(ApiTraceEvent::Frame),
925 m_parentTrace(parentTrace),
930 QStaticText ApiTraceFrame::staticText() const
932 if (m_staticText && !m_staticText->text().isEmpty())
933 return *m_staticText;
937 //mark the frame if it uploads more than a meg a frame
938 if (m_binaryDataSize > (1024*1024)) {
941 "<span style=\"font-weight:bold;\">"
942 "Frame %1</span>"
943 "<span style=\"font-style:italic;\">"
944 " (%2MB)</span>")
946 .arg(double(m_binaryDataSize / (1024.*1024.)), 0, 'g', 2);
950 "<span style=\"font-weight:bold\">Frame %1</span>")
955 m_staticText = new QStaticText(richText);
958 opt.setWrapMode(QTextOption::NoWrap);
959 m_staticText->setTextOption(opt);
960 m_staticText->prepare();
962 return *m_staticText;
965 int ApiTraceFrame::numChildren() const
967 return m_calls.count();
970 ApiTrace * ApiTraceFrame::parentTrace() const
972 return m_parentTrace;
975 void ApiTraceFrame::addCall(ApiTraceCall *call)
977 m_calls.append(call);
978 if (call->hasBinaryData()) {
980 call->arguments()[call->binaryDataIndex()].toByteArray();
981 m_binaryDataSize += data.size();
985 QVector<ApiTraceCall*> ApiTraceFrame::calls() const
990 ApiTraceCall * ApiTraceFrame::call(int idx) const
992 return m_calls.value(idx);
995 int ApiTraceFrame::callIndex(ApiTraceCall *call) const
997 return m_calls.indexOf(call);
1000 bool ApiTraceFrame::isEmpty() const
1002 return m_calls.isEmpty();
1005 int ApiTraceFrame::binaryDataSize() const
1007 return m_binaryDataSize;
1010 void ApiTraceFrame::setCalls(const QVector<ApiTraceCall*> &calls,
1011 quint64 binaryDataSize)
1014 m_binaryDataSize = binaryDataSize;