blob: 87ddd3153d0a80e484dc80eec649010d8ffc423e [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;
87 }
88 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089}
90
91static void toString(const SkPath& path, SkString* str) {
92 if (path.isEmpty()) {
reed@google.com4258c2c2012-08-31 15:41:10 +000093 str->append("path:empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +000094 } else {
reed@android.comd252db02009-04-01 18:31:44 +000095 toString(path.getBounds(), str);
reed@android.com0becfc5b2009-01-13 13:26:44 +000096#if 1
97 SkString s;
98 dumpVerbs(path, &s);
99 str->append(s.c_str());
100#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 str->append("]");
102 str->prepend("path:[");
103 }
104}
105
106static const char* toString(SkRegion::Op op) {
107 static const char* gOpNames[] = {
108 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
109 };
110 return gOpNames[op];
111}
112
113static void toString(const SkRegion& rgn, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000114 str->append("Region:[");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 toString(rgn.getBounds(), str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 str->append("]");
117 if (rgn.isComplex()) {
118 str->append(".complex");
119 }
120}
121
122static const char* toString(SkCanvas::VertexMode vm) {
123 static const char* gVMNames[] = {
124 "TRIANGLES", "STRIP", "FAN"
125 };
126 return gVMNames[vm];
127}
128
129static const char* toString(SkCanvas::PointMode pm) {
130 static const char* gPMNames[] = {
131 "POINTS", "LINES", "POLYGON"
132 };
133 return gPMNames[pm];
134}
135
tomhudson@google.com8afae612012-08-14 15:03:35 +0000136static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkString* str) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000138 // FIXME: this code appears to be untested - and probably unused - and probably wrong
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 switch (enc) {
140 case SkPaint::kUTF8_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000141 str->appendf("\"%.*s\"%s", SkMax32(byteLen, 32), (const char*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000142 byteLen > 32 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 break;
144 case SkPaint::kUTF16_TextEncoding:
caryclark@google.com867cbd82012-09-20 15:45:41 +0000145 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000146 byteLen > 64 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000148 case SkPaint::kUTF32_TextEncoding:
caryclark@google.com867cbd82012-09-20 15:45:41 +0000149 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000150 byteLen > 128 ? "..." : "");
robertphillips@google.com69705572012-03-21 19:46:50 +0000151 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 case SkPaint::kGlyphID_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000153 str->append("<glyphs>");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000155
156 default:
157 SkASSERT(false);
158 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 }
160}
161
162///////////////////////////////////////////////////////////////////////////////
163
reed@google.com6ae24e02012-09-26 13:44:13 +0000164static SkBitmap make_wideopen_bm() {
165 static const int WIDE_OPEN = 16384;
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000166
reed@google.com6ae24e02012-09-26 13:44:13 +0000167 SkBitmap bm;
168 bm.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN);
169 return bm;
170}
171
172SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(make_wideopen_bm()) {
173 fNestLevel = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000174 SkSafeRef(dumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 fDumper = dumper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176}
177
178SkDumpCanvas::~SkDumpCanvas() {
reed@google.com82065d62011-02-07 15:30:46 +0000179 SkSafeUnref(fDumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180}
181
182void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
183 const char format[], ...) {
184 static const size_t BUFFER_SIZE = 1024;
185
186 char buffer[BUFFER_SIZE];
187 va_list args;
188 va_start(args, format);
189 vsnprintf(buffer, BUFFER_SIZE, format, args);
190 va_end(args);
reed@google.com82065d62011-02-07 15:30:46 +0000191
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192 if (fDumper) {
193 fDumper->dump(this, verb, buffer, paint);
194 }
195}
196
197///////////////////////////////////////////////////////////////////////////////
198
199int SkDumpCanvas::save(SaveFlags flags) {
200 this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
201 return this->INHERITED::save(flags);
202}
203
204int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
205 SaveFlags flags) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000206 SkString str;
207 str.printf("saveLayer(0x%X)", flags);
208 if (bounds) {
209 str.append(" bounds");
210 toString(*bounds, &str);
211 }
212 if (paint) {
213 if (paint->getAlpha() != 0xFF) {
214 str.appendf(" alpha:0x%02X", paint->getAlpha());
215 }
216 if (paint->getXfermode()) {
217 str.appendf(" xfermode:%p", paint->getXfermode());
218 }
219 }
220 this->dump(kSave_Verb, paint, str.c_str());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 return this->INHERITED::saveLayer(bounds, paint, flags);
222}
223
224void SkDumpCanvas::restore() {
225 this->INHERITED::restore();
226 this->dump(kRestore_Verb, NULL, "restore");
227}
228
229bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) {
230 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
231 SkScalarToFloat(dx), SkScalarToFloat(dy));
232 return this->INHERITED::translate(dx, dy);
233}
234
235bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) {
236 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
237 SkScalarToFloat(sx), SkScalarToFloat(sy));
238 return this->INHERITED::scale(sx, sy);
239}
240
241bool SkDumpCanvas::rotate(SkScalar degrees) {
242 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees));
243 return this->INHERITED::rotate(degrees);
244}
245
246bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) {
247 this->dump(kMatrix_Verb, NULL, "skew(%g %g)",
248 SkScalarToFloat(sx), SkScalarToFloat(sy));
249 return this->INHERITED::skew(sx, sy);
250}
251
252bool SkDumpCanvas::concat(const SkMatrix& matrix) {
253 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000254 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
256 return this->INHERITED::concat(matrix);
257}
258
259void SkDumpCanvas::setMatrix(const SkMatrix& matrix) {
260 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000261 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
263 this->INHERITED::setMatrix(matrix);
264}
265
266///////////////////////////////////////////////////////////////////////////////
267
reed@google.com071eef92011-10-12 11:52:53 +0000268static const char* bool_to_aastring(bool doAA) {
269 return doAA ? "AA" : "BW";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270}
271
reed@google.com071eef92011-10-12 11:52:53 +0000272bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
273 SkString str;
274 toString(rect, &str);
275 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
276 bool_to_aastring(doAA));
277 return this->INHERITED::clipRect(rect, op, doAA);
278}
279
reed@google.com4ed0fb72012-12-12 20:48:18 +0000280bool SkDumpCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
281 SkString str;
282 toString(rrect, &str);
283 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
284 bool_to_aastring(doAA));
285 return this->INHERITED::clipRRect(rrect, op, doAA);
286}
287
reed@google.com071eef92011-10-12 11:52:53 +0000288bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 SkString str;
290 toString(path, &str);
reed@google.com071eef92011-10-12 11:52:53 +0000291 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
292 bool_to_aastring(doAA));
293 return this->INHERITED::clipPath(path, op, doAA);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294}
295
296bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
297 SkString str;
298 toString(deviceRgn, &str);
299 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
300 toString(op));
301 return this->INHERITED::clipRegion(deviceRgn, op);
302}
303
304///////////////////////////////////////////////////////////////////////////////
305
306void SkDumpCanvas::drawPaint(const SkPaint& paint) {
307 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
308}
309
310void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
311 const SkPoint pts[], const SkPaint& paint) {
312 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
313 count);
314}
315
reed@google.com4ed0fb72012-12-12 20:48:18 +0000316void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
317 SkString str;
318 toString(rect, &str);
319 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
320}
321
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
323 SkString str;
324 toString(rect, &str);
325 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
326}
327
reed@google.com4ed0fb72012-12-12 20:48:18 +0000328void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
329 SkString str;
330 toString(rrect, &str);
331 this->dump(kDrawRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
332}
333
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
335 SkString str;
336 toString(path, &str);
337 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
338}
339
340void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
341 const SkPaint* paint) {
342 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000343 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000344 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 SkScalarToFloat(x), SkScalarToFloat(y));
346}
347
reed@google.com71121732012-09-18 15:14:33 +0000348void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
349 const SkRect& dst, const SkPaint* paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 SkString bs, rs;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000351 bitmap.toString(&bs);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 toString(dst, &rs);
353 // show the src-rect only if its not everything
354 if (src && (src->fLeft > 0 || src->fTop > 0 ||
reed@google.com71121732012-09-18 15:14:33 +0000355 src->fRight < SkIntToScalar(bitmap.width()) ||
356 src->fBottom < SkIntToScalar(bitmap.height()))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 SkString ss;
358 toString(*src, &ss);
359 rs.prependf("%s ", ss.c_str());
360 }
361
reed@google.com71121732012-09-18 15:14:33 +0000362 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 bs.c_str(), rs.c_str());
364}
365
366void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
367 const SkPaint* paint) {
368 SkString bs, ms;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000369 bitmap.toString(&bs);
370 m.toString(&ms);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
372 bs.c_str(), ms.c_str());
373}
374
375void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
376 const SkPaint* paint) {
377 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000378 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000379 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 x, y);
381}
382
383void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
384 SkScalar y, const SkPaint& paint) {
385 SkString str;
386 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000387 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
389}
390
391void SkDumpCanvas::drawPosText(const void* text, size_t byteLength,
392 const SkPoint pos[], 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, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
397 SkScalarToFloat(pos[0].fY));
398}
399
400void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength,
401 const SkScalar xpos[], SkScalar constY,
402 const SkPaint& paint) {
403 SkString str;
404 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000405 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
407 SkScalarToFloat(constY));
408}
409
410void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength,
411 const SkPath& path, const SkMatrix* matrix,
412 const SkPaint& paint) {
413 SkString str;
414 toString(text, byteLength, paint.getTextEncoding(), &str);
415 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
416 str.c_str(), byteLength);
417}
418
419void SkDumpCanvas::drawPicture(SkPicture& picture) {
reed@android.com9b46e772009-06-05 12:24:41 +0000420 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture,
421 picture.width(), picture.height());
422 fNestLevel += 1;
423 this->INHERITED::drawPicture(picture);
424 fNestLevel -= 1;
425 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
426 picture.width(), picture.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427}
428
429void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
430 const SkPoint vertices[], const SkPoint texs[],
431 const SkColor colors[], SkXfermode* xmode,
432 const uint16_t indices[], int indexCount,
433 const SkPaint& paint) {
reed@android.com0becfc5b2009-01-13 13:26:44 +0000434 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
436 SkScalarToFloat(vertices[0].fY));
437}
438
reed@android.comcb608442009-12-04 21:32:27 +0000439void SkDumpCanvas::drawData(const void* data, size_t length) {
440// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
441 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
442 SkMin32(length, 64), data);
443}
444
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000445void SkDumpCanvas::beginCommentGroup(const char* description) {
446 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
447}
448
449void SkDumpCanvas::addComment(const char* kywd, const char* value) {
450 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
451}
452
453void SkDumpCanvas::endCommentGroup() {
454 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
455}
456
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457///////////////////////////////////////////////////////////////////////////////
458///////////////////////////////////////////////////////////////////////////////
459
460SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
461 fProc = proc;
462 fRefcon = refcon;
463}
464
465static void appendPtr(SkString* str, const void* ptr, const char name[]) {
466 if (ptr) {
467 str->appendf(" %s:%p", name, ptr);
468 }
469}
470
471static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
472 const char name[]) {
473 if (ptr) {
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000474 str->appendf(" %s:%p", name, ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475 }
476}
477
478void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
479 const char str[], const SkPaint* p) {
480 SkString msg, tab;
reed@android.com9b46e772009-06-05 12:24:41 +0000481 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482 SkASSERT(level >= 0);
483 for (int i = 0; i < level; i++) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000484#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485 tab.append("\t");
reed@google.com4258c2c2012-08-31 15:41:10 +0000486#else
487 tab.append(" "); // tabs are often too wide to be useful
488#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 }
490 msg.printf("%s%s", tab.c_str(), str);
reed@google.com82065d62011-02-07 15:30:46 +0000491
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 if (p) {
493 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
494 appendFlattenable(&msg, p->getShader(), "shader");
495 appendFlattenable(&msg, p->getXfermode(), "xfermode");
496 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
497 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
498 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
499 appendFlattenable(&msg, p->getColorFilter(), "filter");
reed@google.com82065d62011-02-07 15:30:46 +0000500
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501 if (SkDumpCanvas::kDrawText_Verb == verb) {
502 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
503 appendPtr(&msg, p->getTypeface(), "typeface");
504 }
505 }
reed@google.com82065d62011-02-07 15:30:46 +0000506
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507 fProc(msg.c_str(), fRefcon);
508}
509
510///////////////////////////////////////////////////////////////////////////////
511
512static void dumpToDebugf(const char text[], void*) {
513 SkDebugf("%s\n", text);
514}
515
516SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
517
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000518#endif