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