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" |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame^] | 9 | #include "batches/GrBatch.h" |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 10 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame^] | 11 | void GrAuditTrail::batchingResultCombined(GrBatch* combiner) { |
| 12 | int* indexPtr = fIDLookup.find(combiner); |
| 13 | SkASSERT(indexPtr); |
| 14 | int index = *indexPtr; |
| 15 | SkASSERT(index < fBatches.count()); |
| 16 | Batch& batch = *fBatches[index]; |
| 17 | |
| 18 | // if this is our first child, we also push back a copy of the original batch and its |
| 19 | // bounds |
| 20 | if (batch.fChildren.empty()) { |
| 21 | Batch* firstBatch = new Batch; |
| 22 | firstBatch->fName = batch.fName; |
| 23 | firstBatch->fBounds = batch.fBounds; |
| 24 | fEvents.emplace_back(firstBatch); |
| 25 | batch.fChildren.push_back(firstBatch); |
| 26 | } |
| 27 | batch.fChildren.push_back(fCurrentBatch); |
| 28 | batch.fBounds = combiner->bounds(); |
| 29 | } |
| 30 | |
| 31 | void GrAuditTrail::batchingResultNew(GrBatch* batch) { |
| 32 | fIDLookup.set(batch, fBatches.count()); |
| 33 | fBatches.push_back(fCurrentBatch); |
| 34 | } |
| 35 | |
| 36 | template <typename T> |
| 37 | void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const T& array, |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 38 | bool addComma) { |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 39 | if (array.count()) { |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 40 | if (addComma) { |
| 41 | json->appendf(","); |
| 42 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 43 | json->appendf("\"%s\": [", name); |
| 44 | for (int i = 0; i < array.count(); i++) { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 45 | json->append(array[i]->toJson()); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 46 | if (i < array.count() - 1) { |
| 47 | json->append(","); |
| 48 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 49 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 50 | json->append("]"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 51 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // This will pretty print a very small subset of json |
| 55 | // The parsing rules are straightforward, aside from the fact that we do not want an extra newline |
| 56 | // before ',' and after '}', so we have a comma exception rule. |
| 57 | class PrettyPrintJson { |
| 58 | public: |
| 59 | SkString prettify(const SkString& json) { |
| 60 | fPrettyJson.reset(); |
| 61 | fTabCount = 0; |
| 62 | fFreshLine = false; |
| 63 | fCommaException = false; |
| 64 | for (size_t i = 0; i < json.size(); i++) { |
| 65 | if ('[' == json[i] || '{' == json[i]) { |
| 66 | this->newline(); |
| 67 | this->appendChar(json[i]); |
| 68 | fTabCount++; |
| 69 | this->newline(); |
| 70 | } else if (']' == json[i] || '}' == json[i]) { |
| 71 | fTabCount--; |
| 72 | this->newline(); |
| 73 | this->appendChar(json[i]); |
| 74 | fCommaException = true; |
| 75 | } else if (',' == json[i]) { |
| 76 | this->appendChar(json[i]); |
| 77 | this->newline(); |
| 78 | } else { |
| 79 | this->appendChar(json[i]); |
| 80 | } |
| 81 | } |
| 82 | return fPrettyJson; |
| 83 | } |
| 84 | private: |
| 85 | void appendChar(char appendee) { |
| 86 | if (fCommaException && ',' != appendee) { |
| 87 | this->newline(); |
| 88 | } |
| 89 | this->tab(); |
| 90 | fPrettyJson += appendee; |
| 91 | fFreshLine = false; |
| 92 | fCommaException = false; |
| 93 | } |
| 94 | |
| 95 | void tab() { |
| 96 | if (fFreshLine) { |
| 97 | for (int i = 0; i < fTabCount; i++) { |
| 98 | fPrettyJson += '\t'; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void newline() { |
| 104 | if (!fFreshLine) { |
| 105 | fFreshLine = true; |
| 106 | fPrettyJson += '\n'; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | SkString fPrettyJson; |
| 111 | int fTabCount; |
| 112 | bool fFreshLine; |
| 113 | bool fCommaException; |
| 114 | }; |
| 115 | |
| 116 | static SkString pretty_print_json(SkString json) { |
| 117 | class PrettyPrintJson prettyPrintJson; |
| 118 | return prettyPrintJson.prettify(json); |
| 119 | } |
| 120 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame^] | 121 | SkString GrAuditTrail::toJson(bool batchList, bool prettyPrint) const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 122 | SkString json; |
| 123 | json.append("{"); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame^] | 124 | if (!batchList) { |
| 125 | JsonifyTArray(&json, "Stacks", fFrames, false); |
| 126 | } else { |
| 127 | JsonifyTArray(&json, "Batches", fBatches, false); |
| 128 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 129 | json.append("}"); |
| 130 | |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame] | 131 | if (prettyPrint) { |
| 132 | return pretty_print_json(json); |
| 133 | } else { |
| 134 | return json; |
| 135 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 136 | } |
| 137 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 138 | SkString GrAuditTrail::Frame::toJson() const { |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 139 | SkString json; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 140 | json.append("{"); |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 141 | json.appendf("\"Name\": \"%s\"", fName); |
| 142 | JsonifyTArray(&json, "Frames", fChildren, true); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 143 | json.append("}"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 144 | return json; |
| 145 | } |
| 146 | |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 147 | SkString GrAuditTrail::Batch::toJson() const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 148 | SkString json; |
| 149 | json.append("{"); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 150 | json.appendf("\"Name\": \"%s\",", fName); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 151 | json.append("\"Bounds\": {"); |
| 152 | json.appendf("\"Left\": %f,", fBounds.fLeft); |
| 153 | json.appendf("\"Right\": %f,", fBounds.fRight); |
| 154 | json.appendf("\"Top\": %f,", fBounds.fTop); |
| 155 | json.appendf("\"Bottom\": %f", fBounds.fBottom); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame^] | 156 | JsonifyTArray(&json, "Children", fChildren, true); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 157 | json.append("}"); |
| 158 | json.append("}"); |
| 159 | return json; |
| 160 | } |