• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
12 class SkDumpCanvas : public SkCanvas {
13 public:
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         kDrawShape_Verb,
37         kDrawVertices_Verb,
38         kDrawData_Verb
39     };
40 
41     /** Subclasses of this are installed on the DumpCanvas, and then called for
42         each drawing command.
43      */
44     class Dumper : public SkRefCnt {
45     public:
46         virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
47                           const SkPaint*) = 0;
48     };
49 
getDumper()50     Dumper* getDumper() const { return fDumper; }
51     void    setDumper(Dumper*);
52 
getNestLevel()53     int getNestLevel() const { return fNestLevel; }
54 
55     // overrides from SkCanvas
56 
57     virtual int save(SaveFlags flags = kMatrixClip_SaveFlag);
58     virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
59                           SaveFlags flags = kARGB_ClipLayer_SaveFlag);
60     virtual void restore();
61 
62     virtual bool translate(SkScalar dx, SkScalar dy);
63     virtual bool scale(SkScalar sx, SkScalar sy);
64     virtual bool rotate(SkScalar degrees);
65     virtual bool skew(SkScalar sx, SkScalar sy);
66     virtual bool concat(const SkMatrix& matrix);
67     virtual void setMatrix(const SkMatrix& matrix);
68 
69     virtual bool clipRect(const SkRect& rect,
70                           SkRegion::Op op = SkRegion::kIntersect_Op);
71     virtual bool clipPath(const SkPath& path,
72                           SkRegion::Op op = SkRegion::kIntersect_Op);
73     virtual bool clipRegion(const SkRegion& deviceRgn,
74                             SkRegion::Op op = SkRegion::kIntersect_Op);
75 
76     virtual void drawPaint(const SkPaint& paint);
77     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
78                             const SkPaint& paint);
79     virtual void drawRect(const SkRect& rect, const SkPaint& paint);
80     virtual void drawPath(const SkPath& path, const SkPaint& paint);
81     virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
82                             const SkPaint* paint = NULL);
83     virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
84                                 const SkRect& dst, const SkPaint* paint = NULL);
85     virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
86                                   const SkPaint* paint = NULL);
87     virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
88                             const SkPaint* paint = NULL);
89     virtual void drawText(const void* text, size_t byteLength, SkScalar x,
90                           SkScalar y, const SkPaint& paint);
91     virtual void drawPosText(const void* text, size_t byteLength,
92                              const SkPoint pos[], const SkPaint& paint);
93     virtual void drawPosTextH(const void* text, size_t byteLength,
94                               const SkScalar xpos[], SkScalar constY,
95                               const SkPaint& paint);
96     virtual void drawTextOnPath(const void* text, size_t byteLength,
97                                 const SkPath& path, const SkMatrix* matrix,
98                                 const SkPaint& paint);
99     virtual void drawPicture(SkPicture&);
100     virtual void drawShape(SkShape*);
101     virtual void drawVertices(VertexMode vmode, int vertexCount,
102                               const SkPoint vertices[], const SkPoint texs[],
103                               const SkColor colors[], SkXfermode* xmode,
104                               const uint16_t indices[], int indexCount,
105                               const SkPaint& paint);
106     virtual void drawData(const void*, size_t);
107 
108 private:
109     Dumper* fDumper;
110     int     fNestLevel; // for nesting recursive elements like pictures
111 
112     void dump(Verb, const SkPaint*, const char format[], ...);
113 
114     typedef SkCanvas INHERITED;
115 };
116 
117 /** Formats the draw commands, and send them to a function-pointer provided
118     by the caller.
119  */
120 class SkFormatDumper : public SkDumpCanvas::Dumper {
121 public:
122     SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
123 
124     // override from baseclass that does the formatting, and in turn calls
125     // the function pointer that was passed to the constructor
126     virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
127                       const SkPaint*);
128 
129 private:
130     void (*fProc)(const char*, void*);
131     void* fRefcon;
132 
133     typedef SkDumpCanvas::Dumper INHERITED;
134 };
135 
136 /** Subclass of Dumper that dumps the drawing command to SkDebugf
137  */
138 class SkDebugfDumper : public SkFormatDumper {
139 public:
140     SkDebugfDumper();
141 
142 private:
143     typedef SkFormatDumper INHERITED;
144 };
145 
146 #endif
147