blob: 3bf90da9f5a59a6d0483d014856401e8c38119a5 [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>
bungeman@google.comfab44db2013-10-11 18:50:45 +000017#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
19// needed just to know that these are all subclassed from SkFlattenable
20#include "SkShader.h"
21#include "SkPathEffect.h"
22#include "SkXfermode.h"
23#include "SkColorFilter.h"
24#include "SkPathEffect.h"
25#include "SkMaskFilter.h"
26
27static void toString(const SkRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000028 str->appendf("[%g,%g %g:%g]",
29 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
30 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +000031}
32
33static void toString(const SkIRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000034 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
reed@android.com0becfc5b2009-01-13 13:26:44 +000035}
36
reed@google.com4ed0fb72012-12-12 20:48:18 +000037static void toString(const SkRRect& rrect, SkString* str) {
38 SkRect r = rrect.getBounds();
39 str->appendf("[%g,%g %g:%g]",
40 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
41 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
42 if (rrect.isOval()) {
43 str->append("()");
44 } else if (rrect.isSimple()) {
45 const SkVector& rad = rrect.getSimpleRadii();
46 str->appendf("(%g,%g)", rad.x(), rad.y());
47 } else if (rrect.isComplex()) {
48 SkVector radii[4] = {
49 rrect.radii(SkRRect::kUpperLeft_Corner),
50 rrect.radii(SkRRect::kUpperRight_Corner),
51 rrect.radii(SkRRect::kLowerRight_Corner),
52 rrect.radii(SkRRect::kLowerLeft_Corner),
53 };
54 str->appendf("(%g,%g %g,%g %g,%g %g,%g)",
55 radii[0].x(), radii[0].y(),
56 radii[1].x(), radii[1].y(),
57 radii[2].x(), radii[2].y(),
58 radii[3].x(), radii[3].y());
59 }
60}
61
reed@android.com0becfc5b2009-01-13 13:26:44 +000062static void dumpVerbs(const SkPath& path, SkString* str) {
63 SkPath::Iter iter(path, false);
64 SkPoint pts[4];
65 for (;;) {
reed@google.com4a3b7142012-05-16 17:16:46 +000066 switch (iter.next(pts, false)) {
reed@android.com0becfc5b2009-01-13 13:26:44 +000067 case SkPath::kMove_Verb:
68 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
69 break;
70 case SkPath::kLine_Verb:
71 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
72 break;
73 case SkPath::kQuad_Verb:
74 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY,
75 pts[2].fX, pts[2].fY);
76 break;
77 case SkPath::kCubic_Verb:
78 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY,
79 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
80 break;
81 case SkPath::kClose_Verb:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000082 str->append("X");
reed@android.com0becfc5b2009-01-13 13:26:44 +000083 break;
84 case SkPath::kDone_Verb:
85 return;
reed@google.com277c3f82013-05-31 15:17:50 +000086 case SkPath::kConic_Verb:
87 SkASSERT(0);
88 break;
reed@android.com0becfc5b2009-01-13 13:26:44 +000089 }
90 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000091}
92
93static void toString(const SkPath& path, SkString* str) {
94 if (path.isEmpty()) {
reed@google.com4258c2c2012-08-31 15:41:10 +000095 str->append("path:empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 } else {
reed@android.comd252db02009-04-01 18:31:44 +000097 toString(path.getBounds(), str);
reed@android.com0becfc5b2009-01-13 13:26:44 +000098#if 1
99 SkString s;
100 dumpVerbs(path, &s);
101 str->append(s.c_str());
102#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 str->append("]");
104 str->prepend("path:[");
105 }
106}
107
108static const char* toString(SkRegion::Op op) {
109 static const char* gOpNames[] = {
110 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
111 };
112 return gOpNames[op];
113}
114
115static void toString(const SkRegion& rgn, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000116 str->append("Region:[");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 toString(rgn.getBounds(), str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 str->append("]");
119 if (rgn.isComplex()) {
120 str->append(".complex");
121 }
122}
123
124static const char* toString(SkCanvas::VertexMode vm) {
125 static const char* gVMNames[] = {
126 "TRIANGLES", "STRIP", "FAN"
127 };
128 return gVMNames[vm];
129}
130
131static const char* toString(SkCanvas::PointMode pm) {
132 static const char* gPMNames[] = {
133 "POINTS", "LINES", "POLYGON"
134 };
135 return gPMNames[pm];
136}
137
tomhudson@google.com8afae612012-08-14 15:03:35 +0000138static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 SkString* str) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000140 // FIXME: this code appears to be untested - and probably unused - and probably wrong
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 switch (enc) {
142 case SkPaint::kUTF8_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000143 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000144 byteLen > 32 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 break;
146 case SkPaint::kUTF16_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000147 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000148 byteLen > 64 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000150 case SkPaint::kUTF32_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000151 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000152 byteLen > 128 ? "..." : "");
robertphillips@google.com69705572012-03-21 19:46:50 +0000153 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 case SkPaint::kGlyphID_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000155 str->append("<glyphs>");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000157
158 default:
159 SkASSERT(false);
160 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 }
162}
163
164///////////////////////////////////////////////////////////////////////////////
165
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000166#define WIDE_OPEN 16384
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000167
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000168SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
reed@google.com6ae24e02012-09-26 13:44:13 +0000169 fNestLevel = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000170 SkSafeRef(dumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 fDumper = dumper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172}
173
174SkDumpCanvas::~SkDumpCanvas() {
reed@google.com82065d62011-02-07 15:30:46 +0000175 SkSafeUnref(fDumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176}
177
178void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
179 const char format[], ...) {
180 static const size_t BUFFER_SIZE = 1024;
181
182 char buffer[BUFFER_SIZE];
183 va_list args;
184 va_start(args, format);
185 vsnprintf(buffer, BUFFER_SIZE, format, args);
186 va_end(args);
reed@google.com82065d62011-02-07 15:30:46 +0000187
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 if (fDumper) {
189 fDumper->dump(this, verb, buffer, paint);
190 }
191}
192
193///////////////////////////////////////////////////////////////////////////////
194
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000195void SkDumpCanvas::willSave(SaveFlags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000197 this->INHERITED::willSave(flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000198}
199
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000200SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
201 SaveFlags flags) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000202 SkString str;
203 str.printf("saveLayer(0x%X)", flags);
204 if (bounds) {
205 str.append(" bounds");
206 toString(*bounds, &str);
207 }
208 if (paint) {
209 if (paint->getAlpha() != 0xFF) {
210 str.appendf(" alpha:0x%02X", paint->getAlpha());
211 }
212 if (paint->getXfermode()) {
213 str.appendf(" xfermode:%p", paint->getXfermode());
214 }
215 }
216 this->dump(kSave_Verb, paint, str.c_str());
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000217 return this->INHERITED::willSaveLayer(bounds, paint, flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218}
219
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000220void SkDumpCanvas::willRestore() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 this->dump(kRestore_Verb, NULL, "restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000222 this->INHERITED::willRestore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223}
224
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000225void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 SkString str;
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000227
228 switch (matrix.getType()) {
229 case SkMatrix::kTranslate_Mask:
230 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
231 SkScalarToFloat(matrix.getTranslateX()),
232 SkScalarToFloat(matrix.getTranslateY()));
233 break;
234 case SkMatrix::kScale_Mask:
235 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
236 SkScalarToFloat(matrix.getScaleX()),
237 SkScalarToFloat(matrix.getScaleY()));
238 break;
239 default:
240 matrix.toString(&str);
241 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
242 break;
243 }
244
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000245 this->INHERITED::didConcat(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246}
247
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000248void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000250 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000252 this->INHERITED::didSetMatrix(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253}
254
255///////////////////////////////////////////////////////////////////////////////
256
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000257const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
258 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259}
260
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000261void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com071eef92011-10-12 11:52:53 +0000262 SkString str;
263 toString(rect, &str);
264 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000265 EdgeStyleToAAString(edgeStyle));
266 this->INHERITED::onClipRect(rect, op, edgeStyle);
reed@google.com071eef92011-10-12 11:52:53 +0000267}
268
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000269void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000270 SkString str;
271 toString(rrect, &str);
272 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000273 EdgeStyleToAAString(edgeStyle));
274 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000275}
276
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000277void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000278 SkString str;
279 toString(path, &str);
reed@google.com071eef92011-10-12 11:52:53 +0000280 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000281 EdgeStyleToAAString(edgeStyle));
282 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283}
284
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000285void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 SkString str;
287 toString(deviceRgn, &str);
288 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
289 toString(op));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000290 this->INHERITED::onClipRegion(deviceRgn, op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291}
292
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000293void SkDumpCanvas::onPushCull(const SkRect& cullRect) {
294 SkString str;
295 toString(cullRect, &str);
296 this->dump(kCull_Verb, NULL, "pushCull(%s)", str.c_str());
297}
298
299void SkDumpCanvas::onPopCull() {
300 this->dump(kCull_Verb, NULL, "popCull()");
301}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302///////////////////////////////////////////////////////////////////////////////
303
304void SkDumpCanvas::drawPaint(const SkPaint& paint) {
305 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
306}
307
308void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
309 const SkPoint pts[], const SkPaint& paint) {
310 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
311 count);
312}
313
reed@google.com4ed0fb72012-12-12 20:48:18 +0000314void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
315 SkString str;
316 toString(rect, &str);
317 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
318}
319
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000320void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 SkString str;
322 toString(rect, &str);
323 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
324}
325
reed@google.com4ed0fb72012-12-12 20:48:18 +0000326void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
327 SkString str;
328 toString(rrect, &str);
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000329 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
330}
331
332void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
333 const SkPaint& paint) {
334 SkString str0, str1;
335 toString(outer, &str0);
336 toString(inner, &str0);
337 this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
338 str0.c_str(), str1.c_str());
reed@google.com4ed0fb72012-12-12 20:48:18 +0000339}
340
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000341void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 SkString str;
343 toString(path, &str);
344 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
345}
346
347void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
348 const SkPaint* paint) {
349 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000350 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000351 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 SkScalarToFloat(x), SkScalarToFloat(y));
353}
354
reed@google.com71121732012-09-18 15:14:33 +0000355void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000356 const SkRect& dst, const SkPaint* paint,
357 DrawBitmapRectFlags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358 SkString bs, rs;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000359 bitmap.toString(&bs);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 toString(dst, &rs);
361 // show the src-rect only if its not everything
362 if (src && (src->fLeft > 0 || src->fTop > 0 ||
reed@google.com71121732012-09-18 15:14:33 +0000363 src->fRight < SkIntToScalar(bitmap.width()) ||
364 src->fBottom < SkIntToScalar(bitmap.height()))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 SkString ss;
366 toString(*src, &ss);
367 rs.prependf("%s ", ss.c_str());
368 }
369
reed@google.com71121732012-09-18 15:14:33 +0000370 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 bs.c_str(), rs.c_str());
372}
373
374void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
375 const SkPaint* paint) {
376 SkString bs, ms;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000377 bitmap.toString(&bs);
378 m.toString(&ms);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
380 bs.c_str(), ms.c_str());
381}
382
383void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
384 const SkPaint* paint) {
385 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000386 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000387 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 x, y);
389}
390
391void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
392 SkScalar y, const SkPaint& paint) {
393 SkString str;
394 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000395 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
397}
398
399void SkDumpCanvas::drawPosText(const void* text, size_t byteLength,
400 const SkPoint pos[], const SkPaint& paint) {
401 SkString str;
402 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000403 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
405 SkScalarToFloat(pos[0].fY));
406}
407
408void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength,
409 const SkScalar xpos[], SkScalar constY,
410 const SkPaint& paint) {
411 SkString str;
412 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000413 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
415 SkScalarToFloat(constY));
416}
417
418void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength,
419 const SkPath& path, const SkMatrix* matrix,
420 const SkPaint& paint) {
421 SkString str;
422 toString(text, byteLength, paint.getTextEncoding(), &str);
423 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
424 str.c_str(), byteLength);
425}
426
427void SkDumpCanvas::drawPicture(SkPicture& picture) {
reed@android.com9b46e772009-06-05 12:24:41 +0000428 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture,
429 picture.width(), picture.height());
430 fNestLevel += 1;
431 this->INHERITED::drawPicture(picture);
432 fNestLevel -= 1;
433 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
434 picture.width(), picture.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435}
436
437void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
438 const SkPoint vertices[], const SkPoint texs[],
439 const SkColor colors[], SkXfermode* xmode,
440 const uint16_t indices[], int indexCount,
441 const SkPaint& paint) {
reed@android.com0becfc5b2009-01-13 13:26:44 +0000442 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
444 SkScalarToFloat(vertices[0].fY));
445}
446
reed@android.comcb608442009-12-04 21:32:27 +0000447void SkDumpCanvas::drawData(const void* data, size_t length) {
448// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
449 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
robertphillips@google.comadacc702013-10-14 21:53:24 +0000450 SkTMin<size_t>(length, 64), data);
reed@android.comcb608442009-12-04 21:32:27 +0000451}
452
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000453void SkDumpCanvas::beginCommentGroup(const char* description) {
454 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
455}
456
457void SkDumpCanvas::addComment(const char* kywd, const char* value) {
458 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
459}
460
461void SkDumpCanvas::endCommentGroup() {
462 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
463}
464
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465///////////////////////////////////////////////////////////////////////////////
466///////////////////////////////////////////////////////////////////////////////
467
468SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
469 fProc = proc;
470 fRefcon = refcon;
471}
472
473static void appendPtr(SkString* str, const void* ptr, const char name[]) {
474 if (ptr) {
475 str->appendf(" %s:%p", name, ptr);
476 }
477}
478
479static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
480 const char name[]) {
481 if (ptr) {
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000482 str->appendf(" %s:%p", name, ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 }
484}
485
486void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
487 const char str[], const SkPaint* p) {
488 SkString msg, tab;
reed@android.com9b46e772009-06-05 12:24:41 +0000489 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490 SkASSERT(level >= 0);
491 for (int i = 0; i < level; i++) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000492#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 tab.append("\t");
reed@google.com4258c2c2012-08-31 15:41:10 +0000494#else
495 tab.append(" "); // tabs are often too wide to be useful
496#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497 }
498 msg.printf("%s%s", tab.c_str(), str);
reed@google.com82065d62011-02-07 15:30:46 +0000499
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500 if (p) {
501 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
502 appendFlattenable(&msg, p->getShader(), "shader");
503 appendFlattenable(&msg, p->getXfermode(), "xfermode");
504 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
505 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
506 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
507 appendFlattenable(&msg, p->getColorFilter(), "filter");
reed@google.com82065d62011-02-07 15:30:46 +0000508
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 if (SkDumpCanvas::kDrawText_Verb == verb) {
510 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
511 appendPtr(&msg, p->getTypeface(), "typeface");
512 }
skia.committer@gmail.com73a4b4f2013-06-26 07:00:59 +0000513
reed@google.comb8b830e2013-06-25 20:42:37 +0000514 if (p->getStyle() != SkPaint::kFill_Style) {
515 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
516 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 }
reed@google.com82065d62011-02-07 15:30:46 +0000518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519 fProc(msg.c_str(), fRefcon);
520}
521
522///////////////////////////////////////////////////////////////////////////////
523
524static void dumpToDebugf(const char text[], void*) {
525 SkDebugf("%s\n", text);
526}
527
528SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
529
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000530#endif