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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRect.h" |
| 12 | #include "include/core/SkString.h" |
| 13 | #include "include/gpu/GrConfig.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/private/SkTArray.h" |
| 15 | #include "include/private/SkTHash.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 16 | #include "src/gpu/GrGpuResource.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrRenderTargetProxy.h" |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 18 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 19 | class GrOp; |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 20 | class SkJSONWriter; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them |
| 24 | * to json. |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 25 | * |
| 26 | * 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] | 27 | * 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] | 28 | * ensure that the audit trail is disabled in a timely fashion. Once the information has been dealt |
| 29 | * with, be sure to call reset(), or the log will simply keep growing. |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 30 | */ |
| 31 | class GrAuditTrail { |
| 32 | public: |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 33 | GrAuditTrail() |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 34 | : fClientID(kGrAuditTrailInvalidID) |
| 35 | , fEnabled(false) {} |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 36 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 37 | class AutoEnable { |
| 38 | public: |
| 39 | AutoEnable(GrAuditTrail* auditTrail) |
| 40 | : fAuditTrail(auditTrail) { |
| 41 | SkASSERT(!fAuditTrail->isEnabled()); |
| 42 | fAuditTrail->setEnabled(true); |
| 43 | } |
| 44 | |
| 45 | ~AutoEnable() { |
| 46 | SkASSERT(fAuditTrail->isEnabled()); |
| 47 | fAuditTrail->setEnabled(false); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | GrAuditTrail* fAuditTrail; |
| 52 | }; |
| 53 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 54 | class AutoManageOpsTask { |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 55 | public: |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 56 | AutoManageOpsTask(GrAuditTrail* auditTrail) |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 57 | : fAutoEnable(auditTrail), fAuditTrail(auditTrail) {} |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 58 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 59 | ~AutoManageOpsTask() { fAuditTrail->fullReset(); } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | AutoEnable fAutoEnable; |
| 63 | GrAuditTrail* fAuditTrail; |
| 64 | }; |
| 65 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 66 | class AutoCollectOps { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 67 | public: |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 68 | AutoCollectOps(GrAuditTrail* auditTrail, int clientID) |
| 69 | : fAutoEnable(auditTrail), fAuditTrail(auditTrail) { |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 70 | fAuditTrail->setClientID(clientID); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 73 | ~AutoCollectOps() { fAuditTrail->setClientID(kGrAuditTrailInvalidID); } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 74 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 75 | private: |
| 76 | AutoEnable fAutoEnable; |
| 77 | GrAuditTrail* fAuditTrail; |
| 78 | }; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 79 | |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 80 | void pushFrame(const char* framename) { |
| 81 | SkASSERT(fEnabled); |
| 82 | fCurrentStackTrace.push_back(SkString(framename)); |
| 83 | } |
| 84 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 85 | void addOp(const GrOp*, GrRenderTargetProxy::UniqueID proxyID); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 86 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 87 | void opsCombined(const GrOp* consumer, const GrOp* consumed); |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 88 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 89 | // Because op combining is heavily dependent on sequence of draw calls, these calls will only |
| 90 | // produce valid information for the given draw sequence which preceeded them. Specifically, ops |
| 91 | // of future draw calls may combine with previous ops and thus would invalidate the json. What |
| 92 | // this means is that for some sequence of draw calls N, the below toJson calls will only |
| 93 | // produce JSON which reflects N draw calls. This JSON may or may not be accurate for N + 1 or |
| 94 | // N - 1 draws depending on the actual combining algorithm used. |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 95 | void toJson(SkJSONWriter& writer) const; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 96 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 97 | // returns a json string of all of the ops associated with a given client id |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 98 | void toJson(SkJSONWriter& writer, int clientID) const; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 99 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 100 | bool isEnabled() { return fEnabled; } |
| 101 | void setEnabled(bool enabled) { fEnabled = enabled; } |
| 102 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 103 | void setClientID(int clientID) { fClientID = clientID; } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 104 | |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 105 | // We could just return our internal bookkeeping struct if copying the data out becomes |
| 106 | // a performance issue, but until then its nice to decouple |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 107 | struct OpInfo { |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 108 | struct Op { |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 109 | int fClientID; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 110 | SkRect fBounds; |
| 111 | }; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 112 | |
| 113 | SkRect fBounds; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 114 | GrSurfaceProxy::UniqueID fProxyUniqueID; |
| 115 | SkTArray<Op> fOps; |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 118 | void getBoundsByClientID(SkTArray<OpInfo>* outInfo, int clientID); |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 119 | void getBoundsByOpsTaskID(OpInfo* outInfo, int opsTaskID); |
joshualitt | 10d8fc2 | 2016-02-29 11:15:06 -0800 | [diff] [blame] | 120 | |
joshualitt | df3f2b0 | 2016-03-01 07:47:56 -0800 | [diff] [blame] | 121 | void fullReset(); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 122 | |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 123 | static const int kGrAuditTrailInvalidID; |
| 124 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 125 | private: |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 126 | // TODO if performance becomes an issue, we can move to using SkVarAlloc |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 127 | struct Op { |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 128 | void toJson(SkJSONWriter& writer) const; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 129 | SkString fName; |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 130 | SkTArray<SkString> fStackTrace; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 131 | SkRect fBounds; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 132 | int fClientID; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 133 | int fOpsTaskID; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 134 | int fChildID; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame] | 135 | }; |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 136 | typedef SkTArray<std::unique_ptr<Op>, true> OpPool; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 137 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 138 | typedef SkTArray<Op*> Ops; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 139 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 140 | struct OpNode { |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 141 | OpNode(const GrSurfaceProxy::UniqueID& proxyID) : fProxyUniqueID(proxyID) { } |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 142 | void toJson(SkJSONWriter& writer) const; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 143 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 144 | SkRect fBounds; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 145 | Ops fChildren; |
Robert Phillips | f7a7261 | 2017-03-31 10:03:45 -0400 | [diff] [blame] | 146 | const GrSurfaceProxy::UniqueID fProxyUniqueID; |
joshualitt | b95c772 | 2016-02-29 07:44:02 -0800 | [diff] [blame] | 147 | }; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 148 | typedef SkTArray<std::unique_ptr<OpNode>, true> OpsTask; |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 149 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 150 | void copyOutFromOpsTask(OpInfo* outOpInfo, int opsTask); |
joshualitt | 46b301d | 2016-03-02 08:32:37 -0800 | [diff] [blame] | 151 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 152 | template <typename T> |
Brian Osman | d8a90f9 | 2019-01-28 13:41:19 -0500 | [diff] [blame] | 153 | static void JsonifyTArray(SkJSONWriter& writer, const char* name, const T& array); |
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; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 158 | OpsTask fOpsTask; |
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, ...) \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 167 | if (audit_trail->isEnabled()) audit_trail->invoke(__VA_ARGS__) |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 168 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 169 | #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 170 | GR_AUDIT_TRAIL_INVOKE_GUARD((audit_trail), pushFrame, framename) |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 171 | |
| 172 | #define GR_AUDIT_TRAIL_RESET(audit_trail) \ |
joshualitt | f55c364 | 2016-03-02 08:11:34 -0800 | [diff] [blame] | 173 | //GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, fullReset); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 174 | |
Robert Phillips | 318c419 | 2017-05-17 09:36:38 -0400 | [diff] [blame] | 175 | #define GR_AUDIT_TRAIL_ADD_OP(audit_trail, op, proxy_id) \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 176 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addOp, op, proxy_id) |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 177 | |
Brian Salomon | 42ad83a | 2016-12-20 16:14:45 -0500 | [diff] [blame] | 178 | #define GR_AUDIT_TRAIL_OPS_RESULT_COMBINED(audit_trail, combineWith, op) \ |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 179 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, opsCombined, combineWith, op) |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 180 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 181 | #endif |