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