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" |
| 15 | |
| 16 | /* |
| 17 | * GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them |
| 18 | * to json. |
| 19 | */ |
| 20 | class GrAuditTrail { |
| 21 | public: |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 22 | GrAuditTrail() : fUniqueID(0) {} |
| 23 | |
| 24 | class AutoFrame { |
| 25 | public: |
| 26 | AutoFrame(GrAuditTrail* auditTrail, const char* name) |
| 27 | : fAuditTrail(auditTrail) { |
| 28 | if (GR_BATCH_DEBUGGING_OUTPUT) { |
| 29 | fAuditTrail->pushFrame(name); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | ~AutoFrame() { |
| 34 | if (GR_BATCH_DEBUGGING_OUTPUT) { |
| 35 | fAuditTrail->popFrame(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | GrAuditTrail* fAuditTrail; |
| 41 | }; |
| 42 | |
| 43 | void pushFrame(const char* name) { |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 44 | SkASSERT(GR_BATCH_DEBUGGING_OUTPUT); |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 45 | Frame* frame = new Frame; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 46 | if (fStack.empty()) { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 47 | fFrames.emplace_back(frame); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 48 | } else { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 49 | fStack.back()->fChildren.emplace_back(frame); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | frame->fUniqueID = fUniqueID++; |
| 53 | frame->fName = name; |
| 54 | fStack.push_back(frame); |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 55 | } |
| 56 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 57 | void popFrame() { |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 58 | SkASSERT(GR_BATCH_DEBUGGING_OUTPUT); |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 59 | fStack.pop_back(); |
| 60 | } |
| 61 | |
| 62 | void addBatch(const char* name, const SkRect& bounds) { |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 63 | SkASSERT(GR_BATCH_DEBUGGING_OUTPUT && !fStack.empty()); |
| 64 | Batch* batch = new Batch; |
| 65 | fStack.back()->fChildren.emplace_back(batch); |
| 66 | batch->fName = name; |
| 67 | batch->fBounds = bounds; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 68 | } |
| 69 | |
joshualitt | 6b3cf73 | 2016-02-17 11:20:26 -0800 | [diff] [blame^] | 70 | SkString toJson(bool prettyPrint = false) const; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 71 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 72 | void reset() { SkASSERT(GR_BATCH_DEBUGGING_OUTPUT && fStack.empty()); fFrames.reset(); } |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 73 | |
| 74 | private: |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 75 | // TODO if performance becomes an issue, we can move to using SkVarAlloc |
| 76 | struct Event { |
| 77 | virtual ~Event() {} |
| 78 | virtual SkString toJson() const=0; |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 79 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 80 | const char* fName; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 81 | uint64_t fUniqueID; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
joshualitt | 11fae87 | 2016-01-14 10:58:07 -0800 | [diff] [blame] | 84 | typedef SkTArray<SkAutoTDelete<Event>, true> FrameArray; |
| 85 | struct Frame : public Event { |
| 86 | SkString toJson() const override; |
| 87 | FrameArray fChildren; |
| 88 | }; |
| 89 | |
| 90 | struct Batch : public Event { |
| 91 | SkString toJson() const override; |
| 92 | SkRect fBounds; |
| 93 | }; |
| 94 | |
| 95 | static void JsonifyTArray(SkString* json, const char* name, const FrameArray& array); |
| 96 | |
| 97 | FrameArray fFrames; |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 98 | SkTArray<Frame*> fStack; |
| 99 | uint64_t fUniqueID; |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 102 | #define GR_AUDIT_TRAIL_INVOKE_GUARD(invoke, ...) \ |
| 103 | if (GR_BATCH_DEBUGGING_OUTPUT) { \ |
| 104 | invoke(__VA_ARGS__); \ |
| 105 | } |
| 106 | |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 107 | #define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \ |
| 108 | GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framename); |
joshualitt | 5651ee6 | 2016-01-11 10:39:11 -0800 | [diff] [blame] | 109 | |
| 110 | #define GR_AUDIT_TRAIL_RESET(audit_trail) \ |
| 111 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail->reset); |
| 112 | |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 113 | #define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \ |
joshualitt | 87a721b | 2016-01-12 12:59:28 -0800 | [diff] [blame] | 114 | GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail->addBatch, batchname, bounds); |
joshualitt | 086cee1 | 2016-01-12 06:45:24 -0800 | [diff] [blame] | 115 | |
joshualitt | 27a48dc | 2016-01-08 07:19:47 -0800 | [diff] [blame] | 116 | #endif |