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 | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 11 | const int GrAuditTrail::kGrAuditTrailInvalidID = -1; |
| 12 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 13 | void GrAuditTrail::addBatch(const char* name, const SkRect& bounds) { |
| 14 | SkASSERT(fEnabled); |
| 15 | Batch* batch = new Batch; |
| 16 | fBatchPool.emplace_back(batch); |
| 17 | batch->fName = name; |
| 18 | batch->fBounds = bounds; |
| 19 | batch->fClientID = kGrAuditTrailInvalidID; |
| 20 | batch->fBatchListID = kGrAuditTrailInvalidID; |
| 21 | batch->fChildID = kGrAuditTrailInvalidID; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 22 | |
| 23 | // consume the current stack trace if any |
| 24 | batch->fStackTrace = fCurrentStackTrace; |
| 25 | fCurrentStackTrace.reset(); |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 26 | fCurrentBatch = batch; |
| 27 | |
| 28 | if (fClientID != kGrAuditTrailInvalidID) { |
| 29 | batch->fClientID = fClientID; |
| 30 | Batches** batchesLookup = fClientIDLookup.find(fClientID); |
| 31 | Batches* batches = nullptr; |
| 32 | if (!batchesLookup) { |
| 33 | batches = new Batches; |
| 34 | fClientIDLookup.set(fClientID, batches); |
| 35 | } else { |
| 36 | batches = *batchesLookup; |
| 37 | } |
| 38 | |
| 39 | batches->push_back(fCurrentBatch); |
| 40 | } |
| 41 | } |
| 42 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 43 | void GrAuditTrail::batchingResultCombined(GrBatch* combiner) { |
| 44 | int* indexPtr = fIDLookup.find(combiner); |
| 45 | SkASSERT(indexPtr); |
| 46 | int index = *indexPtr; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 47 | SkASSERT(index < fBatchList.count()); |
| 48 | BatchNode& batch = *fBatchList[index]; |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 49 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 50 | // set the ids for the child batch |
| 51 | fCurrentBatch->fBatchListID = index; |
| 52 | fCurrentBatch->fChildID = batch.fChildren.count(); |
| 53 | |
| 54 | // Update the bounds and store a pointer to the new batch |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 55 | batch.fChildren.push_back(fCurrentBatch); |
| 56 | batch.fBounds = combiner->bounds(); |
| 57 | } |
| 58 | |
| 59 | void GrAuditTrail::batchingResultNew(GrBatch* batch) { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 60 | // Our algorithm doesn't bother to reorder inside of a BatchNode |
| 61 | // so the ChildID will start at 0 |
| 62 | fCurrentBatch->fBatchListID = fBatchList.count(); |
| 63 | fCurrentBatch->fChildID = 0; |
| 64 | |
| 65 | // We use the batch pointer as a key to find the batchnode we are 'glomming' batches onto |
| 66 | fIDLookup.set(batch, fCurrentBatch->fBatchListID); |
| 67 | BatchNode* batchNode = new BatchNode; |
| 68 | batchNode->fBounds = fCurrentBatch->fBounds; |
joshualitt | 1d7decf | 2016-03-01 07:15:52 -0800 | [diff] [blame] | 69 | batchNode->fRenderTargetUniqueID = batch->renderTargetUniqueID(); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 70 | batchNode->fChildren.push_back(fCurrentBatch); |
| 71 | fBatchList.emplace_back(batchNode); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 72 | } |
| 73 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame^] | 74 | void GrAuditTrail::copyOutFromBatchList(BatchInfo* outBatchInfo, int batchListID) { |
| 75 | SkASSERT(batchListID < fBatchList.count()); |
| 76 | const BatchNode* bn = fBatchList[batchListID]; |
| 77 | outBatchInfo->fBounds = bn->fBounds; |
| 78 | outBatchInfo->fRenderTargetUniqueID = bn->fRenderTargetUniqueID; |
| 79 | for (int j = 0; j < bn->fChildren.count(); j++) { |
| 80 | BatchInfo::Batch& outBatch = outBatchInfo->fBatches.push_back(); |
| 81 | const Batch* currentBatch = bn->fChildren[j]; |
| 82 | outBatch.fBounds = currentBatch->fBounds; |
| 83 | outBatch.fClientID = currentBatch->fClientID; |
| 84 | } |
| 85 | } |
| 86 | |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 87 | void GrAuditTrail::getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID) { |
| 88 | Batches** batchesLookup = fClientIDLookup.find(clientID); |
| 89 | if (batchesLookup) { |
| 90 | // We track which batchlistID we're currently looking at. If it changes, then we |
| 91 | // need to push back a new batch info struct. We happen to know that batches are |
| 92 | // in sequential order in the batchlist, otherwise we'd have to do more bookkeeping |
| 93 | int currentBatchListID = kGrAuditTrailInvalidID; |
| 94 | for (int i = 0; i < (*batchesLookup)->count(); i++) { |
| 95 | const Batch* batch = (**batchesLookup)[i]; |
| 96 | |
| 97 | // Because we will copy out all of the batches associated with a given |
| 98 | // batch list id everytime the id changes, we only have to update our struct |
| 99 | // when the id changes. |
| 100 | if (kGrAuditTrailInvalidID == currentBatchListID || |
| 101 | batch->fBatchListID != currentBatchListID) { |
| 102 | BatchInfo& outBatchInfo = outInfo->push_back(); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 103 | |
| 104 | // copy out all of the batches so the client can display them even if |
| 105 | // they have a different clientID |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame^] | 106 | this->copyOutFromBatchList(&outBatchInfo, batch->fBatchListID); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame^] | 112 | void GrAuditTrail::getBoundsByBatchListID(BatchInfo* outInfo, int batchListID) { |
| 113 | this->copyOutFromBatchList(outInfo, batchListID); |
| 114 | } |
| 115 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 116 | void GrAuditTrail::fullReset() { |
| 117 | SkASSERT(fEnabled); |
| 118 | fBatchList.reset(); |
| 119 | fIDLookup.reset(); |
| 120 | // free all client batches |
| 121 | fClientIDLookup.foreach([](const int&, Batches** batches) { delete *batches; }); |
| 122 | fClientIDLookup.reset(); |
| 123 | fBatchPool.reset(); // must be last, frees all of the memory |
| 124 | } |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 125 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 126 | template <typename T> |
| 127 | void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const T& array, |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 128 | bool addComma) { |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 129 | if (array.count()) { |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 130 | if (addComma) { |
| 131 | json->appendf(","); |
| 132 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 133 | json->appendf("\"%s\": [", name); |
| 134 | for (int i = 0; i < array.count(); i++) { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 135 | json->append(array[i]->toJson()); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 136 | if (i < array.count() - 1) { |
| 137 | json->append(","); |
| 138 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 139 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 140 | json->append("]"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 141 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // This will pretty print a very small subset of json |
| 145 | // The parsing rules are straightforward, aside from the fact that we do not want an extra newline |
| 146 | // before ',' and after '}', so we have a comma exception rule. |
| 147 | class PrettyPrintJson { |
| 148 | public: |
| 149 | SkString prettify(const SkString& json) { |
| 150 | fPrettyJson.reset(); |
| 151 | fTabCount = 0; |
| 152 | fFreshLine = false; |
| 153 | fCommaException = false; |
| 154 | for (size_t i = 0; i < json.size(); i++) { |
| 155 | if ('[' == json[i] || '{' == json[i]) { |
| 156 | this->newline(); |
| 157 | this->appendChar(json[i]); |
| 158 | fTabCount++; |
| 159 | this->newline(); |
| 160 | } else if (']' == json[i] || '}' == json[i]) { |
| 161 | fTabCount--; |
| 162 | this->newline(); |
| 163 | this->appendChar(json[i]); |
| 164 | fCommaException = true; |
| 165 | } else if (',' == json[i]) { |
| 166 | this->appendChar(json[i]); |
| 167 | this->newline(); |
| 168 | } else { |
| 169 | this->appendChar(json[i]); |
| 170 | } |
| 171 | } |
| 172 | return fPrettyJson; |
| 173 | } |
| 174 | private: |
| 175 | void appendChar(char appendee) { |
| 176 | if (fCommaException && ',' != appendee) { |
| 177 | this->newline(); |
| 178 | } |
| 179 | this->tab(); |
| 180 | fPrettyJson += appendee; |
| 181 | fFreshLine = false; |
| 182 | fCommaException = false; |
| 183 | } |
| 184 | |
| 185 | void tab() { |
| 186 | if (fFreshLine) { |
| 187 | for (int i = 0; i < fTabCount; i++) { |
| 188 | fPrettyJson += '\t'; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void newline() { |
| 194 | if (!fFreshLine) { |
| 195 | fFreshLine = true; |
| 196 | fPrettyJson += '\n'; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | SkString fPrettyJson; |
| 201 | int fTabCount; |
| 202 | bool fFreshLine; |
| 203 | bool fCommaException; |
| 204 | }; |
| 205 | |
| 206 | static SkString pretty_print_json(SkString json) { |
| 207 | class PrettyPrintJson prettyPrintJson; |
| 208 | return prettyPrintJson.prettify(json); |
| 209 | } |
| 210 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 211 | SkString GrAuditTrail::toJson(bool prettyPrint) const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 212 | SkString json; |
| 213 | json.append("{"); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 214 | JsonifyTArray(&json, "Batches", fBatchList, false); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 215 | json.append("}"); |
| 216 | |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame] | 217 | if (prettyPrint) { |
| 218 | return pretty_print_json(json); |
| 219 | } else { |
| 220 | return json; |
| 221 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 222 | } |
| 223 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 224 | SkString GrAuditTrail::toJson(int clientID, bool prettyPrint) const { |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 225 | SkString json; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 226 | json.append("{"); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 227 | Batches** batches = fClientIDLookup.find(clientID); |
| 228 | if (batches) { |
| 229 | JsonifyTArray(&json, "Batches", **batches, false); |
| 230 | } |
| 231 | json.appendf("}"); |
| 232 | |
| 233 | if (prettyPrint) { |
| 234 | return pretty_print_json(json); |
| 235 | } else { |
| 236 | return json; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static void skrect_to_json(SkString* json, const char* name, const SkRect& rect) { |
| 241 | json->appendf("\"%s\": {", name); |
| 242 | json->appendf("\"Left\": %f,", rect.fLeft); |
| 243 | json->appendf("\"Right\": %f,", rect.fRight); |
| 244 | json->appendf("\"Top\": %f,", rect.fTop); |
| 245 | json->appendf("\"Bottom\": %f", rect.fBottom); |
| 246 | json->append("}"); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 247 | } |
| 248 | |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 249 | SkString GrAuditTrail::Batch::toJson() const { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 250 | SkString json; |
| 251 | json.append("{"); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 252 | json.appendf("\"Name\": \"%s\",", fName.c_str()); |
| 253 | json.appendf("\"ClientID\": \"%d\",", fClientID); |
| 254 | json.appendf("\"BatchListID\": \"%d\",", fBatchListID); |
| 255 | json.appendf("\"ChildID\": \"%d\",", fChildID); |
| 256 | skrect_to_json(&json, "Bounds", fBounds); |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 257 | if (fStackTrace.count()) { |
| 258 | json.append(",\"Stack\": ["); |
| 259 | for (int i = 0; i < fStackTrace.count(); i++) { |
| 260 | json.appendf("\"%s\"", fStackTrace[i].c_str()); |
| 261 | if (i < fStackTrace.count() - 1) { |
| 262 | json.append(","); |
| 263 | } |
| 264 | } |
| 265 | json.append("]"); |
| 266 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 267 | json.append("}"); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 268 | return json; |
| 269 | } |
| 270 | |
| 271 | SkString GrAuditTrail::BatchNode::toJson() const { |
| 272 | SkString json; |
| 273 | json.append("{"); |
joshualitt | 1d7decf | 2016-03-01 07:15:52 -0800 | [diff] [blame] | 274 | json.appendf("\"RenderTarget\": \"%u\",", fRenderTargetUniqueID); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 275 | skrect_to_json(&json, "Bounds", fBounds); |
| 276 | JsonifyTArray(&json, "Batches", fChildren, true); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 277 | json.append("}"); |
| 278 | return json; |
| 279 | } |