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