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 | #ifndef GrAuditTrail_DEFINED |
| 9 | #define GrAuditTrail_DEFINED |
| 10 | |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 11 | #include "GrConfig.h" |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 12 | #include "SkRect.h" |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 13 | #include "SkString.h" |
| 14 | #include "SkTArray.h" |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 15 | #include "SkTHash.h" |
| 16 | |
| 17 | class GrBatch; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them |
| 21 | * to json. |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 22 | * |
| 23 | * Capturing this information is expensive and consumes a lot of memory, therefore it is important |
| 24 | * to enable auditing only when required and disable it promptly. The AutoEnable class helps to |
| 25 | * ensure that the audit trail is disabled in a timely fashion. Once the information has been dealt |
| 26 | * with, be sure to call reset(), or the log will simply keep growing. |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 27 | */ |
| 28 | class GrAuditTrail { |
| 29 | public: |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 30 | GrAuditTrail() |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 31 | : fClientID(kGrAuditTrailInvalidID) |
| 32 | , fEnabled(false) {} |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 33 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 34 | class AutoEnable { |
| 35 | public: |
| 36 | AutoEnable(GrAuditTrail* auditTrail) |
| 37 | : fAuditTrail(auditTrail) { |
| 38 | SkASSERT(!fAuditTrail->isEnabled()); |
| 39 | fAuditTrail->setEnabled(true); |
| 40 | } |
| 41 | |
| 42 | ~AutoEnable() { |
| 43 | SkASSERT(fAuditTrail->isEnabled()); |
| 44 | fAuditTrail->setEnabled(false); |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | GrAuditTrail* fAuditTrail; |
| 49 | }; |
| 50 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 51 | class AutoManageBatchList { |
| 52 | public: |
| 53 | AutoManageBatchList(GrAuditTrail* auditTrail) |
| 54 | : fAutoEnable(auditTrail) |
| 55 | , fAuditTrail(auditTrail) { |
| 56 | } |
| 57 | |
| 58 | ~AutoManageBatchList() { |
| 59 | fAuditTrail->fullReset(); |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | AutoEnable fAutoEnable; |
| 64 | GrAuditTrail* fAuditTrail; |
| 65 | }; |
| 66 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 67 | class AutoCollectBatches { |
| 68 | public: |
| 69 | AutoCollectBatches(GrAuditTrail* auditTrail, int clientID) |
| 70 | : fAutoEnable(auditTrail) |
| 71 | , fAuditTrail(auditTrail) { |
| 72 | fAuditTrail->setClientID(clientID); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 73 | } |
| 74 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 75 | ~AutoCollectBatches() { fAuditTrail->setClientID(kGrAuditTrailInvalidID); } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 76 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 77 | private: |
| 78 | AutoEnable fAutoEnable; |
| 79 | GrAuditTrail* fAuditTrail; |
| 80 | }; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 81 | |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 82 | void pushFrame(const char* framename) { |
| 83 | SkASSERT(fEnabled); |
| 84 | fCurrentStackTrace.push_back(SkString(framename)); |
| 85 | } |
| 86 | |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 87 | void addBatch(const GrBatch* batch); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 88 | |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 89 | void batchingResultCombined(const GrBatch* consumer, const GrBatch* consumed); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 90 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 91 | // Because batching is heavily dependent on sequence of draw calls, these calls will only |
| 92 | // produce valid information for the given draw sequence which preceeded them. |
| 93 | // Specifically, future draw calls may change the batching and thus would invalidate |
| 94 | // the json. What this means is that for some sequence of draw calls N, the below toJson |
| 95 | // calls will only produce JSON which reflects N draw calls. This JSON may or may not be |
| 96 | // accurate for N + 1 or N - 1 draws depending on the actual batching algorithm used. |
| 97 | SkString toJson(bool prettyPrint = false) const; |
| 98 | |
| 99 | // returns a json string of all of the batches associated with a given client id |
| 100 | SkString toJson(int clientID, bool prettyPrint = false) const; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 101 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 102 | bool isEnabled() { return fEnabled; } |
| 103 | void setEnabled(bool enabled) { fEnabled = enabled; } |
| 104 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 105 | void setClientID(int clientID) { fClientID = clientID; } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 106 | |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 107 | // We could just return our internal bookkeeping struct if copying the data out becomes |
| 108 | // a performance issue, but until then its nice to decouple |
| 109 | struct BatchInfo { |
| 110 | SkRect fBounds; |
joshualitt | 1d7decf | 2016-03-01 07:15:52 -0800 | [diff] [blame] | 111 | uint32_t fRenderTargetUniqueID; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 112 | struct Batch { |
| 113 | int fClientID; |
| 114 | SkRect fBounds; |
| 115 | }; |
| 116 | SkTArray<Batch> fBatches; |
| 117 | }; |
| 118 | |
| 119 | void getBoundsByClientID(SkTArray<BatchInfo>* outInfo, int clientID); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 120 | void getBoundsByBatchListID(BatchInfo* outInfo, int batchListID); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 121 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 122 | void fullReset(); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 123 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 124 | static const int kGrAuditTrailInvalidID; |
| 125 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 126 | private: |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 127 | // TODO if performance becomes an issue, we can move to using SkVarAlloc |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 128 | struct Batch { |
| 129 | SkString toJson() const; |
| 130 | SkString fName; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 131 | SkTArray<SkString> fStackTrace; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 132 | SkRect fBounds; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 133 | int fClientID; |
| 134 | int fBatchListID; |
| 135 | int fChildID; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 136 | }; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 137 | typedef SkTArray<SkAutoTDelete<Batch>, true> BatchPool; |
| 138 | |
| 139 | typedef SkTArray<Batch*> Batches; |
| 140 | |
| 141 | struct BatchNode { |
| 142 | SkString toJson() const; |
| 143 | SkRect fBounds; |
| 144 | Batches fChildren; |
joshualitt | 1d7decf | 2016-03-01 07:15:52 -0800 | [diff] [blame] | 145 | uint32_t fRenderTargetUniqueID; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 146 | }; |
| 147 | typedef SkTArray<SkAutoTDelete<BatchNode>, true> BatchList; |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 148 | |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 149 | void copyOutFromBatchList(BatchInfo* outBatchInfo, int batchListID); |
| 150 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 151 | template <typename T> |
| 152 | static void JsonifyTArray(SkString* json, const char* name, const T& array, |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 153 | bool addComma); |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 154 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 155 | BatchPool fBatchPool; |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 156 | SkTHashMap<uint32_t, int> fIDLookup; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 157 | SkTHashMap<int, Batches*> fClientIDLookup; |
| 158 | BatchList fBatchList; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 159 | SkTArray<SkString> fCurrentStackTrace; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 160 | |
| 161 | // The client cas pass in an optional client ID which we will use to mark the batches |
| 162 | int fClientID; |
| 163 | bool fEnabled; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 166 | #define GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, invoke, ...) \ |
| 167 | if (audit_trail->isEnabled()) { \ |
| 168 | audit_trail->invoke(__VA_ARGS__); \ |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 169 | } |
| 170 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 171 | #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 172 | GR_AUDIT_TRAIL_INVOKE_GUARD((audit_trail), pushFrame, framename); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 173 | |
| 174 | #define GR_AUDIT_TRAIL_RESET(audit_trail) \ |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 175 | //GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, fullReset); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 176 | |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 177 | #define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batch) \ |
| 178 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addBatch, batch); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 179 | |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 180 | #define GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(audit_trail, combineWith, batch) \ |
| 181 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, batchingResultCombined, combineWith, batch); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 182 | |
| 183 | #define GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(audit_trail, batch) \ |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 184 | // Doesn't do anything now, one day... |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 185 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 186 | #endif |