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 class GrAuditTrail; 22 class SkNWayCanvas; 23 class SkPicture; 24 25 class DebugCanvas : public SkCanvasVirtualEnforcer<SkCanvas> { 26 public: 27 DebugCanvas(int width, int height); 28 29 DebugCanvas(SkIRect bounds); 30 31 ~DebugCanvas() override; 32 33 /** 34 * Enable or disable overdraw visualization 35 */ 36 void setOverdrawViz(bool overdrawViz); 37 getOverdrawViz()38 bool getOverdrawViz() const { return fOverdrawViz; } 39 40 /** 41 * Set the color of the clip visualization. An alpha of zero renders the clip invisible. 42 */ setClipVizColor(SkColor clipVizColor)43 void setClipVizColor(SkColor clipVizColor) { this->fClipVizColor = clipVizColor; } 44 setDrawGpuOpBounds(bool drawGpuOpBounds)45 void setDrawGpuOpBounds(bool drawGpuOpBounds) { fDrawGpuOpBounds = drawGpuOpBounds; } 46 getDrawGpuOpBounds()47 bool getDrawGpuOpBounds() const { return fDrawGpuOpBounds; } 48 49 /** 50 Executes all draw calls to the canvas. 51 @param canvas The canvas being drawn to 52 */ 53 void draw(SkCanvas* canvas); 54 55 /** 56 Executes the draw calls up to the specified index. 57 @param canvas The canvas being drawn to 58 @param index The index of the final command being executed 59 @param m an optional Mth gpu op to highlight, or -1 60 */ 61 void drawTo(SkCanvas* canvas, int index, int m = -1); 62 63 /** 64 Returns the most recently calculated transformation matrix 65 */ getCurrentMatrix()66 const SkMatrix& getCurrentMatrix() { return fMatrix; } 67 68 /** 69 Returns the most recently calculated clip 70 */ getCurrentClip()71 const SkIRect& getCurrentClip() { return fClip; } 72 73 /** 74 Removes the command at the specified index 75 @param index The index of the command to delete 76 */ 77 void deleteDrawCommandAt(int index); 78 79 /** 80 Returns the draw command at the given index. 81 @param index The index of the command 82 */ 83 DrawCommand* getDrawCommandAt(int index); 84 85 /** 86 Returns length of draw command vector. 87 */ getSize()88 int getSize() const { return fCommandVector.count(); } 89 90 /** 91 Toggles the visibility / execution of the draw command at index i with 92 the value of toggle. 93 */ 94 void toggleCommand(int index, bool toggle); 95 96 /** 97 Returns a JSON object representing up to the Nth draw, where N is less than 98 DebugCanvas::getSize(). The encoder may use the UrlDataManager to store binary data such 99 as images, referring to them via URLs embedded in the JSON. 100 */ 101 void toJSON(SkJSONWriter& writer, UrlDataManager& urlDataManager, int n, SkCanvas*); 102 103 void toJSONOpList(SkJSONWriter& writer, int n, SkCanvas*); 104 detachCommands(SkTDArray<DrawCommand * > * dst)105 void detachCommands(SkTDArray<DrawCommand*>* dst) { fCommandVector.swap(*dst); } 106 107 protected: 108 void willSave() override; 109 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 110 bool onDoSaveBehind(const SkRect*) override; 111 void willRestore() override; 112 113 void didConcat(const SkMatrix&) override; 114 115 void didSetMatrix(const SkMatrix&) override; 116 117 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 118 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 119 void onDrawTextBlob(const SkTextBlob* blob, 120 SkScalar x, 121 SkScalar y, 122 const SkPaint& paint) override; 123 124 void onDrawPatch(const SkPoint cubics[12], 125 const SkColor colors[4], 126 const SkPoint texCoords[4], 127 SkBlendMode, 128 const SkPaint& paint) override; 129 void onDrawPaint(const SkPaint&) override; 130 void onDrawBehind(const SkPaint&) override; 131 132 void onDrawRect(const SkRect&, const SkPaint&) override; 133 void onDrawOval(const SkRect&, const SkPaint&) override; 134 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 135 void onDrawRRect(const SkRRect&, const SkPaint&) override; 136 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 137 void onDrawVerticesObject(const SkVertices*, 138 const SkVertices::Bone bones[], 139 int boneCount, 140 SkBlendMode, 141 const SkPaint&) override; 142 void onDrawPath(const SkPath&, const SkPaint&) override; 143 void onDrawRegion(const SkRegion&, const SkPaint&) override; 144 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override; 145 void onDrawBitmapLattice(const SkBitmap&, 146 const Lattice&, 147 const SkRect&, 148 const SkPaint*) override; 149 void onDrawBitmapRect(const SkBitmap&, 150 const SkRect* src, 151 const SkRect& dst, 152 const SkPaint*, 153 SrcRectConstraint) override; 154 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; 155 void onDrawImageLattice(const SkImage* image, 156 const Lattice& lattice, 157 const SkRect& dst, 158 const SkPaint* paint) override; 159 void onDrawImageRect(const SkImage*, 160 const SkRect* src, 161 const SkRect& dst, 162 const SkPaint*, 163 SrcRectConstraint) override; 164 void onDrawBitmapNine(const SkBitmap&, 165 const SkIRect& center, 166 const SkRect& dst, 167 const SkPaint*) override; 168 void onDrawImageNine(const SkImage*, 169 const SkIRect& center, 170 const SkRect& dst, 171 const SkPaint*) override; 172 void onDrawAtlas(const SkImage*, 173 const SkRSXform[], 174 const SkRect[], 175 const SkColor[], 176 int, 177 SkBlendMode, 178 const SkRect*, 179 const SkPaint*) override; 180 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override; 181 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override; 182 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override; 183 void onClipRegion(const SkRegion& region, SkClipOp) override; 184 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; 185 186 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 187 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 188 189 void onDrawEdgeAAQuad(const SkRect&, 190 const SkPoint[4], 191 QuadAAFlags, 192 SkColor, 193 SkBlendMode) override; 194 void onDrawEdgeAAImageSet(const ImageSetEntry[], 195 int count, 196 const SkPoint[], 197 const SkMatrix[], 198 const SkPaint*, 199 SrcRectConstraint) override; 200 201 private: 202 SkTDArray<DrawCommand*> fCommandVector; 203 SkMatrix fMatrix; 204 SkIRect fClip; 205 206 bool fOverdrawViz; 207 SkColor fClipVizColor; 208 bool fDrawGpuOpBounds; 209 210 /** 211 Adds the command to the class' vector of commands. 212 @param command The draw command for execution 213 */ 214 void addDrawCommand(DrawCommand* command); 215 216 GrAuditTrail* getAuditTrail(SkCanvas*); 217 218 void drawAndCollectOps(int n, SkCanvas*); 219 void cleanupAuditTrail(SkCanvas*); 220 221 typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED; 222 }; 223 224 #endif 225