1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#ifndef SkDumpCanvas_DEFINED
9#define SkDumpCanvas_DEFINED
10
11#include "SkCanvas.h"
12
13#ifdef SK_DEVELOPER
14
15/** This class overrides all the draw methods on SkCanvas, and formats them
16    as text, and then sends that to a Dumper helper object.
17
18    Typical use might be to dump a display list to a log file to see what is
19    being drawn.
20 */
21class SkDumpCanvas : public SkCanvas {
22public:
23    class Dumper;
24
25    explicit SkDumpCanvas(Dumper* = 0);
26    virtual ~SkDumpCanvas();
27
28    enum Verb {
29        kNULL_Verb,
30
31        kSave_Verb,
32        kRestore_Verb,
33
34        kMatrix_Verb,
35
36        kClip_Verb,
37
38        kDrawPaint_Verb,
39        kDrawPoints_Verb,
40        kDrawOval_Verb,
41        kDrawRect_Verb,
42        kDrawRRect_Verb,
43        kDrawDRRect_Verb,
44        kDrawPath_Verb,
45        kDrawBitmap_Verb,
46        kDrawText_Verb,
47        kDrawPicture_Verb,
48        kDrawVertices_Verb,
49        kDrawPatch_Verb,
50        kDrawData_Verb, // obsolete
51
52        kBeginCommentGroup_Verb,
53        kAddComment_Verb,
54        kEndCommentGroup_Verb,
55
56        kCull_Verb
57    };
58
59    /** Subclasses of this are installed on the DumpCanvas, and then called for
60        each drawing command.
61     */
62    class Dumper : public SkRefCnt {
63    public:
64        SK_DECLARE_INST_COUNT(Dumper)
65
66        virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
67                          const SkPaint*) = 0;
68
69    private:
70        typedef SkRefCnt INHERITED;
71    };
72
73    Dumper* getDumper() const { return fDumper; }
74    void    setDumper(Dumper*);
75
76    int getNestLevel() const { return fNestLevel; }
77
78    void beginCommentGroup(const char* description) override;
79    void addComment(const char* kywd, const char* value) override;
80    void endCommentGroup() override;
81
82protected:
83    void willSave() override;
84    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
85    void willRestore() override;
86
87    void didConcat(const SkMatrix&) override;
88    void didSetMatrix(const SkMatrix&) override;
89
90    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
91    virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
92                            const SkPaint&) override;
93    virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
94                               const SkPaint&) override;
95    virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
96                                SkScalar constY, const SkPaint&) override;
97    virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
98                                  const SkMatrix* matrix, const SkPaint&) override;
99    virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
100                                const SkPaint& paint) override;
101    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
102                             const SkPoint texCoords[4], SkXfermode* xmode,
103                             const SkPaint& paint) override;
104
105    void onDrawPaint(const SkPaint&) override;
106    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
107    void onDrawRect(const SkRect&, const SkPaint&) override;
108    void onDrawOval(const SkRect&, const SkPaint&) override;
109    void onDrawRRect(const SkRRect&, const SkPaint&) override;
110    void onDrawPath(const SkPath&, const SkPaint&) override;
111    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
112    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
113                          DrawBitmapRectFlags flags) override;
114    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
115    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
116                         const SkPaint*) override;
117    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
118                          const SkPaint*) override;
119    void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
120    void onDrawVertices(VertexMode vmode, int vertexCount,
121                        const SkPoint vertices[], const SkPoint texs[],
122                        const SkColor colors[], SkXfermode* xmode,
123                        const uint16_t indices[], int indexCount,
124                        const SkPaint&) override;
125
126    void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
127    void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
128    void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
129    void onClipRegion(const SkRegion&, SkRegion::Op) override;
130
131    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
132
133    static const char* EdgeStyleToAAString(ClipEdgeStyle edgeStyle);
134
135private:
136    Dumper* fDumper;
137    int     fNestLevel; // for nesting recursive elements like pictures
138
139    void dump(Verb, const SkPaint*, const char format[], ...);
140
141    typedef SkCanvas INHERITED;
142};
143
144/** Formats the draw commands, and send them to a function-pointer provided
145    by the caller.
146 */
147class SkFormatDumper : public SkDumpCanvas::Dumper {
148public:
149    SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
150
151    // override from baseclass that does the formatting, and in turn calls
152    // the function pointer that was passed to the constructor
153    virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
154                      const SkPaint*) override;
155
156private:
157    void (*fProc)(const char*, void*);
158    void* fRefcon;
159
160    typedef SkDumpCanvas::Dumper INHERITED;
161};
162
163/** Subclass of Dumper that dumps the drawing command to SkDebugf
164 */
165class SkDebugfDumper : public SkFormatDumper {
166public:
167    SkDebugfDumper();
168
169private:
170    typedef SkFormatDumper INHERITED;
171};
172
173#endif
174
175#endif
176