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 SkMatrix& 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 didConcat(const SkMatrix&) override; 146 void didSetMatrix(const SkMatrix&) override; 147 void didScale(SkScalar, SkScalar) override; 148 void didTranslate(SkScalar, SkScalar) override; 149 150 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 151 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 152 void onDrawTextBlob(const SkTextBlob* blob, 153 SkScalar x, 154 SkScalar y, 155 const SkPaint& paint) override; 156 157 void onDrawPatch(const SkPoint cubics[12], 158 const SkColor colors[4], 159 const SkPoint texCoords[4], 160 SkBlendMode, 161 const SkPaint& paint) override; 162 void onDrawPaint(const SkPaint&) override; 163 void onDrawBehind(const SkPaint&) override; 164 165 void onDrawRect(const SkRect&, const SkPaint&) override; 166 void onDrawOval(const SkRect&, const SkPaint&) override; 167 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 168 void onDrawRRect(const SkRRect&, const SkPaint&) override; 169 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 170 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 171 void onDrawPath(const SkPath&, const SkPaint&) override; 172 void onDrawRegion(const SkRegion&, const SkPaint&) override; 173 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; 174 void onDrawImageLattice(const SkImage* image, 175 const Lattice& lattice, 176 const SkRect& dst, 177 const SkPaint* paint) override; 178 void onDrawImageRect(const SkImage*, 179 const SkRect* src, 180 const SkRect& dst, 181 const SkPaint*, 182 SrcRectConstraint) override; 183 void onDrawImageNine(const SkImage*, 184 const SkIRect& center, 185 const SkRect& dst, 186 const SkPaint*) override; 187 void onDrawAtlas(const SkImage*, 188 const SkRSXform[], 189 const SkRect[], 190 const SkColor[], 191 int, 192 SkBlendMode, 193 const SkRect*, 194 const SkPaint*) override; 195 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override; 196 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override; 197 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override; 198 void onClipShader(sk_sp<SkShader>, SkClipOp) override; 199 void onClipRegion(const SkRegion& region, SkClipOp) override; 200 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; 201 202 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 203 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 204 205 void onDrawEdgeAAQuad(const SkRect&, 206 const SkPoint[4], 207 QuadAAFlags, 208 const SkColor4f&, 209 SkBlendMode) override; 210 void onDrawEdgeAAImageSet(const ImageSetEntry[], 211 int count, 212 const SkPoint[], 213 const SkMatrix[], 214 const SkPaint*, 215 SrcRectConstraint) override; 216 217 private: 218 SkTDArray<DrawCommand*> fCommandVector; 219 SkMatrix fMatrix; 220 SkIRect fClip; 221 222 bool fOverdrawViz = false; 223 SkColor fClipVizColor; 224 bool fDrawGpuOpBounds = false; 225 bool fShowAndroidClip = false; 226 bool fShowOrigin = false; 227 228 // When not negative, indicates the render node id of the layer represented by the next 229 // drawPicture call. 230 int fnextDrawPictureLayerId = -1; 231 int fnextDrawImageRectLayerId = -1; 232 SkIRect fnextDrawPictureDirtyRect; 233 // may be null, in which case layer annotations are ignored. 234 DebugLayerManager* fLayerManager = nullptr; 235 // May be set when DebugCanvas is used in playing back an animation. 236 // Only used for passing to fLayerManager to identify itself. 237 int fFrame = -1; 238 SkRect fAndroidClip = SkRect::MakeEmpty(); 239 240 /** 241 Adds the command to the class' vector of commands. 242 @param command The draw command for execution 243 */ 244 void addDrawCommand(DrawCommand* command); 245 246 GrAuditTrail* getAuditTrail(SkCanvas*); 247 248 void drawAndCollectOps(SkCanvas*); 249 void cleanupAuditTrail(SkCanvas*); 250 251 using INHERITED = SkCanvasVirtualEnforcer<SkCanvas>; 252 }; 253 254 #endif 255