1
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 */
8
9#include "SkDumpCanvas.h"
10
11#ifdef SK_DEVELOPER
12#include "SkPatchUtils.h"
13#include "SkPicture.h"
14#include "SkPixelRef.h"
15#include "SkRRect.h"
16#include "SkString.h"
17#include "SkTextBlob.h"
18#include <stdarg.h>
19#include <stdio.h>
20
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) {
30    str->appendf("[%g,%g %g:%g]",
31                 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
32                 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
33}
34
35static void toString(const SkIRect& r, SkString* str) {
36    str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
37}
38
39static 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
64static 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
95static 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
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) {
118    str->append("Region:[");
119    toString(rgn.getBounds(), str);
120    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
140static 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
170SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) {
171    fNestLevel = 0;
172    SkSafeRef(dumper);
173    fDumper = dumper;
174}
175
176SkDumpCanvas::~SkDumpCanvas() {
177    SkSafeUnref(fDumper);
178}
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);
189
190    if (fDumper) {
191        fDumper->dump(this, verb, buffer, paint);
192    }
193}
194
195///////////////////////////////////////////////////////////////////////////////
196
197void SkDumpCanvas::willSave() {
198    this->dump(kSave_Verb, NULL, "save()");
199    this->INHERITED::willSave();
200}
201
202SkCanvas::SaveLayerStrategy SkDumpCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
203                                                        SaveFlags flags) {
204    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());
219    return this->INHERITED::willSaveLayer(bounds, paint, flags);
220}
221
222void SkDumpCanvas::willRestore() {
223    this->dump(kRestore_Verb, NULL, "restore");
224    this->INHERITED::willRestore();
225}
226
227void SkDumpCanvas::didConcat(const SkMatrix& matrix) {
228    SkString str;
229
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
247    this->INHERITED::didConcat(matrix);
248}
249
250void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) {
251    SkString str;
252    matrix.toString(&str);
253    this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
254    this->INHERITED::didSetMatrix(matrix);
255}
256
257///////////////////////////////////////////////////////////////////////////////
258
259const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) {
260    return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW";
261}
262
263void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
264    SkString str;
265    toString(rect, &str);
266    this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
267               EdgeStyleToAAString(edgeStyle));
268    this->INHERITED::onClipRect(rect, op, edgeStyle);
269}
270
271void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
272    SkString str;
273    toString(rrect, &str);
274    this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
275               EdgeStyleToAAString(edgeStyle));
276    this->INHERITED::onClipRRect(rrect, op, edgeStyle);
277}
278
279void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
280    SkString str;
281    toString(path, &str);
282    this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
283               EdgeStyleToAAString(edgeStyle));
284    this->INHERITED::onClipPath(path, op, edgeStyle);
285}
286
287void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
288    SkString str;
289    toString(deviceRgn, &str);
290    this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
291               toString(op));
292    this->INHERITED::onClipRegion(deviceRgn, op);
293}
294
295///////////////////////////////////////////////////////////////////////////////
296
297void SkDumpCanvas::onDrawPaint(const SkPaint& paint) {
298    this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
299}
300
301void SkDumpCanvas::onDrawPoints(PointMode mode, size_t count,
302                               const SkPoint pts[], const SkPaint& paint) {
303    this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
304               count);
305}
306
307void SkDumpCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
308    SkString str;
309    toString(rect, &str);
310    this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
311}
312
313void SkDumpCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
314    SkString str;
315    toString(rect, &str);
316    this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
317}
318
319void SkDumpCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
320    SkString str;
321    toString(rrect, &str);
322    this->dump(kDrawDRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
323}
324
325void SkDumpCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
326                                const SkPaint& paint) {
327    SkString str0, str1;
328    toString(outer, &str0);
329    toString(inner, &str0);
330    this->dump(kDrawRRect_Verb, &paint, "drawDRRect(%s,%s)",
331               str0.c_str(), str1.c_str());
332}
333
334void SkDumpCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
335    SkString str;
336    toString(path, &str);
337    this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
338}
339
340void SkDumpCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
341                                const SkPaint* paint) {
342    SkString str;
343    bitmap.toString(&str);
344    this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
345               SkScalarToFloat(x), SkScalarToFloat(y));
346}
347
348void SkDumpCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
349                                    const SkPaint* paint, DrawBitmapRectFlags flags) {
350    SkString bs, rs;
351    bitmap.toString(&bs);
352    toString(dst, &rs);
353    // show the src-rect only if its not everything
354    if (src && (src->fLeft > 0 || src->fTop > 0 ||
355                src->fRight < SkIntToScalar(bitmap.width()) ||
356                src->fBottom < SkIntToScalar(bitmap.height()))) {
357        SkString ss;
358        toString(*src, &ss);
359        rs.prependf("%s ", ss.c_str());
360    }
361
362    this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
363               bs.c_str(), rs.c_str());
364}
365
366void SkDumpCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
367                                    const SkRect& dst, const SkPaint* paint) {
368    SkString str, centerStr, dstStr;
369    bitmap.toString(&str);
370    toString(center, &centerStr);
371    toString(dst, &dstStr);
372    this->dump(kDrawBitmap_Verb, paint, "drawBitmapNine(%s %s %s)", str.c_str(),
373               centerStr.c_str(), dstStr.c_str());
374}
375
376void SkDumpCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
377    SkString str;
378    image->toString(&str);
379    this->dump(kDrawBitmap_Verb, paint, "drawImage(%s %g %g)", str.c_str(),
380               SkScalarToFloat(x), SkScalarToFloat(y));
381}
382
383void SkDumpCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
384                                   const SkPaint* paint) {
385    SkString bs, rs;
386    image->toString(&bs);
387    toString(dst, &rs);
388    // show the src-rect only if its not everything
389    if (src && (src->fLeft > 0 || src->fTop > 0 ||
390                src->fRight < SkIntToScalar(image->width()) ||
391                src->fBottom < SkIntToScalar(image->height()))) {
392        SkString ss;
393        toString(*src, &ss);
394        rs.prependf("%s ", ss.c_str());
395    }
396
397    this->dump(kDrawBitmap_Verb, paint, "drawImageRectToRect(%s %s)",
398               bs.c_str(), rs.c_str());
399}
400
401void SkDumpCanvas::onDrawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint* paint) {
402    SkString str;
403    bitmap.toString(&str);
404    this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
405               x, y);
406}
407
408void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
409                              const SkPaint& paint) {
410    SkString str;
411    toString(text, byteLength, paint.getTextEncoding(), &str);
412    this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
413               byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
414}
415
416void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
417                                 const SkPaint& paint) {
418    SkString str;
419    toString(text, byteLength, paint.getTextEncoding(), &str);
420    this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
421               str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
422               SkScalarToFloat(pos[0].fY));
423}
424
425void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
426                                  SkScalar constY, const SkPaint& paint) {
427    SkString str;
428    toString(text, byteLength, paint.getTextEncoding(), &str);
429    this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
430               str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
431               SkScalarToFloat(constY));
432}
433
434void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
435                                    const SkMatrix* matrix, const SkPaint& paint) {
436    SkString str;
437    toString(text, byteLength, paint.getTextEncoding(), &str);
438    this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
439               str.c_str(), byteLength);
440}
441
442void SkDumpCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
443                                  const SkPaint& paint) {
444    SkString str;
445    toString(blob->bounds(), &str);
446    this->dump(kDrawText_Verb, &paint, "drawTextBlob(%p) [%s]", blob, str.c_str());
447    // FIXME: dump the actual blob content?
448}
449
450void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
451                                 const SkPaint* paint) {
452    this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %f:%f:%f:%f", picture,
453               picture->cullRect().fLeft, picture->cullRect().fTop,
454               picture->cullRect().fRight, picture->cullRect().fBottom);
455    fNestLevel += 1;
456    this->INHERITED::onDrawPicture(picture, matrix, paint);
457    fNestLevel -= 1;
458    this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %f:%f:%f:%f", &picture,
459               picture->cullRect().fLeft, picture->cullRect().fTop,
460               picture->cullRect().fRight, picture->cullRect().fBottom);
461}
462
463void SkDumpCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
464                                  const SkPoint vertices[], const SkPoint texs[],
465                                  const SkColor colors[], SkXfermode* xmode,
466                                  const uint16_t indices[], int indexCount,
467                                  const SkPaint& paint) {
468    this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
469               toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
470               SkScalarToFloat(vertices[0].fY));
471}
472
473void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
474                               const SkPoint texCoords[4], SkXfermode* xmode,
475                               const SkPaint& paint) {
476    //dumps corner points and colors in clockwise order starting on upper-left corner
477    this->dump(kDrawPatch_Verb, &paint, "drawPatch(Vertices{[%f, %f], [%f, %f], [%f, %f], [%f, %f]}\
478              | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%f,%f], \
479               [%f,%f]})",
480              cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fX,
481              cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fY,
482              cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fX,
483              cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fY,
484              cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fX,
485              cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fY,
486              cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fX,
487              cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fY,
488              colors[0], colors[1], colors[2], colors[3],
489              texCoords[0].x(), texCoords[0].y(), texCoords[1].x(), texCoords[1].y(),
490              texCoords[2].x(), texCoords[2].y(), texCoords[3].x(), texCoords[3].y());
491}
492
493void SkDumpCanvas::beginCommentGroup(const char* description) {
494    this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
495}
496
497void SkDumpCanvas::addComment(const char* kywd, const char* value) {
498    this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
499}
500
501void SkDumpCanvas::endCommentGroup() {
502    this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
503}
504
505///////////////////////////////////////////////////////////////////////////////
506///////////////////////////////////////////////////////////////////////////////
507
508SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
509    fProc = proc;
510    fRefcon = refcon;
511}
512
513static void appendPtr(SkString* str, const void* ptr, const char name[]) {
514    if (ptr) {
515        str->appendf(" %s:%p", name, ptr);
516    }
517}
518
519static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
520                              const char name[]) {
521    if (ptr) {
522        str->appendf(" %s:%p", name, ptr);
523    }
524}
525
526void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
527                          const char str[], const SkPaint* p) {
528    SkString msg, tab;
529    const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
530    SkASSERT(level >= 0);
531    for (int i = 0; i < level; i++) {
532#if 0
533        tab.append("\t");
534#else
535        tab.append("    ");   // tabs are often too wide to be useful
536#endif
537    }
538    msg.printf("%s%s", tab.c_str(), str);
539
540    if (p) {
541        msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
542        appendFlattenable(&msg, p->getShader(), "shader");
543        appendFlattenable(&msg, p->getXfermode(), "xfermode");
544        appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
545        appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
546        appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
547        appendFlattenable(&msg, p->getColorFilter(), "filter");
548
549        if (SkDumpCanvas::kDrawText_Verb == verb) {
550            msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
551            appendPtr(&msg, p->getTypeface(), "typeface");
552        }
553
554        if (p->getStyle() != SkPaint::kFill_Style) {
555            msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
556        }
557    }
558
559    fProc(msg.c_str(), fRefcon);
560}
561
562///////////////////////////////////////////////////////////////////////////////
563
564static void dumpToDebugf(const char text[], void*) {
565    SkDebugf("%s\n", text);
566}
567
568SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
569
570#endif
571