OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrAuditTrail_DEFINED | 8 #ifndef GrAuditTrail_DEFINED |
9 #define GrAuditTrail_DEFINED | 9 #define GrAuditTrail_DEFINED |
10 | 10 |
11 #include "GrConfig.h" | 11 #include "GrConfig.h" |
12 #include "SkRect.h" | 12 #include "SkRect.h" |
13 #include "SkString.h" | 13 #include "SkString.h" |
14 #include "SkTArray.h" | 14 #include "SkTArray.h" |
| 15 #include "SkTHash.h" |
| 16 |
| 17 class GrBatch; |
15 | 18 |
16 /* | 19 /* |
17 * GrAuditTrail collects a list of draw ops, detailed information about those op
s, and can dump them | 20 * GrAuditTrail collects a list of draw ops, detailed information about those op
s, and can dump them |
18 * to json. | 21 * to json. |
19 * | 22 * |
20 * Capturing this information is expensive and consumes a lot of memory, therefo
re it is important | 23 * Capturing this information is expensive and consumes a lot of memory, therefo
re it is important |
21 * to enable auditing only when required and disable it promptly. The AutoEnable
class helps to | 24 * to enable auditing only when required and disable it promptly. The AutoEnable
class helps to |
22 * ensure that the audit trail is disabled in a timely fashion. Once the informa
tion has been dealt | 25 * ensure that the audit trail is disabled in a timely fashion. Once the informa
tion has been dealt |
23 * with, be sure to call reset(), or the log will simply keep growing. | 26 * with, be sure to call reset(), or the log will simply keep growing. |
24 */ | 27 */ |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 60 |
58 ~AutoEnable() { | 61 ~AutoEnable() { |
59 SkASSERT(fAuditTrail->isEnabled()); | 62 SkASSERT(fAuditTrail->isEnabled()); |
60 fAuditTrail->setEnabled(false); | 63 fAuditTrail->setEnabled(false); |
61 } | 64 } |
62 | 65 |
63 private: | 66 private: |
64 GrAuditTrail* fAuditTrail; | 67 GrAuditTrail* fAuditTrail; |
65 }; | 68 }; |
66 | 69 |
| 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 |
67 void pushFrame(const char* name) { | 86 void pushFrame(const char* name) { |
68 SkASSERT(fEnabled); | 87 SkASSERT(fEnabled); |
69 Frame* frame = new Frame; | 88 Frame* frame = new Frame; |
| 89 fEvents.emplace_back(frame); |
70 if (fStack.empty()) { | 90 if (fStack.empty()) { |
71 fFrames.emplace_back(frame); | 91 fFrames.push_back(frame); |
72 } else { | 92 } else { |
73 fStack.back()->fChildren.emplace_back(frame); | 93 fStack.back()->fChildren.push_back(frame); |
74 } | 94 } |
75 | 95 |
76 frame->fUniqueID = fUniqueID++; | 96 frame->fUniqueID = fUniqueID++; |
77 frame->fName = name; | 97 frame->fName = name; |
78 fStack.push_back(frame); | 98 fStack.push_back(frame); |
79 } | 99 } |
80 | 100 |
81 void popFrame() { | 101 void popFrame() { |
82 SkASSERT(fEnabled); | 102 SkASSERT(fEnabled); |
83 fStack.pop_back(); | 103 fStack.pop_back(); |
84 } | 104 } |
85 | 105 |
86 void addBatch(const char* name, const SkRect& bounds) { | 106 void addBatch(const char* name, const SkRect& bounds) { |
87 SkASSERT(fEnabled && !fStack.empty()); | 107 SkASSERT(fEnabled && !fStack.empty()); |
88 Batch* batch = new Batch; | 108 Batch* batch = new Batch; |
89 fStack.back()->fChildren.emplace_back(batch); | 109 fEvents.emplace_back(batch); |
| 110 fStack.back()->fChildren.push_back(batch); |
90 batch->fName = name; | 111 batch->fName = name; |
91 batch->fBounds = bounds; | 112 batch->fBounds = bounds; |
| 113 fCurrentBatch = batch; |
92 } | 114 } |
93 | 115 |
94 SkString toJson(bool prettyPrint = false) const; | 116 void batchingResultCombined(GrBatch* combiner); |
| 117 |
| 118 void batchingResultNew(GrBatch* batch); |
| 119 |
| 120 SkString toJson(bool batchList = false, bool prettyPrint = false) const; |
95 | 121 |
96 bool isEnabled() { return fEnabled; } | 122 bool isEnabled() { return fEnabled; } |
97 void setEnabled(bool enabled) { fEnabled = enabled; } | 123 void setEnabled(bool enabled) { fEnabled = enabled; } |
98 | 124 |
99 void reset() { SkASSERT(fEnabled && fStack.empty()); fFrames.reset(); } | 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 } |
| 135 |
| 136 void fullReset() { |
| 137 SkASSERT(fEnabled); |
| 138 this->reset(); |
| 139 this->resetBatchList(); |
| 140 fEvents.reset(); // must be last, frees all of the memory |
| 141 } |
100 | 142 |
101 private: | 143 private: |
102 // TODO if performance becomes an issue, we can move to using SkVarAlloc | 144 // TODO if performance becomes an issue, we can move to using SkVarAlloc |
103 struct Event { | 145 struct Event { |
104 virtual ~Event() {} | 146 virtual ~Event() {} |
105 virtual SkString toJson() const=0; | 147 virtual SkString toJson() const=0; |
106 | 148 |
107 const char* fName; | 149 const char* fName; |
108 uint64_t fUniqueID; | 150 uint64_t fUniqueID; |
109 }; | 151 }; |
110 | 152 |
111 typedef SkTArray<SkAutoTDelete<Event>, true> FrameArray; | |
112 struct Frame : public Event { | 153 struct Frame : public Event { |
113 SkString toJson() const override; | 154 SkString toJson() const override; |
114 FrameArray fChildren; | 155 SkTArray<Event*> fChildren; |
115 }; | 156 }; |
116 | 157 |
117 struct Batch : public Event { | 158 struct Batch : public Event { |
118 SkString toJson() const override; | 159 SkString toJson() const override; |
119 SkRect fBounds; | 160 SkRect fBounds; |
| 161 SkTArray<Batch*> fChildren; |
120 }; | 162 }; |
| 163 typedef SkTArray<SkAutoTDelete<Event>, true> EventArrayPool; |
121 | 164 |
122 static void JsonifyTArray(SkString* json, const char* name, const FrameArray
& array, | 165 template <typename T> |
| 166 static void JsonifyTArray(SkString* json, const char* name, const T& array, |
123 bool addComma); | 167 bool addComma); |
124 | 168 |
| 169 // We store both an array of frames, and also a flatter array just of the ba
tches |
125 bool fEnabled; | 170 bool fEnabled; |
126 FrameArray fFrames; | 171 EventArrayPool fEvents; // manages the lifetimes of the events |
| 172 SkTArray<Event*> fFrames; |
127 SkTArray<Frame*> fStack; | 173 SkTArray<Frame*> fStack; |
128 uint64_t fUniqueID; | 174 uint64_t fUniqueID; |
| 175 |
| 176 Batch* fCurrentBatch; |
| 177 SkTHashMap<GrBatch*, int> fIDLookup; |
| 178 SkTArray<Batch*> fBatches; |
129 }; | 179 }; |
130 | 180 |
131 #define GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, invoke, ...) \ | 181 #define GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, invoke, ...) \ |
132 if (audit_trail->isEnabled()) { \ | 182 if (audit_trail->isEnabled()) { \ |
133 audit_trail->invoke(__VA_ARGS__); \ | 183 audit_trail->invoke(__VA_ARGS__); \ |
134 } | 184 } |
135 | 185 |
136 #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ | 186 #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ |
137 GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framen
ame); | 187 GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framen
ame); |
138 | 188 |
139 #define GR_AUDIT_TRAIL_RESET(audit_trail) \ | 189 #define GR_AUDIT_TRAIL_RESET(audit_trail) \ |
140 GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, reset); | 190 GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, reset); |
141 | 191 |
142 #define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \ | 192 #define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \ |
143 GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addBatch, batchname, bounds); | 193 GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail, addBatch, batchname, bounds); |
144 | 194 |
| 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 |
145 #endif | 201 #endif |
OLD | NEW |