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