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/SkColor.h" 14 #include "include/core/SkM44.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkRefCnt.h" 17 #include "include/core/SkSamplingOptions.h" 18 #include "include/core/SkScalar.h" 19 #include "include/core/SkTypes.h" 20 #include "include/private/base/SkTDArray.h" 21 22 #include <cstddef> 23 #include <map> 24 #include <vector> 25 26 class DebugLayerManager; 27 class DrawCommand; 28 class GrAuditTrail; 29 class SkData; 30 class SkDrawable; 31 class SkImage; 32 class SkJSONWriter; 33 class SkMatrix; 34 class SkPaint; 35 class SkPath; 36 class SkPicture; 37 class SkRRect; 38 class SkRegion; 39 class SkShader; 40 class SkTextBlob; 41 class SkVertices; 42 class UrlDataManager; 43 enum class SkBlendMode; 44 enum class SkClipOp; 45 struct SkDrawShadowRec; 46 struct SkPoint; 47 struct SkRSXform; 48 49 class DebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> { 50 public: 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 */ setLayerManagerAndFrame(DebugLayerManager * lm,int frame)67 void setLayerManagerAndFrame(DebugLayerManager* lm, int frame) { 68 fLayerManager = lm; 69 fFrame = frame; 70 } 71 72 /** 73 * Enable or disable overdraw visualization 74 */ 75 void setOverdrawViz(bool overdrawViz); 76 getOverdrawViz()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 */ setClipVizColor(SkColor clipVizColor)82 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; } 83 setAndroidClipViz(bool enable)84 void setAndroidClipViz(bool enable) { this->fShowAndroidClip = enable; } 85 setOriginVisible(bool enable)86 void setOriginVisible(bool enable) { this->fShowOrigin = enable; } 87 setDrawGpuOpBounds(bool drawGpuOpBounds)88 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; } 89 getDrawGpuOpBounds()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 */ getCurrentMatrix()111 const SkM44& getCurrentMatrix() { return fMatrix; } 112 113 /** 114 Returns the most recently calculated clip 115 */ getCurrentClip()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 */ getSize()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 detachCommands(SkTDArray<DrawCommand * > * dst)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 157 protected: 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], 178 SkBlendMode, 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 192 void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&, 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], 214 QuadAAFlags, 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 225 private: 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 264