joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 10 | void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const FrameArray& array) { |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 11 | if (array.count()) { |
| 12 | json->appendf("\"%s\": [", name); |
| 13 | for (int i = 0; i < array.count(); i++) { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 14 | json->append(array[i]->toJson()); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 15 | if (i < array.count() - 1) { |
| 16 | json->append(","); |
| 17 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 18 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 19 | json->append("]"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 20 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 21 | } |
| 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. |
| 26 | class PrettyPrintJson { |
| 27 | public: |
| 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 | } |
| 53 | private: |
| 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 | |
| 85 | static SkString pretty_print_json(SkString json) { |
| 86 | class PrettyPrintJson prettyPrintJson; |
| 87 | return prettyPrintJson.prettify(json); |
| 88 | } |
| 89 | |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame^] | 90 | SkString GrAuditTrail::toJson(bool prettyPrint) const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 91 | SkString json; |
| 92 | json.append("{"); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 93 | JsonifyTArray(&json, "Stacks", fFrames); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 94 | json.append("}"); |
| 95 | |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame^] | 96 | if (prettyPrint) { |
| 97 | return pretty_print_json(json); |
| 98 | } else { |
| 99 | return json; |
| 100 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 101 | } |
| 102 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 103 | SkString GrAuditTrail::Frame::toJson() const { |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 104 | SkString json; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 105 | json.append("{"); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 106 | json.appendf("\"Name\": \"%s\",", fName); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 107 | JsonifyTArray(&json, "Frames", fChildren); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 108 | json.append("}"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 109 | return json; |
| 110 | } |
| 111 | |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 112 | SkString GrAuditTrail::Batch::toJson() const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 113 | SkString json; |
| 114 | json.append("{"); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 115 | json.appendf("\"Name\": \"%s\",", fName); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 116 | 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 | } |