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