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