SkDumpCanvas.h revision 0910916c0f7b951ee55c4b7c6358295b9bca0565
1#ifndef SkDumpCanvas_DEFINED
2#define SkDumpCanvas_DEFINED
3
4#include "SkCanvas.h"
5
6/** This class overrides all the draw methods on SkCanvas, and formats them
7    as text, and then sends that to a Dumper helper object.
8
9    Typical use might be to dump a display list to a log file to see what is
10    being drawn.
11 */
12class SkDumpCanvas : public SkCanvas {
13public:
14    class Dumper;
15
16    explicit SkDumpCanvas(Dumper* = 0);
17    virtual ~SkDumpCanvas();
18
19    enum Verb {
20        kNULL_Verb,
21
22        kSave_Verb,
23        kRestore_Verb,
24
25        kMatrix_Verb,
26
27        kClip_Verb,
28
29        kDrawPaint_Verb,
30        kDrawPoints_Verb,
31        kDrawRect_Verb,
32        kDrawPath_Verb,
33        kDrawBitmap_Verb,
34        kDrawText_Verb,
35        kDrawPicture_Verb,
36        kDrawVertices_Verb
37    };
38
39    /** Subclasses of this are installed on the DumpCanvas, and then called for
40        each drawing command.
41     */
42    class Dumper : public SkRefCnt {
43    public:
44        virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
45                          const SkPaint*) = 0;
46    };
47
48    Dumper* getDumper() const { return fDumper; }
49    void    setDumper(Dumper*);
50
51    // overrides from SkCanvas
52
53    virtual int save(SaveFlags flags = kMatrixClip_SaveFlag);
54    virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
55                          SaveFlags flags = kARGB_ClipLayer_SaveFlag);
56    virtual void restore();
57
58    virtual bool translate(SkScalar dx, SkScalar dy);
59    virtual bool scale(SkScalar sx, SkScalar sy);
60    virtual bool rotate(SkScalar degrees);
61    virtual bool skew(SkScalar sx, SkScalar sy);
62    virtual bool concat(const SkMatrix& matrix);
63    virtual void setMatrix(const SkMatrix& matrix);
64
65    virtual bool clipRect(const SkRect& rect,
66                          SkRegion::Op op = SkRegion::kIntersect_Op);
67    virtual bool clipPath(const SkPath& path,
68                          SkRegion::Op op = SkRegion::kIntersect_Op);
69    virtual bool clipRegion(const SkRegion& deviceRgn,
70                            SkRegion::Op op = SkRegion::kIntersect_Op);
71
72    virtual void drawPaint(const SkPaint& paint);
73    virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
74                            const SkPaint& paint);
75    virtual void drawRect(const SkRect& rect, const SkPaint& paint);
76    virtual void drawPath(const SkPath& path, const SkPaint& paint);
77    virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
78                            const SkPaint* paint = NULL);
79    virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
80                                const SkRect& dst, const SkPaint* paint = NULL);
81    virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
82                                  const SkPaint* paint = NULL);
83    virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
84                            const SkPaint* paint = NULL);
85    virtual void drawText(const void* text, size_t byteLength, SkScalar x,
86                          SkScalar y, const SkPaint& paint);
87    virtual void drawPosText(const void* text, size_t byteLength,
88                             const SkPoint pos[], const SkPaint& paint);
89    virtual void drawPosTextH(const void* text, size_t byteLength,
90                              const SkScalar xpos[], SkScalar constY,
91                              const SkPaint& paint);
92    virtual void drawTextOnPath(const void* text, size_t byteLength,
93                                const SkPath& path, const SkMatrix* matrix,
94                                const SkPaint& paint);
95    virtual void drawPicture(SkPicture& picture);
96    virtual void drawVertices(VertexMode vmode, int vertexCount,
97                              const SkPoint vertices[], const SkPoint texs[],
98                              const SkColor colors[], SkXfermode* xmode,
99                              const uint16_t indices[], int indexCount,
100                              const SkPaint& paint);
101
102private:
103    Dumper* fDumper;
104
105    void dump(Verb, const SkPaint*, const char format[], ...);
106
107    typedef SkCanvas INHERITED;
108};
109
110/** Formats the draw commands, and send them to a function-pointer provided
111    by the caller.
112 */
113class SkFormatDumper : public SkDumpCanvas::Dumper {
114public:
115    SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
116
117    // override from baseclass that does the formatting, and in turn calls
118    // the function pointer that was passed to the constructor
119    virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
120                      const SkPaint*);
121
122private:
123    void (*fProc)(const char*, void*);
124    void* fRefcon;
125
126    typedef SkDumpCanvas::Dumper INHERITED;
127};
128
129/** Subclass of Dumper that dumps the drawing command to SkDebugf
130 */
131class SkDebugfDumper : public SkFormatDumper {
132public:
133    SkDebugfDumper();
134
135private:
136    typedef SkFormatDumper INHERITED;
137};
138
139#endif
140