blob: 82dc7f713fc2f3dff2a3a3e781355a6a69ee9899 [file] [log] [blame]
joshualitt27a48dc2016-01-08 07:19:47 -08001/*
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"
joshualitt18d6b752016-02-26 08:07:50 -08009#include "batches/GrBatch.h"
joshualitt27a48dc2016-01-08 07:19:47 -080010
joshualittb95c7722016-02-29 07:44:02 -080011const int GrAuditTrail::kGrAuditTrailInvalidID = -1;
12
joshualittb0666ad2016-03-08 10:43:41 -080013void GrAuditTrail::addBatch(const GrBatch* batch) {
joshualittdf3f2b02016-03-01 07:47:56 -080014 SkASSERT(fEnabled);
joshualittb0666ad2016-03-08 10:43:41 -080015 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;
halcanary9d524f22016-03-29 09:03:52 -070022
joshualittf55c3642016-03-02 08:11:34 -080023 // consume the current stack trace if any
joshualittb0666ad2016-03-08 10:43:41 -080024 auditBatch->fStackTrace = fCurrentStackTrace;
joshualittf55c3642016-03-02 08:11:34 -080025 fCurrentStackTrace.reset();
halcanary9d524f22016-03-29 09:03:52 -070026
joshualittdf3f2b02016-03-01 07:47:56 -080027 if (fClientID != kGrAuditTrailInvalidID) {
joshualittb0666ad2016-03-08 10:43:41 -080028 auditBatch->fClientID = fClientID;
joshualittdf3f2b02016-03-01 07:47:56 -080029 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
joshualittb0666ad2016-03-08 10:43:41 -080038 batches->push_back(auditBatch);
joshualittdf3f2b02016-03-01 07:47:56 -080039 }
joshualittdf3f2b02016-03-01 07:47:56 -080040
joshualittb95c7722016-02-29 07:44:02 -080041 // Our algorithm doesn't bother to reorder inside of a BatchNode
42 // so the ChildID will start at 0
joshualittb0666ad2016-03-08 10:43:41 -080043 auditBatch->fBatchListID = fBatchList.count();
44 auditBatch->fChildID = 0;
joshualittb95c7722016-02-29 07:44:02 -080045
46 // We use the batch pointer as a key to find the batchnode we are 'glomming' batches onto
joshualittb0666ad2016-03-08 10:43:41 -080047 fIDLookup.set(batch->uniqueID(), auditBatch->fBatchListID);
joshualittb95c7722016-02-29 07:44:02 -080048 BatchNode* batchNode = new BatchNode;
joshualittb0666ad2016-03-08 10:43:41 -080049 batchNode->fBounds = batch->bounds();
joshualitt1d7decf2016-03-01 07:15:52 -080050 batchNode->fRenderTargetUniqueID = batch->renderTargetUniqueID();
joshualittb0666ad2016-03-08 10:43:41 -080051 batchNode->fChildren.push_back(auditBatch);
joshualittb95c7722016-02-29 07:44:02 -080052 fBatchList.emplace_back(batchNode);
joshualitt18d6b752016-02-26 08:07:50 -080053}
54
joshualittb0666ad2016-03-08 10:43:41 -080055void 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];
halcanary9d524f22016-03-29 09:03:52 -070073
joshualittb0666ad2016-03-08 10:43:41 -080074 // set the ids for the child batch
75 childBatch->fBatchListID = index;
76 childBatch->fChildID = consumerBatch.fChildren.count();
77 consumerBatch.fChildren.push_back(childBatch);
78 }
halcanary9d524f22016-03-29 09:03:52 -070079
joshualittb0666ad2016-03-08 10:43:41 -080080 // 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
joshualitt46b301d2016-03-02 08:32:37 -080089void GrAuditTrail::copyOutFromBatchList(BatchInfo* outBatchInfo, int batchListID) {
90 SkASSERT(batchListID < fBatchList.count());
91 const BatchNode* bn = fBatchList[batchListID];
joshualittb0666ad2016-03-08 10:43:41 -080092 SkASSERT(bn);
joshualitt46b301d2016-03-02 08:32:37 -080093 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
joshualitt10d8fc22016-02-29 11:15:06 -0800103void 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();
halcanary9d524f22016-03-29 09:03:52 -0700119
joshualitt10d8fc22016-02-29 11:15:06 -0800120 // copy out all of the batches so the client can display them even if
121 // they have a different clientID
joshualitt46b301d2016-03-02 08:32:37 -0800122 this->copyOutFromBatchList(&outBatchInfo, batch->fBatchListID);
joshualitt10d8fc22016-02-29 11:15:06 -0800123 }
124 }
125 }
126}
127
joshualitt46b301d2016-03-02 08:32:37 -0800128void GrAuditTrail::getBoundsByBatchListID(BatchInfo* outInfo, int batchListID) {
129 this->copyOutFromBatchList(outInfo, batchListID);
130}
131
joshualittdf3f2b02016-03-01 07:47:56 -0800132void 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}
joshualitt10d8fc22016-02-29 11:15:06 -0800141
joshualitt18d6b752016-02-26 08:07:50 -0800142template <typename T>
143void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const T& array,
joshualittadab5a22016-02-18 05:04:39 -0800144 bool addComma) {
joshualitt87a721b2016-01-12 12:59:28 -0800145 if (array.count()) {
joshualittadab5a22016-02-18 05:04:39 -0800146 if (addComma) {
147 json->appendf(",");
148 }
joshualitt87a721b2016-01-12 12:59:28 -0800149 json->appendf("\"%s\": [", name);
joshualittae47aee2016-03-10 13:29:36 -0800150 const char* separator = "";
joshualitt87a721b2016-01-12 12:59:28 -0800151 for (int i = 0; i < array.count(); i++) {
joshualittb0666ad2016-03-08 10:43:41 -0800152 // Handle sentinel nullptrs
joshualittae47aee2016-03-10 13:29:36 -0800153 if (array[i]) {
154 json->appendf("%s", separator);
155 json->append(array[i]->toJson());
156 separator = ",";
joshualitt87a721b2016-01-12 12:59:28 -0800157 }
joshualitt27a48dc2016-01-08 07:19:47 -0800158 }
joshualitt87a721b2016-01-12 12:59:28 -0800159 json->append("]");
joshualitt27a48dc2016-01-08 07:19:47 -0800160 }
joshualitt086cee12016-01-12 06:45:24 -0800161}
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.
166class PrettyPrintJson {
167public:
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 }
193private:
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
225static SkString pretty_print_json(SkString json) {
226 class PrettyPrintJson prettyPrintJson;
227 return prettyPrintJson.prettify(json);
228}
229
joshualittb95c7722016-02-29 07:44:02 -0800230SkString GrAuditTrail::toJson(bool prettyPrint) const {
joshualitt086cee12016-01-12 06:45:24 -0800231 SkString json;
232 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800233 JsonifyTArray(&json, "Batches", fBatchList, false);
joshualitt086cee12016-01-12 06:45:24 -0800234 json.append("}");
235
joshualitt6b3cf732016-02-17 11:20:26 -0800236 if (prettyPrint) {
237 return pretty_print_json(json);
238 } else {
239 return json;
240 }
joshualitt27a48dc2016-01-08 07:19:47 -0800241}
242
joshualittb95c7722016-02-29 07:44:02 -0800243SkString GrAuditTrail::toJson(int clientID, bool prettyPrint) const {
joshualitt27a48dc2016-01-08 07:19:47 -0800244 SkString json;
joshualitt086cee12016-01-12 06:45:24 -0800245 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800246 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
259static 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("}");
joshualitt27a48dc2016-01-08 07:19:47 -0800266}
267
joshualitt11fae872016-01-14 10:58:07 -0800268SkString GrAuditTrail::Batch::toJson() const {
joshualitt086cee12016-01-12 06:45:24 -0800269 SkString json;
270 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800271 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);
joshualittf55c3642016-03-02 08:11:34 -0800276 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 }
joshualitt086cee12016-01-12 06:45:24 -0800286 json.append("}");
joshualittb95c7722016-02-29 07:44:02 -0800287 return json;
288}
289
290SkString GrAuditTrail::BatchNode::toJson() const {
291 SkString json;
292 json.append("{");
joshualitt1d7decf2016-03-01 07:15:52 -0800293 json.appendf("\"RenderTarget\": \"%u\",", fRenderTargetUniqueID);
joshualittb95c7722016-02-29 07:44:02 -0800294 skrect_to_json(&json, "Bounds", fBounds);
295 JsonifyTArray(&json, "Batches", fChildren, true);
joshualitt086cee12016-01-12 06:45:24 -0800296 json.append("}");
297 return json;
298}