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" |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 10 | #include "SkJSONWriter.h" |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 11 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 12 | const int GrAuditTrail::kGrAuditTrailInvalidID = -1; |
| 13 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 14 | void GrAuditTrail::addOp(const GrOp* op, GrRenderTargetProxy::UniqueID proxyID) { |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 15 | SkASSERT(fEnabled); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 16 | Op* auditOp = new Op; |
| 17 | fOpPool.emplace_back(auditOp); |
| 18 | auditOp->fName = op->name(); |
| 19 | auditOp->fBounds = op->bounds(); |
| 20 | auditOp->fClientID = kGrAuditTrailInvalidID; |
| 21 | auditOp->fOpListID = kGrAuditTrailInvalidID; |
| 22 | auditOp->fChildID = kGrAuditTrailInvalidID; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 23 | |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 24 | // consume the current stack trace if any |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 25 | auditOp->fStackTrace = fCurrentStackTrace; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 26 | fCurrentStackTrace.reset(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 27 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 28 | if (fClientID != kGrAuditTrailInvalidID) { |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 29 | auditOp->fClientID = fClientID; |
| 30 | Ops** opsLookup = fClientIDLookup.find(fClientID); |
| 31 | Ops* ops = nullptr; |
| 32 | if (!opsLookup) { |
| 33 | ops = new Ops; |
| 34 | fClientIDLookup.set(fClientID, ops); |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 35 | } else { |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 36 | ops = *opsLookup; |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 39 | ops->push_back(auditOp); |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 40 | } |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 41 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 42 | // Our algorithm doesn't bother to reorder inside of an OpNode so the ChildID will start at 0 |
| 43 | auditOp->fOpListID = fOpList.count(); |
| 44 | auditOp->fChildID = 0; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 45 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 46 | // We use the op pointer as a key to find the OpNode we are 'glomming' ops onto |
| 47 | fIDLookup.set(op->uniqueID(), auditOp->fOpListID); |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 48 | OpNode* opNode = new OpNode(proxyID); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 49 | opNode->fBounds = op->bounds(); |
| 50 | opNode->fChildren.push_back(auditOp); |
| 51 | fOpList.emplace_back(opNode); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 54 | void GrAuditTrail::opsCombined(const GrOp* consumer, const GrOp* consumed) { |
| 55 | // Look up the op we are going to glom onto |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 56 | int* indexPtr = fIDLookup.find(consumer->uniqueID()); |
| 57 | SkASSERT(indexPtr); |
| 58 | int index = *indexPtr; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 59 | SkASSERT(index < fOpList.count() && fOpList[index]); |
| 60 | OpNode& consumerOp = *fOpList[index]; |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 61 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 62 | // Look up the op which will be glommed |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 63 | int* consumedPtr = fIDLookup.find(consumed->uniqueID()); |
| 64 | SkASSERT(consumedPtr); |
| 65 | int consumedIndex = *consumedPtr; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 66 | SkASSERT(consumedIndex < fOpList.count() && fOpList[consumedIndex]); |
| 67 | OpNode& consumedOp = *fOpList[consumedIndex]; |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 68 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 69 | // steal all of consumed's ops |
| 70 | for (int i = 0; i < consumedOp.fChildren.count(); i++) { |
| 71 | Op* childOp = consumedOp.fChildren[i]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 73 | // set the ids for the child op |
| 74 | childOp->fOpListID = index; |
| 75 | childOp->fChildID = consumerOp.fChildren.count(); |
| 76 | consumerOp.fChildren.push_back(childOp); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 77 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 78 | |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 79 | // Update the bounds for the combineWith node |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 80 | consumerOp.fBounds = consumer->bounds(); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 81 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 82 | // remove the old node from our opList and clear the combinee's lookup |
| 83 | // NOTE: because we can't change the shape of the oplist, we use a sentinel |
| 84 | fOpList[consumedIndex].reset(nullptr); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 85 | fIDLookup.remove(consumed->uniqueID()); |
| 86 | } |
| 87 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 88 | void GrAuditTrail::copyOutFromOpList(OpInfo* outOpInfo, int opListID) { |
| 89 | SkASSERT(opListID < fOpList.count()); |
| 90 | const OpNode* bn = fOpList[opListID].get(); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 91 | SkASSERT(bn); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 92 | outOpInfo->fBounds = bn->fBounds; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 93 | outOpInfo->fProxyUniqueID = bn->fProxyUniqueID; |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 94 | for (int j = 0; j < bn->fChildren.count(); j++) { |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 95 | OpInfo::Op& outOp = outOpInfo->fOps.push_back(); |
| 96 | const Op* currentOp = bn->fChildren[j]; |
| 97 | outOp.fBounds = currentOp->fBounds; |
| 98 | outOp.fClientID = currentOp->fClientID; |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 102 | void GrAuditTrail::getBoundsByClientID(SkTArray<OpInfo>* outInfo, int clientID) { |
| 103 | Ops** opsLookup = fClientIDLookup.find(clientID); |
| 104 | if (opsLookup) { |
| 105 | // We track which oplistID we're currently looking at. If it changes, then we need to push |
| 106 | // back a new op info struct. We happen to know that ops are in sequential order in the |
| 107 | // oplist, otherwise we'd have to do more bookkeeping |
| 108 | int currentOpListID = kGrAuditTrailInvalidID; |
| 109 | for (int i = 0; i < (*opsLookup)->count(); i++) { |
| 110 | const Op* op = (**opsLookup)[i]; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 111 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 112 | // Because we will copy out all of the ops associated with a given op list id everytime |
| 113 | // the id changes, we only have to update our struct when the id changes. |
| 114 | if (kGrAuditTrailInvalidID == currentOpListID || op->fOpListID != currentOpListID) { |
| 115 | OpInfo& outOpInfo = outInfo->push_back(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 116 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 117 | // copy out all of the ops so the client can display them even if they have a |
| 118 | // different clientID |
| 119 | this->copyOutFromOpList(&outOpInfo, op->fOpListID); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 125 | void GrAuditTrail::getBoundsByOpListID(OpInfo* outInfo, int opListID) { |
| 126 | this->copyOutFromOpList(outInfo, opListID); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 127 | } |
| 128 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 129 | void GrAuditTrail::fullReset() { |
| 130 | SkASSERT(fEnabled); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 131 | fOpList.reset(); |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 132 | fIDLookup.reset(); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 133 | // free all client ops |
| 134 | fClientIDLookup.foreach ([](const int&, Ops** ops) { delete *ops; }); |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 135 | fClientIDLookup.reset(); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 136 | fOpPool.reset(); // must be last, frees all of the memory |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 137 | } |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 138 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 139 | template <typename T> |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 140 | void GrAuditTrail::JsonifyTArray(SkJSONWriter& writer, const char* name, const T& array) { |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 141 | if (array.count()) { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 142 | writer.beginArray(name); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 143 | for (int i = 0; i < array.count(); i++) { |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 144 | // Handle sentinel nullptrs |
joshualitt | ae47aee | 2016-03-10 13:29:36 -0800 | [diff] [blame] | 145 | if (array[i]) { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 146 | array[i]->toJson(writer); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 147 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 148 | } |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 149 | writer.endArray(); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 150 | } |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 153 | void GrAuditTrail::toJson(SkJSONWriter& writer) const { |
| 154 | writer.beginObject(); |
| 155 | JsonifyTArray(writer, "Ops", fOpList); |
| 156 | writer.endObject(); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 159 | void GrAuditTrail::toJson(SkJSONWriter& writer, int clientID) const { |
| 160 | writer.beginObject(); |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 161 | Ops** ops = fClientIDLookup.find(clientID); |
| 162 | if (ops) { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 163 | JsonifyTArray(writer, "Ops", **ops); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 164 | } |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 165 | writer.endObject(); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 168 | static void skrect_to_json(SkJSONWriter& writer, const char* name, const SkRect& rect) { |
| 169 | writer.beginObject(name); |
| 170 | writer.appendFloat("Left", rect.fLeft); |
| 171 | writer.appendFloat("Right", rect.fRight); |
| 172 | writer.appendFloat("Top", rect.fTop); |
| 173 | writer.appendFloat("Bottom", rect.fBottom); |
| 174 | writer.endObject(); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 177 | void GrAuditTrail::Op::toJson(SkJSONWriter& writer) const { |
| 178 | writer.beginObject(); |
| 179 | writer.appendString("Name", fName.c_str()); |
| 180 | writer.appendS32("ClientID", fClientID); |
| 181 | writer.appendS32("OpListID", fOpListID); |
| 182 | writer.appendS32("ChildID", fChildID); |
| 183 | skrect_to_json(writer, "Bounds", fBounds); |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 184 | if (fStackTrace.count()) { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 185 | writer.beginArray("Stack"); |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 186 | for (int i = 0; i < fStackTrace.count(); i++) { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 187 | writer.appendString(fStackTrace[i].c_str()); |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 188 | } |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 189 | writer.endArray(); |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 190 | } |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 191 | writer.endObject(); |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 194 | void GrAuditTrail::OpNode::toJson(SkJSONWriter& writer) const { |
| 195 | writer.beginObject(); |
| 196 | writer.appendU32("ProxyID", fProxyUniqueID.asUInt()); |
| 197 | skrect_to_json(writer, "Bounds", fBounds); |
| 198 | JsonifyTArray(writer, "Ops", fChildren); |
| 199 | writer.endObject(); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 200 | } |