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
295void 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}
304///////////////////////////////////////////////////////////////////////////////
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
316void 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
322void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
323    SkString str;
324    toString(rect, &str);
325    this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
326}
327
328void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
329    SkString str;
330    toString(rrect, &str);
331    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());
341}
342
343void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
344    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;
352    bitmap.toString(&str);
353    this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
354               SkScalarToFloat(x), SkScalarToFloat(y));
355}
356
357void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
358                                        const SkRect& dst, const SkPaint* paint,
359                                        DrawBitmapRectFlags flags) {
360    SkString bs, rs;
361    bitmap.toString(&bs);
362    toString(dst, &rs);
363    // show the src-rect only if its not everything
364    if (src && (src->fLeft > 0 || src->fTop > 0 ||
365                src->fRight < SkIntToScalar(bitmap.width()) ||
366                src->fBottom < SkIntToScalar(bitmap.height()))) {
367        SkString ss;
368        toString(*src, &ss);
369        rs.prependf("%s ", ss.c_str());
370    }
371
372    this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
373               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;
379    bitmap.toString(&bs);
380    m.toString(&ms);
381    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;
388    bitmap.toString(&str);
389    this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
390               x, y);
391}
392
393void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
394                              const SkPaint& paint) {
395    SkString str;
396    toString(text, byteLength, paint.getTextEncoding(), &str);
397    this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
398               byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
399}
400
401void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
402                                 const SkPaint& paint) {
403    SkString str;
404    toString(text, byteLength, paint.getTextEncoding(), &str);
405    this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
406               str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
407               SkScalarToFloat(pos[0].fY));
408}
409
410void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
411                                  SkScalar constY, const SkPaint& paint) {
412    SkString str;
413    toString(text, byteLength, paint.getTextEncoding(), &str);
414    this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
415               str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
416               SkScalarToFloat(constY));
417}
418
419void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
420                                    const SkMatrix* matrix, const SkPaint& paint) {
421    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
427void 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
435void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
436                                 const SkPaint* paint) {
437    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);
440    fNestLevel += 1;
441    this->INHERITED::onDrawPicture(picture, matrix, paint);
442    fNestLevel -= 1;
443    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);
446}
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) {
453    this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
454               toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
455               SkScalarToFloat(vertices[0].fY));
456}
457
458void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
459                               const SkPoint texCoords[4], SkXfermode* xmode,
460                               const SkPaint& paint) {
461    //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]}\
463              | 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());
476}
477
478void 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,
481               SkTMin<size_t>(length, 64), data);
482}
483
484void 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
496///////////////////////////////////////////////////////////////////////////////
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) {
513        str->appendf(" %s:%p", name, ptr);
514    }
515}
516
517void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
518                          const char str[], const SkPaint* p) {
519    SkString msg, tab;
520    const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
521    SkASSERT(level >= 0);
522    for (int i = 0; i < level; i++) {
523#if 0
524        tab.append("\t");
525#else
526        tab.append("    ");   // tabs are often too wide to be useful
527#endif
528    }
529    msg.printf("%s%s", tab.c_str(), str);
530
531    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");
539
540        if (SkDumpCanvas::kDrawText_Verb == verb) {
541            msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
542            appendPtr(&msg, p->getTypeface(), "typeface");
543        }
544
545        if (p->getStyle() != SkPaint::kFill_Style) {
546            msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
547        }
548    }
549
550    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
561#endif
562