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