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