blob: cdb1f19b428b180bfc5578eec5c8e40ca170e87b [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
robertphillips@google.com76f9e932013-01-15 20:17:47 +000013#ifdef SK_DEVELOPER
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015/** 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.org74b46192012-01-28 01:45:11 +000017
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 Typical use might be to dump a display list to a log file to see what is
19 being drawn.
20 */
21class SkDumpCanvas : public SkCanvas {
22public:
23 class Dumper;
24
25 explicit SkDumpCanvas(Dumper* = 0);
26 virtual ~SkDumpCanvas();
vandebo@chromium.org74b46192012-01-28 01:45:11 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 enum Verb {
29 kNULL_Verb,
30
31 kSave_Verb,
32 kRestore_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000033
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 kMatrix_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 kClip_Verb,
vandebo@chromium.org74b46192012-01-28 01:45:11 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 kDrawPaint_Verb,
39 kDrawPoints_Verb,
reed@google.com4ed0fb72012-12-12 20:48:18 +000040 kDrawOval_Verb,
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 kDrawRect_Verb,
reed@google.com4ed0fb72012-12-12 20:48:18 +000042 kDrawRRect_Verb,
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 kDrawPath_Verb,
44 kDrawBitmap_Verb,
45 kDrawText_Verb,
46 kDrawPicture_Verb,
reed@android.comcb608442009-12-04 21:32:27 +000047 kDrawVertices_Verb,
robertphillips@google.com0a4805e2013-05-29 13:24:23 +000048 kDrawData_Verb,
49
50 kBeginCommentGroup_Verb,
51 kAddComment_Verb,
52 kEndCommentGroup_Verb
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 /** 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.com563a3b42012-06-26 19:24:50 +000060 SK_DECLARE_INST_COUNT(Dumper)
61
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
63 const SkPaint*) = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
reed@google.com563a3b42012-06-26 19:24:50 +000065 private:
66 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +000068
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 Dumper* getDumper() const { return fDumper; }
70 void setDumper(Dumper*);
vandebo@chromium.org74b46192012-01-28 01:45:11 +000071
reed@android.com9b46e772009-06-05 12:24:41 +000072 int getNestLevel() const { return fNestLevel; }
vandebo@chromium.org74b46192012-01-28 01:45:11 +000073
reed@google.com43d74842011-12-07 14:46:39 +000074 virtual int save(SaveFlags) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
reed@google.com43d74842011-12-07 14:46:39 +000076 SaveFlags) SK_OVERRIDE;
reed@google.com2d4297c2011-10-06 13:14:12 +000077 virtual void restore() SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
reed@google.com2d4297c2011-10-06 13:14:12 +000079 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.org74b46192012-01-28 01:45:11 +000085
reed@google.com071eef92011-10-12 11:52:53 +000086 virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000087 virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
reed@google.com071eef92011-10-12 11:52:53 +000088 virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 virtual bool clipRegion(const SkRegion& deviceRgn,
reed@google.com43d74842011-12-07 14:46:39 +000090 SkRegion::Op) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091
reed@google.com2d4297c2011-10-06 13:14:12 +000092 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000094 const SkPaint& paint) SK_OVERRIDE;
reed@google.com4ed0fb72012-12-12 20:48:18 +000095 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;
reed@google.com2d4297c2011-10-06 13:14:12 +000098 virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
reed@google.com43d74842011-12-07 14:46:39 +0000100 const SkPaint* paint) SK_OVERRIDE;
reed@google.com71121732012-09-18 15:14:33 +0000101 virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
reed@google.com43d74842011-12-07 14:46:39 +0000102 const SkRect& dst, const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
reed@google.com43d74842011-12-07 14:46:39 +0000104 const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
reed@google.com43d74842011-12-07 14:46:39 +0000106 const SkPaint* paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +0000108 SkScalar y, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +0000110 const SkPoint pos[], const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 virtual void drawPosTextH(const void* text, size_t byteLength,
112 const SkScalar xpos[], SkScalar constY,
reed@google.com2d4297c2011-10-06 13:14:12 +0000113 const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 virtual void drawTextOnPath(const void* text, size_t byteLength,
115 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +0000116 const SkPaint& paint) SK_OVERRIDE;
117 virtual void drawPicture(SkPicture&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 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,
reed@google.com2d4297c2011-10-06 13:14:12 +0000122 const SkPaint& paint) SK_OVERRIDE;
123 virtual void drawData(const void*, size_t) SK_OVERRIDE;
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000124 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;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127
128private:
129 Dumper* fDumper;
reed@android.com9b46e772009-06-05 12:24:41 +0000130 int fNestLevel; // for nesting recursive elements like pictures
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 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 */
140class SkFormatDumper : public SkDumpCanvas::Dumper {
141public:
142 SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 // 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[],
reed@google.com2d4297c2011-10-06 13:14:12 +0000147 const SkPaint*) SK_OVERRIDE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149private:
150 void (*fProc)(const char*, void*);
151 void* fRefcon;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 typedef SkDumpCanvas::Dumper INHERITED;
154};
155
156/** Subclass of Dumper that dumps the drawing command to SkDebugf
157 */
158class SkDebugfDumper : public SkFormatDumper {
159public:
160 SkDebugfDumper();
161
162private:
163 typedef SkFormatDumper INHERITED;
164};
165
166#endif
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000167
168#endif