blob: 4329cbe062f505ff2a08e15fe1a0ed9a47e97bda [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
joshualitt11fae872016-01-14 10:58:07 -080010void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const FrameArray& array) {
joshualitt87a721b2016-01-12 12:59:28 -080011 if (array.count()) {
12 json->appendf("\"%s\": [", name);
13 for (int i = 0; i < array.count(); i++) {
joshualitt11fae872016-01-14 10:58:07 -080014 json->append(array[i]->toJson());
joshualitt87a721b2016-01-12 12:59:28 -080015 if (i < array.count() - 1) {
16 json->append(",");
17 }
joshualitt27a48dc2016-01-08 07:19:47 -080018 }
joshualitt87a721b2016-01-12 12:59:28 -080019 json->append("]");
joshualitt27a48dc2016-01-08 07:19:47 -080020 }
joshualitt086cee12016-01-12 06:45:24 -080021}
22
23// This will pretty print a very small subset of json
24// The parsing rules are straightforward, aside from the fact that we do not want an extra newline
25// before ',' and after '}', so we have a comma exception rule.
26class PrettyPrintJson {
27public:
28 SkString prettify(const SkString& json) {
29 fPrettyJson.reset();
30 fTabCount = 0;
31 fFreshLine = false;
32 fCommaException = false;
33 for (size_t i = 0; i < json.size(); i++) {
34 if ('[' == json[i] || '{' == json[i]) {
35 this->newline();
36 this->appendChar(json[i]);
37 fTabCount++;
38 this->newline();
39 } else if (']' == json[i] || '}' == json[i]) {
40 fTabCount--;
41 this->newline();
42 this->appendChar(json[i]);
43 fCommaException = true;
44 } else if (',' == json[i]) {
45 this->appendChar(json[i]);
46 this->newline();
47 } else {
48 this->appendChar(json[i]);
49 }
50 }
51 return fPrettyJson;
52 }
53private:
54 void appendChar(char appendee) {
55 if (fCommaException && ',' != appendee) {
56 this->newline();
57 }
58 this->tab();
59 fPrettyJson += appendee;
60 fFreshLine = false;
61 fCommaException = false;
62 }
63
64 void tab() {
65 if (fFreshLine) {
66 for (int i = 0; i < fTabCount; i++) {
67 fPrettyJson += '\t';
68 }
69 }
70 }
71
72 void newline() {
73 if (!fFreshLine) {
74 fFreshLine = true;
75 fPrettyJson += '\n';
76 }
77 }
78
79 SkString fPrettyJson;
80 int fTabCount;
81 bool fFreshLine;
82 bool fCommaException;
83};
84
85static SkString pretty_print_json(SkString json) {
86 class PrettyPrintJson prettyPrintJson;
87 return prettyPrintJson.prettify(json);
88}
89
joshualitt6b3cf732016-02-17 11:20:26 -080090SkString GrAuditTrail::toJson(bool prettyPrint) const {
joshualitt086cee12016-01-12 06:45:24 -080091 SkString json;
92 json.append("{");
joshualitt11fae872016-01-14 10:58:07 -080093 JsonifyTArray(&json, "Stacks", fFrames);
joshualitt086cee12016-01-12 06:45:24 -080094 json.append("}");
95
joshualitt6b3cf732016-02-17 11:20:26 -080096 if (prettyPrint) {
97 return pretty_print_json(json);
98 } else {
99 return json;
100 }
joshualitt27a48dc2016-01-08 07:19:47 -0800101}
102
joshualitt87a721b2016-01-12 12:59:28 -0800103SkString GrAuditTrail::Frame::toJson() const {
joshualitt27a48dc2016-01-08 07:19:47 -0800104 SkString json;
joshualitt086cee12016-01-12 06:45:24 -0800105 json.append("{");
joshualitt87a721b2016-01-12 12:59:28 -0800106 json.appendf("\"Name\": \"%s\",", fName);
joshualitt11fae872016-01-14 10:58:07 -0800107 JsonifyTArray(&json, "Frames", fChildren);
joshualitt086cee12016-01-12 06:45:24 -0800108 json.append("}");
joshualitt27a48dc2016-01-08 07:19:47 -0800109 return json;
110}
111
joshualitt11fae872016-01-14 10:58:07 -0800112SkString GrAuditTrail::Batch::toJson() const {
joshualitt086cee12016-01-12 06:45:24 -0800113 SkString json;
114 json.append("{");
joshualitt87a721b2016-01-12 12:59:28 -0800115 json.appendf("\"Name\": \"%s\",", fName);
joshualitt086cee12016-01-12 06:45:24 -0800116 json.append("\"Bounds\": {");
117 json.appendf("\"Left\": %f,", fBounds.fLeft);
118 json.appendf("\"Right\": %f,", fBounds.fRight);
119 json.appendf("\"Top\": %f,", fBounds.fTop);
120 json.appendf("\"Bottom\": %f", fBounds.fBottom);
121 json.append("}");
122 json.append("}");
123 return json;
124}