1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkData.h"
9 #include "SkDrawable.h"
10 #include "SkDumpCanvas.h"
11 #include "SkImage.h"
12 #include "SkPatchUtils.h"
13 #include "SkPicture.h"
14 #include "SkPixelRef.h"
15 #include "SkRegion.h"
16 #include "SkRRect.h"
17 #include "SkString.h"
18 #include "SkTextBlob.h"
19 #include <stdarg.h>
20 #include <stdio.h>
21
22 // needed just to know that these are all subclassed from SkFlattenable
23 #include "SkShader.h"
24 #include "SkPathEffect.h"
25 #include "SkColorFilter.h"
26 #include "SkPathEffect.h"
27 #include "SkMaskFilter.h"
28
toString(const SkRect & r,SkString * str)29 static void toString(const SkRect& r, SkString* str) {
30 str->appendf("[%g,%g %g:%g]",
31 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
32 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
33 }
34
toString(const SkIRect & r,SkString * str)35 static void toString(const SkIRect& r, SkString* str) {
36 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
37 }
38
toString(const SkRRect & rrect,SkString * str)39 static 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
dumpVerbs(const SkPath & path,SkString * str)64 static void dumpVerbs(const SkPath& path, SkString* str) {
65 SkPath::Iter iter(path, false);
66 SkPoint pts[4];
67 for (;;) {
68 switch (iter.next(pts, false)) {
69 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:
84 str->append("X");
85 break;
86 case SkPath::kDone_Verb:
87 return;
88 case SkPath::kConic_Verb:
89 SkASSERT(0);
90 break;
91 }
92 }
93 }
94
toString(const SkPath & path,SkString * str)95 static void toString(const SkPath& path, SkString* str) {
96 if (path.isEmpty()) {
97 str->append("path:empty");
98 } else {
99 toString(path.getBounds(), str);
100 #if 1
101 SkString s;
102 dumpVerbs(path, &s);
103 str->append(s.c_str());
104 #endif
105 str->append("]");
106 str->prepend("path:[");
107 }
108 }
109
toString(SkClipOp op)110 static const char* toString(SkClipOp op) {
111 static const char* gOpNames[] = {
112 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
113 };
114 return gOpNames[static_cast<int>(op)];
115 }
116
toString(const SkRegion & rgn,SkString * str)117 static void toString(const SkRegion& rgn, SkString* str) {
118 str->append("Region:[");
119 toString(rgn.getBounds(), str);
120 str->append("]");
121 if (rgn.isComplex()) {
122 str->append(".complex");
123 }
124 }
125
toString(SkVertices::VertexMode vm)126 static const char* toString(SkVertices::VertexMode vm) {
127 static const char* gVMNames[] = {
128 "TRIANGLES", "STRIP", "FAN"
129 };
130 return gVMNames[vm];
131 }
132
toString(SkCanvas::PointMode pm)133 static const char* toString(SkCanvas::PointMode pm) {
134 static const char* gPMNames[] = {
135 "POINTS", "LINES", "POLYGON"
136 };
137 return gPMNames[pm];
138 }
139
toString(const void * text,size_t byteLen,SkPaint::TextEncoding enc,SkString * str)140 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
141 SkString* str) {
142 // FIXME: this code appears to be untested - and probably unused - and probably wrong
143 switch (enc) {
144 case SkPaint::kUTF8_TextEncoding:
145 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
146 byteLen > 32 ? "..." : "");
147 break;
148 case SkPaint::kUTF16_TextEncoding:
149 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
150 byteLen > 64 ? "..." : "");
151 break;
152 case SkPaint::kUTF32_TextEncoding:
153 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
154 byteLen > 128 ? "..." : "");
155 break;
156 case SkPaint::kGlyphID_TextEncoding:
157 str->append("<glyphs>");
158 break;
159
160 default:
161 SkASSERT(false);
162 break;
163 }
164 }
165
166 ///////////////////////////////////////////////////////////////////////////////
167
168 #define WIDE_OPEN 16384
169
SkDumpCanvas(Dumper * dumper)170 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
171 fNestLevel = 0;
172 SkSafeRef(dumper);
173 fDumper = dumper;
174 }
175
~SkDumpCanvas()176 SkDumpCanvas::~SkDumpCanvas() {
177 SkSafeUnref(fDumper);
178 }
179
dump(Verb verb,const SkPaint * paint,const char format[],...)180 void 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);
189
190 if (fDumper) {
191 fDumper->dump(this, verb, buffer, paint);
192 }
193 }
194
195 ///////////////////////////////////////////////////////////////////////////////
196
willSave()197 void SkDumpCanvas::willSave() {
198 this->dump(kSave_Verb, nullptr, "save()");
199 this->INHERITED::willSave();
200 }
201
getSaveLayerStrategy(const SaveLayerRec & rec)202 SkCanvas::SaveLayerStrategy SkDumpCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
203 SkString str;
204 str.printf("saveLayer(0x%X)", rec.fSaveLayerFlags);
205 if (rec.fBounds) {
206 str.append(" bounds");
207 toString(*rec.fBounds, &str);
208 }
209 const SkPaint* paint = rec.fPaint;
210 if (paint) {
211 if (paint->getAlpha() != 0xFF) {
212 str.appendf(" alpha:0x%02X", paint->getAlpha());
213 }
214 if (!paint->isSrcOver()) {
215 str.appendf(" blendmode:%d", (int)paint->getBlendMode());
216 }
217 }
218 this->dump(kSave_Verb, paint, str.c_str());
219 return this->INHERITED::getSaveLayerStrategy(rec);
220 }
221
willRestore()222 void SkDumpCanvas::willRestore() {
223 this->dump(kRestore_Verb, nullptr, "restore");
224 this->INHERITED::willRestore();
225 }
226
didConcat(const SkMatrix & matrix)227 void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
228 SkString str;
229
230 switch (matrix.getType()) {
231 case SkMatrix::kTranslate_Mask:
232 this->dump(kMatrix_Verb, nullptr, "translate(%g %g)",
233 SkScalarToFloat(matrix.getTranslateX()),
234 SkScalarToFloat(matrix.getTranslateY()));
235 break;
236 case SkMatrix::kScale_Mask:
237 this->dump(kMatrix_Verb, nullptr, "scale(%g %g)",
238 SkScalarToFloat(matrix.getScaleX()),
239 SkScalarToFloat(matrix.getScaleY()));
240 break;
241 default:
242 matrix.toString(&str);
243 this->dump(kMatrix_Verb, nullptr, "concat(%s)", str.c_str());
244 break;
245 }
246
247 this->INHERITED::didConcat(matrix);
248 }
249
didSetMatrix(const SkMatrix & matrix)250 void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
251 SkString str;
252 matrix.toString(&str);
253 this->dump(kMatrix_Verb, nullptr, "setMatrix(%s)", str.c_str());
254 this->INHERITED::didSetMatrix(matrix);
255 }
256
257 ///////////////////////////////////////////////////////////////////////////////
258
EdgeStyleToAAString(ClipEdgeStyle edgeStyle)259 const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
260 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
261 }
262
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle edgeStyle)263 void SkDumpCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) {
264 SkString str;
265 toString(rect, &str);
266 this->dump(kClip_Verb, nullptr, "clipRect(%s %s %s)", str.c_str(), toString(op),
267 EdgeStyleToAAString(edgeStyle));
268 this->INHERITED::onClipRect(rect, op, edgeStyle);
269 }
270
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)271 void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
272 SkString str;
273 toString(rrect, &str);
274 this->dump(kClip_Verb, nullptr, "clipRRect(%s %s %s)", str.c_str(), toString(op),
275 EdgeStyleToAAString(edgeStyle));
276 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
277 }
278
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)279 void SkDumpCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
280 SkString str;
281 toString(path, &str);
282 this->dump(kClip_Verb, nullptr, "clipPath(%s %s %s)", str.c_str(), toString(op),
283 EdgeStyleToAAString(edgeStyle));
284 this->INHERITED::onClipPath(path, op, edgeStyle);
285 }
286
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)287 void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
288 SkString str;
289 toString(deviceRgn, &str);
290 this->dump(kClip_Verb, nullptr, "clipRegion(%s %s)", str.c_str(), toString(op));
291 this->INHERITED::onClipRegion(deviceRgn, op);
292 }
293
294 ///////////////////////////////////////////////////////////////////////////////
295
onDrawPaint(const SkPaint & paint)296 void SkDumpCanvas::onDrawPaint(const SkPaint& paint) {
297 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
298 }
299
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)300 void SkDumpCanvas::onDrawPoints(PointMode mode, size_t count,
301 const SkPoint pts[], const SkPaint& paint) {
302 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
303 count);
304 }
305
onDrawOval(const SkRect & rect,const SkPaint & paint)306 void SkDumpCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
307 SkString str;
308 toString(rect, &str);
309 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
310 }
311
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)312 void SkDumpCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
313 bool useCenter, const SkPaint& paint) {
314 SkString str;
315 toString(rect, &str);
316 this->dump(kDrawArc_Verb, &paint, "drawArc(%s, %g, %g, %d)", str.c_str(), startAngle,
317 sweepAngle, useCenter);
318 }
319
onDrawRect(const SkRect & rect,const SkPaint & paint)320 void SkDumpCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
321 SkString str;
322 toString(rect, &str);
323 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
324 }
325
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)326 void SkDumpCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
327 SkString str;
328 toString(rrect, &str);
329 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
330 }
331
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)332 void 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());
339 }
340
onDrawPath(const SkPath & path,const SkPaint & paint)341 void SkDumpCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
342 SkString str;
343 toString(path, &str);
344 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
345 }
346
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint * paint)347 void SkDumpCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
348 const SkPaint* paint) {
349 SkString str;
350 bitmap.toString(&str);
351 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
352 SkScalarToFloat(x), SkScalarToFloat(y));
353 }
354
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)355 void SkDumpCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
356 const SkPaint* paint, SrcRectConstraint) {
357 SkString bs, rs;
358 bitmap.toString(&bs);
359 toString(dst, &rs);
360 // show the src-rect only if its not everything
361 if (src && (src->fLeft > 0 || src->fTop > 0 ||
362 src->fRight < SkIntToScalar(bitmap.width()) ||
363 src->fBottom < SkIntToScalar(bitmap.height()))) {
364 SkString ss;
365 toString(*src, &ss);
366 rs.prependf("%s ", ss.c_str());
367 }
368
369 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRect(%s %s)", bs.c_str(), rs.c_str());
370 }
371
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)372 void SkDumpCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
373 const SkRect& dst, const SkPaint* paint) {
374 SkString str, centerStr, dstStr;
375 bitmap.toString(&str);
376 toString(center, ¢erStr);
377 toString(dst, &dstStr);
378 this->dump(kDrawBitmap_Verb, paint, "drawBitmapNine(%s %s %s)", str.c_str(),
379 centerStr.c_str(), dstStr.c_str());
380 }
381
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint * paint)382 void SkDumpCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
383 SkString str;
384 image->toString(&str);
385 this->dump(kDrawBitmap_Verb, paint, "drawImage(%s %g %g)", str.c_str(),
386 SkScalarToFloat(x), SkScalarToFloat(y));
387 }
388
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)389 void SkDumpCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
390 const SkPaint* paint, SrcRectConstraint) {
391 SkString bs, rs;
392 image->toString(&bs);
393 toString(dst, &rs);
394 // show the src-rect only if its not everything
395 if (src && (src->fLeft > 0 || src->fTop > 0 ||
396 src->fRight < SkIntToScalar(image->width()) ||
397 src->fBottom < SkIntToScalar(image->height()))) {
398 SkString ss;
399 toString(*src, &ss);
400 rs.prependf("%s ", ss.c_str());
401 }
402
403 this->dump(kDrawBitmap_Verb, paint, "drawImageRectToRect(%s %s)",
404 bs.c_str(), rs.c_str());
405 }
406
onDrawText(const void * text,size_t byteLength,SkScalar x,SkScalar y,const SkPaint & paint)407 void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
408 const SkPaint& paint) {
409 SkString str;
410 toString(text, byteLength, paint.getTextEncoding(), &str);
411 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
412 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
413 }
414
onDrawPosText(const void * text,size_t byteLength,const SkPoint pos[],const SkPaint & paint)415 void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
416 const SkPaint& paint) {
417 SkString str;
418 toString(text, byteLength, paint.getTextEncoding(), &str);
419 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
420 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
421 SkScalarToFloat(pos[0].fY));
422 }
423
onDrawPosTextH(const void * text,size_t byteLength,const SkScalar xpos[],SkScalar constY,const SkPaint & paint)424 void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
425 SkScalar constY, const SkPaint& paint) {
426 SkString str;
427 toString(text, byteLength, paint.getTextEncoding(), &str);
428 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
429 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
430 SkScalarToFloat(constY));
431 }
432
onDrawTextOnPath(const void * text,size_t byteLength,const SkPath & path,const SkMatrix * matrix,const SkPaint & paint)433 void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
434 const SkMatrix* matrix, const SkPaint& paint) {
435 SkString str;
436 toString(text, byteLength, paint.getTextEncoding(), &str);
437 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
438 str.c_str(), byteLength);
439 }
440
onDrawTextRSXform(const void * text,size_t byteLength,const SkRSXform xform[],const SkRect * cull,const SkPaint & paint)441 void SkDumpCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
442 const SkRect* cull, const SkPaint& paint) {
443 SkString str;
444 toString(text, byteLength, paint.getTextEncoding(), &str);
445 this->dump(kDrawText_Verb, &paint, "drawTextRSXform(%s [%d])",
446 str.c_str(), byteLength);
447 }
448
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)449 void SkDumpCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
450 const SkPaint& paint) {
451 SkString str;
452 toString(blob->bounds(), &str);
453 this->dump(kDrawText_Verb, &paint, "drawTextBlob(%p) [%s]", blob, str.c_str());
454 // FIXME: dump the actual blob content?
455 }
456
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)457 void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
458 const SkPaint* paint) {
459 this->dump(kDrawPicture_Verb, nullptr, "drawPicture(%p) %f:%f:%f:%f", picture,
460 picture->cullRect().fLeft, picture->cullRect().fTop,
461 picture->cullRect().fRight, picture->cullRect().fBottom);
462 fNestLevel += 1;
463 this->INHERITED::onDrawPicture(picture, matrix, paint);
464 fNestLevel -= 1;
465 this->dump(kDrawPicture_Verb, nullptr, "endPicture(%p) %f:%f:%f:%f", picture,
466 picture->cullRect().fLeft, picture->cullRect().fTop,
467 picture->cullRect().fRight, picture->cullRect().fBottom);
468 }
469
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)470 void SkDumpCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
471 const auto bounds = drawable->getBounds();
472 this->dump(kDrawPicture_Verb, nullptr, "drawDrawable(%p) %f:%f:%f:%f", drawable,
473 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
474 fNestLevel += 1;
475 this->INHERITED::onDrawDrawable(drawable, matrix);
476 fNestLevel -= 1;
477 this->dump(kDrawPicture_Verb, nullptr, "endDrawable(%p) %f:%f:%f:%f", drawable,
478 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
479 }
480
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode,const SkPaint & paint)481 void SkDumpCanvas::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode,
482 const SkPaint& paint) {
483 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] ...)",
484 toString(vertices->mode()), vertices->vertexCount());
485 }
486
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode,const SkPaint & paint)487 void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
488 const SkPoint texCoords[4], SkBlendMode,
489 const SkPaint& paint) {
490 //dumps corner points and colors in clockwise order starting on upper-left corner
491 this->dump(kDrawPatch_Verb, &paint, "drawPatch(Vertices{[%f, %f], [%f, %f], [%f, %f], [%f, %f]}\
492 | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%f,%f], \
493 [%f,%f]})",
494 cubics[0].fX, cubics[0].fY,
495 cubics[3].fX, cubics[3].fY,
496 cubics[6].fX, cubics[6].fY,
497 cubics[9].fX, cubics[9].fY,
498 colors[0], colors[1], colors[2], colors[3],
499 texCoords[0].x(), texCoords[0].y(), texCoords[1].x(), texCoords[1].y(),
500 texCoords[2].x(), texCoords[2].y(), texCoords[3].x(), texCoords[3].y());
501 }
502
onDrawAnnotation(const SkRect & rect,const char key[],SkData * value)503 void SkDumpCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
504 SkString str;
505 toString(rect, &str);
506 this->dump(kDrawAnnotation_Verb, nullptr, "drawAnnotation(%s \"%s\" (%zu))",
507 str.c_str(), key, value ? value->size() : 0);
508 }
509
510 ///////////////////////////////////////////////////////////////////////////////
511 ///////////////////////////////////////////////////////////////////////////////
512
SkFormatDumper(void (* proc)(const char *,void *),void * refcon)513 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
514 fProc = proc;
515 fRefcon = refcon;
516 }
517
appendPtr(SkString * str,const void * ptr,const char name[])518 static void appendPtr(SkString* str, const void* ptr, const char name[]) {
519 if (ptr) {
520 str->appendf(" %s:%p", name, ptr);
521 }
522 }
523
appendFlattenable(SkString * str,const SkFlattenable * ptr,const char name[])524 static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
525 const char name[]) {
526 if (ptr) {
527 str->appendf(" %s:%p", name, ptr);
528 }
529 }
530
dump(SkDumpCanvas * canvas,SkDumpCanvas::Verb verb,const char str[],const SkPaint * p)531 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
532 const char str[], const SkPaint* p) {
533 SkString msg, tab;
534 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
535 SkASSERT(level >= 0);
536 for (int i = 0; i < level; i++) {
537 #if 0
538 tab.append("\t");
539 #else
540 tab.append(" "); // tabs are often too wide to be useful
541 #endif
542 }
543 msg.printf("%s%s", tab.c_str(), str);
544
545 if (p) {
546 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
547 if (!p->isSrcOver()) {
548 msg.appendf(" blendmode:%d", (int)p->getBlendMode());
549 }
550 appendFlattenable(&msg, p->getShader(), "shader");
551 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
552 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
553 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
554 appendFlattenable(&msg, p->getColorFilter(), "filter");
555
556 if (SkDumpCanvas::kDrawText_Verb == verb) {
557 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
558 appendPtr(&msg, p->getTypeface(), "typeface");
559 }
560
561 if (p->getStyle() != SkPaint::kFill_Style) {
562 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
563 }
564 }
565
566 fProc(msg.c_str(), fRefcon);
567 }
568
569 ///////////////////////////////////////////////////////////////////////////////
570
dumpToDebugf(const char text[],void *)571 static void dumpToDebugf(const char text[], void*) {
572 SkDebugf("%s\n", text);
573 }
574
SkDebugfDumper()575 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, nullptr) {}
576