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