• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
199     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
200     void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
201                     const SkPaint&) override;
202     void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
203                        const SkPaint&) override;
204     void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
205                         SkScalar constY, const SkPaint&) override;
206     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
207                           const SkMatrix* matrix, const SkPaint&) override;
208     void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[], const SkRect*,
209                            const SkPaint&) override;
210     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
211                         const SkPaint& paint) override;
212 
213     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
214                      const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
215     void onDrawPaint(const SkPaint&) override;
216 
217     void onDrawRect(const SkRect&, const SkPaint&) override;
218     void onDrawOval(const SkRect&, const SkPaint&) override;
219     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
220     void onDrawRRect(const SkRRect&, const SkPaint&) override;
221     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
222     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
223     void onDrawPath(const SkPath&, const SkPaint&) override;
224     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
225     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
226                           SrcRectConstraint) override;
227     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
228     void onDrawImageLattice(const SkImage* image, const Lattice& lattice,
229                             const SkRect& dst, const SkPaint* paint) override;
230     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
231                          const SkPaint*, SrcRectConstraint) override;
232     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
233                           const SkPaint*) override;
234     void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
235     void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
236     void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
237     void onClipRegion(const SkRegion& region, SkClipOp) override;
238 
239     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
240 
241     void markActiveCommands(int index);
242 
243 private:
244     SkTDArray<SkDrawCommand*> fCommandVector;
245     SkPicture* fPicture;
246     bool fFilter;
247     bool fMegaVizMode;
248     SkMatrix fUserMatrix;
249     SkMatrix fMatrix;
250     SkIRect fClip;
251 
252     SkString fClipStackData;
253     bool fCalledAddStackData;
254     SkPath fSaveDevPath;
255 
256     bool fOverdrawViz;
257     bool fOverrideFilterQuality;
258     SkFilterQuality fFilterQuality;
259     SkColor fClipVizColor;
260     bool fDrawGpuOpBounds;
261 
262     /**
263         The active saveLayer commands at a given point in the renderering.
264         Only used when "mega" visualization is enabled.
265     */
266     SkTDArray<SkDrawCommand*> fActiveLayers;
267 
268     /**
269         Adds the command to the classes vector of commands.
270         @param command  The draw command for execution
271      */
272     void addDrawCommand(SkDrawCommand* command);
273 
274     /**
275         Applies any panning and zooming the user has specified before
276         drawing anything else into the canvas.
277      */
278     void applyUserTransform(SkCanvas* canvas);
279 
resetClipStackData()280     void resetClipStackData() { fClipStackData.reset(); fCalledAddStackData = false; }
281 
282     void addClipStackData(const SkPath& devPath, const SkPath& operand, SkClipOp elementOp);
283     void addPathData(const SkPath& path, const char* pathName);
284     bool lastClipStackData(const SkPath& devPath);
285     void outputConicPoints(const SkPoint* pts, SkScalar weight);
286     void outputPoints(const SkPoint* pts, int count);
287     void outputPointsCommon(const SkPoint* pts, int count);
288     void outputScalar(SkScalar num);
289 
290     GrAuditTrail* getAuditTrail(SkCanvas*);
291 
292     void drawAndCollectOps(int n, SkCanvas*);
293     void cleanupAuditTrail(SkCanvas*);
294 
295     typedef SkCanvas INHERITED;
296 };
297 
298 #endif
299