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