blob: 7a05f0918d74255b1a4b00d2816274790a4f5bd4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001#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
14static void toString(const SkRect& r, SkString* str) {
reed@android.com0becfc5b2009-01-13 13:26:44 +000015 str->printf("[%g,%g %g:%g]",
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 SkScalarToFloat(r.fLeft), SkScalarToFloat(r.fTop),
17 SkScalarToFloat(r.width()), SkScalarToFloat(r.height()));
18}
19
20static void toString(const SkIRect& r, SkString* str) {
reed@android.com0becfc5b2009-01-13 13:26:44 +000021 str->printf("[%d,%d %d:%d]", r.fLeft, r.fTop, r.width(), r.height());
22}
23
24static 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.com8a1c16f2008-12-17 15:59:43 +000050}
51
52static void toString(const SkPath& path, SkString* str) {
53 if (path.isEmpty()) {
54 str->set("path:empty");
55 } else {
reed@android.comd252db02009-04-01 18:31:44 +000056 toString(path.getBounds(), str);
reed@android.com0becfc5b2009-01-13 13:26:44 +000057#if 1
58 SkString s;
59 dumpVerbs(path, &s);
60 str->append(s.c_str());
61#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 str->append("]");
63 str->prepend("path:[");
64 }
65}
66
67static 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
74static 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
83static const char* toString(SkCanvas::VertexMode vm) {
84 static const char* gVMNames[] = {
85 "TRIANGLES", "STRIP", "FAN"
86 };
87 return gVMNames[vm];
88}
89
90static const char* toString(SkCanvas::PointMode pm) {
91 static const char* gPMNames[] = {
92 "POINTS", "LINES", "POLYGON"
93 };
94 return gPMNames[pm];
95}
96
97static 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
104static 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
122static 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
141SkDumpCanvas::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
152SkDumpCanvas::~SkDumpCanvas() {
153 fDumper->safeUnref();
154}
155
156void 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
173int SkDumpCanvas::save(SaveFlags flags) {
174 this->dump(kSave_Verb, NULL, "save(0x%X)", flags);
175 return this->INHERITED::save(flags);
176}
177
178int 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
184void SkDumpCanvas::restore() {
185 this->INHERITED::restore();
186 this->dump(kRestore_Verb, NULL, "restore");
187}
188
189bool 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
195bool 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
201bool SkDumpCanvas::rotate(SkScalar degrees) {
202 this->dump(kMatrix_Verb, NULL, "rotate(%g)", SkScalarToFloat(degrees));
203 return this->INHERITED::rotate(degrees);
204}
205
206bool 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
212bool 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
219void 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
228bool 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
235bool 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
242bool 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
252void SkDumpCanvas::drawPaint(const SkPaint& paint) {
253 this->dump(kDrawPaint_Verb, &paint, "drawPaint()");
254}
255
256void 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
262void 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
268void 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
274void SkDumpCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
275 const SkPaint* paint) {
276 SkString str;
277 toString(bitmap, &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000278 this->dump(kDrawBitmap_Verb, paint, "drawBitmap(%s %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 SkScalarToFloat(x), SkScalarToFloat(y));
280}
281
282void 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
300void 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
309void SkDumpCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
310 const SkPaint* paint) {
311 SkString str;
312 toString(bitmap, &str);
reed@android.com0becfc5b2009-01-13 13:26:44 +0000313 this->dump(kDrawBitmap_Verb, paint, "drawSprite(%s %d %d)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 x, y);
315}
316
317void 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.com0becfc5b2009-01-13 13:26:44 +0000321 this->dump(kDrawText_Verb, &paint, "drawText(%s [%d] %g %g)", str.c_str(),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 byteLength, SkScalarToFloat(x), SkScalarToFloat(y));
323}
324
325void 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.com0becfc5b2009-01-13 13:26:44 +0000329 this->dump(kDrawText_Verb, &paint, "drawPosText(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 str.c_str(), byteLength, SkScalarToFloat(pos[0].fX),
331 SkScalarToFloat(pos[0].fY));
332}
333
334void 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.com0becfc5b2009-01-13 13:26:44 +0000339 this->dump(kDrawText_Verb, &paint, "drawPosTextH(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 str.c_str(), byteLength, SkScalarToFloat(xpos[0]),
341 SkScalarToFloat(constY));
342}
343
344void 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.comf76bacf2009-05-13 14:00:33 +0000353void SkDumpCanvas::drawShape(SkShape* shape) {
354 this->dump(kDrawShape_Verb, NULL, "drawShape(%p)", shape);
355}
356
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357void SkDumpCanvas::drawPicture(SkPicture& picture) {
358 this->dump(kDrawPicture_Verb, NULL, "drawPicture(%p)", &picture);
359}
360
361void 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.com0becfc5b2009-01-13 13:26:44 +0000366 this->dump(kDrawVertices_Verb, &paint, "drawVertices(%s [%d] %g %g ...)",
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 toString(vmode), vertexCount, SkScalarToFloat(vertices[0].fX),
368 SkScalarToFloat(vertices[0].fY));
369}
370
371///////////////////////////////////////////////////////////////////////////////
372///////////////////////////////////////////////////////////////////////////////
373
374SkFormatDumper::SkFormatDumper(void (*proc)(const char*, void*), void* refcon) {
375 fProc = proc;
376 fRefcon = refcon;
377}
378
379static void appendPtr(SkString* str, const void* ptr, const char name[]) {
380 if (ptr) {
381 str->appendf(" %s:%p", name, ptr);
382 }
383}
384
385static 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
397void 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
427static void dumpToDebugf(const char text[], void*) {
428 SkDebugf("%s\n", text);
429}
430
431SkDebugfDumper::SkDebugfDumper() : INHERITED(dumpToDebugf, NULL) {}
432
433