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