blob: 17e7cde67c5208e76cc3300550dea1fefffe96ec [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.
15
16 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();
25
26 enum Verb {
27 kNULL_Verb,
28
29 kSave_Verb,
30 kRestore_Verb,
31
32 kMatrix_Verb,
33
34 kClip_Verb,
35
36 kDrawPaint_Verb,
37 kDrawPoints_Verb,
38 kDrawRect_Verb,
39 kDrawPath_Verb,
40 kDrawBitmap_Verb,
41 kDrawText_Verb,
42 kDrawPicture_Verb,
reed@android.comcb608442009-12-04 21:32:27 +000043 kDrawVertices_Verb,
44 kDrawData_Verb
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 };
46
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 };
55
56 Dumper* getDumper() const { return fDumper; }
57 void setDumper(Dumper*);
58
reed@android.com9b46e772009-06-05 12:24:41 +000059 int getNestLevel() const { return fNestLevel; }
60
reed@google.com2d4297c2011-10-06 13:14:12 +000061 virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
reed@google.com2d4297c2011-10-06 13:14:12 +000063 SaveFlags flags = kARGB_ClipLayer_SaveFlag) SK_OVERRIDE;
64 virtual void restore() SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
reed@google.com2d4297c2011-10-06 13:14:12 +000066 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;
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73 virtual bool clipRect(const SkRect& rect,
reed@google.com2d4297c2011-10-06 13:14:12 +000074 SkRegion::Op op = SkRegion::kIntersect_Op) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 virtual bool clipPath(const SkPath& path,
reed@google.com2d4297c2011-10-06 13:14:12 +000076 SkRegion::Op op = SkRegion::kIntersect_Op) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 virtual bool clipRegion(const SkRegion& deviceRgn,
reed@google.com2d4297c2011-10-06 13:14:12 +000078 SkRegion::Op op = SkRegion::kIntersect_Op) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
reed@google.com2d4297c2011-10-06 13:14:12 +000080 virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
reed@google.com2d4297c2011-10-06 13:14:12 +000082 const SkPaint& paint) SK_OVERRIDE;
83 virtual void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
84 virtual void drawPath(const SkPath& path, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
reed@google.com2d4297c2011-10-06 13:14:12 +000086 const SkPaint* paint = NULL) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src,
reed@google.com2d4297c2011-10-06 13:14:12 +000088 const SkRect& dst, const SkPaint* paint = NULL) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
reed@google.com2d4297c2011-10-06 13:14:12 +000090 const SkPaint* paint = NULL) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
reed@google.com2d4297c2011-10-06 13:14:12 +000092 const SkPaint* paint = NULL) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 virtual void drawText(const void* text, size_t byteLength, SkScalar x,
reed@google.com2d4297c2011-10-06 13:14:12 +000094 SkScalar y, const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 virtual void drawPosText(const void* text, size_t byteLength,
reed@google.com2d4297c2011-10-06 13:14:12 +000096 const SkPoint pos[], const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 virtual void drawPosTextH(const void* text, size_t byteLength,
98 const SkScalar xpos[], SkScalar constY,
reed@google.com2d4297c2011-10-06 13:14:12 +000099 const SkPaint& paint) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 virtual void drawTextOnPath(const void* text, size_t byteLength,
101 const SkPath& path, const SkMatrix* matrix,
reed@google.com2d4297c2011-10-06 13:14:12 +0000102 const SkPaint& paint) SK_OVERRIDE;
103 virtual void drawPicture(SkPicture&) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 virtual void drawVertices(VertexMode vmode, int vertexCount,
105 const SkPoint vertices[], const SkPoint texs[],
106 const SkColor colors[], SkXfermode* xmode,
107 const uint16_t indices[], int indexCount,
reed@google.com2d4297c2011-10-06 13:14:12 +0000108 const SkPaint& paint) SK_OVERRIDE;
109 virtual void drawData(const void*, size_t) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
111private:
112 Dumper* fDumper;
reed@android.com9b46e772009-06-05 12:24:41 +0000113 int fNestLevel; // for nesting recursive elements like pictures
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114
115 void dump(Verb, const SkPaint*, const char format[], ...);
116
117 typedef SkCanvas INHERITED;
118};
119
120/** Formats the draw commands, and send them to a function-pointer provided
121 by the caller.
122 */
123class SkFormatDumper : public SkDumpCanvas::Dumper {
124public:
125 SkFormatDumper(void (*)(const char text[], void* refcon), void* refcon);
126
127 // override from baseclass that does the formatting, and in turn calls
128 // the function pointer that was passed to the constructor
129 virtual void dump(SkDumpCanvas*, SkDumpCanvas::Verb, const char str[],
reed@google.com2d4297c2011-10-06 13:14:12 +0000130 const SkPaint*) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131
132private:
133 void (*fProc)(const char*, void*);
134 void* fRefcon;
135
136 typedef SkDumpCanvas::Dumper INHERITED;
137};
138
139/** Subclass of Dumper that dumps the drawing command to SkDebugf
140 */
141class SkDebugfDumper : public SkFormatDumper {
142public:
143 SkDebugfDumper();
144
145private:
146 typedef SkFormatDumper INHERITED;
147};
148
149#endif