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