1 #ifndef SkDumpCanvas_DEFINED 2 #define SkDumpCanvas_DEFINED 3 4 #include "SkCanvas.h" 5 6 /** This class overrides all the draw methods on SkCanvas, and formats them 7 as text, and then sends that to a Dumper helper object. 8 9 Typical use might be to dump a display list to a log file to see what is 10 being drawn. 11 */ 12 class SkDumpCanvas : public SkCanvas { 13 public: 14 class Dumper; 15 16 explicit SkDumpCanvas(Dumper* = 0); 17 virtual ~SkDumpCanvas(); 18 19 enum Verb { 20 kNULL_Verb, 21 22 kSave_Verb, 23 kRestore_Verb, 24 25 kMatrix_Verb, 26 27 kClip_Verb, 28 29 kDrawPaint_Verb, 30 kDrawPoints_Verb, 31 kDrawRect_Verb, 32 kDrawPath_Verb, 33 kDrawBitmap_Verb, 34 kDrawText_Verb, 35 kDrawPicture_Verb, 36 kDrawShape_Verb, 37 kDrawVertices_Verb 38 }; 39 40 /** Subclasses of this are installed on the DumpCanvas, and then called for 41 each drawing command. 42 */ 43 class Dumper : public SkRefCnt { 44 public: 45 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[], 46 const SkPaint*) = 0; 47 }; 48 getDumper()49 Dumper* getDumper() const { return fDumper; } 50 void setDumper(Dumper*); 51 getNestLevel()52 int getNestLevel() const { return fNestLevel; } 53 54 // overrides from SkCanvas 55 56 virtual int save(SaveFlags flags = kMatrixClip_SaveFlag); 57 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, 58 SaveFlags flags = kARGB_ClipLayer_SaveFlag); 59 virtual void restore(); 60 61 virtual bool translate(SkScalar dx, SkScalar dy); 62 virtual bool scale(SkScalar sx, SkScalar sy); 63 virtual bool rotate(SkScalar degrees); 64 virtual bool skew(SkScalar sx, SkScalar sy); 65 virtual bool concat(const SkMatrix& matrix); 66 virtual void setMatrix(const SkMatrix& matrix); 67 68 virtual bool clipRect(const SkRect& rect, 69 SkRegion::Op op = SkRegion::kIntersect_Op); 70 virtual bool clipPath(const SkPath& path, 71 SkRegion::Op op = SkRegion::kIntersect_Op); 72 virtual bool clipRegion(const SkRegion& deviceRgn, 73 SkRegion::Op op = SkRegion::kIntersect_Op); 74 75 virtual void drawPaint(const SkPaint& paint); 76 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], 77 const SkPaint& paint); 78 virtual void drawRect(const SkRect& rect, const SkPaint& paint); 79 virtual void drawPath(const SkPath& path, const SkPaint& paint); 80 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, 81 const SkPaint* paint = NULL); 82 virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, 83 const SkRect& dst, const SkPaint* paint = NULL); 84 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, 85 const SkPaint* paint = NULL); 86 virtual void drawSprite(const SkBitmap& bitmap, int left, int top, 87 const SkPaint* paint = NULL); 88 virtual void drawText(const void* text, size_t byteLength, SkScalar x, 89 SkScalar y, const SkPaint& paint); 90 virtual void drawPosText(const void* text, size_t byteLength, 91 const SkPoint pos[], const SkPaint& paint); 92 virtual void drawPosTextH(const void* text, size_t byteLength, 93 const SkScalar xpos[], SkScalar constY, 94 const SkPaint& paint); 95 virtual void drawTextOnPath(const void* text, size_t byteLength, 96 const SkPath& path, const SkMatrix* matrix, 97 const SkPaint& paint); 98 virtual void drawPicture(SkPicture&); 99 virtual void drawShape(SkShape*); 100 virtual void drawVertices(VertexMode vmode, int vertexCount, 101 const SkPoint vertices[], const SkPoint texs[], 102 const SkColor colors[], SkXfermode* xmode, 103 const uint16_t indices[], int indexCount, 104 const SkPaint& paint); 105 106 private: 107 Dumper* fDumper; 108 int fNestLevel; // for nesting recursive elements like pictures 109 110 void dump(Verb, const SkPaint*, const char format[], ...); 111 112 typedef SkCanvas INHERITED; 113 }; 114 115 /** Formats the draw commands, and send them to a function-pointer provided 116 by the caller. 117 */ 118 class SkFormatDumper : public SkDumpCanvas::Dumper { 119 public: 120 SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon); 121 122 // override from baseclass that does the formatting, and in turn calls 123 // the function pointer that was passed to the constructor 124 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[], 125 const SkPaint*); 126 127 private: 128 void (*fProc)(const char*, void*); 129 void* fRefcon; 130 131 typedef SkDumpCanvas::Dumper INHERITED; 132 }; 133 134 /** Subclass of Dumper that dumps the drawing command to SkDebugf 135 */ 136 class SkDebugfDumper : public SkFormatDumper { 137 public: 138 SkDebugfDumper(); 139 140 private: 141 typedef SkFormatDumper INHERITED; 142 }; 143 144 #endif 145