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