blob: 752074de9c0ac1ed1b33b093a90e066c33d563fb [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
joshualittdf3f2b02016-03-01 07:47:56 -080013void GrAuditTrail::addBatch(const char* name, const SkRect& bounds) {
14 SkASSERT(fEnabled);
15 Batch* batch = new Batch;
16 fBatchPool.emplace_back(batch);
17 batch->fName = name;
18 batch->fBounds = bounds;
19 batch->fClientID = kGrAuditTrailInvalidID;
20 batch->fBatchListID = kGrAuditTrailInvalidID;
21 batch->fChildID = kGrAuditTrailInvalidID;
joshualittf55c3642016-03-02 08:11:34 -080022
23 // consume the current stack trace if any
24 batch->fStackTrace = fCurrentStackTrace;
25 fCurrentStackTrace.reset();
joshualittdf3f2b02016-03-01 07:47:56 -080026 fCurrentBatch = batch;
27
28 if (fClientID != kGrAuditTrailInvalidID) {
29 batch->fClientID = fClientID;
30 Batches** batchesLookup = fClientIDLookup.find(fClientID);
31 Batches* batches = nullptr;
32 if (!batchesLookup) {
33 batches = new Batches;
34 fClientIDLookup.set(fClientID, batches);
35 } else {
36 batches = *batchesLookup;
37 }
38
39 batches->push_back(fCurrentBatch);
40 }
41}
42
joshualitt18d6b752016-02-26 08:07:50 -080043void GrAuditTrail::batchingResultCombined(GrBatch* combiner) {
44 int* indexPtr = fIDLookup.find(combiner);
45 SkASSERT(indexPtr);
46 int index = *indexPtr;
joshualittb95c7722016-02-29 07:44:02 -080047 SkASSERT(index < fBatchList.count());
48 BatchNode& batch = *fBatchList[index];
joshualitt18d6b752016-02-26 08:07:50 -080049
joshualittb95c7722016-02-29 07:44:02 -080050 // set the ids for the child batch
51 fCurrentBatch->fBatchListID = index;
52 fCurrentBatch->fChildID = batch.fChildren.count();
53
54 // Update the bounds and store a pointer to the new batch
joshualitt18d6b752016-02-26 08:07:50 -080055 batch.fChildren.push_back(fCurrentBatch);
56 batch.fBounds = combiner->bounds();
57}
58
59void GrAuditTrail::batchingResultNew(GrBatch* batch) {
joshualittb95c7722016-02-29 07:44:02 -080060 // Our algorithm doesn't bother to reorder inside of a BatchNode
61 // so the ChildID will start at 0
62 fCurrentBatch->fBatchListID = fBatchList.count();
63 fCurrentBatch->fChildID = 0;
64
65 // We use the batch pointer as a key to find the batchnode we are 'glomming' batches onto
66 fIDLookup.set(batch, fCurrentBatch->fBatchListID);
67 BatchNode* batchNode = new BatchNode;
68 batchNode->fBounds = fCurrentBatch->fBounds;
joshualitt1d7decf2016-03-01 07:15:52 -080069 batchNode->fRenderTargetUniqueID = batch->renderTargetUniqueID();
joshualittb95c7722016-02-29 07:44:02 -080070 batchNode->fChildren.push_back(fCurrentBatch);
71 fBatchList.emplace_back(batchNode);
joshualitt18d6b752016-02-26 08:07:50 -080072}
73
joshualitt46b301d2016-03-02 08:32:37 -080074void GrAuditTrail::copyOutFromBatchList(BatchInfo* outBatchInfo, int batchListID) {
75 SkASSERT(batchListID < fBatchList.count());
76 const BatchNode* bn = fBatchList[batchListID];
77 outBatchInfo->fBounds = bn->fBounds;
78 outBatchInfo->fRenderTargetUniqueID = bn->fRenderTargetUniqueID;
79 for (int j = 0; j < bn->fChildren.count(); j++) {
80 BatchInfo::Batch& outBatch = outBatchInfo->fBatches.push_back();
81 const Batch* currentBatch = bn->fChildren[j];
82 outBatch.fBounds = currentBatch->fBounds;
83 outBatch.fClientID = currentBatch->fClientID;
84 }
85}
86
joshualitt10d8fc22016-02-29 11:15:06 -080087void GrAuditTrail::getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID) {
88 Batches** batchesLookup = fClientIDLookup.find(clientID);
89 if (batchesLookup) {
90 // We track which batchlistID we're currently looking at. If it changes, then we
91 // need to push back a new batch info struct. We happen to know that batches are
92 // in sequential order in the batchlist, otherwise we'd have to do more bookkeeping
93 int currentBatchListID = kGrAuditTrailInvalidID;
94 for (int i = 0; i < (*batchesLookup)->count(); i++) {
95 const Batch* batch = (**batchesLookup)[i];
96
97 // Because we will copy out all of the batches associated with a given
98 // batch list id everytime the id changes, we only have to update our struct
99 // when the id changes.
100 if (kGrAuditTrailInvalidID == currentBatchListID ||
101 batch->fBatchListID != currentBatchListID) {
102 BatchInfo& outBatchInfo = outInfo->push_back();
joshualitt10d8fc22016-02-29 11:15:06 -0800103
104 // copy out all of the batches so the client can display them even if
105 // they have a different clientID
joshualitt46b301d2016-03-02 08:32:37 -0800106 this->copyOutFromBatchList(&outBatchInfo, batch->fBatchListID);
joshualitt10d8fc22016-02-29 11:15:06 -0800107 }
108 }
109 }
110}
111
joshualitt46b301d2016-03-02 08:32:37 -0800112void GrAuditTrail::getBoundsByBatchListID(BatchInfo* outInfo, int batchListID) {
113 this->copyOutFromBatchList(outInfo, batchListID);
114}
115
joshualittdf3f2b02016-03-01 07:47:56 -0800116void GrAuditTrail::fullReset() {
117 SkASSERT(fEnabled);
118 fBatchList.reset();
119 fIDLookup.reset();
120 // free all client batches
121 fClientIDLookup.foreach([](const int&, Batches** batches) { delete *batches; });
122 fClientIDLookup.reset();
123 fBatchPool.reset(); // must be last, frees all of the memory
124}
joshualitt10d8fc22016-02-29 11:15:06 -0800125
joshualitt18d6b752016-02-26 08:07:50 -0800126template <typename T>
127void GrAuditTrail::JsonifyTArray(SkString* json, const char* name, const T& array,
joshualittadab5a22016-02-18 05:04:39 -0800128 bool addComma) {
joshualitt87a721b2016-01-12 12:59:28 -0800129 if (array.count()) {
joshualittadab5a22016-02-18 05:04:39 -0800130 if (addComma) {
131 json->appendf(",");
132 }
joshualitt87a721b2016-01-12 12:59:28 -0800133 json->appendf("\"%s\": [", name);
134 for (int i = 0; i < array.count(); i++) {
joshualitt11fae872016-01-14 10:58:07 -0800135 json->append(array[i]->toJson());
joshualitt87a721b2016-01-12 12:59:28 -0800136 if (i < array.count() - 1) {
137 json->append(",");
138 }
joshualitt27a48dc2016-01-08 07:19:47 -0800139 }
joshualitt87a721b2016-01-12 12:59:28 -0800140 json->append("]");
joshualitt27a48dc2016-01-08 07:19:47 -0800141 }
joshualitt086cee12016-01-12 06:45:24 -0800142}
143
144// This will pretty print a very small subset of json
145// The parsing rules are straightforward, aside from the fact that we do not want an extra newline
146// before ',' and after '}', so we have a comma exception rule.
147class PrettyPrintJson {
148public:
149 SkString prettify(const SkString& json) {
150 fPrettyJson.reset();
151 fTabCount = 0;
152 fFreshLine = false;
153 fCommaException = false;
154 for (size_t i = 0; i < json.size(); i++) {
155 if ('[' == json[i] || '{' == json[i]) {
156 this->newline();
157 this->appendChar(json[i]);
158 fTabCount++;
159 this->newline();
160 } else if (']' == json[i] || '}' == json[i]) {
161 fTabCount--;
162 this->newline();
163 this->appendChar(json[i]);
164 fCommaException = true;
165 } else if (',' == json[i]) {
166 this->appendChar(json[i]);
167 this->newline();
168 } else {
169 this->appendChar(json[i]);
170 }
171 }
172 return fPrettyJson;
173 }
174private:
175 void appendChar(char appendee) {
176 if (fCommaException && ',' != appendee) {
177 this->newline();
178 }
179 this->tab();
180 fPrettyJson += appendee;
181 fFreshLine = false;
182 fCommaException = false;
183 }
184
185 void tab() {
186 if (fFreshLine) {
187 for (int i = 0; i < fTabCount; i++) {
188 fPrettyJson += '\t';
189 }
190 }
191 }
192
193 void newline() {
194 if (!fFreshLine) {
195 fFreshLine = true;
196 fPrettyJson += '\n';
197 }
198 }
199
200 SkString fPrettyJson;
201 int fTabCount;
202 bool fFreshLine;
203 bool fCommaException;
204};
205
206static SkString pretty_print_json(SkString json) {
207 class PrettyPrintJson prettyPrintJson;
208 return prettyPrintJson.prettify(json);
209}
210
joshualittb95c7722016-02-29 07:44:02 -0800211SkString GrAuditTrail::toJson(bool prettyPrint) const {
joshualitt086cee12016-01-12 06:45:24 -0800212 SkString json;
213 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800214 JsonifyTArray(&json, "Batches", fBatchList, false);
joshualitt086cee12016-01-12 06:45:24 -0800215 json.append("}");
216
joshualitt6b3cf732016-02-17 11:20:26 -0800217 if (prettyPrint) {
218 return pretty_print_json(json);
219 } else {
220 return json;
221 }
joshualitt27a48dc2016-01-08 07:19:47 -0800222}
223
joshualittb95c7722016-02-29 07:44:02 -0800224SkString GrAuditTrail::toJson(int clientID, bool prettyPrint) const {
joshualitt27a48dc2016-01-08 07:19:47 -0800225 SkString json;
joshualitt086cee12016-01-12 06:45:24 -0800226 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800227 Batches** batches = fClientIDLookup.find(clientID);
228 if (batches) {
229 JsonifyTArray(&json, "Batches", **batches, false);
230 }
231 json.appendf("}");
232
233 if (prettyPrint) {
234 return pretty_print_json(json);
235 } else {
236 return json;
237 }
238}
239
240static void skrect_to_json(SkString* json, const char* name, const SkRect& rect) {
241 json->appendf("\"%s\": {", name);
242 json->appendf("\"Left\": %f,", rect.fLeft);
243 json->appendf("\"Right\": %f,", rect.fRight);
244 json->appendf("\"Top\": %f,", rect.fTop);
245 json->appendf("\"Bottom\": %f", rect.fBottom);
246 json->append("}");
joshualitt27a48dc2016-01-08 07:19:47 -0800247}
248
joshualitt11fae872016-01-14 10:58:07 -0800249SkString GrAuditTrail::Batch::toJson() const {
joshualitt086cee12016-01-12 06:45:24 -0800250 SkString json;
251 json.append("{");
joshualittb95c7722016-02-29 07:44:02 -0800252 json.appendf("\"Name\": \"%s\",", fName.c_str());
253 json.appendf("\"ClientID\": \"%d\",", fClientID);
254 json.appendf("\"BatchListID\": \"%d\",", fBatchListID);
255 json.appendf("\"ChildID\": \"%d\",", fChildID);
256 skrect_to_json(&json, "Bounds", fBounds);
joshualittf55c3642016-03-02 08:11:34 -0800257 if (fStackTrace.count()) {
258 json.append(",\"Stack\": [");
259 for (int i = 0; i < fStackTrace.count(); i++) {
260 json.appendf("\"%s\"", fStackTrace[i].c_str());
261 if (i < fStackTrace.count() - 1) {
262 json.append(",");
263 }
264 }
265 json.append("]");
266 }
joshualitt086cee12016-01-12 06:45:24 -0800267 json.append("}");
joshualittb95c7722016-02-29 07:44:02 -0800268 return json;
269}
270
271SkString GrAuditTrail::BatchNode::toJson() const {
272 SkString json;
273 json.append("{");
joshualitt1d7decf2016-03-01 07:15:52 -0800274 json.appendf("\"RenderTarget\": \"%u\",", fRenderTargetUniqueID);
joshualittb95c7722016-02-29 07:44:02 -0800275 skrect_to_json(&json, "Bounds", fBounds);
276 JsonifyTArray(&json, "Batches", fChildren, true);
joshualitt086cee12016-01-12 06:45:24 -0800277 json.append("}");
278 return json;
279}