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 } 88 } 89 } 90 91 static void toString(const SkPath& path, SkString* str) { 92 if (path.isEmpty()) { 93 str->append("path:empty"); 94 } else { 95 toString(path.getBounds(), str); 96 #if 1 97 SkString s; 98 dumpVerbs(path, &s); 99 str->append(s.c_str()); 100 #endif 101 str->append("]"); 102 str->prepend("path:["); 103 } 104 } 105 106 static const char* toString(SkRegion::Op op) { 107 static const char* gOpNames[] = { 108 "DIFF", "SECT", "UNION", "XOR", "RDIFF", "REPLACE" 109 }; 110 return gOpNames[op]; 111 } 112 113 static void toString(const SkRegion& rgn, SkString* str) { 114 str->append("Region:["); 115 toString(rgn.getBounds(), str); 116 str->append("]"); 117 if (rgn.isComplex()) { 118 str->append(".complex"); 119 } 120 } 121 122 static const char* toString(SkCanvas::VertexMode vm) { 123 static const char* gVMNames[] = { 124 "TRIANGLES", "STRIP", "FAN" 125 }; 126 return gVMNames[vm]; 127 } 128 129 static const char* toString(SkCanvas::PointMode pm) { 130 static const char* gPMNames[] = { 131 "POINTS", "LINES", "POLYGON" 132 }; 133 return gPMNames[pm]; 134 } 135 136 static void toString(const void* text, size_t byteLen, SkPaint::TextEncoding enc, 137 SkString* str) { 138 // FIXME: this code appears to be untested - and probably unused - and probably wrong 139 switch (enc) { 140 case SkPaint::kUTF8_TextEncoding: 141 str->appendf("\"%.*s\"%s", SkMax32(byteLen, 32), (const char*) text, 142 byteLen > 32 ? "..." : ""); 143 break; 144 case SkPaint::kUTF16_TextEncoding: 145 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text, 146 byteLen > 64 ? "..." : ""); 147 break; 148 case SkPaint::kUTF32_TextEncoding: 149 str->appendf("\"%.*ls\"%s", SkMax32(byteLen, 32), (const wchar_t*) text, 150 byteLen > 128 ? "..." : ""); 151 break; 152 case SkPaint::kGlyphID_TextEncoding: 153 str->append("<glyphs>"); 154 break; 155 156 default: 157 SkASSERT(false); 158 break; 159 } 160 } 161 162 /////////////////////////////////////////////////////////////////////////////// 163 164 static SkBitmap make_wideopen_bm() { 165 static const int WIDE_OPEN = 16384; 166 167 SkBitmap bm; 168 bm.setConfig(SkBitmap::kNo_Config, WIDE_OPEN, WIDE_OPEN); 169 return bm; 170 } 171 172 SkDumpCanvas::SkDumpCanvas(Dumper* dumper) : INHERITED(make_wideopen_bm()) { 173 fNestLevel = 0; 174 SkSafeRef(dumper); 175 fDumper = dumper; 176 } 177 178 SkDumpCanvas::~SkDumpCanvas() { 179 SkSafeUnref(fDumper); 180 } 181 182 void SkDumpCanvas::dump(Verb verb, const SkPaint* paint, 183 const char format[], ...) { 184 static const size_t BUFFER_SIZE = 1024; 185 186 char buffer[BUFFER_SIZE]; 187 va_list args; 188 va_start(args, format); 189 vsnprintf(buffer, BUFFER_SIZE, format, args); 190 va_end(args); 191 192 if (fDumper) { 193 fDumper->dump(this, verb, buffer, paint); 194 } 195 } 196 197 /////////////////////////////////////////////////////////////////////////////// 198 199 int SkDumpCanvas::save(SaveFlags flags) { 200 this->dump(kSave_Verb, NULL, "save(0x%X)", flags); 201 return this->INHERITED::save(flags); 202 } 203 204 int SkDumpCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, 205 SaveFlags flags) { 206 SkString str; 207 str.printf("saveLayer(0x%X)", flags); 208 if (bounds) { 209 str.append(" bounds"); 210 toString(*bounds, &str); 211 } 212 if (paint) { 213 if (paint->getAlpha() != 0xFF) { 214 str.appendf(" alpha:0x%02X", paint->getAlpha()); 215 } 216 if (paint->getXfermode()) { 217 str.appendf(" xfermode:%p", paint->getXfermode()); 218 } 219 } 220 this->dump(kSave_Verb, paint, str.c_str()); 221 return this->INHERITED::saveLayer(bounds, paint, flags); 222 } 223 224 void SkDumpCanvas::restore() { 225 this->INHERITED::restore(); 226 this->dump(kRestore_Verb, NULL, "restore"); 227 } 228 229 bool SkDumpCanvas::translate(SkScalar dx, SkScalar dy) { 230 this->dump(kMatrix_Verb, NULL, "translate(%g %g)", 231 SkScalarToFloat(dx), SkScalarToFloat(dy)); 232 return this->INHERITED::translate(dx, dy); 233 } 234 235 bool SkDumpCanvas::scale(SkScalar sx, SkScalar sy) { 236 this->dump(kMatrix_Verb, NULL, "scale(%g %g)", 237 SkScalarToFloat(sx), SkScalarToFloat(sy)); 238 return this->INHERITED::scale(sx, sy); 239 } 240 241 bool SkDumpCanvas::rotate(SkScalar degrees) { 242 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees)); 243 return this->INHERITED::rotate(degrees); 244 } 245 246 bool SkDumpCanvas::skew(SkScalar sx, SkScalar sy) { 247 this->dump(kMatrix_Verb, NULL, "skew(%g %g)", 248 SkScalarToFloat(sx), SkScalarToFloat(sy)); 249 return this->INHERITED::skew(sx, sy); 250 } 251 252 bool SkDumpCanvas::concat(const SkMatrix& matrix) { 253 SkString str; 254 matrix.toString(&str); 255 this->dump(kMatrix_Verb, NULL, "concat(%s)", str.c_str()); 256 return this->INHERITED::concat(matrix); 257 } 258 259 void SkDumpCanvas::setMatrix(const SkMatrix& matrix) { 260 SkString str; 261 matrix.toString(&str); 262 this->dump(kMatrix_Verb, NULL, "setMatrix(%s)", str.c_str()); 263 this->INHERITED::setMatrix(matrix); 264 } 265 266 /////////////////////////////////////////////////////////////////////////////// 267 268 static const char* bool_to_aastring(bool doAA) { 269 return doAA ? "AA" : "BW"; 270 } 271 272 bool SkDumpCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { 273 SkString str; 274 toString(rect, &str); 275 this->dump(kClip_Verb, NULL, "clipRect(%s %s %s)", str.c_str(), toString(op), 276 bool_to_aastring(doAA)); 277 return this->INHERITED::clipRect(rect, op, doAA); 278 } 279 280 bool SkDumpCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { 281 SkString str; 282 toString(rrect, &str); 283 this->dump(kClip_Verb, NULL, "clipRRect(%s %s %s)", str.c_str(), toString(op), 284 bool_to_aastring(doAA)); 285 return this->INHERITED::clipRRect(rrect, op, doAA); 286 } 287 288 bool SkDumpCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { 289 SkString str; 290 toString(path, &str); 291 this->dump(kClip_Verb, NULL, "clipPath(%s %s %s)", str.c_str(), toString(op), 292 bool_to_aastring(doAA)); 293 return this->INHERITED::clipPath(path, op, doAA); 294 } 295 296 bool SkDumpCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { 297 SkString str; 298 toString(deviceRgn, &str); 299 this->dump(kClip_Verb, NULL, "clipRegion(%s %s)", str.c_str(), 300 toString(op)); 301 return this->INHERITED::clipRegion(deviceRgn, op); 302 } 303 304 /////////////////////////////////////////////////////////////////////////////// 305 306 void SkDumpCanvas::drawPaint(const SkPaint& paint) { 307 this->dump(kDrawPaint_Verb, &paint, "drawPaint()"); 308 } 309 310 void 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 316 void 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 322 void 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 328 void SkDumpCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { 329 SkString str; 330 toString(rrect, &str); 331 this->dump(kDrawRRect_Verb, &paint, "drawRRect(%s)", str.c_str()); 332 } 333 334 void SkDumpCanvas::drawPath(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::drawBitmap(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::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, 349 const SkRect& dst, const SkPaint* paint) { 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, "drawBitmapRectToRect(%s %s)", 363 bs.c_str(), rs.c_str()); 364 } 365 366 void SkDumpCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, 367 const SkPaint* paint) { 368 SkString bs, ms; 369 bitmap.toString(&bs); 370 m.toString(&ms); 371 this->dump(kDrawBitmap_Verb, paint, "drawBitmapMatrix(%s %s)", 372 bs.c_str(), ms.c_str()); 373 } 374 375 void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, 376 const SkPaint* paint) { 377 SkString str; 378 bitmap.toString(&str); 379 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(), 380 x, y); 381 } 382 383 void SkDumpCanvas::drawText(const void* text, size_t byteLength, SkScalar x, 384 SkScalar y, const SkPaint& paint) { 385 SkString str; 386 toString(text, byteLength, paint.getTextEncoding(), &str); 387 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(), 388 byteLength, SkScalarToFloat(x), SkScalarToFloat(y)); 389 } 390 391 void SkDumpCanvas::drawPosText(const void* text, size_t byteLength, 392 const SkPoint pos[], const SkPaint& paint) { 393 SkString str; 394 toString(text, byteLength, paint.getTextEncoding(), &str); 395 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)", 396 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX), 397 SkScalarToFloat(pos[0].fY)); 398 } 399 400 void SkDumpCanvas::drawPosTextH(const void* text, size_t byteLength, 401 const SkScalar xpos[], SkScalar constY, 402 const SkPaint& paint) { 403 SkString str; 404 toString(text, byteLength, paint.getTextEncoding(), &str); 405 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)", 406 str.c_str(), byteLength, SkScalarToFloat(xpos[0]), 407 SkScalarToFloat(constY)); 408 } 409 410 void SkDumpCanvas::drawTextOnPath(const void* text, size_t byteLength, 411 const SkPath& path, const SkMatrix* matrix, 412 const SkPaint& paint) { 413 SkString str; 414 toString(text, byteLength, paint.getTextEncoding(), &str); 415 this->dump(kDrawText_Verb, &paint, "drawTextOnPath(%s [%d])", 416 str.c_str(), byteLength); 417 } 418 419 void SkDumpCanvas::drawPicture(SkPicture& picture) { 420 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p) %d:%d", &picture, 421 picture.width(), picture.height()); 422 fNestLevel += 1; 423 this->INHERITED::drawPicture(picture); 424 fNestLevel -= 1; 425 this->dump(kDrawPicture_Verb, NULL, "endPicture(%p) %d:%d", &picture, 426 picture.width(), picture.height()); 427 } 428 429 void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount, 430 const SkPoint vertices[], const SkPoint texs[], 431 const SkColor colors[], SkXfermode* xmode, 432 const uint16_t indices[], int indexCount, 433 const SkPaint& paint) { 434 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)", 435 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX), 436 SkScalarToFloat(vertices[0].fY)); 437 } 438 439 void SkDumpCanvas::drawData(const void* data, size_t length) { 440 // this->dump(kDrawData_Verb, NULL, "drawData(%d)", length); 441 this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length, 442 SkMin32(length, 64), data); 443 } 444 445 /////////////////////////////////////////////////////////////////////////////// 446 /////////////////////////////////////////////////////////////////////////////// 447 448 SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) { 449 fProc = proc; 450 fRefcon = refcon; 451 } 452 453 static void appendPtr(SkString* str, const void* ptr, const char name[]) { 454 if (ptr) { 455 str->appendf(" %s:%p", name, ptr); 456 } 457 } 458 459 static void appendFlattenable(SkString* str, const SkFlattenable* ptr, 460 const char name[]) { 461 if (ptr) { 462 str->appendf(" %s:%p", name, ptr); 463 } 464 } 465 466 void SkFormatDumper::dump(SkDumpCanvas* canvas, SkDumpCanvas::Verb verb, 467 const char str[], const SkPaint* p) { 468 SkString msg, tab; 469 const int level = canvas->getNestLevel() + canvas->getSaveCount() - 1; 470 SkASSERT(level >= 0); 471 for (int i = 0; i < level; i++) { 472 #if 0 473 tab.append("\t"); 474 #else 475 tab.append(" "); // tabs are often too wide to be useful 476 #endif 477 } 478 msg.printf("%s%s", tab.c_str(), str); 479 480 if (p) { 481 msg.appendf(" color:0x%08X flags:%X", p->getColor(), p->getFlags()); 482 appendFlattenable(&msg, p->getShader(), "shader"); 483 appendFlattenable(&msg, p->getXfermode(), "xfermode"); 484 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 485 appendFlattenable(&msg, p->getMaskFilter(), "maskFilter"); 486 appendFlattenable(&msg, p->getPathEffect(), "pathEffect"); 487 appendFlattenable(&msg, p->getColorFilter(), "filter"); 488 489 if (SkDumpCanvas::kDrawText_Verb == verb) { 490 msg.appendf(" textSize:%g", SkScalarToFloat(p->getTextSize())); 491 appendPtr(&msg, p->getTypeface(), "typeface"); 492 } 493 } 494 495 fProc(msg.c_str(), fRefcon); 496 } 497 498 /////////////////////////////////////////////////////////////////////////////// 499 500 static void dumpToDebugf(const char text[], void*) { 501 SkDebugf("%s\n", text); 502 } 503 504 SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {} 505 506 #endif 507