Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DebugCanvas.h
Go to the documentation of this file.
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#ifndef SKDEBUGCANVAS_H_
9#define SKDEBUGCANVAS_H_
10
14#include "include/core/SkM44.h"
15#include "include/core/SkRect.h"
21
22#include <cstddef>
23#include <map>
24#include <vector>
25
27class DrawCommand;
28class GrAuditTrail;
29class SkData;
30class SkDrawable;
31class SkImage;
32class SkJSONWriter;
33class SkMatrix;
34class SkPaint;
35class SkPath;
36class SkPicture;
37class SkRRect;
38class SkRegion;
39class SkShader;
40class SkTextBlob;
41class SkVertices;
42class UrlDataManager;
43enum class SkBlendMode;
44enum class SkClipOp;
45struct SkDrawShadowRec;
46struct SkPoint;
47struct SkRSXform;
48
49class DebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
50public:
51 DebugCanvas(int width, int height);
52
53 DebugCanvas(SkIRect bounds);
54
55 ~DebugCanvas() override;
56
57 /**
58 * Provide a DebugLayerManager for mskp files containing layer information
59 * when set this DebugCanvas will attempt to parse layer info from annotations.
60 * it will store layer pictures to the layer manager, and interpret some drawImageRects
61 * as layer draws, deferring to the layer manager for images.
62 * Provide a frame number that will be passed to all layer manager functions to identify this
63 * DebugCanvas.
64 *
65 * Used only in wasm debugger animations.
66 */
68 fLayerManager = lm;
69 fFrame = frame;
70 }
71
72 /**
73 * Enable or disable overdraw visualization
74 */
75 void setOverdrawViz(bool overdrawViz);
76
77 bool getOverdrawViz() const { return fOverdrawViz; }
78
79 /**
80 * Set the color of the clip visualization. An alpha of zero renders the clip invisible.
81 */
82 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; }
83
84 void setAndroidClipViz(bool enable) { this->fShowAndroidClip = enable; }
85
86 void setOriginVisible(bool enable) { this->fShowOrigin = enable; }
87
88 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; }
89
90 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; }
91
92 /**
93 Executes all draw calls to the canvas.
94 @param canvas The canvas being drawn to
95 */
96 void draw(SkCanvas* canvas);
97
98 /**
99 Executes the draw calls up to the specified index.
100 Does not clear the canvas to transparent black first,
101 if needed, caller should do that first.
102 @param canvas The canvas being drawn to
103 @param index The index of the final command being executed
104 @param m an optional Mth gpu op to highlight, or -1
105 */
106 void drawTo(SkCanvas* canvas, int index, int m = -1);
107
108 /**
109 Returns the most recently calculated transformation matrix
110 */
111 const SkM44& getCurrentMatrix() { return fMatrix; }
112
113 /**
114 Returns the most recently calculated clip
115 */
116 const SkIRect& getCurrentClip() { return fClip; }
117
118 /**
119 Removes the command at the specified index
120 @param index The index of the command to delete
121 */
122 void deleteDrawCommandAt(int index);
123
124 /**
125 Returns the draw command at the given index.
126 @param index The index of the command
127 */
128 DrawCommand* getDrawCommandAt(int index) const;
129
130 /**
131 Returns length of draw command vector.
132 */
133 int getSize() const { return fCommandVector.size(); }
134
135 /**
136 Toggles the visibility / execution of the draw command at index i with
137 the value of toggle.
138 */
139 void toggleCommand(int index, bool toggle);
140
141 /**
142 Returns a JSON object representing all commands in the picture.
143 The encoder may use the UrlDataManager to store binary data such
144 as images, referring to them via URLs embedded in the JSON.
145 */
146 void toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager, SkCanvas*);
147
148 void toJSONOpsTask(SkJSONWriter& writer, SkCanvas*);
149
150 void detachCommands(SkTDArray<DrawCommand*>* dst) { fCommandVector.swap(*dst); }
151
152 /**
153 Returns a map from image IDs to command indices where they are used.
154 */
155 std::map<int, std::vector<int>> getImageIdToCommandMap(UrlDataManager& udm) const;
156
157protected:
158 void willSave() override;
159 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
160 bool onDoSaveBehind(const SkRect*) override;
161 void willRestore() override;
162
163 void didConcat44(const SkM44&) override;
164 void didSetM44(const SkM44&) override;
165 void didScale(SkScalar, SkScalar) override;
166 void didTranslate(SkScalar, SkScalar) override;
167
168 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
169 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
170 void onDrawTextBlob(const SkTextBlob* blob,
171 SkScalar x,
172 SkScalar y,
173 const SkPaint& paint) override;
174
175 void onDrawPatch(const SkPoint cubics[12],
176 const SkColor colors[4],
177 const SkPoint texCoords[4],
179 const SkPaint& paint) override;
180 void onDrawPaint(const SkPaint&) override;
181 void onDrawBehind(const SkPaint&) override;
182
183 void onDrawRect(const SkRect&, const SkPaint&) override;
184 void onDrawOval(const SkRect&, const SkPaint&) override;
185 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
186 void onDrawRRect(const SkRRect&, const SkPaint&) override;
187 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
188 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
189 void onDrawPath(const SkPath&, const SkPaint&) override;
190 void onDrawRegion(const SkRegion&, const SkPaint&) override;
191
193 const SkPaint*) override;
194 void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&,
195 const SkPaint*, SrcRectConstraint) override;
196 void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode,
197 const SkPaint*) override;
198 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
199 SkBlendMode, const SkSamplingOptions&, const SkRect*, const SkPaint*) override;
200
201 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
202 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
203 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
204 void onClipShader(sk_sp<SkShader>, SkClipOp) override;
205 void onClipRegion(const SkRegion& region, SkClipOp) override;
206 void onResetClip() override;
207
208 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
209 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
210 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
211
212 void onDrawEdgeAAQuad(const SkRect&,
213 const SkPoint[4],
215 const SkColor4f&,
216 SkBlendMode) override;
217 void onDrawEdgeAAImageSet2(const ImageSetEntry[],
218 int count,
219 const SkPoint[],
220 const SkMatrix[],
221 const SkSamplingOptions&,
222 const SkPaint*,
223 SrcRectConstraint) override;
224
225private:
226 SkTDArray<DrawCommand*> fCommandVector;
227 SkM44 fMatrix;
228 SkIRect fClip;
229
230 bool fOverdrawViz = false;
231 SkColor fClipVizColor;
232 bool fDrawGpuOpBounds = false;
233 bool fShowAndroidClip = false;
234 bool fShowOrigin = false;
235
236 // When not negative, indicates the render node id of the layer represented by the next
237 // drawPicture call.
238 int fnextDrawPictureLayerId = -1;
239 int fnextDrawImageRectLayerId = -1;
240 SkIRect fnextDrawPictureDirtyRect;
241 // may be null, in which case layer annotations are ignored.
242 DebugLayerManager* fLayerManager = nullptr;
243 // May be set when DebugCanvas is used in playing back an animation.
244 // Only used for passing to fLayerManager to identify itself.
245 int fFrame = -1;
246 SkRect fAndroidClip = SkRect::MakeEmpty();
247
248 /**
249 Adds the command to the class' vector of commands.
250 @param command The draw command for execution
251 */
252 void addDrawCommand(DrawCommand* command);
253
254#if defined(SK_GANESH)
255 GrAuditTrail* getAuditTrail(SkCanvas*);
256 void drawAndCollectOps(SkCanvas*);
257 void cleanupAuditTrail(GrAuditTrail*);
258#endif
259
260 using INHERITED = SkCanvasVirtualEnforcer<SkCanvas>;
261};
262
263#endif
int count
SkBlendMode
Definition SkBlendMode.h:38
SkClipOp
Definition SkClipOp.h:13
uint32_t SkColor
Definition SkColor.h:37
SkFilterMode
void onClipRegion(const SkRegion &region, SkClipOp) override
DrawCommand * getDrawCommandAt(int index) const
void setClipVizColor(SkColor clipVizColor)
Definition DebugCanvas.h:82
void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], SkBlendMode, const SkPaint &paint) override
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &) override
void setOriginVisible(bool enable)
Definition DebugCanvas.h:86
bool getDrawGpuOpBounds() const
Definition DebugCanvas.h:90
void setAndroidClipViz(bool enable)
Definition DebugCanvas.h:84
void onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) override
void onResetClip() override
const SkIRect & getCurrentClip()
void setOverdrawViz(bool overdrawViz)
void willSave() override
void onDrawRect(const SkRect &, const SkPaint &) override
void didScale(SkScalar, SkScalar) override
void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint &) override
void onDrawPath(const SkPath &, const SkPaint &) override
void onClipPath(const SkPath &, SkClipOp, ClipEdgeStyle) override
void didConcat44(const SkM44 &) override
void onDrawRRect(const SkRRect &, const SkPaint &) override
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
bool getOverdrawViz() const
Definition DebugCanvas.h:77
void onDrawPicture(const SkPicture *, const SkMatrix *, const SkPaint *) override
std::map< int, std::vector< int > > getImageIdToCommandMap(UrlDataManager &udm) const
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
void onClipRRect(const SkRRect &, SkClipOp, ClipEdgeStyle) override
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
void didTranslate(SkScalar, SkScalar) override
void drawTo(SkCanvas *canvas, int index, int m=-1)
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
bool onDoSaveBehind(const SkRect *) override
void detachCommands(SkTDArray< DrawCommand * > *dst)
void onClipRect(const SkRect &, SkClipOp, ClipEdgeStyle) override
const SkM44 & getCurrentMatrix()
void onDrawPaint(const SkPaint &) override
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
~DebugCanvas() override
void toggleCommand(int index, bool toggle)
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
void toJSONOpsTask(SkJSONWriter &writer, SkCanvas *)
void onDrawShadowRec(const SkPath &, const SkDrawShadowRec &) override
int getSize() const
void setLayerManagerAndFrame(DebugLayerManager *lm, int frame)
Definition DebugCanvas.h:67
void onDrawRegion(const SkRegion &, const SkPaint &) override
void toJSON(SkJSONWriter &writer, UrlDataManager &urlDataManager, SkCanvas *)
void onClipShader(sk_sp< SkShader >, SkClipOp) override
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
void onDrawOval(const SkRect &, const SkPaint &) override
void onDrawImage2(const SkImage *, SkScalar, SkScalar, const SkSamplingOptions &, const SkPaint *) override
void onDrawBehind(const SkPaint &) override
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
void draw(SkCanvas *canvas)
void setDrawGpuOpBounds(bool drawGpuOpBounds)
Definition DebugCanvas.h:88
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
void deleteDrawCommandAt(int index)
void didSetM44(const SkM44 &) override
void willRestore() override
SrcRectConstraint
Definition SkCanvas.h:1541
SaveLayerStrategy
Definition SkCanvas.h:2263
Definition SkM44.h:150
int size() const
Definition SkTDArray.h:138
void swap(SkTDArray< T > &that)
Definition SkTDArray.h:130
const Paint & paint
double frame
Definition examples.cpp:31
float SkScalar
Definition extension.cpp:12
double y
double x
int32_t height
int32_t width
static constexpr SkRect MakeEmpty()
Definition SkRect.h:595