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 "SkPicture.h"
13 #include "SkPixelRef.h"
19 // needed just to know that these are all subclassed from SkFlattenable
21 #include "SkPathEffect.h"
22 #include "SkXfermode.h"
23 #include "SkColorFilter.h"
24 #include "SkPathEffect.h"
25 #include "SkMaskFilter.h"
27 static void toString(const SkRect& r, SkString* str) {
28 str->appendf("[%g,%g %g:%g]",
29 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
30 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
33 static void toString(const SkIRect& r, SkString* str) {
34 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
37 static void toString(const SkRRect& rrect, SkString* str) {
38 SkRect r = rrect.getBounds();
39 str->appendf("[%g,%g %g:%g]",
40 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
41 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
44 } else if (rrect.isSimple()) {
45 const SkVector& rad = rrect.getSimpleRadii();
46 str->appendf("(%g,%g)", rad.x(), rad.y());
47 } else if (rrect.isComplex()) {
49 rrect.radii(SkRRect::kUpperLeft_Corner),
50 rrect.radii(SkRRect::kUpperRight_Corner),
51 rrect.radii(SkRRect::kLowerRight_Corner),
52 rrect.radii(SkRRect::kLowerLeft_Corner),
54 str->appendf("(%g,%g %g,%g %g,%g %g,%g)",
55 radii[0].x(), radii[0].y(),
56 radii[1].x(), radii[1].y(),
57 radii[2].x(), radii[2].y(),
58 radii[3].x(), radii[3].y());
62 static void dumpVerbs(const SkPath& path, SkString* str) {
63 SkPath::Iter iter(path, false);
66 switch (iter.next(pts, false)) {
67 case SkPath::kMove_Verb:
68 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
70 case SkPath::kLine_Verb:
71 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
73 case SkPath::kQuad_Verb:
74 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY,
75 pts[2].fX, pts[2].fY);
77 case SkPath::kCubic_Verb:
78 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY,
79 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
81 case SkPath::kClose_Verb:
84 case SkPath::kDone_Verb:
86 case SkPath::kConic_Verb:
93 static void toString(const SkPath& path, SkString* str) {
95 str->append("path:empty");
97 toString(path.getBounds(), str);
101 str->append(s.c_str());
104 str->prepend("path:[");
108 static const char* toString(SkRegion::Op op) {
109 static const char* gOpNames[] = {
110 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
115 static void toString(const SkRegion& rgn, SkString* str) {
116 str->append("Region:[");
117 toString(rgn.getBounds(), str);
119 if (rgn.isComplex()) {
120 str->append(".complex");
124 static const char* toString(SkCanvas::VertexMode vm) {
125 static const char* gVMNames[] = {
126 "TRIANGLES", "STRIP", "FAN"
131 static const char* toString(SkCanvas::PointMode pm) {
132 static const char* gPMNames[] = {
133 "POINTS", "LINES", "POLYGON"
138 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
140 // FIXME: this code appears to be untested - and probably unused - and probably wrong
142 case SkPaint::kUTF8_TextEncoding:
143 str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
144 byteLen > 32 ? "..." : "");
146 case SkPaint::kUTF16_TextEncoding:
147 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
148 byteLen > 64 ? "..." : "");
150 case SkPaint::kUTF32_TextEncoding:
151 str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
152 byteLen > 128 ? "..." : "");
154 case SkPaint::kGlyphID_TextEncoding:
155 str->append("<glyphs>");
164 ///////////////////////////////////////////////////////////////////////////////
166 #define WIDE_OPEN 16384
168 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
174 SkDumpCanvas::~SkDumpCanvas() {
175 SkSafeUnref(fDumper);
178 void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
179 const char format[], ...) {
180 static const size_t BUFFER_SIZE = 1024;
182 char buffer[BUFFER_SIZE];
184 va_start(args, format);
185 vsnprintf(buffer, BUFFER_SIZE, format, args);
189 fDumper->dump(this, verb, buffer, paint);
193 ///////////////////////////////////////////////////////////////////////////////
195 void SkDumpCanvas::willSave(SaveFlags flags) {
196 this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
197 this->INHERITED::willSave(flags);
200 SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
203 str.printf("saveLayer(0x%X)", flags);
205 str.append(" bounds");
206 toString(*bounds, &str);
209 if (paint->getAlpha() != 0xFF) {
210 str.appendf(" alpha:0x%02X", paint->getAlpha());
212 if (paint->getXfermode()) {
213 str.appendf(" xfermode:%p", paint->getXfermode());
216 this->dump(kSave_Verb, paint, str.c_str());
217 return this->INHERITED::willSaveLayer(bounds, paint, flags);
220 void SkDumpCanvas::willRestore() {
221 this->dump(kRestore_Verb, NULL, "restore");
222 this->INHERITED::willRestore();
225 void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
228 switch (matrix.getType()) {
229 case SkMatrix::kTranslate_Mask:
230 this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
231 SkScalarToFloat(matrix.getTranslateX()),
232 SkScalarToFloat(matrix.getTranslateY()));
234 case SkMatrix::kScale_Mask:
235 this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
236 SkScalarToFloat(matrix.getScaleX()),
237 SkScalarToFloat(matrix.getScaleY()));
240 matrix.toString(&str);
241 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
245 this->INHERITED::didConcat(matrix);
248 void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
250 matrix.toString(&str);
251 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
252 this->INHERITED::didSetMatrix(matrix);
255 ///////////////////////////////////////////////////////////////////////////////
257 const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
258 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
261 void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
263 toString(rect, &str);
264 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
265 EdgeStyleToAAString(edgeStyle));
266 this->INHERITED::onClipRect(rect, op, edgeStyle);
269 void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
271 toString(rrect, &str);
272 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
273 EdgeStyleToAAString(edgeStyle));
274 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
277 void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
279 toString(path, &str);
280 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
281 EdgeStyleToAAString(edgeStyle));
282 this->INHERITED::onClipPath(path, op, edgeStyle);
285 void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
287 toString(deviceRgn, &str);
288 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
290 this->INHERITED::onClipRegion(deviceRgn, op);
293 void SkDumpCanvas::onPushCull(const SkRect& cullRect) {
295 toString(cullRect, &str);
296 this->dump(kCull_Verb, NULL, "pushCull(%s)", str.c_str());
299 void SkDumpCanvas::onPopCull() {
300 this->dump(kCull_Verb, NULL, "popCull()");
302 ///////////////////////////////////////////////////////////////////////////////
304 void SkDumpCanvas::drawPaint(const SkPaint& paint) {
305 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
308 void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
309 const SkPoint pts[], const SkPaint& paint) {
310 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
314 void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
316 toString(rect, &str);
317 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
320 void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
322 toString(rect, &str);
323 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
326 void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
328 toString(rrect, &str);
329 this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
332 void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
333 const SkPaint& paint) {
335 toString(outer, &str0);
336 toString(inner, &str0);
337 this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
338 str0.c_str(), str1.c_str());
341 void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
343 toString(path, &str);
344 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
347 void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
348 const SkPaint* paint) {
350 bitmap.toString(&str);
351 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
352 SkScalarToFloat(x), SkScalarToFloat(y));
355 void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
356 const SkRect& dst, const SkPaint* paint,
357 DrawBitmapRectFlags flags) {
359 bitmap.toString(&bs);
361 // show the src-rect only if its not everything
362 if (src && (src->fLeft > 0 || src->fTop > 0 ||
363 src->fRight < SkIntToScalar(bitmap.width()) ||
364 src->fBottom < SkIntToScalar(bitmap.height()))) {
367 rs.prependf("%s ", ss.c_str());
370 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
371 bs.c_str(), rs.c_str());
374 void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
375 const SkPaint* paint) {
377 bitmap.toString(&bs);
379 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
380 bs.c_str(), ms.c_str());
383 void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
384 const SkPaint* paint) {
386 bitmap.toString(&str);
387 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
391 void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
392 const SkPaint& paint) {
394 toString(text, byteLength, paint.getTextEncoding(), &str);
395 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
396 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
399 void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
400 const SkPaint& paint) {
402 toString(text, byteLength, paint.getTextEncoding(), &str);
403 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
404 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
405 SkScalarToFloat(pos[0].fY));
408 void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
409 SkScalar constY, const SkPaint& paint) {
411 toString(text, byteLength, paint.getTextEncoding(), &str);
412 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
413 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
414 SkScalarToFloat(constY));
417 void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
418 const SkMatrix* matrix, const SkPaint& paint) {
420 toString(text, byteLength, paint.getTextEncoding(), &str);
421 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
422 str.c_str(), byteLength);
425 void SkDumpCanvas::drawPicture(SkPicture& picture) {
426 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture,
427 picture.width(), picture.height());
429 this->INHERITED::drawPicture(picture);
431 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
432 picture.width(), picture.height());
435 void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
436 const SkPoint vertices[], const SkPoint texs[],
437 const SkColor colors[], SkXfermode* xmode,
438 const uint16_t indices[], int indexCount,
439 const SkPaint& paint) {
440 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
441 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
442 SkScalarToFloat(vertices[0].fY));
445 void SkDumpCanvas::drawData(const void* data, size_t length) {
446 // this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
447 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
448 SkTMin<size_t>(length, 64), data);
451 void SkDumpCanvas::beginCommentGroup(const char* description) {
452 this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
455 void SkDumpCanvas::addComment(const char* kywd, const char* value) {
456 this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
459 void SkDumpCanvas::endCommentGroup() {
460 this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
463 ///////////////////////////////////////////////////////////////////////////////
464 ///////////////////////////////////////////////////////////////////////////////
466 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
471 static void appendPtr(SkString* str, const void* ptr, const char name[]) {
473 str->appendf(" %s:%p", name, ptr);
477 static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
480 str->appendf(" %s:%p", name, ptr);
484 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
485 const char str[], const SkPaint* p) {
487 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
488 SkASSERT(level >= 0);
489 for (int i = 0; i < level; i++) {
493 tab.append(" "); // tabs are often too wide to be useful
496 msg.printf("%s%s", tab.c_str(), str);
499 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
500 appendFlattenable(&msg, p->getShader(), "shader");
501 appendFlattenable(&msg, p->getXfermode(), "xfermode");
502 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
503 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
504 appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
505 appendFlattenable(&msg, p->getColorFilter(), "filter");
507 if (SkDumpCanvas::kDrawText_Verb == verb) {
508 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
509 appendPtr(&msg, p->getTypeface(), "typeface");
512 if (p->getStyle() != SkPaint::kFill_Style) {
513 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
517 fProc(msg.c_str(), fRefcon);
520 ///////////////////////////////////////////////////////////////////////////////
522 static void dumpToDebugf(const char text[], void*) {
523 SkDebugf("%s\n", text);
526 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}