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 18 // needed just to know that these are all subclassed from SkFlattenable 19 #include "SkShader.h" 20 #include "SkPathEffect.h" 21 #include "SkXfermode.h" 22 #include "SkColorFilter.h" 23 #include "SkPathEffect.h" 24 #include "SkMaskFilter.h" 25 26 SK_DEFINE_INST_COUNT(SkDumpCanvas::Dumper) 27 28 static void toString(const SkRect& r, SkString* str) { 29 str->appendf("[%g,%g %g:%g]", 30 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop), 31 SkScalarToFloat(r.width()), SkScalarToFloat(r.height())); 32 } 33 34 static void toString(const SkIRect& r, SkString* str) { 35 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height()); 36 } 37 38 static void toString(const SkRRect& rrect, SkString* str) { 39 SkRect r = rrect.getBounds(); 40 str->appendf("[%g,%g %g:%g]", 41 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop), 42 SkScalarToFloat(r.width()), SkScalarToFloat(r.height())); 43 if (rrect.isOval()) { 44 str->append("()"); 45 } else if (rrect.isSimple()) { 46 const SkVector& rad = rrect.getSimpleRadii(); 47 str->appendf("(%g,%g)", rad.x(), rad.y()); 48 } else if (rrect.isComplex()) { 49 SkVector radii[4] = { 50 rrect.radii(SkRRect::kUpperLeft_Corner), 51 rrect.radii(SkRRect::kUpperRight_Corner), 52 rrect.radii(SkRRect::kLowerRight_Corner), 53 rrect.radii(SkRRect::kLowerLeft_Corner), 54 }; 55 str->appendf("(%g,%g %g,%g %g,%g %g,%g)", 56 radii[0].x(), radii[0].y(), 57 radii[1].x(), radii[1].y(), 58 radii[2].x(), radii[2].y(), 59 radii[3].x(), radii[3].y()); 60 } 61 } 62 63 static void dumpVerbs(const SkPath& path, SkString* str) { 64 SkPath::Iter iter(path, false); 65 SkPoint pts[4]; 66 for (;;) { 67 switch (iter.next(pts, false)) { 68 case SkPath::kMove_Verb: 69 str->appendf(" M%g,%g", pts[0].fX, pts[0].fY); 70 break; 71 case SkPath::kLine_Verb: 72 str->appendf(" L%g,%g", pts[0].fX, pts[0].fY); 73 break; 74 case SkPath::kQuad_Verb: 75 str->appendf(" Q%g,%g,%g,%g", pts[1].fX, pts[1].fY, 76 pts[2].fX, pts[2].fY); 77 break; 78 case SkPath::kCubic_Verb: 79 str->appendf(" C%g,%g,%g,%g,%g,%g", pts[1].fX, pts[1].fY, 80 pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY); 81 break; 82 case SkPath::kClose_Verb: 83 str->append("X"); 84 break; 85 case SkPath::kDone_Verb: 86 return; 87 case SkPath::kConic_Verb: 88 SkASSERT(0); 89 break; 90 } 91 } 92 } 93 94 static void toString(const SkPath& path, SkString* str) { 95 if (path.isEmpty()) { 96 str->append("path:empty"); 97 } else { 98 toString(path.getBounds(), str); 99 #if 1 100 SkString s; 101 dumpVerbs(path, &s); 102 str->append(s.c_str()); 103 #endif 104 str->append("]"); 105 str->prepend("path:["); 106 } 107 } 108 109 static const char* toString(SkRegion::Op op) { 110 static const char* gOpNames[] = { 111 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE" 112 }; 113 return gOpNames[op]; 114 } 115 116 static void toString(const SkRegion& rgn, SkString* str) { 117 str->append("Region:["); 118 toString(rgn.getBounds(), str); 119 str->append("]"); 120 if (rgn.isComplex()) { 121 str->append(".complex"); 122 } 123 } 124 125 static const char* toString(SkCanvas::VertexMode vm) { 126 static const char* gVMNames[] = { 127 "TRIANGLES", "STRIP", "FAN" 128 }; 129 return gVMNames[vm]; 130 } 131 132 static const char* toString(SkCanvas::PointMode pm) { 133 static const char* gPMNames[] = { 134 "POINTS", "LINES", "POLYGON" 135 }; 136 return gPMNames[pm]; 137 } 138 139 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc, 140 SkString* str) { 141 // FIXME: this code appears to be untested - and probably unused - and probably wrong 142 switch (enc) { 143 case SkPaint::kUTF8_TextEncoding: 144 str->appendf("\"%.*s\"%s", SkMax32(byteLen, 32), (const char*) text, 145 byteLen > 32 ? "..." : ""); 146 break; 147 case SkPaint::kUTF16_TextEncoding: 148 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text, 149 byteLen > 64 ? "..." : ""); 150 break; 151 case SkPaint::kUTF32_TextEncoding: 152 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text, 153 byteLen > 128 ? "..." : ""); 154 break; 155 case SkPaint::kGlyphID_TextEncoding: 156 str->append("<glyphs>"); 157 break; 158 159 default: 160 SkASSERT(false); 161 break; 162 } 163 } 164 165 /////////////////////////////////////////////////////////////////////////////// 166 167 static SkBitmap make_wideopen_bm() { 168 static const int WIDE_OPEN = 16384; 169 170 SkBitmap bm; 171 bm.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN); 172 return bm; 173 } 174 175 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(make_wideopen_bm()) { 176 fNestLevel = 0; 177 SkSafeRef(dumper); 178 fDumper = dumper; 179 } 180 181 SkDumpCanvas::~SkDumpCanvas() { 182 SkSafeUnref(fDumper); 183 } 184 185 void SkDumpCanvas::dump(Verb verb, const SkPaint* paint, 186 const char format[], ...) { 187 static const size_t BUFFER_SIZE = 1024; 188 189 char buffer[BUFFER_SIZE]; 190 va_list args; 191 va_start(args, format); 192 vsnprintf(buffer, BUFFER_SIZE, format, args); 193 va_end(args); 194 195 if (fDumper) { 196 fDumper->dump(this, verb, buffer, paint); 197 } 198 } 199 200 /////////////////////////////////////////////////////////////////////////////// 201 202 int SkDumpCanvas::save(SaveFlags flags) { 203 this->dump(kSave_Verb, NULL, "save(0x%X)", flags); 204 return this->INHERITED::save(flags); 205 } 206 207 int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, 208 SaveFlags flags) { 209 SkString str; 210 str.printf("saveLayer(0x%X)", flags); 211 if (bounds) { 212 str.append(" bounds"); 213 toString(*bounds, &str); 214 } 215 if (paint) { 216 if (paint->getAlpha() != 0xFF) { 217 str.appendf(" alpha:0x%02X", paint->getAlpha()); 218 } 219 if (paint->getXfermode()) { 220 str.appendf(" xfermode:%p", paint->getXfermode()); 221 } 222 } 223 this->dump(kSave_Verb, paint, str.c_str()); 224 return this->INHERITED::saveLayer(bounds, paint, flags); 225 } 226 227 void SkDumpCanvas::restore() { 228 this->INHERITED::restore(); 229 this->dump(kRestore_Verb, NULL, "restore"); 230 } 231 232 bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) { 233 this->dump(kMatrix_Verb, NULL, "translate(%g %g)", 234 SkScalarToFloat(dx), SkScalarToFloat(dy)); 235 return this->INHERITED::translate(dx, dy); 236 } 237 238 bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) { 239 this->dump(kMatrix_Verb, NULL, "scale(%g %g)", 240 SkScalarToFloat(sx), SkScalarToFloat(sy)); 241 return this->INHERITED::scale(sx, sy); 242 } 243 244 bool SkDumpCanvas::rotate(SkScalar degrees) { 245 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees)); 246 return this->INHERITED::rotate(degrees); 247 } 248 249 bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) { 250 this->dump(kMatrix_Verb, NULL, "skew(%g %g)", 251 SkScalarToFloat(sx), SkScalarToFloat(sy)); 252 return this->INHERITED::skew(sx, sy); 253 } 254 255 bool SkDumpCanvas::concat(const SkMatrix& matrix) { 256 SkString str; 257 matrix.toString(&str); 258 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str()); 259 return this->INHERITED::concat(matrix); 260 } 261 262 void SkDumpCanvas::setMatrix(const SkMatrix& matrix) { 263 SkString str; 264 matrix.toString(&str); 265 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str()); 266 this->INHERITED::setMatrix(matrix); 267 } 268 269 /////////////////////////////////////////////////////////////////////////////// 270 271 static const char* bool_to_aastring(bool doAA) { 272 return doAA ? "AA" : "BW"; 273 } 274 275 bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { 276 SkString str; 277 toString(rect, &str); 278 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op), 279 bool_to_aastring(doAA)); 280 return this->INHERITED::clipRect(rect, op, doAA); 281 } 282 283 bool SkDumpCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { 284 SkString str; 285 toString(rrect, &str); 286 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op), 287 bool_to_aastring(doAA)); 288 return this->INHERITED::clipRRect(rrect, op, doAA); 289 } 290 291 bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { 292 SkString str; 293 toString(path, &str); 294 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op), 295 bool_to_aastring(doAA)); 296 return this->INHERITED::clipPath(path, op, doAA); 297 } 298 299 bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { 300 SkString str; 301 toString(deviceRgn, &str); 302 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(), 303 toString(op)); 304 return this->INHERITED::clipRegion(deviceRgn, op); 305 } 306 307 /////////////////////////////////////////////////////////////////////////////// 308 309 void SkDumpCanvas::drawPaint(const SkPaint& paint) { 310 this->dump(kDrawPaint_Verb, &paint, "drawPaint()"); 311 } 312 313 void SkDumpCanvas::drawPoints(PointMode mode, size_t count, 314 const SkPoint pts[], const SkPaint& paint) { 315 this->dump(kDrawPoints_Verb, &paint, "drawPoints(%s, %d)", toString(mode), 316 count); 317 } 318 319 void SkDumpCanvas::drawOval(const SkRect& rect, const SkPaint& paint) { 320 SkString str; 321 toString(rect, &str); 322 this->dump(kDrawOval_Verb, &paint, "drawOval(%s)", str.c_str()); 323 } 324 325 void SkDumpCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { 326 SkString str; 327 toString(rect, &str); 328 this->dump(kDrawRect_Verb, &paint, "drawRect(%s)", str.c_str()); 329 } 330 331 void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { 332 SkString str; 333 toString(rrect, &str); 334 this->dump(kDrawRRect_Verb, &paint, "drawRRect(%s)", str.c_str()); 335 } 336 337 void SkDumpCanvas::drawPath(const SkPath& path, const SkPaint& paint) { 338 SkString str; 339 toString(path, &str); 340 this->dump(kDrawPath_Verb, &paint, "drawPath(%s)", str.c_str()); 341 } 342 343 void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, 344 const SkPaint* paint) { 345 SkString str; 346 bitmap.toString(&str); 347 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(), 348 SkScalarToFloat(x), SkScalarToFloat(y)); 349 } 350 351 void SkDumpCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, 352 const SkRect& dst, const SkPaint* paint) { 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 SkMin32(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