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