blob: 6f014595a57f6d7882d57cdee552957e3012d114 [file] [log] [blame]
Mike Klein8f4e2242019-03-20 11:59:00 -05001/*
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#ifndef SKDEBUGCANVAS_H_
9#define SKDEBUGCANVAS_H_
10
11#include "DrawCommand.h"
12#include "SkCanvas.h"
13#include "SkCanvasVirtualEnforcer.h"
14#include "SkPath.h"
15#include "SkPathOps.h"
16#include "SkString.h"
17#include "SkTArray.h"
18#include "SkVertices.h"
19#include "UrlDataManager.h"
20
21class GrAuditTrail;
22class SkNWayCanvas;
23class SkPicture;
24
25class DebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
26public:
27 DebugCanvas(int width, int height);
28
29 DebugCanvas(SkIRect bounds);
30
31 ~DebugCanvas() override;
32
33 /**
34 * Enable or disable overdraw visualization
35 */
36 void setOverdrawViz(bool overdrawViz);
37
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 */
43 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
44
45 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
46
47 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
48
49 /**
50 Executes all draw calls to the canvas.
51 @param canvas The canvas being drawn to
52 */
53 void draw(SkCanvas* canvas);
54
55 /**
56 Executes the draw calls up to the specified index.
57 @param canvas The canvas being drawn to
58 @param index The index of the final command being executed
59 @param m an optional Mth gpu op to highlight, or -1
60 */
61 void drawTo(SkCanvas* canvas, int index, int m = -1);
62
63 /**
64 Returns the most recently calculated transformation matrix
65 */
66 const SkMatrix& getCurrentMatrix() { return fMatrix; }
67
68 /**
69 Returns the most recently calculated clip
70 */
71 const SkIRect& getCurrentClip() { return fClip; }
72
73 /**
74 Removes the command at the specified index
75 @param index The index of the command to delete
76 */
77 void deleteDrawCommandAt(int index);
78
79 /**
80 Returns the draw command at the given index.
81 @param index The index of the command
82 */
83 DrawCommand* getDrawCommandAt(int index);
84
85 /**
86 Returns length of draw command vector.
87 */
88 int getSize() const { return fCommandVector.count(); }
89
90 /**
91 Toggles the visibility / execution of the draw command at index i with
92 the value of toggle.
93 */
94 void toggleCommand(int index, bool toggle);
95
96 /**
97 Returns a JSON object representing up to the Nth draw, where N is less than
98 DebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such
99 as images, referring to them via URLs embedded in the JSON.
100 */
101 void toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager, int n, SkCanvas*);
102
103 void toJSONOpList(SkJSONWriter& writer, int n, SkCanvas*);
104
105 void detachCommands(SkTDArray<DrawCommand*>* dst) { fCommandVector.swap(*dst); }
106
107protected:
108 void willSave() override;
109 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
110 bool onDoSaveBehind(const SkRect*) override;
111 void willRestore() override;
112
113 void didConcat(const SkMatrix&) override;
114
115 void didSetMatrix(const SkMatrix&) override;
116
117 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
118 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
119 void onDrawTextBlob(const SkTextBlob* blob,
120 SkScalar x,
121 SkScalar y,
122 const SkPaint& paint) override;
123
124 void onDrawPatch(const SkPoint cubics[12],
125 const SkColor colors[4],
126 const SkPoint texCoords[4],
127 SkBlendMode,
128 const SkPaint& paint) override;
129 void onDrawPaint(const SkPaint&) override;
130
131 void onDrawRect(const SkRect&, const SkPaint&) override;
132 void onDrawOval(const SkRect&, const SkPaint&) override;
133 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
134 void onDrawRRect(const SkRRect&, const SkPaint&) override;
135 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
136 void onDrawVerticesObject(const SkVertices*,
137 const SkVertices::Bone bones[],
138 int boneCount,
139 SkBlendMode,
140 const SkPaint&) override;
141 void onDrawPath(const SkPath&, const SkPaint&) override;
142 void onDrawRegion(const SkRegion&, const SkPaint&) override;
143 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
144 void onDrawBitmapLattice(const SkBitmap&,
145 const Lattice&,
146 const SkRect&,
147 const SkPaint*) override;
148 void onDrawBitmapRect(const SkBitmap&,
149 const SkRect* src,
150 const SkRect& dst,
151 const SkPaint*,
152 SrcRectConstraint) override;
153 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
154 void onDrawImageLattice(const SkImage* image,
155 const Lattice& lattice,
156 const SkRect& dst,
157 const SkPaint* paint) override;
158 void onDrawImageRect(const SkImage*,
159 const SkRect* src,
160 const SkRect& dst,
161 const SkPaint*,
162 SrcRectConstraint) override;
163 void onDrawBitmapNine(const SkBitmap&,
164 const SkIRect& center,
165 const SkRect& dst,
166 const SkPaint*) override;
167 void onDrawImageNine(const SkImage*,
168 const SkIRect& center,
169 const SkRect& dst,
170 const SkPaint*) override;
171 void onDrawAtlas(const SkImage*,
172 const SkRSXform[],
173 const SkRect[],
174 const SkColor[],
175 int,
176 SkBlendMode,
177 const SkRect*,
178 const SkPaint*) override;
179 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
180 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
181 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
182 void onClipRegion(const SkRegion& region, SkClipOp) override;
183 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
184
185 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
186 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
187
188 void onDrawEdgeAAQuad(const SkRect&,
189 const SkPoint[4],
190 QuadAAFlags,
191 SkColor,
192 SkBlendMode) override;
193 void onDrawEdgeAAImageSet(const ImageSetEntry[],
194 int count,
195 const SkPoint[],
196 const SkMatrix[],
197 const SkPaint*,
198 SrcRectConstraint) override;
199
200private:
201 SkTDArray<DrawCommand*> fCommandVector;
202 SkMatrix fMatrix;
203 SkIRect fClip;
204
205 bool fOverdrawViz;
206 SkColor fClipVizColor;
207 bool fDrawGpuOpBounds;
208
209 /**
210 Adds the command to the class' vector of commands.
211 @param command The draw command for execution
212 */
213 void addDrawCommand(DrawCommand* command);
214
215 GrAuditTrail* getAuditTrail(SkCanvas*);
216
217 void drawAndCollectOps(int n, SkCanvas*);
218 void cleanupAuditTrail(SkCanvas*);
219
220 typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
221};
222
223#endif