1#include "SkDumpCanvas.h" 2#include "SkPicture.h" 3#include "SkPixelRef.h" 4#include "SkString.h" 5#include <stdarg.h> 6 7// needed just to know that these are all subclassed from SkFlattenable 8#include "SkShader.h" 9#include "SkPathEffect.h" 10#include "SkXfermode.h" 11#include "SkColorFilter.h" 12#include "SkPathEffect.h" 13#include "SkMaskFilter.h" 14 15static void toString(const SkRect& r, SkString* str) { 16 str->printf("[%g,%g %g:%g]", 17 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop), 18 SkScalarToFloat(r.width()), SkScalarToFloat(r.height())); 19} 20 21static void toString(const SkIRect& r, SkString* str) { 22 str->printf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height()); 23} 24 25static void dumpVerbs(const SkPath& path, SkString* str) { 26 SkPath::Iter iter(path, false); 27 SkPoint pts[4]; 28 for (;;) { 29 switch (iter.next(pts)) { 30 case SkPath::kMove_Verb: 31 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY); 32 break; 33 case SkPath::kLine_Verb: 34 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY); 35 break; 36 case SkPath::kQuad_Verb: 37 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY, 38 pts[2].fX, pts[2].fY); 39 break; 40 case SkPath::kCubic_Verb: 41 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY, 42 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY); 43 break; 44 case SkPath::kClose_Verb: 45 str->appendf("X"); 46 break; 47 case SkPath::kDone_Verb: 48 return; 49 } 50 } 51} 52 53static void toString(const SkPath& path, SkString* str) { 54 if (path.isEmpty()) { 55 str->set("path:empty"); 56 } else { 57 toString(path.getBounds(), str); 58#if 1 59 SkString s; 60 dumpVerbs(path, &s); 61 str->append(s.c_str()); 62#endif 63 str->append("]"); 64 str->prepend("path:["); 65 } 66} 67 68static const char* toString(SkRegion::Op op) { 69 static const char* gOpNames[] = { 70 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE" 71 }; 72 return gOpNames[op]; 73} 74 75static void toString(const SkRegion& rgn, SkString* str) { 76 toString(rgn.getBounds(), str); 77 str->prepend("Region:["); 78 str->append("]"); 79 if (rgn.isComplex()) { 80 str->append(".complex"); 81 } 82} 83 84static const char* toString(SkCanvas::VertexMode vm) { 85 static const char* gVMNames[] = { 86 "TRIANGLES", "STRIP", "FAN" 87 }; 88 return gVMNames[vm]; 89} 90 91static const char* toString(SkCanvas::PointMode pm) { 92 static const char* gPMNames[] = { 93 "POINTS", "LINES", "POLYGON" 94 }; 95 return gPMNames[pm]; 96} 97 98static const char* toString(SkBitmap::Config config) { 99 static const char* gConfigNames[] = { 100 "NONE", "A1", "A8", "INDEX8", "565", "4444", "8888", "RLE" 101 }; 102 return gConfigNames[config]; 103} 104 105static void toString(const SkBitmap& bm, SkString* str) { 106 str->printf("bitmap:[%d %d] %s", bm.width(), bm.height(), 107 toString(bm.config())); 108 109 SkPixelRef* pr = bm.pixelRef(); 110 if (NULL == pr) { 111 // show null or the explicit pixel address (rare) 112 str->appendf(" pixels:%p", bm.getPixels()); 113 } else { 114 const char* uri = pr->getURI(); 115 if (uri) { 116 str->appendf(" uri:\"%s\"", uri); 117 } else { 118 str->appendf(" pixelref:%p", pr); 119 } 120 } 121} 122 123static void toString(const void* text, size_t len, SkPaint::TextEncoding enc, 124 SkString* str, const SkPaint& paint, 125 const SkScalar xpos[] = NULL) { 126 switch (enc) { 127 case SkPaint::kUTF8_TextEncoding: 128 str->printf("\"%.*s\"%s", SkMax32(len, 32), text, 129 len > 32 ? "..." : ""); 130 break; 131 case SkPaint::kUTF16_TextEncoding: 132 str->printf("\"%.*S\"%s", SkMax32(len, 32), text, 133 len > 64 ? "..." : ""); 134 break; 135 case SkPaint::kGlyphID_TextEncoding: { 136 const uint16_t* glyphs = (const uint16_t*)text; 137 const int max = 32; 138 SkUnichar uni[max]; 139 int count = SkMin32(len >> 1, max); 140 paint.glyphsToUnichars(glyphs, count, uni); 141 str->append("\""); 142 for (int i = 0; i < count; i++) { 143 str->appendUnichar(uni[i]); 144 } 145 if ((size_t)count < (len >> 1)) { 146 str->append("..."); 147 } 148 str->append("\""); 149 } break; 150 } 151} 152 153/////////////////////////////////////////////////////////////////////////////// 154 155SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : fNestLevel(0) { 156 dumper->safeRef(); 157 fDumper = dumper; 158 159 static const int WIDE_OPEN = 16384; 160 SkBitmap emptyBitmap; 161 162 emptyBitmap.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN); 163 this->setBitmapDevice(emptyBitmap); 164} 165 166SkDumpCanvas::~SkDumpCanvas() { 167 fDumper->safeUnref(); 168} 169 170void SkDumpCanvas::dump(Verb verb, const SkPaint* paint, 171 const char format[], ...) { 172 static const size_t BUFFER_SIZE = 1024; 173 174 char buffer[BUFFER_SIZE]; 175 va_list args; 176 va_start(args, format); 177 vsnprintf(buffer, BUFFER_SIZE, format, args); 178 va_end(args); 179 180 if (fDumper) { 181 fDumper->dump(this, verb, buffer, paint); 182 } 183} 184 185/////////////////////////////////////////////////////////////////////////////// 186 187int SkDumpCanvas::save(SaveFlags flags) { 188 this->dump(kSave_Verb, NULL, "save(0x%X)", flags); 189 return this->INHERITED::save(flags); 190} 191 192int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, 193 SaveFlags flags) { 194 this->dump(kSave_Verb, paint, "saveLayer(0x%X)", flags); 195 return this->INHERITED::saveLayer(bounds, paint, flags); 196} 197 198void SkDumpCanvas::restore() { 199 this->INHERITED::restore(); 200 this->dump(kRestore_Verb, NULL, "restore"); 201} 202 203bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) { 204 this->dump(kMatrix_Verb, NULL, "translate(%g %g)", 205 SkScalarToFloat(dx), SkScalarToFloat(dy)); 206 return this->INHERITED::translate(dx, dy); 207} 208 209bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) { 210 this->dump(kMatrix_Verb, NULL, "scale(%g %g)", 211 SkScalarToFloat(sx), SkScalarToFloat(sy)); 212 return this->INHERITED::scale(sx, sy); 213} 214 215bool SkDumpCanvas::rotate(SkScalar degrees) { 216 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees)); 217 return this->INHERITED::rotate(degrees); 218} 219 220bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) { 221 this->dump(kMatrix_Verb, NULL, "skew(%g %g)", 222 SkScalarToFloat(sx), SkScalarToFloat(sy)); 223 return this->INHERITED::skew(sx, sy); 224} 225 226bool SkDumpCanvas::concat(const SkMatrix& matrix) { 227 SkString str; 228 matrix.toDumpString(&str); 229 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str()); 230 return this->INHERITED::concat(matrix); 231} 232 233void SkDumpCanvas::setMatrix(const SkMatrix& matrix) { 234 SkString str; 235 matrix.toDumpString(&str); 236 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str()); 237 this->INHERITED::setMatrix(matrix); 238} 239 240/////////////////////////////////////////////////////////////////////////////// 241 242bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op) { 243 SkString str; 244 toString(rect, &str); 245 this->dump(kClip_Verb, NULL, "clipRect(%s %s)", str.c_str(), toString(op)); 246 return this->INHERITED::clipRect(rect, op); 247} 248 249bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op) { 250 SkString str; 251 toString(path, &str); 252 this->dump(kClip_Verb, NULL, "clipPath(%s %s)", str.c_str(), toString(op)); 253 return this->INHERITED::clipPath(path, op); 254} 255 256bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { 257 SkString str; 258 toString(deviceRgn, &str); 259 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(), 260 toString(op)); 261 return this->INHERITED::clipRegion(deviceRgn, op); 262} 263 264/////////////////////////////////////////////////////////////////////////////// 265 266void SkDumpCanvas::drawPaint(const SkPaint& paint) { 267 this->dump(kDrawPaint_Verb, &paint, "drawPaint()"); 268} 269 270void SkDumpCanvas::drawPoints(PointMode mode, size_t count, 271 const SkPoint pts[], const SkPaint& paint) { 272 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode), 273 count); 274} 275 276void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { 277 SkString str; 278 toString(rect, &str); 279 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str()); 280} 281 282void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) { 283 SkString str; 284 toString(path, &str); 285 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str()); 286} 287 288void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, 289 const SkPaint* paint) { 290 SkString str; 291 toString(bitmap, &str); 292 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(), 293 SkScalarToFloat(x), SkScalarToFloat(y)); 294} 295 296void SkDumpCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, 297 const SkRect& dst, const SkPaint* paint) { 298 SkString bs, rs; 299 toString(bitmap, &bs); 300 toString(dst, &rs); 301 // show the src-rect only if its not everything 302 if (src && (src->fLeft > 0 || src->fTop > 0 || 303 src->fRight < bitmap.width() || 304 src->fBottom < bitmap.height())) { 305 SkString ss; 306 toString(*src, &ss); 307 rs.prependf("%s ", ss.c_str()); 308 } 309 310 this->dump(kDrawBitmap_Verb, paint, "drawBitmapRect(%s %s)", 311 bs.c_str(), rs.c_str()); 312} 313 314void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, 315 const SkPaint* paint) { 316 SkString bs, ms; 317 toString(bitmap, &bs); 318 m.toDumpString(&ms); 319 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)", 320 bs.c_str(), ms.c_str()); 321} 322 323void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, 324 const SkPaint* paint) { 325 SkString str; 326 toString(bitmap, &str); 327 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(), 328 x, y); 329} 330 331void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x, 332 SkScalar y, const SkPaint& paint) { 333 SkString str; 334 toString(text, byteLength, paint.getTextEncoding(), &str, paint); 335 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(), 336 byteLength, SkScalarToFloat(x), SkScalarToFloat(y)); 337} 338 339void SkDumpCanvas::drawPosText(const void* text, size_t byteLength, 340 const SkPoint pos[], const SkPaint& paint) { 341 SkString str; 342 toString(text, byteLength, paint.getTextEncoding(), &str, paint); 343 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)", 344 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX), 345 SkScalarToFloat(pos[0].fY)); 346} 347 348void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength, 349 const SkScalar xpos[], SkScalar constY, 350 const SkPaint& paint) { 351 SkString str; 352 toString(text, byteLength, paint.getTextEncoding(), &str, paint, xpos); 353 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)", 354 str.c_str(), byteLength, SkScalarToFloat(xpos[0]), 355 SkScalarToFloat(constY)); 356} 357 358void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength, 359 const SkPath& path, const SkMatrix* matrix, 360 const SkPaint& paint) { 361 SkString str; 362 toString(text, byteLength, paint.getTextEncoding(), &str, paint); 363 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])", 364 str.c_str(), byteLength); 365} 366 367void SkDumpCanvas::drawShape(SkShape* shape) { 368 this->dump(kDrawShape_Verb, NULL, "drawShape(%p)", shape); 369 fNestLevel += 1; 370 this->INHERITED::drawShape(shape); 371 fNestLevel -= 1; 372 this->dump(kDrawShape_Verb, NULL, "endShape(%p)", shape); 373} 374 375void SkDumpCanvas::drawPicture(SkPicture& picture) { 376 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture, 377 picture.width(), picture.height()); 378 fNestLevel += 1; 379 this->INHERITED::drawPicture(picture); 380 fNestLevel -= 1; 381 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture, 382 picture.width(), picture.height()); 383} 384 385void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount, 386 const SkPoint vertices[], const SkPoint texs[], 387 const SkColor colors[], SkXfermode* xmode, 388 const uint16_t indices[], int indexCount, 389 const SkPaint& paint) { 390 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)", 391 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX), 392 SkScalarToFloat(vertices[0].fY)); 393} 394 395void SkDumpCanvas::drawData(const void* data, size_t length) { 396// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length); 397 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length, 398 SkMin32(length, 64), data); 399} 400 401/////////////////////////////////////////////////////////////////////////////// 402/////////////////////////////////////////////////////////////////////////////// 403 404SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) { 405 fProc = proc; 406 fRefcon = refcon; 407} 408 409static void appendPtr(SkString* str, const void* ptr, const char name[]) { 410 if (ptr) { 411 str->appendf(" %s:%p", name, ptr); 412 } 413} 414 415static void appendFlattenable(SkString* str, const SkFlattenable* ptr, 416 const char name[]) { 417 if (ptr) { 418 SkString info; 419 if (ptr->toDumpString(&info)) { 420 str->appendf(" %s", info.c_str()); 421 } else { 422 str->appendf(" %s:%p", name, ptr); 423 } 424 } 425} 426 427void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb, 428 const char str[], const SkPaint* p) { 429 SkString msg, tab; 430 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1; 431 SkASSERT(level >= 0); 432 for (int i = 0; i < level; i++) { 433 tab.append("\t"); 434 } 435 msg.printf("%s%s", tab.c_str(), str); 436 437 if (p) { 438 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags()); 439 appendFlattenable(&msg, p->getShader(), "shader"); 440 appendFlattenable(&msg, p->getXfermode(), "xfermode"); 441 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 442 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter"); 443 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 444 appendFlattenable(&msg, p->getColorFilter(), "filter"); 445 446 if (SkDumpCanvas::kDrawText_Verb == verb) { 447 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize())); 448 appendPtr(&msg, p->getTypeface(), "typeface"); 449 } 450 } 451 452 fProc(msg.c_str(), fRefcon); 453} 454 455/////////////////////////////////////////////////////////////////////////////// 456 457static void dumpToDebugf(const char text[], void*) { 458 SkDebugf("%s\n", text); 459} 460 461SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {} 462 463 464