3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkDumpCanvas.h"
12 #include "SkPatchUtils.h"
13 #include "SkPicture.h"
14 #include "SkPixelRef.h"
20 // needed just to know that these are all subclassed from SkFlattenable
22 #include "SkPathEffect.h"
23 #include "SkXfermode.h"
24 #include "SkColorFilter.h"
25 #include "SkPathEffect.h"
26 #include "SkMaskFilter.h"
28 static void toString(const SkRect& r, SkString* str) {
29 str->appendf("[%g,%g %g:%g]",
30 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
31 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
34 static void toString(const SkIRect& r, SkString* str) {
35 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
38 static 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()));
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()) {
50 rrect.radii(SkRRect::kUpperLeft_Corner),
51 rrect.radii(SkRRect::kUpperRight_Corner),
52 rrect.radii(SkRRect::kLowerRight_Corner),
53 rrect.radii(SkRRect::kLowerLeft_Corner),
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());
63 static void dumpVerbs(const SkPath& path, SkString* str) {
64 SkPath::Iter iter(path, false);
67 switch (iter.next(pts, false)) {
68 case SkPath::kMove_Verb:
69 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
71 case SkPath::kLine_Verb:
72 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
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);
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);
82 case SkPath::kClose_Verb:
85 case SkPath::kDone_Verb:
87 case SkPath::kConic_Verb:
94 static void toString(const SkPath& path, SkString* str) {
96 str->append("path:empty");
98 toString(path.getBounds(), str);
102 str->append(s.c_str());
105 str->prepend("path:[");
109 static const char* toString(SkRegion::Op op) {
110 static const char* gOpNames[] = {
111 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
116 static void toString(const SkRegion& rgn, SkString* str) {
117 str->append("Region:[");
118 toString(rgn.getBounds(), str);
120 if (rgn.isComplex()) {
121 str->append(".complex");
125 static const char* toString(SkCanvas::VertexMode vm) {
126 static const char* gVMNames[] = {
127 "TRIANGLES", "STRIP", "FAN"
132 static const char* toString(SkCanvas::PointMode pm) {
133 static const char* gPMNames[] = {
134 "POINTS", "LINES", "POLYGON"
139 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
141 // FIXME: this code appears to be untested - and probably unused - and probably wrong
143 case SkPaint::kUTF8_TextEncoding:
144 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
145 byteLen > 32 ? "..." : "");
147 case SkPaint::kUTF16_TextEncoding:
148 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
149 byteLen > 64 ? "..." : "");
151 case SkPaint::kUTF32_TextEncoding:
152 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
153 byteLen > 128 ? "..." : "");
155 case SkPaint::kGlyphID_TextEncoding:
156 str->append("<glyphs>");
165 ///////////////////////////////////////////////////////////////////////////////
167 #define WIDE_OPEN 16384
169 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
175 SkDumpCanvas::~SkDumpCanvas() {
176 SkSafeUnref(fDumper);
179 void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
180 const char format[], ...) {
181 static const size_t BUFFER_SIZE = 1024;
183 char buffer[BUFFER_SIZE];
185 va_start(args, format);
186 vsnprintf(buffer, BUFFER_SIZE, format, args);
190 fDumper->dump(this, verb, buffer, paint);
194 ///////////////////////////////////////////////////////////////////////////////
196 void SkDumpCanvas::willSave() {
197 this->dump(kSave_Verb, NULL, "save()");
198 this->INHERITED::willSave();
201 SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
204 str.printf("saveLayer(0x%X)", flags);
206 str.append(" bounds");
207 toString(*bounds, &str);
210 if (paint->getAlpha() != 0xFF) {
211 str.appendf(" alpha:0x%02X", paint->getAlpha());
213 if (paint->getXfermode()) {
214 str.appendf(" xfermode:%p", paint->getXfermode());
217 this->dump(kSave_Verb, paint, str.c_str());
218 return this->INHERITED::willSaveLayer(bounds, paint, flags);
221 void SkDumpCanvas::willRestore() {
222 this->dump(kRestore_Verb, NULL, "restore");
223 this->INHERITED::willRestore();
226 void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
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()));
235 case SkMatrix::kScale_Mask:
236 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
237 SkScalarToFloat(matrix.getScaleX()),
238 SkScalarToFloat(matrix.getScaleY()));
241 matrix.toString(&str);
242 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
246 this->INHERITED::didConcat(matrix);
249 void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
251 matrix.toString(&str);
252 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
253 this->INHERITED::didSetMatrix(matrix);
256 ///////////////////////////////////////////////////////////////////////////////
258 const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
259 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
262 void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
264 toString(rect, &str);
265 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
266 EdgeStyleToAAString(edgeStyle));
267 this->INHERITED::onClipRect(rect, op, edgeStyle);
270 void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
272 toString(rrect, &str);
273 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
274 EdgeStyleToAAString(edgeStyle));
275 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
278 void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
280 toString(path, &str);
281 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
282 EdgeStyleToAAString(edgeStyle));
283 this->INHERITED::onClipPath(path, op, edgeStyle);
286 void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
288 toString(deviceRgn, &str);
289 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
291 this->INHERITED::onClipRegion(deviceRgn, op);
294 void SkDumpCanvas::onPushCull(const SkRect& cullRect) {
296 toString(cullRect, &str);
297 this->dump(kCull_Verb, NULL, "pushCull(%s)", str.c_str());
300 void SkDumpCanvas::onPopCull() {
301 this->dump(kCull_Verb, NULL, "popCull()");
303 ///////////////////////////////////////////////////////////////////////////////
305 void SkDumpCanvas::drawPaint(const SkPaint& paint) {
306 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
309 void 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),
315 void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
317 toString(rect, &str);
318 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
321 void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
323 toString(rect, &str);
324 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
327 void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
329 toString(rrect, &str);
330 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
333 void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
334 const SkPaint& paint) {
336 toString(outer, &str0);
337 toString(inner, &str0);
338 this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
339 str0.c_str(), str1.c_str());
342 void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
344 toString(path, &str);
345 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
348 void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
349 const SkPaint* paint) {
351 bitmap.toString(&str);
352 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
353 SkScalarToFloat(x), SkScalarToFloat(y));
356 void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
357 const SkRect& dst, const SkPaint* paint,
358 DrawBitmapRectFlags flags) {
360 bitmap.toString(&bs);
362 // show the src-rect only if its not everything
363 if (src && (src->fLeft > 0 || src->fTop > 0 ||
364 src->fRight < SkIntToScalar(bitmap.width()) ||
365 src->fBottom < SkIntToScalar(bitmap.height()))) {
368 rs.prependf("%s ", ss.c_str());
371 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
372 bs.c_str(), rs.c_str());
375 void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
376 const SkPaint* paint) {
378 bitmap.toString(&bs);
380 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
381 bs.c_str(), ms.c_str());
384 void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
385 const SkPaint* paint) {
387 bitmap.toString(&str);
388 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
392 void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
393 const SkPaint& paint) {
395 toString(text, byteLength, paint.getTextEncoding(), &str);
396 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
397 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
400 void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
401 const SkPaint& paint) {
403 toString(text, byteLength, paint.getTextEncoding(), &str);
404 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
405 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
406 SkScalarToFloat(pos[0].fY));
409 void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
410 SkScalar constY, const SkPaint& paint) {
412 toString(text, byteLength, paint.getTextEncoding(), &str);
413 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
414 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
415 SkScalarToFloat(constY));
418 void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
419 const SkMatrix* matrix, const SkPaint& paint) {
421 toString(text, byteLength, paint.getTextEncoding(), &str);
422 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
423 str.c_str(), byteLength);
426 void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
427 const SkPaint* paint) {
428 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", picture,
429 picture->width(), picture->height());
431 this->INHERITED::onDrawPicture(picture, matrix, paint);
433 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
434 picture->width(), picture->height());
437 void 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) {
442 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
443 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
444 SkScalarToFloat(vertices[0].fY));
447 void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
448 const SkPoint texCoords[4], SkXfermode* xmode,
449 const SkPaint& paint) {
450 //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]}\
452 | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%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());
467 void 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,
470 SkTMin<size_t>(length, 64), data);
473 void SkDumpCanvas::beginCommentGroup(const char* description) {
474 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
477 void SkDumpCanvas::addComment(const char* kywd, const char* value) {
478 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
481 void SkDumpCanvas::endCommentGroup() {
482 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
485 ///////////////////////////////////////////////////////////////////////////////
486 ///////////////////////////////////////////////////////////////////////////////
488 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
493 static void appendPtr(SkString* str, const void* ptr, const char name[]) {
495 str->appendf(" %s:%p", name, ptr);
499 static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
502 str->appendf(" %s:%p", name, ptr);
506 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
507 const char str[], const SkPaint* p) {
509 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
510 SkASSERT(level >= 0);
511 for (int i = 0; i < level; i++) {
515 tab.append(" "); // tabs are often too wide to be useful
518 msg.printf("%s%s", tab.c_str(), str);
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");
529 if (SkDumpCanvas::kDrawText_Verb == verb) {
530 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
531 appendPtr(&msg, p->getTypeface(), "typeface");
534 if (p->getStyle() != SkPaint::kFill_Style) {
535 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
539 fProc(msg.c_str(), fRefcon);
542 ///////////////////////////////////////////////////////////////////////////////
544 static void dumpToDebugf(const char text[], void*) {
545 SkDebugf("%s\n", text);
548 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}