• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
21 class SkDumpCanvas : public SkCanvas {
22 public:
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 
getDumper()69     Dumper* getDumper() const { return fDumper; }
70     void    setDumper(Dumper*);
71 
getNestLevel()72     int getNestLevel() const { return fNestLevel; }
73 
74 protected:
75     void willSave() override;
76     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) 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 onDrawVertices(VertexMode vmode, int vertexCount,
112                         const SkPoint vertices[], const SkPoint texs[],
113                         const SkColor colors[], SkXfermode* xmode,
114                         const uint16_t indices[], int indexCount,
115                         const SkPaint&) override;
116 
117     void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
118     void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
119     void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
120     void onClipRegion(const SkRegion&, SkRegion::Op) override;
121 
122     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
123 
124     static const char* EdgeStyleToAAString(ClipEdgeStyle edgeStyle);
125 
126 private:
127     Dumper* fDumper;
128     int     fNestLevel; // for nesting recursive elements like pictures
129 
130     void dump(Verb, const SkPaint*, const char format[], ...);
131 
132     typedef SkCanvas INHERITED;
133 };
134 
135 /** Formats the draw commands, and send them to a function-pointer provided
136     by the caller.
137  */
138 class SkFormatDumper : public SkDumpCanvas::Dumper {
139 public:
140     SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
141 
142     // override from baseclass that does the formatting, and in turn calls
143     // the function pointer that was passed to the constructor
144     virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
145                       const SkPaint*) override;
146 
147 private:
148     void (*fProc)(const char*, void*);
149     void* fRefcon;
150 
151     typedef SkDumpCanvas::Dumper INHERITED;
152 };
153 
154 /** Subclass of Dumper that dumps the drawing command to SkDebugf
155  */
156 class SkDebugfDumper : public SkFormatDumper {
157 public:
158     SkDebugfDumper();
159 
160 private:
161     typedef SkFormatDumper INHERITED;
162 };
163 
164 #endif
165 
166 #endif
167