| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 |  */ | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkDumpCanvas_DEFINED | 
 | 9 | #define SkDumpCanvas_DEFINED | 
 | 10 |  | 
 | 11 | #include "SkCanvas.h" | 
 | 12 |  | 
| robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 13 | #ifdef SK_DEVELOPER | 
 | 14 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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. | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 17 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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(); | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 27 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 |     enum Verb { | 
 | 29 |         kNULL_Verb, | 
 | 30 |  | 
 | 31 |         kSave_Verb, | 
 | 32 |         kRestore_Verb, | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 33 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 |         kMatrix_Verb, | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 35 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 |         kClip_Verb, | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 37 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 |         kDrawPaint_Verb, | 
 | 39 |         kDrawPoints_Verb, | 
| reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 40 |         kDrawOval_Verb, | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 |         kDrawRect_Verb, | 
| reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 42 |         kDrawRRect_Verb, | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 |         kDrawPath_Verb, | 
 | 44 |         kDrawBitmap_Verb, | 
 | 45 |         kDrawText_Verb, | 
 | 46 |         kDrawPicture_Verb, | 
| reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 47 |         kDrawVertices_Verb, | 
| robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 48 |         kDrawData_Verb, | 
 | 49 |  | 
 | 50 |         kBeginCommentGroup_Verb, | 
 | 51 |         kAddComment_Verb, | 
 | 52 |         kEndCommentGroup_Verb | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 |     }; | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 54 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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: | 
| reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 60 |         SK_DECLARE_INST_COUNT(Dumper) | 
 | 61 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 |         virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[], | 
 | 63 |                           const SkPaint*) = 0; | 
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 64 |  | 
| reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 65 |     private: | 
 | 66 |         typedef SkRefCnt INHERITED; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 |     }; | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 68 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 |     Dumper* getDumper() const { return fDumper; } | 
 | 70 |     void    setDumper(Dumper*); | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 71 |  | 
| reed@android.com | 9b46e77 | 2009-06-05 12:24:41 +0000 | [diff] [blame] | 72 |     int getNestLevel() const { return fNestLevel; } | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 73 |  | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 74 |     virtual int save(SaveFlags) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 |     virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 76 |                           SaveFlags) SK_OVERRIDE; | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 77 |     virtual void restore() SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 |  | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 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; | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 85 |  | 
| reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 86 |     virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; | 
| reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 87 |     virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE; | 
| reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 88 |     virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 |     virtual bool clipRegion(const SkRegion& deviceRgn, | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 90 |                             SkRegion::Op) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 |  | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 92 |     virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 |     virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 94 |                             const SkPaint& paint) SK_OVERRIDE; | 
| reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 95 |     virtual void drawOval(const SkRect&, const SkPaint& paint) SK_OVERRIDE; | 
| bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 96 |     virtual void drawRect(const SkRect&, const SkPaint& paint) SK_OVERRIDE; | 
| reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 97 |     virtual void drawRRect(const SkRRect&, const SkPaint& paint) SK_OVERRIDE; | 
| bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 98 |     virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 |     virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 100 |                             const SkPaint* paint) SK_OVERRIDE; | 
| reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 101 |     virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, | 
| commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 102 |                                       const SkRect& dst, const SkPaint* paint, | 
 | 103 |                                       DrawBitmapRectFlags flags) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 |     virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 105 |                                   const SkPaint* paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 |     virtual void drawSprite(const SkBitmap& bitmap, int left, int top, | 
| reed@google.com | 43d7484 | 2011-12-07 14:46:39 +0000 | [diff] [blame] | 107 |                             const SkPaint* paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 |     virtual void drawText(const void* text, size_t byteLength, SkScalar x, | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 109 |                           SkScalar y, const SkPaint& paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 |     virtual void drawPosText(const void* text, size_t byteLength, | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 111 |                              const SkPoint pos[], const SkPaint& paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 |     virtual void drawPosTextH(const void* text, size_t byteLength, | 
 | 113 |                               const SkScalar xpos[], SkScalar constY, | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 114 |                               const SkPaint& paint) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 |     virtual void drawTextOnPath(const void* text, size_t byteLength, | 
 | 116 |                                 const SkPath& path, const SkMatrix* matrix, | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 117 |                                 const SkPaint& paint) SK_OVERRIDE; | 
 | 118 |     virtual void drawPicture(SkPicture&) SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 |     virtual void drawVertices(VertexMode vmode, int vertexCount, | 
 | 120 |                               const SkPoint vertices[], const SkPoint texs[], | 
 | 121 |                               const SkColor colors[], SkXfermode* xmode, | 
 | 122 |                               const uint16_t indices[], int indexCount, | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 123 |                               const SkPaint& paint) SK_OVERRIDE; | 
 | 124 |     virtual void drawData(const void*, size_t) SK_OVERRIDE; | 
| robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 125 |     virtual void beginCommentGroup(const char* description) SK_OVERRIDE; | 
 | 126 |     virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; | 
 | 127 |     virtual void endCommentGroup() SK_OVERRIDE; | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 |  | 
 | 129 | private: | 
 | 130 |     Dumper* fDumper; | 
| reed@android.com | 9b46e77 | 2009-06-05 12:24:41 +0000 | [diff] [blame] | 131 |     int     fNestLevel; // for nesting recursive elements like pictures | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 132 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 |     void dump(Verb, const SkPaint*, const char format[], ...); | 
 | 134 |  | 
 | 135 |     typedef SkCanvas INHERITED; | 
 | 136 | }; | 
 | 137 |  | 
 | 138 | /** Formats the draw commands, and send them to a function-pointer provided | 
 | 139 |     by the caller. | 
 | 140 |  */ | 
 | 141 | class SkFormatDumper : public SkDumpCanvas::Dumper { | 
 | 142 | public: | 
 | 143 |     SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon); | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 144 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 |     // override from baseclass that does the formatting, and in turn calls | 
 | 146 |     // the function pointer that was passed to the constructor | 
 | 147 |     virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[], | 
| reed@google.com | 2d4297c | 2011-10-06 13:14:12 +0000 | [diff] [blame] | 148 |                       const SkPaint*) SK_OVERRIDE; | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 149 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | private: | 
 | 151 |     void (*fProc)(const char*, void*); | 
 | 152 |     void* fRefcon; | 
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 153 |  | 
| reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 |     typedef SkDumpCanvas::Dumper INHERITED; | 
 | 155 | }; | 
 | 156 |  | 
 | 157 | /** Subclass of Dumper that dumps the drawing command to SkDebugf | 
 | 158 |  */ | 
 | 159 | class SkDebugfDumper : public SkFormatDumper { | 
 | 160 | public: | 
 | 161 |     SkDebugfDumper(); | 
 | 162 |  | 
 | 163 | private: | 
 | 164 |     typedef SkFormatDumper INHERITED; | 
 | 165 | }; | 
 | 166 |  | 
 | 167 | #endif | 
| robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 168 |  | 
 | 169 | #endif |