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