aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/GrAuditTrail.h
blob: 67eecadf6741b4d081c3d52ba3c690bbc1a2083e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrAuditTrail_DEFINED
#define GrAuditTrail_DEFINED

#include "GrConfig.h"
#include "SkRect.h"
#include "SkString.h"
#include "SkTArray.h"

/*
 * GrAuditTrail collects a list of draw ops, detailed information about those ops, and can dump them
 * to json.
 */
class GrAuditTrail {
public:
    GrAuditTrail() : fUniqueID(0) {}

    class AutoFrame {
    public:
        AutoFrame(GrAuditTrail* auditTrail, const char* name)
            : fAuditTrail(auditTrail) {
            if (GR_BATCH_DEBUGGING_OUTPUT) {
                fAuditTrail->pushFrame(name);
            }
        }

        ~AutoFrame() {
            if (GR_BATCH_DEBUGGING_OUTPUT) {
                fAuditTrail->popFrame();
            }
        }

    private:
        GrAuditTrail* fAuditTrail;
    };

    void pushFrame(const char* name) {
        SkASSERT(GR_BATCH_DEBUGGING_OUTPUT);
        Frame* frame = new Frame;
        if (fStack.empty()) {
            fFrames.emplace_back(frame);
        } else {
            fStack.back()->fChildren.emplace_back(frame);
        }

        frame->fUniqueID = fUniqueID++;
        frame->fName = name;
        fStack.push_back(frame);
    }

    void popFrame() {
        SkASSERT(GR_BATCH_DEBUGGING_OUTPUT);
        fStack.pop_back();
    }

    void addBatch(const char* name, const SkRect& bounds) {
        SkASSERT(GR_BATCH_DEBUGGING_OUTPUT && !fStack.empty());
        Batch* batch = new Batch;
        fStack.back()->fChildren.emplace_back(batch);
        batch->fName = name;
        batch->fBounds = bounds;
    }

    SkString toJson(bool prettyPrint = false) const;

    void reset() { SkASSERT(GR_BATCH_DEBUGGING_OUTPUT && fStack.empty()); fFrames.reset(); }

private:
    // TODO if performance becomes an issue, we can move to using SkVarAlloc
    struct Event {
        virtual ~Event() {}
        virtual SkString toJson() const=0;

        const char* fName;
        uint64_t fUniqueID;
    };

    typedef SkTArray<SkAutoTDelete<Event>, true> FrameArray;
    struct Frame : public Event {
        SkString toJson() const override;
        FrameArray fChildren;
    };

    struct Batch : public Event {
        SkString toJson() const override;
        SkRect fBounds;
    };

    static void JsonifyTArray(SkString* json, const char* name, const FrameArray& array);

    FrameArray fFrames;
    SkTArray<Frame*> fStack;
    uint64_t fUniqueID;
};

#define GR_AUDIT_TRAIL_INVOKE_GUARD(invoke, ...) \
    if (GR_BATCH_DEBUGGING_OUTPUT) {             \
        invoke(__VA_ARGS__);                     \
    }

#define GR_AUDIT_TRAIL_AUTO_FRAME(audit_trail, framename) \
    GrAuditTrail::AutoFrame SK_MACRO_APPEND_LINE(auto_frame)(audit_trail, framename);

#define GR_AUDIT_TRAIL_RESET(audit_trail) \
    GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail->reset);

#define GR_AUDIT_TRAIL_ADDBATCH(audit_trail, batchname, bounds) \
    GR_AUDIT_TRAIL_INVOKE_GUARD(audit_trail->addBatch, batchname, bounds);

#endif