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