SkDumpCanvas.h revision 32704674f64cb6a14356dfebe060cd3484c06cc7
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        kCull_Verb
53    };
54
55    /** Subclasses of this are installed on the DumpCanvas, and then called for
56        each drawing command.
57     */
58    class Dumper : public SkRefCnt {
59    public:
60
61
62        virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
63                          const SkPaint*) = 0;
64
65    private:
66        typedef SkRefCnt INHERITED;
67    };
68
69    Dumper* getDumper() const { return fDumper; }
70    void    setDumper(Dumper*);
71
72    int getNestLevel() const { return fNestLevel; }
73
74protected:
75    void willSave() override;
76    SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
77    void willRestore() override;
78
79    void didConcat(const SkMatrix&) override;
80    void didSetMatrix(const SkMatrix&) override;
81
82    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
83    virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
84                            const SkPaint&) override;
85    virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
86                               const SkPaint&) override;
87    virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
88                                SkScalar constY, const SkPaint&) override;
89    virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
90                                  const SkMatrix* matrix, const SkPaint&) override;
91    virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
92                                const SkPaint& paint) override;
93    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
94                             const SkPoint texCoords[4], SkXfermode* xmode,
95                             const SkPaint& paint) override;
96
97    void onDrawPaint(const SkPaint&) override;
98    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
99    void onDrawRect(const SkRect&, const SkPaint&) override;
100    void onDrawOval(const SkRect&, const SkPaint&) override;
101    void onDrawRRect(const SkRRect&, const SkPaint&) override;
102    void onDrawPath(const SkPath&, const SkPaint&) override;
103    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
104    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
105                          SrcRectConstraint) override;
106    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
107    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
108                         const SkPaint*, SrcRectConstraint) override;
109    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
110                          const SkPaint*) override;
111    void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
112    void onDrawVertices(VertexMode vmode, int vertexCount,
113                        const SkPoint vertices[], const SkPoint texs[],
114                        const SkColor colors[], SkXfermode* xmode,
115                        const uint16_t indices[], int indexCount,
116                        const SkPaint&) override;
117
118    void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
119    void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
120    void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
121    void onClipRegion(const SkRegion&, SkRegion::Op) override;
122
123    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
124
125    static const char* EdgeStyleToAAString(ClipEdgeStyle edgeStyle);
126
127private:
128    Dumper* fDumper;
129    int     fNestLevel; // for nesting recursive elements like pictures
130
131    void dump(Verb, const SkPaint*, const char format[], ...);
132
133    typedef SkCanvas INHERITED;
134};
135
136/** Formats the draw commands, and send them to a function-pointer provided
137    by the caller.
138 */
139class SkFormatDumper : public SkDumpCanvas::Dumper {
140public:
141    SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
142
143    // override from baseclass that does the formatting, and in turn calls
144    // the function pointer that was passed to the constructor
145    virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
146                      const SkPaint*) override;
147
148private:
149    void (*fProc)(const char*, void*);
150    void* fRefcon;
151
152    typedef SkDumpCanvas::Dumper INHERITED;
153};
154
155/** Subclass of Dumper that dumps the drawing command to SkDebugf
156 */
157class SkDebugfDumper : public SkFormatDumper {
158public:
159    SkDebugfDumper();
160
161private:
162    typedef SkFormatDumper INHERITED;
163};
164
165#endif
166
167#endif
168