Home | History | Annotate | Download | only in utils
      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 "SkPicture.h"
     13 #include "SkPixelRef.h"
     14 #include "SkRRect.h"
     15 #include "SkString.h"
     16 #include <stdarg.h>
     17 #include <stdio.h>
     18 
     19 // needed just to know that these are all subclassed from SkFlattenable
     20 #include "SkShader.h"
     21 #include "SkPathEffect.h"
     22 #include "SkXfermode.h"
     23 #include "SkColorFilter.h"
     24 #include "SkPathEffect.h"
     25 #include "SkMaskFilter.h"
     26 
     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()));
     31 }
     32 
     33 static void toString(const SkIRect& r, SkString* str) {
     34     str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
     35 }
     36 
     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()));
     42     if (rrect.isOval()) {
     43         str->append("()");
     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()) {
     48         SkVector radii[4] = {
     49             rrect.radii(SkRRect::kUpperLeft_Corner),
     50             rrect.radii(SkRRect::kUpperRight_Corner),
     51             rrect.radii(SkRRect::kLowerRight_Corner),
     52             rrect.radii(SkRRect::kLowerLeft_Corner),
     53         };
     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());
     59     }
     60 }
     61 
     62 static void dumpVerbs(const SkPath& path, SkString* str) {
     63     SkPath::Iter iter(path, false);
     64     SkPoint pts[4];
     65     for (;;) {
     66         switch (iter.next(pts, false)) {
     67             case SkPath::kMove_Verb:
     68                 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY);
     69                 break;
     70             case SkPath::kLine_Verb:
     71                 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY);
     72                 break;
     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);
     76                 break;
     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);
     80                 break;
     81             case SkPath::kClose_Verb:
     82                 str->append("X");
     83                 break;
     84             case SkPath::kDone_Verb:
     85                 return;
     86             case SkPath::kConic_Verb:
     87                 SkASSERT(0);
     88                 break;
     89         }
     90     }
     91 }
     92 
     93 static void toString(const SkPath& path, SkString* str) {
     94     if (path.isEmpty()) {
     95         str->append("path:empty");
     96     } else {
     97         toString(path.getBounds(), str);
     98 #if 1
     99         SkString s;
    100         dumpVerbs(path, &s);
    101         str->append(s.c_str());
    102 #endif
    103         str->append("]");
    104         str->prepend("path:[");
    105     }
    106 }
    107 
    108 static const char* toString(SkRegion::Op op) {
    109     static const char* gOpNames[] = {
    110         "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE"
    111     };
    112     return gOpNames[op];
    113 }
    114 
    115 static void toString(const SkRegion& rgn, SkString* str) {
    116     str->append("Region:[");
    117     toString(rgn.getBounds(), str);
    118     str->append("]");
    119     if (rgn.isComplex()) {
    120         str->append(".complex");
    121     }
    122 }
    123 
    124 static const char* toString(SkCanvas::VertexMode vm) {
    125     static const char* gVMNames[] = {
    126         "TRIANGLES", "STRIP", "FAN"
    127     };
    128     return gVMNames[vm];
    129 }
    130 
    131 static const char* toString(SkCanvas::PointMode pm) {
    132     static const char* gPMNames[] = {
    133         "POINTS", "LINES", "POLYGON"
    134     };
    135     return gPMNames[pm];
    136 }
    137 
    138 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc,
    139                      SkString* str) {
    140     // FIXME: this code appears to be untested - and probably unused - and probably wrong
    141     switch (enc) {
    142         case SkPaint::kUTF8_TextEncoding:
    143             str->appendf("\"%.*s\"%s", (int)SkTMax<size_t>(byteLen, 32), (const char*) text,
    144                         byteLen > 32 ? "..." : "");
    145             break;
    146         case SkPaint::kUTF16_TextEncoding:
    147             str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
    148                         byteLen > 64 ? "..." : "");
    149             break;
    150         case SkPaint::kUTF32_TextEncoding:
    151             str->appendf("\"%.*ls\"%s", (int)SkTMax<size_t>(byteLen, 32), (const wchar_t*) text,
    152                         byteLen > 128 ? "..." : "");
    153             break;
    154         case SkPaint::kGlyphID_TextEncoding:
    155             str->append("<glyphs>");
    156             break;
    157 
    158         default:
    159             SkASSERT(false);
    160             break;
    161     }
    162 }
    163 
    164 ///////////////////////////////////////////////////////////////////////////////
    165 
    166 static SkBitmap make_wideopen_bm() {
    167     static const int WIDE_OPEN = 16384;
    168 
    169     SkBitmap bm;
    170     bm.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN);
    171     return bm;
    172 }
    173 
    174 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(make_wideopen_bm()) {
    175     fNestLevel = 0;
    176     SkSafeRef(dumper);
    177     fDumper = dumper;
    178 }
    179 
    180 SkDumpCanvas::~SkDumpCanvas() {
    181     SkSafeUnref(fDumper);
    182 }
    183 
    184 void SkDumpCanvas::dump(Verb verb, const SkPaint* paint,
    185                         const char format[], ...) {
    186     static const size_t BUFFER_SIZE = 1024;
    187 
    188     char    buffer[BUFFER_SIZE];
    189     va_list args;
    190     va_start(args, format);
    191     vsnprintf(buffer, BUFFER_SIZE, format, args);
    192     va_end(args);
    193 
    194     if (fDumper) {
    195         fDumper->dump(this, verb, buffer, paint);
    196     }
    197 }
    198 
    199 ///////////////////////////////////////////////////////////////////////////////
    200 
    201 int SkDumpCanvas::save(SaveFlags flags) {
    202     this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
    203     return this->INHERITED::save(flags);
    204 }
    205 
    206 int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
    207                              SaveFlags flags) {
    208     SkString str;
    209     str.printf("saveLayer(0x%X)", flags);
    210     if (bounds) {
    211         str.append(" bounds");
    212         toString(*bounds, &str);
    213     }
    214     if (paint) {
    215         if (paint->getAlpha() != 0xFF) {
    216             str.appendf(" alpha:0x%02X", paint->getAlpha());
    217         }
    218         if (paint->getXfermode()) {
    219             str.appendf(" xfermode:%p", paint->getXfermode());
    220         }
    221     }
    222     this->dump(kSave_Verb, paint, str.c_str());
    223     return this->INHERITED::saveLayer(bounds, paint, flags);
    224 }
    225 
    226 void SkDumpCanvas::restore() {
    227     this->INHERITED::restore();
    228     this->dump(kRestore_Verb, NULL, "restore");
    229 }
    230 
    231 bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) {
    232     this->dump(kMatrix_Verb, NULL, "translate(%g %g)",
    233                SkScalarToFloat(dx), SkScalarToFloat(dy));
    234     return this->INHERITED::translate(dx, dy);
    235 }
    236 
    237 bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) {
    238     this->dump(kMatrix_Verb, NULL, "scale(%g %g)",
    239                SkScalarToFloat(sx), SkScalarToFloat(sy));
    240     return this->INHERITED::scale(sx, sy);
    241 }
    242 
    243 bool SkDumpCanvas::rotate(SkScalar degrees) {
    244     this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees));
    245     return this->INHERITED::rotate(degrees);
    246 }
    247 
    248 bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) {
    249     this->dump(kMatrix_Verb, NULL, "skew(%g %g)",
    250                SkScalarToFloat(sx), SkScalarToFloat(sy));
    251     return this->INHERITED::skew(sx, sy);
    252 }
    253 
    254 bool SkDumpCanvas::concat(const SkMatrix& matrix) {
    255     SkString str;
    256     matrix.toString(&str);
    257     this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str());
    258     return this->INHERITED::concat(matrix);
    259 }
    260 
    261 void SkDumpCanvas::setMatrix(const SkMatrix& matrix) {
    262     SkString str;
    263     matrix.toString(&str);
    264     this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str());
    265     this->INHERITED::setMatrix(matrix);
    266 }
    267 
    268 ///////////////////////////////////////////////////////////////////////////////
    269 
    270 static const char* bool_to_aastring(bool doAA) {
    271     return doAA ? "AA" : "BW";
    272 }
    273 
    274 bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
    275     SkString str;
    276     toString(rect, &str);
    277     this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op),
    278                bool_to_aastring(doAA));
    279     return this->INHERITED::clipRect(rect, op, doAA);
    280 }
    281 
    282 bool SkDumpCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
    283     SkString str;
    284     toString(rrect, &str);
    285     this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op),
    286                bool_to_aastring(doAA));
    287     return this->INHERITED::clipRRect(rrect, op, doAA);
    288 }
    289 
    290 bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
    291     SkString str;
    292     toString(path, &str);
    293     this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op),
    294                bool_to_aastring(doAA));
    295     return this->INHERITED::clipPath(path, op, doAA);
    296 }
    297 
    298 bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
    299     SkString str;
    300     toString(deviceRgn, &str);
    301     this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(),
    302                toString(op));
    303     return this->INHERITED::clipRegion(deviceRgn, op);
    304 }
    305 
    306 ///////////////////////////////////////////////////////////////////////////////
    307 
    308 void SkDumpCanvas::drawPaint(const SkPaint& paint) {
    309     this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
    310 }
    311 
    312 void SkDumpCanvas::drawPoints(PointMode mode, size_t count,
    313                                const SkPoint pts[], const SkPaint& paint) {
    314     this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode),
    315                count);
    316 }
    317 
    318 void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
    319     SkString str;
    320     toString(rect, &str);
    321     this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str());
    322 }
    323 
    324 void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
    325     SkString str;
    326     toString(rect, &str);
    327     this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str());
    328 }
    329 
    330 void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
    331     SkString str;
    332     toString(rrect, &str);
    333     this->dump(kDrawRRect_Verb, &paint, "drawRRect(%s)", str.c_str());
    334 }
    335 
    336 void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
    337     SkString str;
    338     toString(path, &str);
    339     this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str());
    340 }
    341 
    342 void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
    343                                const SkPaint* paint) {
    344     SkString str;
    345     bitmap.toString(&str);
    346     this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
    347                SkScalarToFloat(x), SkScalarToFloat(y));
    348 }
    349 
    350 void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
    351                                         const SkRect& dst, const SkPaint* paint,
    352                                         DrawBitmapRectFlags flags) {
    353     SkString bs, rs;
    354     bitmap.toString(&bs);
    355     toString(dst, &rs);
    356     // show the src-rect only if its not everything
    357     if (src && (src->fLeft > 0 || src->fTop > 0 ||
    358                 src->fRight < SkIntToScalar(bitmap.width()) ||
    359                 src->fBottom < SkIntToScalar(bitmap.height()))) {
    360         SkString ss;
    361         toString(*src, &ss);
    362         rs.prependf("%s ", ss.c_str());
    363     }
    364 
    365     this->dump(kDrawBitmap_Verb, paint, "drawBitmapRectToRect(%s %s)",
    366                bs.c_str(), rs.c_str());
    367 }
    368 
    369 void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
    370                                      const SkPaint* paint) {
    371     SkString bs, ms;
    372     bitmap.toString(&bs);
    373     m.toString(&ms);
    374     this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)",
    375                bs.c_str(), ms.c_str());
    376 }
    377 
    378 void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
    379                                const SkPaint* paint) {
    380     SkString str;
    381     bitmap.toString(&str);
    382     this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
    383                x, y);
    384 }
    385 
    386 void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
    387                              SkScalar y, const SkPaint& paint) {
    388     SkString str;
    389     toString(text, byteLength, paint.getTextEncoding(), &str);
    390     this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
    391                byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
    392 }
    393 
    394 void SkDumpCanvas::drawPosText(const void* text, size_t byteLength,
    395                                 const SkPoint pos[], const SkPaint& paint) {
    396     SkString str;
    397     toString(text, byteLength, paint.getTextEncoding(), &str);
    398     this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
    399                str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
    400                SkScalarToFloat(pos[0].fY));
    401 }
    402 
    403 void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength,
    404                                  const SkScalar xpos[], SkScalar constY,
    405                                  const SkPaint& paint) {
    406     SkString str;
    407     toString(text, byteLength, paint.getTextEncoding(), &str);
    408     this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
    409                str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
    410                SkScalarToFloat(constY));
    411 }
    412 
    413 void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength,
    414                                    const SkPath& path, const SkMatrix* matrix,
    415                                    const SkPaint& paint) {
    416     SkString str;
    417     toString(text, byteLength, paint.getTextEncoding(), &str);
    418     this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])",
    419                str.c_str(), byteLength);
    420 }
    421 
    422 void SkDumpCanvas::drawPicture(SkPicture& picture) {
    423     this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture,
    424                picture.width(), picture.height());
    425     fNestLevel += 1;
    426     this->INHERITED::drawPicture(picture);
    427     fNestLevel -= 1;
    428     this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture,
    429                picture.width(), picture.height());
    430 }
    431 
    432 void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount,
    433                                  const SkPoint vertices[], const SkPoint texs[],
    434                                  const SkColor colors[], SkXfermode* xmode,
    435                                  const uint16_t indices[], int indexCount,
    436                                  const SkPaint& paint) {
    437     this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
    438                toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
    439                SkScalarToFloat(vertices[0].fY));
    440 }
    441 
    442 void SkDumpCanvas::drawData(const void* data, size_t length) {
    443 //    this->dump(kDrawData_Verb, NULL, "drawData(%d)", length);
    444     this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length,
    445                SkTMin<size_t>(length, 64), data);
    446 }
    447 
    448 void SkDumpCanvas::beginCommentGroup(const char* description) {
    449     this->dump(kBeginCommentGroup_Verb, NULL, "beginCommentGroup(%s)", description);
    450 }
    451 
    452 void SkDumpCanvas::addComment(const char* kywd, const char* value) {
    453     this->dump(kAddComment_Verb, NULL, "addComment(%s, %s)", kywd, value);
    454 }
    455 
    456 void SkDumpCanvas::endCommentGroup() {
    457     this->dump(kEndCommentGroup_Verb, NULL, "endCommentGroup()");
    458 }
    459 
    460 ///////////////////////////////////////////////////////////////////////////////
    461 ///////////////////////////////////////////////////////////////////////////////
    462 
    463 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
    464     fProc = proc;
    465     fRefcon = refcon;
    466 }
    467 
    468 static void appendPtr(SkString* str, const void* ptr, const char name[]) {
    469     if (ptr) {
    470         str->appendf(" %s:%p", name, ptr);
    471     }
    472 }
    473 
    474 static void appendFlattenable(SkString* str, const SkFlattenable* ptr,
    475                               const char name[]) {
    476     if (ptr) {
    477         str->appendf(" %s:%p", name, ptr);
    478     }
    479 }
    480 
    481 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb,
    482                           const char str[], const SkPaint* p) {
    483     SkString msg, tab;
    484     const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1;
    485     SkASSERT(level >= 0);
    486     for (int i = 0; i < level; i++) {
    487 #if 0
    488         tab.append("\t");
    489 #else
    490         tab.append("    ");   // tabs are often too wide to be useful
    491 #endif
    492     }
    493     msg.printf("%s%s", tab.c_str(), str);
    494 
    495     if (p) {
    496         msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags());
    497         appendFlattenable(&msg, p->getShader(), "shader");
    498         appendFlattenable(&msg, p->getXfermode(), "xfermode");
    499         appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
    500         appendFlattenable(&msg, p->getMaskFilter(), "maskFilter");
    501         appendFlattenable(&msg, p->getPathEffect(), "pathEffect");
    502         appendFlattenable(&msg, p->getColorFilter(), "filter");
    503 
    504         if (SkDumpCanvas::kDrawText_Verb == verb) {
    505             msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize()));
    506             appendPtr(&msg, p->getTypeface(), "typeface");
    507         }
    508 
    509         if (p->getStyle() != SkPaint::kFill_Style) {
    510             msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth()));
    511         }
    512     }
    513 
    514     fProc(msg.c_str(), fRefcon);
    515 }
    516 
    517 ///////////////////////////////////////////////////////////////////////////////
    518 
    519 static void dumpToDebugf(const char text[], void*) {
    520     SkDebugf("%s\n", text);
    521 }
    522 
    523 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
    524 
    525 #endif
    526