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" |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetProxy.h" |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 14 | #include "SkRect.h" |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 15 | #include "SkString.h" |
| 16 | #include "SkTArray.h" |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 17 | #include "SkTHash.h" |
| 18 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 19 | class GrOp; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them |
| 23 | * to json. |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 24 | * |
| 25 | * Capturing this information is expensive and consumes a lot of memory, therefore it is important |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 26 | * to enable auditing only when required and disable it promptly. The AutoEnable class helps to |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 27 | * ensure that the audit trail is disabled in a timely fashion. Once the information has been dealt |
| 28 | * with, be sure to call reset(), or the log will simply keep growing. |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 29 | */ |
| 30 | class GrAuditTrail { |
| 31 | public: |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 32 | GrAuditTrail() |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 33 | : fClientID(kGrAuditTrailInvalidID) |
| 34 | , fEnabled(false) {} |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 35 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 36 | class AutoEnable { |
| 37 | public: |
| 38 | AutoEnable(GrAuditTrail* auditTrail) |
| 39 | : fAuditTrail(auditTrail) { |
| 40 | SkASSERT(!fAuditTrail->isEnabled()); |
| 41 | fAuditTrail->setEnabled(true); |
| 42 | } |
| 43 | |
| 44 | ~AutoEnable() { |
| 45 | SkASSERT(fAuditTrail->isEnabled()); |
| 46 | fAuditTrail->setEnabled(false); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | GrAuditTrail* fAuditTrail; |
| 51 | }; |
| 52 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 53 | class AutoManageOpList { |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 54 | public: |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 55 | AutoManageOpList(GrAuditTrail* auditTrail) |
| 56 | : fAutoEnable(auditTrail), fAuditTrail(auditTrail) {} |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 57 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 58 | ~AutoManageOpList() { fAuditTrail->fullReset(); } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | AutoEnable fAutoEnable; |
| 62 | GrAuditTrail* fAuditTrail; |
| 63 | }; |
| 64 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 65 | class AutoCollectOps { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 66 | public: |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 67 | AutoCollectOps(GrAuditTrail* auditTrail, int clientID) |
| 68 | : fAutoEnable(auditTrail), fAuditTrail(auditTrail) { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 69 | fAuditTrail->setClientID(clientID); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 72 | ~AutoCollectOps() { fAuditTrail->setClientID(kGrAuditTrailInvalidID); } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 73 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 74 | private: |
| 75 | AutoEnable fAutoEnable; |
| 76 | GrAuditTrail* fAuditTrail; |
| 77 | }; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 78 | |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 79 | void pushFrame(const char* framename) { |
| 80 | SkASSERT(fEnabled); |
| 81 | fCurrentStackTrace.push_back(SkString(framename)); |
| 82 | } |
| 83 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 84 | void addOp(const GrOp*, GrRenderTargetProxy::UniqueID proxyID); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 85 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 86 | void opsCombined(const GrOp* consumer, const GrOp* consumed); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 87 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 88 | // Because op combining is heavily dependent on sequence of draw calls, these calls will only |
| 89 | // produce valid information for the given draw sequence which preceeded them. Specifically, ops |
| 90 | // of future draw calls may combine with previous ops and thus would invalidate the json. What |
| 91 | // this means is that for some sequence of draw calls N, the below toJson calls will only |
| 92 | // produce JSON which reflects N draw calls. This JSON may or may not be accurate for N + 1 or |
| 93 | // N - 1 draws depending on the actual combining algorithm used. |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 94 | SkString toJson(bool prettyPrint = false) const; |
| 95 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 96 | // returns a json string of all of the ops associated with a given client id |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 97 | SkString toJson(int clientID, bool prettyPrint = false) const; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 98 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 99 | bool isEnabled() { return fEnabled; } |
| 100 | void setEnabled(bool enabled) { fEnabled = enabled; } |
| 101 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 102 | void setClientID(int clientID) { fClientID = clientID; } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 103 | |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 104 | // We could just return our internal bookkeeping struct if copying the data out becomes |
| 105 | // a performance issue, but until then its nice to decouple |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 106 | struct OpInfo { |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 107 | struct Op { |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 108 | int fClientID; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 109 | SkRect fBounds; |
| 110 | }; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 111 | |
| 112 | SkRect fBounds; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 113 | GrSurfaceProxy::UniqueID fProxyUniqueID; |
| 114 | SkTArray<Op> fOps; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 117 | void getBoundsByClientID(SkTArray<OpInfo>* outInfo, int clientID); |
| 118 | void getBoundsByOpListID(OpInfo* outInfo, int opListID); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 119 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 120 | void fullReset(); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 121 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 122 | static const int kGrAuditTrailInvalidID; |
| 123 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 124 | private: |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 125 | // TODO if performance becomes an issue, we can move to using SkVarAlloc |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 126 | struct Op { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 127 | SkString toJson() const; |
| 128 | SkString fName; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 129 | SkTArray<SkString> fStackTrace; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 130 | SkRect fBounds; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 131 | int fClientID; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 132 | int fOpListID; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 133 | int fChildID; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 134 | }; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 135 | typedef SkTArray<std::unique_ptr<Op>, true> OpPool; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 136 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 137 | typedef SkTArray<Op*> Ops; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 138 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 139 | struct OpNode { |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 140 | OpNode(const GrSurfaceProxy::UniqueID& proxyID) : fProxyUniqueID(proxyID) { } |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 141 | SkString toJson() const; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 142 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 143 | SkRect fBounds; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 144 | Ops fChildren; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 145 | const GrSurfaceProxy::UniqueID fProxyUniqueID; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 146 | }; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 147 | typedef SkTArray<std::unique_ptr<OpNode>, true> OpList; |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 148 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 149 | void copyOutFromOpList(OpInfo* outOpInfo, int opListID); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 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); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 154 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 155 | OpPool fOpPool; |
joshualitt | b0666ad | 2016-03-08 10:43:41 -0800 | [diff] [blame] | 156 | SkTHashMap<uint32_t, int> fIDLookup; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 157 | SkTHashMap<int, Ops*> fClientIDLookup; |
| 158 | OpList fOpList; |
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 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 161 | // The client can pass in an optional client ID which we will use to mark the ops |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 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 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 177 | #define GR_AUDIT_TRAIL_ADD_OP(audit_trail, op, proxy_id) \ |
| 178 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addOp, op, proxy_id); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 179 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 180 | #define GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(audit_trail, combineWith, op) \ |
| 181 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, opsCombined, combineWith, op); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 182 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 183 | #define GR_AUDIT_TRAIL_OP_RESULT_NEW(audit_trail, op) // Doesn't do anything now, one day... |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 184 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 185 | #endif |