blob: 661f0d8860fc40c8a37c3e1ff84e30e6bf4f5383 [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
dandovb3c9d1c2014-08-12 08:34:29 -070012#include "SkPatchUtils.h"
reed@google.com82065d62011-02-07 15:30:46 +000013#include "SkPicture.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPixelRef.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000015#include "SkRRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkString.h"
17#include <stdarg.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000018#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
20// needed just to know that these are all subclassed from SkFlattenable
21#include "SkShader.h"
22#include "SkPathEffect.h"
23#include "SkXfermode.h"
24#include "SkColorFilter.h"
25#include "SkPathEffect.h"
26#include "SkMaskFilter.h"
27
28static 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:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000144 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(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:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000148 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(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:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000152 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(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
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000167#define WIDE_OPEN 16384
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000168
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000169SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
reed@google.com6ae24e02012-09-26 13:44:13 +0000170 fNestLevel = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000171 SkSafeRef(dumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 fDumper = dumper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173}
174
175SkDumpCanvas::~SkDumpCanvas() {
reed@google.com82065d62011-02-07 15:30:46 +0000176 SkSafeUnref(fDumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177}
178
179void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
180 const char format[], ...) {
181 static const size_t BUFFER_SIZE = 1024;
182
183 char buffer[BUFFER_SIZE];
184 va_list args;
185 va_start(args, format);
186 vsnprintf(buffer, BUFFER_SIZE, format, args);
187 va_end(args);
reed@google.com82065d62011-02-07 15:30:46 +0000188
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 if (fDumper) {
190 fDumper->dump(this, verb, buffer, paint);
191 }
192}
193
194///////////////////////////////////////////////////////////////////////////////
195
Florin Malita5f6102d2014-06-30 10:13:28 -0400196void SkDumpCanvas::willSave() {
197 this->dump(kSave_Verb, NULL, "save()");
198 this->INHERITED::willSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199}
200
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000201SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
202 SaveFlags flags) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000203 SkString str;
204 str.printf("saveLayer(0x%X)", flags);
205 if (bounds) {
206 str.append(" bounds");
207 toString(*bounds, &str);
208 }
209 if (paint) {
210 if (paint->getAlpha() != 0xFF) {
211 str.appendf(" alpha:0x%02X", paint->getAlpha());
212 }
213 if (paint->getXfermode()) {
214 str.appendf(" xfermode:%p", paint->getXfermode());
215 }
216 }
217 this->dump(kSave_Verb, paint, str.c_str());
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000218 return this->INHERITED::willSaveLayer(bounds, paint, flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219}
220
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000221void SkDumpCanvas::willRestore() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 this->dump(kRestore_Verb, NULL, "restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000223 this->INHERITED::willRestore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224}
225
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000226void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 SkString str;
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000228
229 switch (matrix.getType()) {
230 case SkMatrix::kTranslate_Mask:
231 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
232 SkScalarToFloat(matrix.getTranslateX()),
233 SkScalarToFloat(matrix.getTranslateY()));
234 break;
235 case SkMatrix::kScale_Mask:
236 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
237 SkScalarToFloat(matrix.getScaleX()),
238 SkScalarToFloat(matrix.getScaleY()));
239 break;
240 default:
241 matrix.toString(&str);
242 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
243 break;
244 }
245
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000246 this->INHERITED::didConcat(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247}
248
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000249void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000251 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000253 this->INHERITED::didSetMatrix(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254}
255
256///////////////////////////////////////////////////////////////////////////////
257
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000258const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
259 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260}
261
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000262void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com071eef92011-10-12 11:52:53 +0000263 SkString str;
264 toString(rect, &str);
265 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000266 EdgeStyleToAAString(edgeStyle));
267 this->INHERITED::onClipRect(rect, op, edgeStyle);
reed@google.com071eef92011-10-12 11:52:53 +0000268}
269
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000270void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000271 SkString str;
272 toString(rrect, &str);
273 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000274 EdgeStyleToAAString(edgeStyle));
275 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000276}
277
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000278void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 SkString str;
280 toString(path, &str);
reed@google.com071eef92011-10-12 11:52:53 +0000281 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000282 EdgeStyleToAAString(edgeStyle));
283 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284}
285
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000286void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 SkString str;
288 toString(deviceRgn, &str);
289 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
290 toString(op));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000291 this->INHERITED::onClipRegion(deviceRgn, op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292}
293
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000294void SkDumpCanvas::onPushCull(const SkRect& cullRect) {
295 SkString str;
296 toString(cullRect, &str);
297 this->dump(kCull_Verb, NULL, "pushCull(%s)", str.c_str());
298}
299
300void SkDumpCanvas::onPopCull() {
301 this->dump(kCull_Verb, NULL, "popCull()");
302}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303///////////////////////////////////////////////////////////////////////////////
304
305void SkDumpCanvas::drawPaint(const SkPaint& paint) {
306 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
307}
308
309void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
310 const SkPoint pts[], const SkPaint& paint) {
311 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
312 count);
313}
314
reed@google.com4ed0fb72012-12-12 20:48:18 +0000315void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
316 SkString str;
317 toString(rect, &str);
318 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
319}
320
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000321void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 SkString str;
323 toString(rect, &str);
324 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
325}
326
reed@google.com4ed0fb72012-12-12 20:48:18 +0000327void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
328 SkString str;
329 toString(rrect, &str);
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000330 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
331}
332
333void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
334 const SkPaint& paint) {
335 SkString str0, str1;
336 toString(outer, &str0);
337 toString(inner, &str0);
338 this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
339 str0.c_str(), str1.c_str());
reed@google.com4ed0fb72012-12-12 20:48:18 +0000340}
341
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000342void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 SkString str;
344 toString(path, &str);
345 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
346}
347
348void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
349 const SkPaint* paint) {
350 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000351 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000352 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 SkScalarToFloat(x), SkScalarToFloat(y));
354}
355
reed@google.com71121732012-09-18 15:14:33 +0000356void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000357 const SkRect& dst, const SkPaint* paint,
358 DrawBitmapRectFlags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 SkString bs, rs;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000360 bitmap.toString(&bs);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 toString(dst, &rs);
362 // show the src-rect only if its not everything
363 if (src && (src->fLeft > 0 || src->fTop > 0 ||
reed@google.com71121732012-09-18 15:14:33 +0000364 src->fRight < SkIntToScalar(bitmap.width()) ||
365 src->fBottom < SkIntToScalar(bitmap.height()))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 SkString ss;
367 toString(*src, &ss);
368 rs.prependf("%s ", ss.c_str());
369 }
370
reed@google.com71121732012-09-18 15:14:33 +0000371 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 bs.c_str(), rs.c_str());
373}
374
375void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
376 const SkPaint* paint) {
377 SkString bs, ms;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000378 bitmap.toString(&bs);
379 m.toString(&ms);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
381 bs.c_str(), ms.c_str());
382}
383
384void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
385 const SkPaint* paint) {
386 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000387 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000388 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 x, y);
390}
391
reed@google.come0d9ce82014-04-23 04:00:17 +0000392void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
393 const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 SkString str;
395 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000396 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
398}
399
reed@google.come0d9ce82014-04-23 04:00:17 +0000400void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
401 const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402 SkString str;
403 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000404 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
406 SkScalarToFloat(pos[0].fY));
407}
408
reed@google.come0d9ce82014-04-23 04:00:17 +0000409void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
410 SkScalar constY, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 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
reed@google.come0d9ce82014-04-23 04:00:17 +0000418void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
419 const SkMatrix* matrix, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 SkString str;
421 toString(text, byteLength, paint.getTextEncoding(), &str);
422 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
423 str.c_str(), byteLength);
424}
425
reedd5fa1a42014-08-09 11:08:05 -0700426void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
427 const SkPaint* paint) {
robertphillips9b14f262014-06-04 05:40:44 -0700428 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", picture,
429 picture->width(), picture->height());
reed@android.com9b46e772009-06-05 12:24:41 +0000430 fNestLevel += 1;
reedd5fa1a42014-08-09 11:08:05 -0700431 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@android.com9b46e772009-06-05 12:24:41 +0000432 fNestLevel -= 1;
433 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
robertphillips9b14f262014-06-04 05:40:44 -0700434 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
dandovb3c9d1c2014-08-12 08:34:29 -0700447void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
448 const SkPoint texCoords[4], SkXfermode* xmode,
449 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -0700450 //dumps corner points and colors in clockwise order starting on upper-left corner
451 this->dump(kDrawPatch_Verb, &paint, "drawPatch(Vertices{[%f, %f], [%f, %f], [%f, %f], [%f, %f]}\
dandovb3c9d1c2014-08-12 08:34:29 -0700452 | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%f,%f], \
453 [%f,%f]})",
454 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fX,
455 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fY,
456 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fX,
457 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fY,
458 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fX,
459 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fY,
460 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fX,
461 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fY,
462 colors[0], colors[1], colors[2], colors[3],
463 texCoords[0].x(), texCoords[0].y(), texCoords[1].x(), texCoords[1].y(),
464 texCoords[2].x(), texCoords[2].y(), texCoords[3].x(), texCoords[3].y());
dandov963137b2014-08-07 07:49:53 -0700465}
466
reed@android.comcb608442009-12-04 21:32:27 +0000467void SkDumpCanvas::drawData(const void* data, size_t length) {
468// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
469 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
robertphillips@google.comadacc702013-10-14 21:53:24 +0000470 SkTMin<size_t>(length, 64), data);
reed@android.comcb608442009-12-04 21:32:27 +0000471}
472
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000473void SkDumpCanvas::beginCommentGroup(const char* description) {
474 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
475}
476
477void SkDumpCanvas::addComment(const char* kywd, const char* value) {
478 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
479}
480
481void SkDumpCanvas::endCommentGroup() {
482 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
483}
484
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485///////////////////////////////////////////////////////////////////////////////
486///////////////////////////////////////////////////////////////////////////////
487
488SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
489 fProc = proc;
490 fRefcon = refcon;
491}
492
493static void appendPtr(SkString* str, const void* ptr, const char name[]) {
494 if (ptr) {
495 str->appendf(" %s:%p", name, ptr);
496 }
497}
498
499static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
500 const char name[]) {
501 if (ptr) {
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000502 str->appendf(" %s:%p", name, ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 }
504}
505
506void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
507 const char str[], const SkPaint* p) {
508 SkString msg, tab;
reed@android.com9b46e772009-06-05 12:24:41 +0000509 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 SkASSERT(level >= 0);
511 for (int i = 0; i < level; i++) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000512#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 tab.append("\t");
reed@google.com4258c2c2012-08-31 15:41:10 +0000514#else
515 tab.append(" "); // tabs are often too wide to be useful
516#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517 }
518 msg.printf("%s%s", tab.c_str(), str);
reed@google.com82065d62011-02-07 15:30:46 +0000519
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 if (p) {
521 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
522 appendFlattenable(&msg, p->getShader(), "shader");
523 appendFlattenable(&msg, p->getXfermode(), "xfermode");
524 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
525 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
526 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
527 appendFlattenable(&msg, p->getColorFilter(), "filter");
reed@google.com82065d62011-02-07 15:30:46 +0000528
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 if (SkDumpCanvas::kDrawText_Verb == verb) {
530 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
531 appendPtr(&msg, p->getTypeface(), "typeface");
532 }
skia.committer@gmail.com73a4b4f2013-06-26 07:00:59 +0000533
reed@google.comb8b830e2013-06-25 20:42:37 +0000534 if (p->getStyle() != SkPaint::kFill_Style) {
535 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
536 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537 }
reed@google.com82065d62011-02-07 15:30:46 +0000538
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 fProc(msg.c_str(), fRefcon);
540}
541
542///////////////////////////////////////////////////////////////////////////////
543
544static void dumpToDebugf(const char text[], void*) {
545 SkDebugf("%s\n", text);
546}
547
548SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
549
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000550#endif