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