blob: 608ab01334a0320d87a718bdf11a01a49f2b07f3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#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.org74b46192012-01-28 01:45:11 +000015
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 Typical use might be to dump a display list to a log file to see what is
17 being drawn.
18 */
19class SkDumpCanvas : public SkCanvas {
20public:
21 class Dumper;
22
23 explicit SkDumpCanvas(Dumper* = 0);
24 virtual ~SkDumpCanvas();
vandebo@chromium.org74b46192012-01-28 01:45:11 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 enum Verb {
27 kNULL_Verb,
28
29 kSave_Verb,
30 kRestore_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 kMatrix_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 kClip_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 kDrawPaint_Verb,
37 kDrawPoints_Verb,
reed@google.com4ed0fb72012-12-12 20:48:18 +000038 kDrawOval_Verb,
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 kDrawRect_Verb,
reed@google.com4ed0fb72012-12-12 20:48:18 +000040 kDrawRRect_Verb,
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 kDrawPath_Verb,
42 kDrawBitmap_Verb,
43 kDrawText_Verb,
44 kDrawPicture_Verb,
reed@android.comcb608442009-12-04 21:32:27 +000045 kDrawVertices_Verb,
46 kDrawData_Verb
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 /** Subclasses of this are installed on the DumpCanvas, and then called for
50 each drawing command.
51 */
52 class Dumper : public SkRefCnt {
53 public:
reed@google.com563a3b42012-06-26 19:24:50 +000054 SK_DECLARE_INST_COUNT(Dumper)
55
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
57 const SkPaint*) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058
reed@google.com563a3b42012-06-26 19:24:50 +000059 private:
60 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 Dumper* getDumper() const { return fDumper; }
64 void setDumper(Dumper*);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000065
reed@android.com9b46e772009-06-05 12:24:41 +000066 int getNestLevel() const { return fNestLevel; }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000067
reed@google.com43d74842011-12-07 14:46:39 +000068 virtual int save(SaveFlags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
reed@google.com43d74842011-12-07 14:46:39 +000070 SaveFlags) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000071 virtual void restore() SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
reed@google.com2d4297c2011-10-06 13:14:12 +000073 virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
74 virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
75 virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
76 virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
77 virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
78 virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +000079
reed@google.com071eef92011-10-12 11:52:53 +000080 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000081 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000082 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 virtual bool clipRegion(const SkRegion& deviceRgn,
reed@google.com43d74842011-12-07 14:46:39 +000084 SkRegion::Op) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
reed@google.com2d4297c2011-10-06 13:14:12 +000086 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000088 const SkPaint& paint) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000089 virtual void drawOval(const SkRect&, const SkPaint& paint) SK_OVERRIDE;
90 virtual void drawRect(const SkRect&, const SkPaint& paint) SK_OVERRIDE;
91 virtual void drawRRect(const SkRRect&, const SkPaint& paint) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000092 virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
reed@google.com43d74842011-12-07 14:46:39 +000094 const SkPaint* paint) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +000095 virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
reed@google.com43d74842011-12-07 14:46:39 +000096 const SkRect& dst, const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
reed@google.com43d74842011-12-07 14:46:39 +000098 const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
reed@google.com43d74842011-12-07 14:46:39 +0000100 const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +0000102 SkScalar y, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +0000104 const SkPoint pos[], const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 virtual void drawPosTextH(const void* text, size_t byteLength,
106 const SkScalar xpos[], SkScalar constY,
reed@google.com2d4297c2011-10-06 13:14:12 +0000107 const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 virtual void drawTextOnPath(const void* text, size_t byteLength,
109 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +0000110 const SkPaint& paint) SK_OVERRIDE;
111 virtual void drawPicture(SkPicture&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 virtual void drawVertices(VertexMode vmode, int vertexCount,
113 const SkPoint vertices[], const SkPoint texs[],
114 const SkColor colors[], SkXfermode* xmode,
115 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +0000116 const SkPaint& paint) SK_OVERRIDE;
117 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
119private:
120 Dumper* fDumper;
reed@android.com9b46e772009-06-05 12:24:41 +0000121 int fNestLevel; // for nesting recursive elements like pictures
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 void dump(Verb, const SkPaint*, const char format[], ...);
124
125 typedef SkCanvas INHERITED;
126};
127
128/** Formats the draw commands, and send them to a function-pointer provided
129 by the caller.
130 */
131class SkFormatDumper : public SkDumpCanvas::Dumper {
132public:
133 SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 // override from baseclass that does the formatting, and in turn calls
136 // the function pointer that was passed to the constructor
137 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
reed@google.com2d4297c2011-10-06 13:14:12 +0000138 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000139
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140private:
141 void (*fProc)(const char*, void*);
142 void* fRefcon;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 typedef SkDumpCanvas::Dumper INHERITED;
145};
146
147/** Subclass of Dumper that dumps the drawing command to SkDebugf
148 */
149class SkDebugfDumper : public SkFormatDumper {
150public:
151 SkDebugfDumper();
152
153private:
154 typedef SkFormatDumper INHERITED;
155};
156
157#endif