blob: aa527fb4b0a1d078b71a4caa7864ebc41632da0f [file] [log] [blame]
joshualitt27a48dc2016-01-08 07:19:47 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAuditTrail.h"
9
joshualittadab5a22016-02-18 05:04:39 -080010void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const FrameArray& array,
11 bool addComma) {
joshualitt87a721b2016-01-12 12:59:28 -080012 if (array.count()) {
joshualittadab5a22016-02-18 05:04:39 -080013 if (addComma) {
14 json->appendf(",");
15 }
joshualitt87a721b2016-01-12 12:59:28 -080016 json->appendf("\"%s\": [", name);
17 for (int i = 0; i < array.count(); i++) {
joshualitt11fae872016-01-14 10:58:07 -080018 json->append(array[i]->toJson());
joshualitt87a721b2016-01-12 12:59:28 -080019 if (i < array.count() - 1) {
20 json->append(",");
21 }
joshualitt27a48dc2016-01-08 07:19:47 -080022 }
joshualitt87a721b2016-01-12 12:59:28 -080023 json->append("]");
joshualitt27a48dc2016-01-08 07:19:47 -080024 }
joshualitt086cee12016-01-12 06:45:24 -080025}
26
27// This will pretty print a very small subset of json
28// The parsing rules are straightforward, aside from the fact that we do not want an extra newline
29// before ',' and after '}', so we have a comma exception rule.
30class PrettyPrintJson {
31public:
32 SkString prettify(const SkString& json) {
33 fPrettyJson.reset();
34 fTabCount = 0;
35 fFreshLine = false;
36 fCommaException = false;
37 for (size_t i = 0; i < json.size(); i++) {
38 if ('[' == json[i] || '{' == json[i]) {
39 this->newline();
40 this->appendChar(json[i]);
41 fTabCount++;
42 this->newline();
43 } else if (']' == json[i] || '}' == json[i]) {
44 fTabCount--;
45 this->newline();
46 this->appendChar(json[i]);
47 fCommaException = true;
48 } else if (',' == json[i]) {
49 this->appendChar(json[i]);
50 this->newline();
51 } else {
52 this->appendChar(json[i]);
53 }
54 }
55 return fPrettyJson;
56 }
57private:
58 void appendChar(char appendee) {
59 if (fCommaException && ',' != appendee) {
60 this->newline();
61 }
62 this->tab();
63 fPrettyJson += appendee;
64 fFreshLine = false;
65 fCommaException = false;
66 }
67
68 void tab() {
69 if (fFreshLine) {
70 for (int i = 0; i < fTabCount; i++) {
71 fPrettyJson += '\t';
72 }
73 }
74 }
75
76 void newline() {
77 if (!fFreshLine) {
78 fFreshLine = true;
79 fPrettyJson += '\n';
80 }
81 }
82
83 SkString fPrettyJson;
84 int fTabCount;
85 bool fFreshLine;
86 bool fCommaException;
87};
88
89static SkString pretty_print_json(SkString json) {
90 class PrettyPrintJson prettyPrintJson;
91 return prettyPrintJson.prettify(json);
92}
93
joshualitt6b3cf732016-02-17 11:20:26 -080094SkString GrAuditTrail::toJson(bool prettyPrint) const {
joshualitt086cee12016-01-12 06:45:24 -080095 SkString json;
96 json.append("{");
joshualittadab5a22016-02-18 05:04:39 -080097 JsonifyTArray(&json, "Stacks", fFrames, false);
joshualitt086cee12016-01-12 06:45:24 -080098 json.append("}");
99
joshualitt6b3cf732016-02-17 11:20:26 -0800100 if (prettyPrint) {
101 return pretty_print_json(json);
102 } else {
103 return json;
104 }
joshualitt27a48dc2016-01-08 07:19:47 -0800105}
106
joshualitt87a721b2016-01-12 12:59:28 -0800107SkString GrAuditTrail::Frame::toJson() const {
joshualitt27a48dc2016-01-08 07:19:47 -0800108 SkString json;
joshualitt086cee12016-01-12 06:45:24 -0800109 json.append("{");
joshualittadab5a22016-02-18 05:04:39 -0800110 json.appendf("\"Name\": \"%s\"", fName);
111 JsonifyTArray(&json, "Frames", fChildren, true);
joshualitt086cee12016-01-12 06:45:24 -0800112 json.append("}");
joshualitt27a48dc2016-01-08 07:19:47 -0800113 return json;
114}
115
joshualitt11fae872016-01-14 10:58:07 -0800116SkString GrAuditTrail::Batch::toJson() const {
joshualitt086cee12016-01-12 06:45:24 -0800117 SkString json;
118 json.append("{");
joshualitt87a721b2016-01-12 12:59:28 -0800119 json.appendf("\"Name\": \"%s\",", fName);
joshualitt086cee12016-01-12 06:45:24 -0800120 json.append("\"Bounds\": {");
121 json.appendf("\"Left\": %f,", fBounds.fLeft);
122 json.appendf("\"Right\": %f,", fBounds.fRight);
123 json.appendf("\"Top\": %f,", fBounds.fTop);
124 json.appendf("\"Bottom\": %f", fBounds.fBottom);
125 json.append("}");
126 json.append("}");
127 return json;
128}