1 
2 /*
3  * Copyright 2012 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 
9 
10 #ifndef SKDEBUGCANVAS_H_
11 #define SKDEBUGCANVAS_H_
12 
13 #include "SkCanvas.h"
14 #include "SkDrawCommand.h"
15 #include "SkPathOps.h"
16 #include "SkPicture.h"
17 #include "SkTArray.h"
18 #include "SkString.h"
19 
20 class SkNWayCanvas;
21 
22 class SK_API SkDebugCanvas : public SkCanvas {
23 public:
24     SkDebugCanvas(int width, int height);
25     virtual ~SkDebugCanvas();
26 
toggleFilter(bool toggle)27     void toggleFilter(bool toggle) { fFilter = toggle; }
28 
setMegaVizMode(bool megaVizMode)29     void setMegaVizMode(bool megaVizMode) { fMegaVizMode = megaVizMode; }
getMegaVizMode()30     bool getMegaVizMode() const { return fMegaVizMode; }
31 
32     /**
33      * Enable or disable overdraw visualization
34      */
35     void setOverdrawViz(bool overdrawViz);
getOverdrawViz()36     bool getOverdrawViz() const { return fOverdrawViz; }
37 
getAllowSimplifyClip()38     bool getAllowSimplifyClip() const { return fAllowSimplifyClip; }
39 
setPicture(SkPicture * picture)40     void setPicture(SkPicture* picture) { fPicture = picture; }
41 
42     /**
43      * Enable or disable texure filtering override
44      */
45     void overrideTexFiltering(bool overrideTexFiltering, SkFilterQuality);
46 
47     /**
48         Executes all draw calls to the canvas.
49         @param canvas  The canvas being drawn to
50      */
51     void draw(SkCanvas* canvas);
52 
53     /**
54         Executes the draw calls up to the specified index.
55         @param canvas  The canvas being drawn to
56         @param index  The index of the final command being executed
57      */
58     void drawTo(SkCanvas* canvas, int index);
59 
60     /**
61         Returns the most recently calculated transformation matrix
62      */
getCurrentMatrix()63     const SkMatrix& getCurrentMatrix() {
64         return fMatrix;
65     }
66 
67     /**
68         Returns the most recently calculated clip
69      */
getCurrentClip()70     const SkIRect& getCurrentClip() {
71         return fClip;
72     }
73 
74     /**
75         Returns the index of the last draw command to write to the pixel at (x,y)
76      */
77     int getCommandAtPoint(int x, int y, int index);
78 
79     /**
80         Removes the command at the specified index
81         @param index  The index of the command to delete
82      */
83     void deleteDrawCommandAt(int index);
84 
85     /**
86         Returns the draw command at the given index.
87         @param index  The index of the command
88      */
89     SkDrawCommand* getDrawCommandAt(int index);
90 
91     /**
92         Sets the draw command for a given index.
93         @param index  The index to overwrite
94         @param command The new command
95      */
96     void setDrawCommandAt(int index, SkDrawCommand* command);
97 
98     /**
99         Returns information about the command at the given index.
100         @param index  The index of the command
101      */
102     const SkTDArray<SkString*>* getCommandInfo(int index) const;
103 
104     /**
105         Returns the visibility of the command at the given index.
106         @param index  The index of the command
107      */
108     bool getDrawCommandVisibilityAt(int index);
109 
110     /**
111         Returns the vector of draw commands
112      */
113     SK_ATTR_DEPRECATED("please use getDrawCommandAt and getSize instead")
114     const SkTDArray<SkDrawCommand*>& getDrawCommands() const;
115 
116     /**
117         Returns the vector of draw commands. Do not use this entry
118         point - it is going away!
119      */
120     SkTDArray<SkDrawCommand*>& getDrawCommands();
121 
122     /**
123         Returns length of draw command vector.
124      */
getSize()125     int getSize() const {
126         return fCommandVector.count();
127     }
128 
129     /**
130         Toggles the visibility / execution of the draw command at index i with
131         the value of toggle.
132      */
133     void toggleCommand(int index, bool toggle);
134 
setUserMatrix(SkMatrix matrix)135     void setUserMatrix(SkMatrix matrix) {
136         fUserMatrix = matrix;
137     }
138 
clipStackData()139     SkString clipStackData() const { return fClipStackData; }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 // Inherited from SkCanvas
143 ////////////////////////////////////////////////////////////////////////////////
144 
145     void beginCommentGroup(const char* description) override;
146     void addComment(const char* kywd, const char* value) override;
147     void endCommentGroup() override;
148 
149     static const int kVizImageHeight = 256;
150     static const int kVizImageWidth = 256;
151 
isClipEmpty()152     bool isClipEmpty() const override { return false; }
isClipRect()153     bool isClipRect() const override { return true; }
getClipBounds(SkRect * bounds)154     bool getClipBounds(SkRect* bounds) const override {
155         if (bounds) {
156             bounds->setXYWH(0, 0,
157                             SkIntToScalar(this->imageInfo().width()),
158                             SkIntToScalar(this->imageInfo().height()));
159         }
160         return true;
161     }
getClipDeviceBounds(SkIRect * bounds)162     bool getClipDeviceBounds(SkIRect* bounds) const override {
163         if (bounds) {
164             bounds->setLargest();
165         }
166         return true;
167     }
168 
169 protected:
170     void willSave() override;
171     SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) override;
172     void willRestore() override;
173 
174     void didConcat(const SkMatrix&) override;
175     void didSetMatrix(const SkMatrix&) override;
176 
177     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
178     void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
179                     const SkPaint&) override;
180     void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
181                        const SkPaint&) override;
182     void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
183                         SkScalar constY, const SkPaint&) override;
184     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
185                           const SkMatrix* matrix, const SkPaint&) override;
186     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
187                         const SkPaint& paint) override;
188 
189     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
190                      const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
191     void onDrawPaint(const SkPaint&) override;
192 
193     void onDrawRect(const SkRect&, const SkPaint&) override;
194     void onDrawOval(const SkRect&, const SkPaint&) override;
195     void onDrawRRect(const SkRRect&, const SkPaint&) override;
196     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
197     void onDrawVertices(VertexMode vmode, int vertexCount,
198                         const SkPoint vertices[], const SkPoint texs[],
199                         const SkColor colors[], SkXfermode* xmode,
200                         const uint16_t indices[], int indexCount,
201                         const SkPaint&) override;
202     void onDrawPath(const SkPath&, const SkPaint&) override;
203     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
204     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
205                           DrawBitmapRectFlags flags) override;
206     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
207     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
208                          const SkPaint*) override;
209     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
210                           const SkPaint*) override;
211     void onDrawSprite(const SkBitmap&, int left, int top, const SkPaint*) override;
212     void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
213     void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
214     void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
215     void onClipRegion(const SkRegion& region, SkRegion::Op) override;
216 
217     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
218 
219     void markActiveCommands(int index);
220 
221 private:
222     SkTDArray<SkDrawCommand*> fCommandVector;
223     SkPicture* fPicture;
224     bool fFilter;
225     bool fMegaVizMode;
226     SkMatrix fUserMatrix;
227     SkMatrix fMatrix;
228     SkIRect fClip;
229 
230     SkString fClipStackData;
231     bool fCalledAddStackData;
232     SkPath fSaveDevPath;
233 
234     bool fOverdrawViz;
235     bool fOverrideFilterQuality;
236     SkFilterQuality fFilterQuality;
237 
238     SkAutoTUnref<SkNWayCanvas> fPaintFilterCanvas;
239 
240     /**
241         The active saveLayer commands at a given point in the renderering.
242         Only used when "mega" visualization is enabled.
243     */
244     SkTDArray<SkDrawCommand*> fActiveLayers;
245 
246     /**
247         Adds the command to the classes vector of commands.
248         @param command  The draw command for execution
249      */
250     void addDrawCommand(SkDrawCommand* command);
251 
252     /**
253         Applies any panning and zooming the user has specified before
254         drawing anything else into the canvas.
255      */
256     void applyUserTransform(SkCanvas* canvas);
257 
resetClipStackData()258     void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
259 
260     void addClipStackData(const SkPath& devPath, const SkPath& operand, SkRegion::Op elementOp);
261     void addPathData(const SkPath& path, const char* pathName);
262     bool lastClipStackData(const SkPath& devPath);
263     void outputConicPoints(const SkPoint* pts, SkScalar weight);
264     void outputPoints(const SkPoint* pts, int count);
265     void outputPointsCommon(const SkPoint* pts, int count);
266     void outputScalar(SkScalar num);
267 
268     void updatePaintFilterCanvas();
269 
270     typedef SkCanvas INHERITED;
271 };
272 
273 #endif
274