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