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 29 static 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 35 static void toString(const SkIRect& r, SkString* str) { 36 str->appendf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height()); 37 } 38 39 static 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 64 static 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 95 static 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 110 static 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 117 static 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 126 static const char* toString(SkCanvas::VertexMode vm) { 127 static const char* gVMNames[] = { 128 "TRIANGLES", "STRIP", "FAN" 129 }; 130 return gVMNames[vm]; 131 } 132 133 static const char* toString(SkCanvas::PointMode pm) { 134 static const char* gPMNames[] = { 135 "POINTS", "LINES", "POLYGON" 136 }; 137 return gPMNames[pm]; 138 } 139 140 static 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 170 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(WIDE_OPEN, WIDE_OPEN) { 171 fNestLevel = 0; 172 SkSafeRef(dumper); 173 fDumper = dumper; 174 } 175 176 SkDumpCanvas::~SkDumpCanvas() { 177 SkSafeUnref(fDumper); 178 } 179 180 void 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 197 void SkDumpCanvas::willSave() { 198 this->dump(kSave_Verb, nullptr, "save()"); 199 this->INHERITED::willSave(); 200 } 201 202 SkCanvas::SaveLayerStrategy SkDumpCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { 203 SkString str; 204 str.printf("saveLayer(0x%X)", rec.fSaveLayerFlags); 205 if (rec.fBounds) { 206 str.append(" bounds"); 207 toString(*rec.fBounds, &str); 208 } 209 const SkPaint* paint = rec.fPaint; 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::getSaveLayerStrategy(rec); 220 } 221 222 void SkDumpCanvas::willRestore() { 223 this->dump(kRestore_Verb, nullptr, "restore"); 224 this->INHERITED::willRestore(); 225 } 226 227 void SkDumpCanvas::didConcat(const SkMatrix& matrix) { 228 SkString str; 229 230 switch (matrix.getType()) { 231 case SkMatrix::kTranslate_Mask: 232 this->dump(kMatrix_Verb, nullptr, "translate(%g %g)", 233 SkScalarToFloat(matrix.getTranslateX()), 234 SkScalarToFloat(matrix.getTranslateY())); 235 break; 236 case SkMatrix::kScale_Mask: 237 this->dump(kMatrix_Verb, nullptr, "scale(%g %g)", 238 SkScalarToFloat(matrix.getScaleX()), 239 SkScalarToFloat(matrix.getScaleY())); 240 break; 241 default: 242 matrix.toString(&str); 243 this->dump(kMatrix_Verb, nullptr, "concat(%s)", str.c_str()); 244 break; 245 } 246 247 this->INHERITED::didConcat(matrix); 248 } 249 250 void SkDumpCanvas::didSetMatrix(const SkMatrix& matrix) { 251 SkString str; 252 matrix.toString(&str); 253 this->dump(kMatrix_Verb, nullptr, "setMatrix(%s)", str.c_str()); 254 this->INHERITED::didSetMatrix(matrix); 255 } 256 257 /////////////////////////////////////////////////////////////////////////////// 258 259 const char* SkDumpCanvas::EdgeStyleToAAString(ClipEdgeStyle edgeStyle) { 260 return kSoft_ClipEdgeStyle == edgeStyle ? "AA" : "BW"; 261 } 262 263 void SkDumpCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { 264 SkString str; 265 toString(rect, &str); 266 this->dump(kClip_Verb, nullptr, "clipRect(%s %s %s)", str.c_str(), toString(op), 267 EdgeStyleToAAString(edgeStyle)); 268 this->INHERITED::onClipRect(rect, op, edgeStyle); 269 } 270 271 void SkDumpCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { 272 SkString str; 273 toString(rrect, &str); 274 this->dump(kClip_Verb, nullptr, "clipRRect(%s %s %s)", str.c_str(), toString(op), 275 EdgeStyleToAAString(edgeStyle)); 276 this->INHERITED::onClipRRect(rrect, op, edgeStyle); 277 } 278 279 void SkDumpCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { 280 SkString str; 281 toString(path, &str); 282 this->dump(kClip_Verb, nullptr, "clipPath(%s %s %s)", str.c_str(), toString(op), 283 EdgeStyleToAAString(edgeStyle)); 284 this->INHERITED::onClipPath(path, op, edgeStyle); 285 } 286 287 void SkDumpCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { 288 SkString str; 289 toString(deviceRgn, &str); 290 this->dump(kClip_Verb, nullptr, "clipRegion(%s %s)", str.c_str(), 291 toString(op)); 292 this->INHERITED::onClipRegion(deviceRgn, op); 293 } 294 295 /////////////////////////////////////////////////////////////////////////////// 296 297 void SkDumpCanvas::onDrawPaint(const SkPaint& paint) { 298 this->dump(kDrawPaint_Verb, &paint, "drawPaint()"); 299 } 300 301 void 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 307 void 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 313 void 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 319 void 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 325 void 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 334 void 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 340 void 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 348 void SkDumpCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, 349 const SkPaint* paint, SrcRectConstraint) { 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, "drawBitmapRect(%s %s)", bs.c_str(), rs.c_str()); 363 } 364 365 void SkDumpCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, 366 const SkRect& dst, const SkPaint* paint) { 367 SkString str, centerStr, dstStr; 368 bitmap.toString(&str); 369 toString(center, ¢erStr); 370 toString(dst, &dstStr); 371 this->dump(kDrawBitmap_Verb, paint, "drawBitmapNine(%s %s %s)", str.c_str(), 372 centerStr.c_str(), dstStr.c_str()); 373 } 374 375 void SkDumpCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { 376 SkString str; 377 image->toString(&str); 378 this->dump(kDrawBitmap_Verb, paint, "drawImage(%s %g %g)", str.c_str(), 379 SkScalarToFloat(x), SkScalarToFloat(y)); 380 } 381 382 void SkDumpCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, 383 const SkPaint* paint, SrcRectConstraint) { 384 SkString bs, rs; 385 image->toString(&bs); 386 toString(dst, &rs); 387 // show the src-rect only if its not everything 388 if (src && (src->fLeft > 0 || src->fTop > 0 || 389 src->fRight < SkIntToScalar(image->width()) || 390 src->fBottom < SkIntToScalar(image->height()))) { 391 SkString ss; 392 toString(*src, &ss); 393 rs.prependf("%s ", ss.c_str()); 394 } 395 396 this->dump(kDrawBitmap_Verb, paint, "drawImageRectToRect(%s %s)", 397 bs.c_str(), rs.c_str()); 398 } 399 400 void SkDumpCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, 401 const SkPaint& paint) { 402 SkString str; 403 toString(text, byteLength, paint.getTextEncoding(), &str); 404 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(), 405 byteLength, SkScalarToFloat(x), SkScalarToFloat(y)); 406 } 407 408 void SkDumpCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], 409 const SkPaint& paint) { 410 SkString str; 411 toString(text, byteLength, paint.getTextEncoding(), &str); 412 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)", 413 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX), 414 SkScalarToFloat(pos[0].fY)); 415 } 416 417 void SkDumpCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], 418 SkScalar constY, const SkPaint& paint) { 419 SkString str; 420 toString(text, byteLength, paint.getTextEncoding(), &str); 421 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)", 422 str.c_str(), byteLength, SkScalarToFloat(xpos[0]), 423 SkScalarToFloat(constY)); 424 } 425 426 void SkDumpCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 427 const SkMatrix* matrix, const SkPaint& paint) { 428 SkString str; 429 toString(text, byteLength, paint.getTextEncoding(), &str); 430 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])", 431 str.c_str(), byteLength); 432 } 433 434 void SkDumpCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 435 const SkPaint& paint) { 436 SkString str; 437 toString(blob->bounds(), &str); 438 this->dump(kDrawText_Verb, &paint, "drawTextBlob(%p) [%s]", blob, str.c_str()); 439 // FIXME: dump the actual blob content? 440 } 441 442 void SkDumpCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, 443 const SkPaint* paint) { 444 this->dump(kDrawPicture_Verb, nullptr, "drawPicture(%p) %f:%f:%f:%f", picture, 445 picture->cullRect().fLeft, picture->cullRect().fTop, 446 picture->cullRect().fRight, picture->cullRect().fBottom); 447 fNestLevel += 1; 448 this->INHERITED::onDrawPicture(picture, matrix, paint); 449 fNestLevel -= 1; 450 this->dump(kDrawPicture_Verb, nullptr, "endPicture(%p) %f:%f:%f:%f", &picture, 451 picture->cullRect().fLeft, picture->cullRect().fTop, 452 picture->cullRect().fRight, picture->cullRect().fBottom); 453 } 454 455 void SkDumpCanvas::onDrawVertices(VertexMode vmode, int vertexCount, 456 const SkPoint vertices[], const SkPoint texs[], 457 const SkColor colors[], SkXfermode* xmode, 458 const uint16_t indices[], int indexCount, 459 const SkPaint& paint) { 460 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)", 461 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX), 462 SkScalarToFloat(vertices[0].fY)); 463 } 464 465 void SkDumpCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 466 const SkPoint texCoords[4], SkXfermode* xmode, 467 const SkPaint& paint) { 468 //dumps corner points and colors in clockwise order starting on upper-left corner 469 this->dump(kDrawPatch_Verb, &paint, "drawPatch(Vertices{[%f, %f], [%f, %f], [%f, %f], [%f, %f]}\ 470 | Colors{[0x%x], [0x%x], [0x%x], [0x%x]} | TexCoords{[%f,%f], [%f,%f], [%f,%f], \ 471 [%f,%f]})", 472 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fX, 473 cubics[SkPatchUtils::kTopP0_CubicCtrlPts].fY, 474 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fX, 475 cubics[SkPatchUtils::kTopP3_CubicCtrlPts].fY, 476 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fX, 477 cubics[SkPatchUtils::kBottomP3_CubicCtrlPts].fY, 478 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fX, 479 cubics[SkPatchUtils::kBottomP0_CubicCtrlPts].fY, 480 colors[0], colors[1], colors[2], colors[3], 481 texCoords[0].x(), texCoords[0].y(), texCoords[1].x(), texCoords[1].y(), 482 texCoords[2].x(), texCoords[2].y(), texCoords[3].x(), texCoords[3].y()); 483 } 484 485 /////////////////////////////////////////////////////////////////////////////// 486 /////////////////////////////////////////////////////////////////////////////// 487 488 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) { 489 fProc = proc; 490 fRefcon = refcon; 491 } 492 493 static void appendPtr(SkString* str, const void* ptr, const char name[]) { 494 if (ptr) { 495 str->appendf(" %s:%p", name, ptr); 496 } 497 } 498 499 static void appendFlattenable(SkString* str, const SkFlattenable* ptr, 500 const char name[]) { 501 if (ptr) { 502 str->appendf(" %s:%p", name, ptr); 503 } 504 } 505 506 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb, 507 const char str[], const SkPaint* p) { 508 SkString msg, tab; 509 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1; 510 SkASSERT(level >= 0); 511 for (int i = 0; i < level; i++) { 512 #if 0 513 tab.append("\t"); 514 #else 515 tab.append(" "); // tabs are often too wide to be useful 516 #endif 517 } 518 msg.printf("%s%s", tab.c_str(), str); 519 520 if (p) { 521 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags()); 522 appendFlattenable(&msg, p->getShader(), "shader"); 523 appendFlattenable(&msg, p->getXfermode(), "xfermode"); 524 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 525 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter"); 526 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 527 appendFlattenable(&msg, p->getColorFilter(), "filter"); 528 529 if (SkDumpCanvas::kDrawText_Verb == verb) { 530 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize())); 531 appendPtr(&msg, p->getTypeface(), "typeface"); 532 } 533 534 if (p->getStyle() != SkPaint::kFill_Style) { 535 msg.appendf(" strokeWidth:%g", SkScalarToFloat(p->getStrokeWidth())); 536 } 537 } 538 539 fProc(msg.c_str(), fRefcon); 540 } 541 542 /////////////////////////////////////////////////////////////////////////////// 543 544 static void dumpToDebugf(const char text[], void*) { 545 SkDebugf("%s\n", text); 546 } 547 548 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, nullptr) {} 549 550 #endif 551