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 | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 31 | : fEnabled(false) |
| 32 | , fUniqueID(0) {} |
| 33 | |
| 34 | class AutoFrame { |
| 35 | public: |
| 36 | AutoFrame(GrAuditTrail* auditTrail, const char* name) |
| 37 | : fAuditTrail(auditTrail) { |
| 38 | if (fAuditTrail->fEnabled) { |
| 39 | fAuditTrail->pushFrame(name); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | ~AutoFrame() { |
| 44 | if (fAuditTrail->fEnabled) { |
| 45 | fAuditTrail->popFrame(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | GrAuditTrail* fAuditTrail; |
| 51 | }; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 52 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 53 | class AutoEnable { |
| 54 | public: |
| 55 | AutoEnable(GrAuditTrail* auditTrail) |
| 56 | : fAuditTrail(auditTrail) { |
| 57 | SkASSERT(!fAuditTrail->isEnabled()); |
| 58 | fAuditTrail->setEnabled(true); |
| 59 | } |
| 60 | |
| 61 | ~AutoEnable() { |
| 62 | SkASSERT(fAuditTrail->isEnabled()); |
| 63 | fAuditTrail->setEnabled(false); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | GrAuditTrail* fAuditTrail; |
| 68 | }; |
| 69 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 70 | class AutoManageBatchList { |
| 71 | public: |
| 72 | AutoManageBatchList(GrAuditTrail* auditTrail) |
| 73 | : fAutoEnable(auditTrail) |
| 74 | , fAuditTrail(auditTrail) { |
| 75 | } |
| 76 | |
| 77 | ~AutoManageBatchList() { |
| 78 | fAuditTrail->fullReset(); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | AutoEnable fAutoEnable; |
| 83 | GrAuditTrail* fAuditTrail; |
| 84 | }; |
| 85 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 86 | void pushFrame(const char* name) { |
| 87 | SkASSERT(fEnabled); |
| 88 | Frame* frame = new Frame; |
| 89 | fEvents.emplace_back(frame); |
| 90 | if (fStack.empty()) { |
| 91 | fFrames.push_back(frame); |
| 92 | } else { |
| 93 | fStack.back()->fChildren.push_back(frame); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 94 | } |
| 95 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 96 | frame->fUniqueID = fUniqueID++; |
| 97 | frame->fName = name; |
| 98 | fStack.push_back(frame); |
| 99 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 100 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 101 | void popFrame() { |
| 102 | SkASSERT(fEnabled); |
| 103 | fStack.pop_back(); |
| 104 | } |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 105 | |
| 106 | void addBatch(const char* name, const SkRect& bounds) { |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 107 | SkASSERT(fEnabled && !fStack.empty()); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 108 | Batch* batch = new Batch; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 109 | fEvents.emplace_back(batch); |
| 110 | fStack.back()->fChildren.push_back(batch); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 111 | batch->fName = name; |
| 112 | batch->fBounds = bounds; |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 113 | fCurrentBatch = batch; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 114 | } |
| 115 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 116 | void batchingResultCombined(GrBatch* combiner); |
| 117 | |
| 118 | void batchingResultNew(GrBatch* batch); |
| 119 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 120 | SkString toJson(bool batchList = false, bool prettyPrint = false) const; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 121 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 122 | bool isEnabled() { return fEnabled; } |
| 123 | void setEnabled(bool enabled) { fEnabled = enabled; } |
| 124 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 125 | void reset() { |
| 126 | SkASSERT(fEnabled && fStack.empty()); |
| 127 | fFrames.reset(); |
| 128 | } |
| 129 | |
| 130 | void resetBatchList() { |
| 131 | SkASSERT(fEnabled); |
| 132 | fBatches.reset(); |
| 133 | fIDLookup.reset(); |
| 134 | } |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 135 | |
| 136 | void fullReset() { |
| 137 | SkASSERT(fEnabled); |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 138 | this->reset(); |
| 139 | this->resetBatchList(); |
| 140 | fEvents.reset(); // must be last, frees all of the memory |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 141 | } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 142 | |
| 143 | private: |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 144 | // TODO if performance becomes an issue, we can move to using SkVarAlloc |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 145 | struct Event { |
| 146 | virtual ~Event() {} |
| 147 | virtual SkString toJson() const=0; |
joshualitt | 9b48a6e | 2016-02-29 05:20:38 -0800 | [diff] [blame] | 148 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 149 | const char* fName; |
| 150 | uint64_t fUniqueID; |
joshualitt | 9b48a6e | 2016-02-29 05:20:38 -0800 | [diff] [blame] | 151 | }; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 152 | |
| 153 | struct Frame : public Event { |
| 154 | SkString toJson() const override; |
| 155 | SkTArray<Event*> fChildren; |
| 156 | }; |
| 157 | |
| 158 | struct Batch : public Event { |
| 159 | SkString toJson() const override; |
| 160 | SkRect fBounds; |
| 161 | SkTArray<Batch*> fChildren; |
| 162 | }; |
| 163 | typedef SkTArray<SkAutoTDelete<Event>, true> EventArrayPool; |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 164 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 165 | template <typename T> |
| 166 | static void JsonifyTArray(SkString* json, const char* name, const T& array, |
joshualitt | adab5a2 | 2016-02-18 05:04:39 -0800 | [diff] [blame] | 167 | bool addComma); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 168 | |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 169 | // We store both an array of frames, and also a flatter array just of the batches |
joshualitt | 9b48a6e | 2016-02-29 05:20:38 -0800 | [diff] [blame] | 170 | bool fEnabled; |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 171 | EventArrayPool fEvents; // manages the lifetimes of the events |
| 172 | SkTArray<Event*> fFrames; |
| 173 | SkTArray<Frame*> fStack; |
| 174 | uint64_t fUniqueID; |
| 175 | |
| 176 | Batch* fCurrentBatch; |
| 177 | SkTHashMap<GrBatch*, int> fIDLookup; |
| 178 | SkTArray<Batch*> fBatches; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 181 | #define GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, invoke, ...) \ |
| 182 | if (audit_trail->isEnabled()) { \ |
| 183 | audit_trail->invoke(__VA_ARGS__); \ |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 184 | } |
| 185 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 186 | #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 187 | GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framename); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 188 | |
| 189 | #define GR_AUDIT_TRAIL_RESET(audit_trail) \ |
joshualitt | 08fc807 | 2016-02-29 06:32:24 -0800 | [diff] [blame^] | 190 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, reset); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 191 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 192 | #define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \ |
ethannicholas | c85d9fb | 2016-02-18 13:45:39 -0800 | [diff] [blame] | 193 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addBatch, batchname, bounds); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 194 | |
joshualitt | 18d6b75 | 2016-02-26 08:07:50 -0800 | [diff] [blame] | 195 | #define GR_AUDIT_TRAIL_BATCHING_RESULT_COMBINED(audit_trail, combiner) \ |
| 196 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, batchingResultCombined, combiner); |
| 197 | |
| 198 | #define GR_AUDIT_TRAIL_BATCHING_RESULT_NEW(audit_trail, batch) \ |
| 199 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, batchingResultNew, batch); |
| 200 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 201 | #endif |