1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkDumpCanvas_DEFINED
9#define SkDumpCanvas_DEFINED
10
11#include "SkCanvas.h"
12#include "SkVertices.h"
13
14/** This class overrides all the draw methods on SkCanvas, and formats them
15    as text, and then sends that to a Dumper helper object.
16
17    Typical use might be to dump a display list to a log file to see what is
18    being drawn.
19 */
20class SkDumpCanvas : public SkCanvas {
21public:
22    class Dumper;
23
24    explicit SkDumpCanvas(Dumper* = 0);
25    ~SkDumpCanvas() override;
26
27    enum Verb {
28        kNULL_Verb,
29
30        kSave_Verb,
31        kRestore_Verb,
32
33        kMatrix_Verb,
34
35        kClip_Verb,
36
37        kDrawPaint_Verb,
38        kDrawPoints_Verb,
39        kDrawOval_Verb,
40        kDrawArc_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        kDrawAnnotation_Verb,
52
53        kCull_Verb
54    };
55
56    /** Subclasses of this are installed on the DumpCanvas, and then called for
57        each drawing command.
58     */
59    class Dumper : public SkRefCnt {
60    public:
61
62
63        virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
64                          const SkPaint*) = 0;
65
66    private:
67        typedef SkRefCnt INHERITED;
68    };
69
70    Dumper* getDumper() const { return fDumper; }
71    void    setDumper(Dumper*);
72
73    int getNestLevel() const { return fNestLevel; }
74
75protected:
76    void willSave() override;
77    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
78    void willRestore() override;
79
80    void didConcat(const SkMatrix&) override;
81    void didSetMatrix(const SkMatrix&) override;
82
83    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
84    virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
85                            const SkPaint&) override;
86    virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
87                               const SkPaint&) override;
88    virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
89                                SkScalar constY, const SkPaint&) override;
90    virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
91                                  const SkMatrix* matrix, const SkPaint&) override;
92    void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
93                           const SkRect* cull, const SkPaint& paint) override;
94    virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
95                                const SkPaint& paint) override;
96    virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
97                             const SkPoint texCoords[4], SkBlendMode,
98                             const SkPaint& paint) override;
99
100    void onDrawPaint(const SkPaint&) override;
101    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
102    void onDrawRect(const SkRect&, const SkPaint&) override;
103    void onDrawOval(const SkRect&, const SkPaint&) override;
104    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
105    void onDrawRRect(const SkRRect&, const SkPaint&) override;
106    void onDrawPath(const SkPath&, const SkPaint&) override;
107    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
108    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
109                          SrcRectConstraint) override;
110    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
111    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
112                         const SkPaint*, SrcRectConstraint) override;
113    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
114                          const SkPaint*) override;
115    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
116
117    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
118    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
119    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
120    void onClipRegion(const SkRegion&, SkClipOp) override;
121
122    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
123    void onDrawAnnotation(const SkRect&, const char key[], SkData* value) 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