blob: f04316e660010f6a8c46c7b0e9f17cd333249672 [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 */
robertphillips@google.com76f9e932013-01-15 20:17:47 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkDumpCanvas.h"
robertphillips@google.com76f9e932013-01-15 20:17:47 +000010
11#ifdef SK_DEVELOPER
reed@google.com82065d62011-02-07 15:30:46 +000012#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkPixelRef.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000014#include "SkRRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkString.h"
16#include <stdarg.h>
17
18// needed just to know that these are all subclassed from SkFlattenable
19#include "SkShader.h"
20#include "SkPathEffect.h"
21#include "SkXfermode.h"
22#include "SkColorFilter.h"
23#include "SkPathEffect.h"
24#include "SkMaskFilter.h"
25
reed@google.com563a3b42012-06-26 19:24:50 +000026SK_DEFINE_INST_COUNT(SkDumpCanvas::Dumper)
27
reed@android.com8a1c16f2008-12-17 15:59:43 +000028static void toString(const SkRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000029 str->appendf("[%g,%g %g:%g]",
30 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
31 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +000032}
33
34static void toString(const SkIRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000035 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
reed@android.com0becfc5b2009-01-13 13:26:44 +000036}
37
reed@google.com4ed0fb72012-12-12 20:48:18 +000038static void toString(const SkRRect& rrect, SkString* str) {
39 SkRect r = rrect.getBounds();
40 str->appendf("[%g,%g %g:%g]",
41 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
42 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
43 if (rrect.isOval()) {
44 str->append("()");
45 } else if (rrect.isSimple()) {
46 const SkVector& rad = rrect.getSimpleRadii();
47 str->appendf("(%g,%g)", rad.x(), rad.y());
48 } else if (rrect.isComplex()) {
49 SkVector radii[4] = {
50 rrect.radii(SkRRect::kUpperLeft_Corner),
51 rrect.radii(SkRRect::kUpperRight_Corner),
52 rrect.radii(SkRRect::kLowerRight_Corner),
53 rrect.radii(SkRRect::kLowerLeft_Corner),
54 };
55 str->appendf("(%g,%g %g,%g %g,%g %g,%g)",
56 radii[0].x(), radii[0].y(),
57 radii[1].x(), radii[1].y(),
58 radii[2].x(), radii[2].y(),
59 radii[3].x(), radii[3].y());
60 }
61}
62
reed@android.com0becfc5b2009-01-13 13:26:44 +000063static void dumpVerbs(const SkPath& path, SkString* str) {
64 SkPath::Iter iter(path, false);
65 SkPoint pts[4];
66 for (;;) {
reed@google.com4a3b7142012-05-16 17:16:46 +000067 switch (iter.next(pts, false)) {
reed@android.com0becfc5b2009-01-13 13:26:44 +000068 case SkPath::kMove_Verb:
69 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
70 break;
71 case SkPath::kLine_Verb:
72 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
73 break;
74 case SkPath::kQuad_Verb:
75 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY,
76 pts[2].fX, pts[2].fY);
77 break;
78 case SkPath::kCubic_Verb:
79 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY,
80 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
81 break;
82 case SkPath::kClose_Verb:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000083 str->append("X");
reed@android.com0becfc5b2009-01-13 13:26:44 +000084 break;
85 case SkPath::kDone_Verb:
86 return;
reed@google.com277c3f82013-05-31 15:17:50 +000087 case SkPath::kConic_Verb:
88 SkASSERT(0);
89 break;
reed@android.com0becfc5b2009-01-13 13:26:44 +000090 }
91 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000092}
93
94static void toString(const SkPath& path, SkString* str) {
95 if (path.isEmpty()) {
reed@google.com4258c2c2012-08-31 15:41:10 +000096 str->append("path:empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 } else {
reed@android.comd252db02009-04-01 18:31:44 +000098 toString(path.getBounds(), str);
reed@android.com0becfc5b2009-01-13 13:26:44 +000099#if 1
100 SkString s;
101 dumpVerbs(path, &s);
102 str->append(s.c_str());
103#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 str->append("]");
105 str->prepend("path:[");
106 }
107}
108
109static const char* toString(SkRegion::Op op) {
110 static const char* gOpNames[] = {
111 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
112 };
113 return gOpNames[op];
114}
115
116static void toString(const SkRegion& rgn, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000117 str->append("Region:[");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 toString(rgn.getBounds(), str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 str->append("]");
120 if (rgn.isComplex()) {
121 str->append(".complex");
122 }
123}
124
125static const char* toString(SkCanvas::VertexMode vm) {
126 static const char* gVMNames[] = {
127 "TRIANGLES", "STRIP", "FAN"
128 };
129 return gVMNames[vm];
130}
131
132static const char* toString(SkCanvas::PointMode pm) {
133 static const char* gPMNames[] = {
134 "POINTS", "LINES", "POLYGON"
135 };
136 return gPMNames[pm];
137}
138
tomhudson@google.com8afae612012-08-14 15:03:35 +0000139static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkString* str) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000141 // FIXME: this code appears to be untested - and probably unused - and probably wrong
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 switch (enc) {
143 case SkPaint::kUTF8_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000144 str->appendf("\"%.*s\"%s", SkMax32(byteLen, 32), (const char*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000145 byteLen > 32 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 break;
147 case SkPaint::kUTF16_TextEncoding:
caryclark@google.com867cbd82012-09-20 15:45:41 +0000148 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000149 byteLen > 64 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000151 case SkPaint::kUTF32_TextEncoding:
caryclark@google.com867cbd82012-09-20 15:45:41 +0000152 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000153 byteLen > 128 ? "..." : "");
robertphillips@google.com69705572012-03-21 19:46:50 +0000154 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 case SkPaint::kGlyphID_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000156 str->append("<glyphs>");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000157 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000158
159 default:
160 SkASSERT(false);
161 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
163}
164
165///////////////////////////////////////////////////////////////////////////////
166
reed@google.com6ae24e02012-09-26 13:44:13 +0000167static SkBitmap make_wideopen_bm() {
168 static const int WIDE_OPEN = 16384;
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000169
reed@google.com6ae24e02012-09-26 13:44:13 +0000170 SkBitmap bm;
171 bm.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN);
172 return bm;
173}
174
175SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(make_wideopen_bm()) {
176 fNestLevel = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000177 SkSafeRef(dumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 fDumper = dumper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
181SkDumpCanvas::~SkDumpCanvas() {
reed@google.com82065d62011-02-07 15:30:46 +0000182 SkSafeUnref(fDumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183}
184
185void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
186 const char format[], ...) {
187 static const size_t BUFFER_SIZE = 1024;
188
189 char buffer[BUFFER_SIZE];
190 va_list args;
191 va_start(args, format);
192 vsnprintf(buffer, BUFFER_SIZE, format, args);
193 va_end(args);
reed@google.com82065d62011-02-07 15:30:46 +0000194
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 if (fDumper) {
196 fDumper->dump(this, verb, buffer, paint);
197 }
198}
199
200///////////////////////////////////////////////////////////////////////////////
201
202int SkDumpCanvas::save(SaveFlags flags) {
203 this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
204 return this->INHERITED::save(flags);
205}
206
207int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
208 SaveFlags flags) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000209 SkString str;
210 str.printf("saveLayer(0x%X)", flags);
211 if (bounds) {
212 str.append(" bounds");
213 toString(*bounds, &str);
214 }
215 if (paint) {
216 if (paint->getAlpha() != 0xFF) {
217 str.appendf(" alpha:0x%02X", paint->getAlpha());
218 }
219 if (paint->getXfermode()) {
220 str.appendf(" xfermode:%p", paint->getXfermode());
221 }
222 }
223 this->dump(kSave_Verb, paint, str.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 return this->INHERITED::saveLayer(bounds, paint, flags);
225}
226
227void SkDumpCanvas::restore() {
228 this->INHERITED::restore();
229 this->dump(kRestore_Verb, NULL, "restore");
230}
231
232bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) {
233 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
234 SkScalarToFloat(dx), SkScalarToFloat(dy));
235 return this->INHERITED::translate(dx, dy);
236}
237
238bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) {
239 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
240 SkScalarToFloat(sx), SkScalarToFloat(sy));
241 return this->INHERITED::scale(sx, sy);
242}
243
244bool SkDumpCanvas::rotate(SkScalar degrees) {
245 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees));
246 return this->INHERITED::rotate(degrees);
247}
248
249bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) {
250 this->dump(kMatrix_Verb, NULL, "skew(%g %g)",
251 SkScalarToFloat(sx), SkScalarToFloat(sy));
252 return this->INHERITED::skew(sx, sy);
253}
254
255bool SkDumpCanvas::concat(const SkMatrix& matrix) {
256 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000257 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
259 return this->INHERITED::concat(matrix);
260}
261
262void SkDumpCanvas::setMatrix(const SkMatrix& matrix) {
263 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000264 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
266 this->INHERITED::setMatrix(matrix);
267}
268
269///////////////////////////////////////////////////////////////////////////////
270
reed@google.com071eef92011-10-12 11:52:53 +0000271static const char* bool_to_aastring(bool doAA) {
272 return doAA ? "AA" : "BW";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273}
274
reed@google.com071eef92011-10-12 11:52:53 +0000275bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
276 SkString str;
277 toString(rect, &str);
278 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
279 bool_to_aastring(doAA));
280 return this->INHERITED::clipRect(rect, op, doAA);
281}
282
reed@google.com4ed0fb72012-12-12 20:48:18 +0000283bool SkDumpCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
284 SkString str;
285 toString(rrect, &str);
286 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
287 bool_to_aastring(doAA));
288 return this->INHERITED::clipRRect(rrect, op, doAA);
289}
290
reed@google.com071eef92011-10-12 11:52:53 +0000291bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 SkString str;
293 toString(path, &str);
reed@google.com071eef92011-10-12 11:52:53 +0000294 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
295 bool_to_aastring(doAA));
296 return this->INHERITED::clipPath(path, op, doAA);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297}
298
299bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
300 SkString str;
301 toString(deviceRgn, &str);
302 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
303 toString(op));
304 return this->INHERITED::clipRegion(deviceRgn, op);
305}
306
307///////////////////////////////////////////////////////////////////////////////
308
309void SkDumpCanvas::drawPaint(const SkPaint& paint) {
310 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
311}
312
313void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
314 const SkPoint pts[], const SkPaint& paint) {
315 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
316 count);
317}
318
reed@google.com4ed0fb72012-12-12 20:48:18 +0000319void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
320 SkString str;
321 toString(rect, &str);
322 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
323}
324
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
326 SkString str;
327 toString(rect, &str);
328 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
329}
330
reed@google.com4ed0fb72012-12-12 20:48:18 +0000331void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
332 SkString str;
333 toString(rrect, &str);
334 this->dump(kDrawRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
335}
336
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
338 SkString str;
339 toString(path, &str);
340 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
341}
342
343void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
344 const SkPaint* paint) {
345 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000346 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000347 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 SkScalarToFloat(x), SkScalarToFloat(y));
349}
350
reed@google.com71121732012-09-18 15:14:33 +0000351void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
352 const SkRect& dst, const SkPaint* paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 SkString bs, rs;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000354 bitmap.toString(&bs);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 toString(dst, &rs);
356 // show the src-rect only if its not everything
357 if (src && (src->fLeft > 0 || src->fTop > 0 ||
reed@google.com71121732012-09-18 15:14:33 +0000358 src->fRight < SkIntToScalar(bitmap.width()) ||
359 src->fBottom < SkIntToScalar(bitmap.height()))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 SkString ss;
361 toString(*src, &ss);
362 rs.prependf("%s ", ss.c_str());
363 }
364
reed@google.com71121732012-09-18 15:14:33 +0000365 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 bs.c_str(), rs.c_str());
367}
368
369void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
370 const SkPaint* paint) {
371 SkString bs, ms;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000372 bitmap.toString(&bs);
373 m.toString(&ms);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
375 bs.c_str(), ms.c_str());
376}
377
378void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
379 const SkPaint* paint) {
380 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000381 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000382 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 x, y);
384}
385
386void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
387 SkScalar y, const SkPaint& paint) {
388 SkString str;
389 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000390 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
392}
393
394void SkDumpCanvas::drawPosText(const void* text, size_t byteLength,
395 const SkPoint pos[], const SkPaint& paint) {
396 SkString str;
397 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000398 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
400 SkScalarToFloat(pos[0].fY));
401}
402
403void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength,
404 const SkScalar xpos[], SkScalar constY,
405 const SkPaint& paint) {
406 SkString str;
407 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000408 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
410 SkScalarToFloat(constY));
411}
412
413void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength,
414 const SkPath& path, const SkMatrix* matrix,
415 const SkPaint& paint) {
416 SkString str;
417 toString(text, byteLength, paint.getTextEncoding(), &str);
418 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
419 str.c_str(), byteLength);
420}
421
422void SkDumpCanvas::drawPicture(SkPicture& picture) {
reed@android.com9b46e772009-06-05 12:24:41 +0000423 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture,
424 picture.width(), picture.height());
425 fNestLevel += 1;
426 this->INHERITED::drawPicture(picture);
427 fNestLevel -= 1;
428 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
429 picture.width(), picture.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430}
431
432void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
433 const SkPoint vertices[], const SkPoint texs[],
434 const SkColor colors[], SkXfermode* xmode,
435 const uint16_t indices[], int indexCount,
436 const SkPaint& paint) {
reed@android.com0becfc5b2009-01-13 13:26:44 +0000437 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
439 SkScalarToFloat(vertices[0].fY));
440}
441
reed@android.comcb608442009-12-04 21:32:27 +0000442void SkDumpCanvas::drawData(const void* data, size_t length) {
443// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
444 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
445 SkMin32(length, 64), data);
446}
447
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000448void SkDumpCanvas::beginCommentGroup(const char* description) {
449 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
450}
451
452void SkDumpCanvas::addComment(const char* kywd, const char* value) {
453 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
454}
455
456void SkDumpCanvas::endCommentGroup() {
457 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
458}
459
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460///////////////////////////////////////////////////////////////////////////////
461///////////////////////////////////////////////////////////////////////////////
462
463SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
464 fProc = proc;
465 fRefcon = refcon;
466}
467
468static void appendPtr(SkString* str, const void* ptr, const char name[]) {
469 if (ptr) {
470 str->appendf(" %s:%p", name, ptr);
471 }
472}
473
474static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
475 const char name[]) {
476 if (ptr) {
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000477 str->appendf(" %s:%p", name, ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 }
479}
480
481void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
482 const char str[], const SkPaint* p) {
483 SkString msg, tab;
reed@android.com9b46e772009-06-05 12:24:41 +0000484 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485 SkASSERT(level >= 0);
486 for (int i = 0; i < level; i++) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000487#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488 tab.append("\t");
reed@google.com4258c2c2012-08-31 15:41:10 +0000489#else
490 tab.append(" "); // tabs are often too wide to be useful
491#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 }
493 msg.printf("%s%s", tab.c_str(), str);
reed@google.com82065d62011-02-07 15:30:46 +0000494
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 if (p) {
496 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
497 appendFlattenable(&msg, p->getShader(), "shader");
498 appendFlattenable(&msg, p->getXfermode(), "xfermode");
499 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
500 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
501 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
502 appendFlattenable(&msg, p->getColorFilter(), "filter");
reed@google.com82065d62011-02-07 15:30:46 +0000503
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 if (SkDumpCanvas::kDrawText_Verb == verb) {
505 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
506 appendPtr(&msg, p->getTypeface(), "typeface");
507 }
reed@google.comb8b830e2013-06-25 20:42:37 +0000508
509 if (p->getStyle() != SkPaint::kFill_Style) {
510 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
511 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512 }
reed@google.com82065d62011-02-07 15:30:46 +0000513
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 fProc(msg.c_str(), fRefcon);
515}
516
517///////////////////////////////////////////////////////////////////////////////
518
519static void dumpToDebugf(const char text[], void*) {
520 SkDebugf("%s\n", text);
521}
522
523SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
524
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000525#endif