blob: e7b64a4d2fcabf52bfa17aa7d653a4207fae00a8 [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"
fmalitab7425172014-08-26 07:56:44 -070017#include "SkTextBlob.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include <stdarg.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000019#include <stdio.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
21// needed just to know that these are all subclassed from SkFlattenable
22#include "SkShader.h"
23#include "SkPathEffect.h"
24#include "SkXfermode.h"
25#include "SkColorFilter.h"
26#include "SkPathEffect.h"
27#include "SkMaskFilter.h"
28
29static void toString(const SkRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000030 str->appendf("[%g,%g %g:%g]",
31 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
32 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
reed@android.com8a1c16f2008-12-17 15:59:43 +000033}
34
35static void toString(const SkIRect& r, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +000036 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
reed@android.com0becfc5b2009-01-13 13:26:44 +000037}
38
reed@google.com4ed0fb72012-12-12 20:48:18 +000039static void toString(const SkRRect& rrect, SkString* str) {
40 SkRect r = rrect.getBounds();
41 str->appendf("[%g,%g %g:%g]",
42 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
43 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
44 if (rrect.isOval()) {
45 str->append("()");
46 } else if (rrect.isSimple()) {
47 const SkVector& rad = rrect.getSimpleRadii();
48 str->appendf("(%g,%g)", rad.x(), rad.y());
49 } else if (rrect.isComplex()) {
50 SkVector radii[4] = {
51 rrect.radii(SkRRect::kUpperLeft_Corner),
52 rrect.radii(SkRRect::kUpperRight_Corner),
53 rrect.radii(SkRRect::kLowerRight_Corner),
54 rrect.radii(SkRRect::kLowerLeft_Corner),
55 };
56 str->appendf("(%g,%g %g,%g %g,%g %g,%g)",
57 radii[0].x(), radii[0].y(),
58 radii[1].x(), radii[1].y(),
59 radii[2].x(), radii[2].y(),
60 radii[3].x(), radii[3].y());
61 }
62}
63
reed@android.com0becfc5b2009-01-13 13:26:44 +000064static void dumpVerbs(const SkPath& path, SkString* str) {
65 SkPath::Iter iter(path, false);
66 SkPoint pts[4];
67 for (;;) {
reed@google.com4a3b7142012-05-16 17:16:46 +000068 switch (iter.next(pts, false)) {
reed@android.com0becfc5b2009-01-13 13:26:44 +000069 case SkPath::kMove_Verb:
70 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
71 break;
72 case SkPath::kLine_Verb:
73 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
74 break;
75 case SkPath::kQuad_Verb:
76 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY,
77 pts[2].fX, pts[2].fY);
78 break;
79 case SkPath::kCubic_Verb:
80 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY,
81 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
82 break;
83 case SkPath::kClose_Verb:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +000084 str->append("X");
reed@android.com0becfc5b2009-01-13 13:26:44 +000085 break;
86 case SkPath::kDone_Verb:
87 return;
reed@google.com277c3f82013-05-31 15:17:50 +000088 case SkPath::kConic_Verb:
89 SkASSERT(0);
90 break;
reed@android.com0becfc5b2009-01-13 13:26:44 +000091 }
92 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093}
94
95static void toString(const SkPath& path, SkString* str) {
96 if (path.isEmpty()) {
reed@google.com4258c2c2012-08-31 15:41:10 +000097 str->append("path:empty");
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 } else {
reed@android.comd252db02009-04-01 18:31:44 +000099 toString(path.getBounds(), str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000100#if 1
101 SkString s;
102 dumpVerbs(path, &s);
103 str->append(s.c_str());
104#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 str->append("]");
106 str->prepend("path:[");
107 }
108}
109
110static const char* toString(SkRegion::Op op) {
111 static const char* gOpNames[] = {
112 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
113 };
114 return gOpNames[op];
115}
116
117static void toString(const SkRegion& rgn, SkString* str) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000118 str->append("Region:[");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 toString(rgn.getBounds(), str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 str->append("]");
121 if (rgn.isComplex()) {
122 str->append(".complex");
123 }
124}
125
126static const char* toString(SkCanvas::VertexMode vm) {
127 static const char* gVMNames[] = {
128 "TRIANGLES", "STRIP", "FAN"
129 };
130 return gVMNames[vm];
131}
132
133static const char* toString(SkCanvas::PointMode pm) {
134 static const char* gPMNames[] = {
135 "POINTS", "LINES", "POLYGON"
136 };
137 return gPMNames[pm];
138}
139
tomhudson@google.com8afae612012-08-14 15:03:35 +0000140static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 SkString* str) {
tomhudson@google.com8afae612012-08-14 15:03:35 +0000142 // FIXME: this code appears to be untested - and probably unused - and probably wrong
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 switch (enc) {
144 case SkPaint::kUTF8_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000145 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000146 byteLen > 32 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 break;
148 case SkPaint::kUTF16_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000149 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000150 byteLen > 64 ? "..." : "");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000152 case SkPaint::kUTF32_TextEncoding:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000153 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
tomhudson@google.com8afae612012-08-14 15:03:35 +0000154 byteLen > 128 ? "..." : "");
robertphillips@google.com69705572012-03-21 19:46:50 +0000155 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 case SkPaint::kGlyphID_TextEncoding:
reed@google.com4258c2c2012-08-31 15:41:10 +0000157 str->append("<glyphs>");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 break;
robertphillips@google.com69705572012-03-21 19:46:50 +0000159
160 default:
161 SkASSERT(false);
162 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 }
164}
165
166///////////////////////////////////////////////////////////////////////////////
167
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000168#define WIDE_OPEN 16384
skia.committer@gmail.com44d49882012-09-27 02:01:04 +0000169
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000170SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
reed@google.com6ae24e02012-09-26 13:44:13 +0000171 fNestLevel = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000172 SkSafeRef(dumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 fDumper = dumper;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174}
175
176SkDumpCanvas::~SkDumpCanvas() {
reed@google.com82065d62011-02-07 15:30:46 +0000177 SkSafeUnref(fDumper);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178}
179
180void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
181 const char format[], ...) {
182 static const size_t BUFFER_SIZE = 1024;
183
184 char buffer[BUFFER_SIZE];
185 va_list args;
186 va_start(args, format);
187 vsnprintf(buffer, BUFFER_SIZE, format, args);
188 va_end(args);
reed@google.com82065d62011-02-07 15:30:46 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 if (fDumper) {
191 fDumper->dump(this, verb, buffer, paint);
192 }
193}
194
195///////////////////////////////////////////////////////////////////////////////
196
Florin Malita5f6102d2014-06-30 10:13:28 -0400197void SkDumpCanvas::willSave() {
198 this->dump(kSave_Verb, NULL, "save()");
199 this->INHERITED::willSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200}
201
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000202SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
203 SaveFlags flags) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000204 SkString str;
205 str.printf("saveLayer(0x%X)", flags);
206 if (bounds) {
207 str.append(" bounds");
208 toString(*bounds, &str);
209 }
210 if (paint) {
211 if (paint->getAlpha() != 0xFF) {
212 str.appendf(" alpha:0x%02X", paint->getAlpha());
213 }
214 if (paint->getXfermode()) {
215 str.appendf(" xfermode:%p", paint->getXfermode());
216 }
217 }
218 this->dump(kSave_Verb, paint, str.c_str());
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000219 return this->INHERITED::willSaveLayer(bounds, paint, flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220}
221
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000222void SkDumpCanvas::willRestore() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223 this->dump(kRestore_Verb, NULL, "restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000224 this->INHERITED::willRestore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225}
226
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000227void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228 SkString str;
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000229
230 switch (matrix.getType()) {
231 case SkMatrix::kTranslate_Mask:
232 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
233 SkScalarToFloat(matrix.getTranslateX()),
234 SkScalarToFloat(matrix.getTranslateY()));
235 break;
236 case SkMatrix::kScale_Mask:
237 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
238 SkScalarToFloat(matrix.getScaleX()),
239 SkScalarToFloat(matrix.getScaleY()));
240 break;
241 default:
242 matrix.toString(&str);
243 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
244 break;
245 }
246
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000247 this->INHERITED::didConcat(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248}
249
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000250void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000252 matrix.toString(&str);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000254 this->INHERITED::didSetMatrix(matrix);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255}
256
257///////////////////////////////////////////////////////////////////////////////
258
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000259const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
260 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261}
262
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000263void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com071eef92011-10-12 11:52:53 +0000264 SkString str;
265 toString(rect, &str);
266 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000267 EdgeStyleToAAString(edgeStyle));
268 this->INHERITED::onClipRect(rect, op, edgeStyle);
reed@google.com071eef92011-10-12 11:52:53 +0000269}
270
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000271void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@google.com4ed0fb72012-12-12 20:48:18 +0000272 SkString str;
273 toString(rrect, &str);
274 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000275 EdgeStyleToAAString(edgeStyle));
276 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000277}
278
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000279void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 SkString str;
281 toString(path, &str);
reed@google.com071eef92011-10-12 11:52:53 +0000282 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000283 EdgeStyleToAAString(edgeStyle));
284 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285}
286
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000287void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288 SkString str;
289 toString(deviceRgn, &str);
290 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
291 toString(op));
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000292 this->INHERITED::onClipRegion(deviceRgn, op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293}
294
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +0000295void SkDumpCanvas::onPushCull(const SkRect& cullRect) {
296 SkString str;
297 toString(cullRect, &str);
298 this->dump(kCull_Verb, NULL, "pushCull(%s)", str.c_str());
299}
300
301void SkDumpCanvas::onPopCull() {
302 this->dump(kCull_Verb, NULL, "popCull()");
303}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304///////////////////////////////////////////////////////////////////////////////
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
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000322void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 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);
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000331 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
332}
333
334void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
335 const SkPaint& paint) {
336 SkString str0, str1;
337 toString(outer, &str0);
338 toString(inner, &str0);
339 this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
340 str0.c_str(), str1.c_str());
reed@google.com4ed0fb72012-12-12 20:48:18 +0000341}
342
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000343void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 SkString str;
345 toString(path, &str);
346 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
347}
348
349void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
350 const SkPaint* paint) {
351 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000352 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000353 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354 SkScalarToFloat(x), SkScalarToFloat(y));
355}
356
reed@google.com71121732012-09-18 15:14:33 +0000357void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000358 const SkRect& dst, const SkPaint* paint,
359 DrawBitmapRectFlags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 SkString bs, rs;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000361 bitmap.toString(&bs);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362 toString(dst, &rs);
363 // show the src-rect only if its not everything
364 if (src && (src->fLeft > 0 || src->fTop > 0 ||
reed@google.com71121732012-09-18 15:14:33 +0000365 src->fRight < SkIntToScalar(bitmap.width()) ||
366 src->fBottom < SkIntToScalar(bitmap.height()))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 SkString ss;
368 toString(*src, &ss);
369 rs.prependf("%s ", ss.c_str());
370 }
371
reed@google.com71121732012-09-18 15:14:33 +0000372 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 bs.c_str(), rs.c_str());
374}
375
376void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
377 const SkPaint* paint) {
378 SkString bs, ms;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000379 bitmap.toString(&bs);
380 m.toString(&ms);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
382 bs.c_str(), ms.c_str());
383}
384
385void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
386 const SkPaint* paint) {
387 SkString str;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000388 bitmap.toString(&str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000389 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390 x, y);
391}
392
reed@google.come0d9ce82014-04-23 04:00:17 +0000393void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
394 const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 SkString str;
396 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000397 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
399}
400
reed@google.come0d9ce82014-04-23 04:00:17 +0000401void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
402 const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403 SkString str;
404 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000405 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
407 SkScalarToFloat(pos[0].fY));
408}
409
reed@google.come0d9ce82014-04-23 04:00:17 +0000410void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
411 SkScalar constY, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 SkString str;
413 toString(text, byteLength, paint.getTextEncoding(), &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000414 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
416 SkScalarToFloat(constY));
417}
418
reed@google.come0d9ce82014-04-23 04:00:17 +0000419void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
420 const SkMatrix* matrix, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 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
fmalitab7425172014-08-26 07:56:44 -0700427void SkDumpCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
428 const SkPaint& paint) {
429 SkString str;
430 toString(blob->bounds(), &str);
431 this->dump(kDrawText_Verb, &paint, "drawTextBlob(%p) [%s]", blob, str.c_str());
432 // FIXME: dump the actual blob content?
433}
434
reedd5fa1a42014-08-09 11:08:05 -0700435void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
436 const SkPaint* paint) {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700437 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %f:%f:%f:%f", picture,
438 picture->cullRect().fLeft, picture->cullRect().fTop,
439 picture->cullRect().fRight, picture->cullRect().fBottom);
reed@android.com9b46e772009-06-05 12:24:41 +0000440 fNestLevel += 1;
reedd5fa1a42014-08-09 11:08:05 -0700441 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@android.com9b46e772009-06-05 12:24:41 +0000442 fNestLevel -= 1;
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700443 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %f:%f:%f:%f", &picture,
444 picture->cullRect().fLeft, picture->cullRect().fTop,
445 picture->cullRect().fRight, picture->cullRect().fBottom);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446}
447
448void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
449 const SkPoint vertices[], const SkPoint texs[],
450 const SkColor colors[], SkXfermode* xmode,
451 const uint16_t indices[], int indexCount,
452 const SkPaint& paint) {
reed@android.com0becfc5b2009-01-13 13:26:44 +0000453 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
455 SkScalarToFloat(vertices[0].fY));
456}
457
dandovb3c9d1c2014-08-12 08:34:29 -0700458void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
459 const SkPoint texCoords[4], SkXfermode* xmode,
460 const SkPaint& paint) {
dandov963137b2014-08-07 07:49:53 -0700461 //dumps corner points and colors in clockwise order starting on upper-left corner
462 this->dump(kDrawPatch_Verb, &paint, "drawPatch(Vertices{[%f, %f], [%f, %f], [%f, %f], [%f, %f]}\
dandovb3c9d1c2014-08-12 08:34:29 -0700463 | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%f,%f], \
464 [%f,%f]})",
465 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fX,
466 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fY,
467 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fX,
468 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fY,
469 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fX,
470 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fY,
471 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fX,
472 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fY,
473 colors[0], colors[1], colors[2], colors[3],
474 texCoords[0].x(), texCoords[0].y(), texCoords[1].x(), texCoords[1].y(),
475 texCoords[2].x(), texCoords[2].y(), texCoords[3].x(), texCoords[3].y());
dandov963137b2014-08-07 07:49:53 -0700476}
477
reed@android.comcb608442009-12-04 21:32:27 +0000478void SkDumpCanvas::drawData(const void* data, size_t length) {
479// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
480 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
robertphillips@google.comadacc702013-10-14 21:53:24 +0000481 SkTMin<size_t>(length, 64), data);
reed@android.comcb608442009-12-04 21:32:27 +0000482}
483
robertphillips@google.com0a4805e2013-05-29 13:24:23 +0000484void SkDumpCanvas::beginCommentGroup(const char* description) {
485 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
486}
487
488void SkDumpCanvas::addComment(const char* kywd, const char* value) {
489 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
490}
491
492void SkDumpCanvas::endCommentGroup() {
493 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
494}
495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496///////////////////////////////////////////////////////////////////////////////
497///////////////////////////////////////////////////////////////////////////////
498
499SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
500 fProc = proc;
501 fRefcon = refcon;
502}
503
504static void appendPtr(SkString* str, const void* ptr, const char name[]) {
505 if (ptr) {
506 str->appendf(" %s:%p", name, ptr);
507 }
508}
509
510static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
511 const char name[]) {
512 if (ptr) {
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000513 str->appendf(" %s:%p", name, ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000514 }
515}
516
517void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
518 const char str[], const SkPaint* p) {
519 SkString msg, tab;
reed@android.com9b46e772009-06-05 12:24:41 +0000520 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 SkASSERT(level >= 0);
522 for (int i = 0; i < level; i++) {
reed@google.com4258c2c2012-08-31 15:41:10 +0000523#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 tab.append("\t");
reed@google.com4258c2c2012-08-31 15:41:10 +0000525#else
526 tab.append(" "); // tabs are often too wide to be useful
527#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 }
529 msg.printf("%s%s", tab.c_str(), str);
reed@google.com82065d62011-02-07 15:30:46 +0000530
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531 if (p) {
532 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
533 appendFlattenable(&msg, p->getShader(), "shader");
534 appendFlattenable(&msg, p->getXfermode(), "xfermode");
535 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
536 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
537 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
538 appendFlattenable(&msg, p->getColorFilter(), "filter");
reed@google.com82065d62011-02-07 15:30:46 +0000539
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540 if (SkDumpCanvas::kDrawText_Verb == verb) {
541 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
542 appendPtr(&msg, p->getTypeface(), "typeface");
543 }
skia.committer@gmail.com73a4b4f2013-06-26 07:00:59 +0000544
reed@google.comb8b830e2013-06-25 20:42:37 +0000545 if (p->getStyle() != SkPaint::kFill_Style) {
546 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
547 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 }
reed@google.com82065d62011-02-07 15:30:46 +0000549
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 fProc(msg.c_str(), fRefcon);
551}
552
553///////////////////////////////////////////////////////////////////////////////
554
555static void dumpToDebugf(const char text[], void*) {
556 SkDebugf("%s\n", text);
557}
558
559SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
560
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000561#endif