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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkMatrix.h"
#include "SkColor.h"
#include "SkTDArray.h"
#include "SkRandom.h"
#include "SkRRect.h"
enum RandomAddPath {
kMoveToPath,
kRMoveToPath,
kLineToPath,
kRLineToPath,
kQuadToPath,
kRQuadToPath,
kConicToPath,
kRConicToPath,
kCubicToPath,
kRCubicToPath,
kArcToPath,
kArcTo2Path,
kClosePath,
kAddArc,
kAddRoundRect1,
kAddRoundRect2,
kAddRRect,
kAddPoly,
kAddPath1,
kAddPath2,
kAddPath3,
kReverseAddPath,
};
const int kRandomAddPath_Last = kReverseAddPath;
const char* gRandomAddPathNames[] = {
"kMoveToPath",
"kRMoveToPath",
"kLineToPath",
"kRLineToPath",
"kQuadToPath",
"kRQuadToPath",
"kConicToPath",
"kRConicToPath",
"kCubicToPath",
"kRCubicToPath",
"kArcToPath",
"kArcTo2Path",
"kClosePath",
"kAddArc",
"kAddRoundRect1",
"kAddRoundRect2",
"kAddRRect",
"kAddPoly",
"kAddPath1",
"kAddPath2",
"kAddPath3",
"kReverseAddPath",
};
enum RandomSetRRect {
kSetEmpty,
kSetRect,
kSetOval,
kSetRectXY,
kSetNinePatch,
kSetRectRadii,
};
const char* gRandomSetRRectNames[] = {
"kSetEmpty",
"kSetRect",
"kSetOval",
"kSetRectXY",
"kSetNinePatch",
"kSetRectRadii",
};
int kRandomSetRRect_Last = kSetRectRadii;
enum RandomSetMatrix {
kSetIdentity,
kSetTranslate,
kSetTranslateX,
kSetTranslateY,
kSetScale,
kSetScaleTranslate,
kSetScaleX,
kSetScaleY,
kSetSkew,
kSetSkewTranslate,
kSetSkewX,
kSetSkewY,
kSetRotate,
kSetRotateTranslate,
kSetPerspectiveX,
kSetPerspectiveY,
kSetAll,
};
int kRandomSetMatrix_Last = kSetAll;
const char* gRandomSetMatrixNames[] = {
"kSetIdentity",
"kSetTranslate",
"kSetTranslateX",
"kSetTranslateY",
"kSetScale",
"kSetScaleTranslate",
"kSetScaleX",
"kSetScaleY",
"kSetSkew",
"kSetSkewTranslate",
"kSetSkewX",
"kSetSkewY",
"kSetRotate",
"kSetRotateTranslate",
"kSetPerspectiveX",
"kSetPerspectiveY",
"kSetAll",
};
class FuzzPath {
public:
FuzzPath()
: fFloatMin(0)
, fFloatMax(800)
, fAddCount(0)
, fPrintName(false)
, fStrokeOnly(false)
, fValidate(false)
{
fTab = " ";
}
void randomize() {
fPathDepth = 0;
fPathDepthLimit = fRand.nextRangeU(1, 2);
fPathContourCount = fRand.nextRangeU(1, 4);
fPathSegmentLimit = fRand.nextRangeU(1, 8);
fClip = makePath();
SkASSERT(!fPathDepth);
fMatrix = makeMatrix();
fPaint = makePaint();
fPathDepthLimit = fRand.nextRangeU(1, 3);
fPathContourCount = fRand.nextRangeU(1, 6);
fPathSegmentLimit = fRand.nextRangeU(1, 16);
fPath = makePath();
SkASSERT(!fPathDepth);
}
const SkPath& getClip() const {
return fClip;
}
const SkMatrix& getMatrix() const {
return fMatrix;
}
const SkPaint& getPaint() const {
return fPaint;
}
const SkPath& getPath() const {
return fPath;
}
void setSeed(int seed) {
fRand.setSeed(seed);
}
void setStrokeOnly() {
fStrokeOnly = true;
}
private:
SkPath::AddPathMode makeAddPathMode() {
return (SkPath::AddPathMode) fRand.nextRangeU(SkPath::kAppend_AddPathMode,
SkPath::kExtend_AddPathMode);
}
RandomAddPath makeAddPathType() {
return (RandomAddPath) fRand.nextRangeU(0, kRandomAddPath_Last);
}
SkScalar makeAngle() {
SkScalar angle;
angle = fRand.nextF();
return angle;
}
bool makeBool() {
return fRand.nextBool();
}
SkPath::Direction makeDirection() {
return (SkPath::Direction) fRand.nextRangeU(SkPath::kCW_Direction, SkPath::kCCW_Direction);
}
SkMatrix makeMatrix() {
SkMatrix matrix;
matrix.reset();
RandomSetMatrix setMatrix = (RandomSetMatrix) fRand.nextRangeU(0, kRandomSetMatrix_Last);
if (fPrintName) {
SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomSetMatrixNames[setMatrix]);
}
switch (setMatrix) {
case kSetIdentity:
break;
case kSetTranslateX:
matrix.setTranslateX(makeScalar());
break;
case kSetTranslateY:
matrix.setTranslateY(makeScalar());
break;
case kSetTranslate:
matrix.setTranslate(makeScalar(), makeScalar());
break;
case kSetScaleX:
matrix.setScaleX(makeScalar());
break;
case kSetScaleY:
matrix.setScaleY(makeScalar());
break;
case kSetScale:
matrix.setScale(makeScalar(), makeScalar());
break;
case kSetScaleTranslate:
matrix.setScale(makeScalar(), makeScalar(), makeScalar(), makeScalar());
break;
case kSetSkewX:
matrix.setSkewX(makeScalar());
break;
case kSetSkewY:
matrix.setSkewY(makeScalar());
break;
case kSetSkew:
matrix.setSkew(makeScalar(), makeScalar());
break;
case kSetSkewTranslate:
matrix.setSkew(makeScalar(), makeScalar(), makeScalar(), makeScalar());
break;
case kSetRotate:
matrix.setRotate(makeScalar());
break;
case kSetRotateTranslate:
matrix.setRotate(makeScalar(), makeScalar(), makeScalar());
break;
case kSetPerspectiveX:
matrix.setPerspX(makeScalar());
break;
case kSetPerspectiveY:
matrix.setPerspY(makeScalar());
break;
case kSetAll:
matrix.setAll(makeScalar(), makeScalar(), makeScalar(),
makeScalar(), makeScalar(), makeScalar(),
makeScalar(), makeScalar(), makeScalar());
break;
}
return matrix;
}
SkPaint makePaint() {
SkPaint paint;
bool antiAlias = fRand.nextBool();
paint.setAntiAlias(antiAlias);
SkPaint::Style style = fStrokeOnly ? SkPaint::kStroke_Style :
(SkPaint::Style) fRand.nextRangeU(SkPaint::kFill_Style, SkPaint::kStrokeAndFill_Style);
paint.setStyle(style);
SkColor color = (SkColor) fRand.nextU();
paint.setColor(color);
SkScalar width = fRand.nextRangeF(0, 10);
paint.setStrokeWidth(width);
SkScalar miter = makeScalar();
paint.setStrokeMiter(miter);
SkPaint::Cap cap = (SkPaint::Cap) fRand.nextRangeU(SkPaint::kButt_Cap, SkPaint::kSquare_Cap);
paint.setStrokeCap(cap);
SkPaint::Join join = (SkPaint::Join) fRand.nextRangeU(SkPaint::kMiter_Join,
SkPaint::kBevel_Join);
paint.setStrokeJoin(join);
return paint;
}
SkPoint makePoint() {
SkPoint result;
makeScalarArray(2, &result.fX);
return result;
}
void makePointArray(size_t arrayCount, SkPoint* points) {
for (size_t index = 0; index < arrayCount; ++index) {
points[index] = makePoint();
}
}
void makePointArray(SkTDArray<SkPoint>* points) {
size_t arrayCount = fRand.nextRangeU(1, 10);
for (size_t index = 0; index < arrayCount; ++index) {
*points->append() = makePoint();
}
}
SkRect makeRect() {
SkRect result;
makeScalarArray(4, &result.fLeft);
return result;
}
SkRRect makeRRect() {
SkRRect rrect;
RandomSetRRect rrectType = makeSetRRectType();
if (fPrintName) {
SkDebugf("%.*s%s\n", fPathDepth * 3, fTab, gRandomSetRRectNames[rrectType]);
}
switch (rrectType) {
case kSetEmpty:
rrect.setEmpty();
break;
case kSetRect: {
SkRect rect = makeRect();
rrect.setRect(rect);
} break;
case kSetOval: {
SkRect oval = makeRect();
rrect.setOval(oval);
} break;
case kSetRectXY: {
SkRect rect = makeRect();
SkScalar xRad = makeScalar();
SkScalar yRad = makeScalar();
rrect.setRectXY(rect, xRad, yRad);
} break;
case kSetNinePatch: {
SkRect rect = makeRect();
SkScalar leftRad = makeScalar();
SkScalar topRad = makeScalar();
SkScalar rightRad = makeScalar();
SkScalar bottomRad = makeScalar();
rrect.setNinePatch(rect, leftRad, topRad, rightRad, bottomRad);
SkDebugf(""); // keep locals in scope
} break;
case kSetRectRadii: {
SkRect rect = makeRect();
SkVector radii[4];
makeVectorArray(SK_ARRAY_COUNT(radii), radii);
rrect.setRectRadii(rect, radii);
} break;
}
return rrect;
}
SkPath makePath() {
SkPath path;
for (uint32_t cIndex = 0; cIndex < fPathContourCount; ++cIndex) {
uint32_t segments = makeSegmentCount();
for (uint32_t sIndex = 0; sIndex < segments; ++sIndex) {
RandomAddPath addPathType = makeAddPathType();
++fAddCount;
if (fPrintName) {
SkDebugf("%.*s%s\n", fPathDepth * 3, fTab,
gRandomAddPathNames[addPathType]);
}
switch (addPathType) {
case kAddArc: {
SkRect oval = makeRect();
SkScalar startAngle = makeAngle();
SkScalar sweepAngle = makeAngle();
path.addArc(oval, startAngle, sweepAngle);
validate(path);
} break;
case kAddRoundRect1: {
SkRect rect = makeRect();
SkScalar rx = makeScalar(), ry = makeScalar();
SkPath::Direction dir = makeDirection();
path.addRoundRect(rect, rx, ry, dir);
validate(path);
} break;
case kAddRoundRect2: {
SkRect rect = makeRect();
SkScalar radii[8];
makeScalarArray(SK_ARRAY_COUNT(radii), radii);
SkPath::Direction dir = makeDirection();
path.addRoundRect(rect, radii, dir);
validate(path);
} break;
case kAddRRect: {
SkRRect rrect = makeRRect();
SkPath::Direction dir = makeDirection();
path.addRRect(rrect, dir);
validate(path);
} break;
case kAddPoly: {
SkTDArray<SkPoint> points;
makePointArray(&points);
bool close = makeBool();
path.addPoly(&points[0], points.count(), close);
validate(path);
} break;
case kAddPath1:
if (fPathDepth < fPathDepthLimit) {
++fPathDepth;
SkPath src = makePath();
validate(src);
SkScalar dx = makeScalar();
SkScalar dy = makeScalar();
SkPath::AddPathMode mode = makeAddPathMode();
path.addPath(src, dx, dy, mode);
--fPathDepth;
validate(path);
}
break;
case kAddPath2:
if (fPathDepth < fPathDepthLimit) {
++fPathDepth;
SkPath src = makePath();
validate(src);
SkPath::AddPathMode mode = makeAddPathMode();
path.addPath(src, mode);
--fPathDepth;
validate(path);
}
break;
case kAddPath3:
if (fPathDepth < fPathDepthLimit) {
++fPathDepth;
SkPath src = makePath();
validate(src);
SkMatrix matrix = makeMatrix();
SkPath::AddPathMode mode = makeAddPathMode();
path.addPath(src, matrix, mode);
--fPathDepth;
validate(path);
}
break;
case kReverseAddPath:
if (fPathDepth < fPathDepthLimit) {
++fPathDepth;
SkPath src = makePath();
validate(src);
path.reverseAddPath(src);
--fPathDepth;
validate(path);
}
break;
case kMoveToPath: {
SkScalar x = makeScalar();
SkScalar y = makeScalar();
path.moveTo(x, y);
validate(path);
} break;
case kRMoveToPath: {
SkScalar x = makeScalar();
SkScalar y = makeScalar();
path.rMoveTo(x, y);
validate(path);
} break;
case kLineToPath: {
SkScalar x = makeScalar();
SkScalar y = makeScalar();
path.lineTo(x, y);
validate(path);
} break;
case kRLineToPath: {
SkScalar x = makeScalar();
SkScalar y = makeScalar();
path.rLineTo(x, y);
validate(path);
} break;
case kQuadToPath: {
SkPoint pt[2];
makePointArray(SK_ARRAY_COUNT(pt), pt);
path.quadTo(pt[0], pt[1]);
validate(path);
} break;
case kRQuadToPath: {
SkPoint pt[2];
makePointArray(SK_ARRAY_COUNT(pt), pt);
path.rQuadTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY);
validate(path);
} break;
case kConicToPath: {
SkPoint pt[2];
makePointArray(SK_ARRAY_COUNT(pt), pt);
SkScalar weight = makeScalar();
path.conicTo(pt[0], pt[1], weight);
validate(path);
} break;
case kRConicToPath: {
SkPoint pt[2];
makePointArray(SK_ARRAY_COUNT(pt), pt);
SkScalar weight = makeScalar();
path.rConicTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY, weight);
validate(path);
} break;
case kCubicToPath: {
SkPoint pt[3];
makePointArray(SK_ARRAY_COUNT(pt), pt);
path.cubicTo(pt[0], pt[1], pt[2]);
validate(path);
} break;
case kRCubicToPath: {
SkPoint pt[3];
makePointArray(SK_ARRAY_COUNT(pt), pt);
path.rCubicTo(pt[0].fX, pt[0].fY, pt[1].fX, pt[1].fY, pt[2].fX, pt[2].fY);
validate(path);
} break;
case kArcToPath: {
SkPoint pt[2];
makePointArray(SK_ARRAY_COUNT(pt), pt);
SkScalar radius = makeScalar();
path.arcTo(pt[0], pt[1], radius);
validate(path);
} break;
case kArcTo2Path: {
SkRect oval = makeRect();
SkScalar startAngle = makeAngle();
SkScalar sweepAngle = makeAngle();
bool forceMoveTo = makeBool();
path.arcTo(oval, startAngle, sweepAngle, forceMoveTo);
validate(path);
} break;
case kClosePath:
path.close();
validate(path);
break;
}
}
}
return path;
}
uint32_t makeSegmentCount() {
return fRand.nextRangeU(1, fPathSegmentLimit);
}
RandomSetRRect makeSetRRectType() {
return (RandomSetRRect) fRand.nextRangeU(0, kRandomSetRRect_Last);
}
SkScalar makeScalar() {
SkScalar scalar;
scalar = fRand.nextRangeF(fFloatMin, fFloatMax);
return scalar;
}
void makeScalarArray(size_t arrayCount, SkScalar* array) {
for (size_t index = 0; index < arrayCount; ++index) {
array[index] = makeScalar();
}
}
void makeVectorArray(size_t arrayCount, SkVector* array) {
for (size_t index = 0; index < arrayCount; ++index) {
array[index] = makeVector();
}
}
SkVector makeVector() {
SkVector result;
makeScalarArray(2, &result.fX);
return result;
}
void validate(const SkPath& path) {
if (fValidate) {
SkDEBUGCODE(path.experimentalValidateRef());
}
}
SkRandom fRand;
SkMatrix fMatrix;
SkPath fClip;
SkPaint fPaint;
SkPath fPath;
SkScalar fFloatMin;
SkScalar fFloatMax;
uint32_t fPathContourCount;
int fPathDepth;
int fPathDepthLimit;
uint32_t fPathSegmentLimit;
int fAddCount;
bool fPrintName;
bool fStrokeOnly;
bool fValidate;
const char* fTab;
};
static bool contains_only_moveTo(const SkPath& path) {
int verbCount = path.countVerbs();
if (verbCount == 0) {
return true;
}
SkTDArray<uint8_t> verbs;
verbs.setCount(verbCount);
SkDEBUGCODE(int getVerbResult = ) path.getVerbs(verbs.begin(), verbCount);
SkASSERT(getVerbResult == verbCount);
for (int index = 0; index < verbCount; ++index) {
if (verbs[index] != SkPath::kMove_Verb) {
return false;
}
}
return true;
}
#include "SkGraphics.h"
#include "SkSurface.h"
#include "SkTaskGroup.h"
#include "SkTDArray.h"
static void path_fuzz_stroker(SkBitmap* bitmap, int seed) {
SkTaskGroup().batch(100, [&](int i) {
int localSeed = seed + i;
FuzzPath fuzzPath;
fuzzPath.setStrokeOnly();
fuzzPath.setSeed(localSeed);
fuzzPath.randomize();
const SkPath& path = fuzzPath.getPath();
const SkPaint& paint = fuzzPath.getPaint();
const SkImageInfo& info = bitmap->info();
SkCanvas* canvas(
SkCanvas::NewRasterDirect(info, bitmap->getPixels(), bitmap->rowBytes()));
int w = info.width() / 4;
int h = info.height() / 4;
int x = localSeed / 4 % 4;
int y = localSeed % 4;
SkRect clipBounds = SkRect::MakeXYWH(SkIntToScalar(x) * w, SkIntToScalar(y) * h,
SkIntToScalar(w), SkIntToScalar(h));
canvas->save();
canvas->clipRect(clipBounds);
canvas->translate(SkIntToScalar(x) * w, SkIntToScalar(y) * h);
canvas->drawPath(path, paint);
canvas->restore();
});
}
class PathFuzzView : public SampleView {
public:
PathFuzzView()
: fOneDraw(false)
{
}
protected:
// overrides from SkEventSink
bool onQuery(SkEvent* evt) override {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "PathFuzzer");
return true;
}
return this->INHERITED::onQuery(evt);
}
void onOnceBeforeDraw() override {
fIndex = 0;
SkImageInfo info(SkImageInfo::MakeN32Premul(SkScalarRoundToInt(width()),
SkScalarRoundToInt(height())));
offscreen.allocPixels(info);
path_fuzz_stroker(&offscreen, fIndex);
}
void onDrawContent(SkCanvas* canvas) override {
if (fOneDraw) {
fuzzPath.randomize();
const SkPath& path = fuzzPath.getPath();
const SkPaint& paint = fuzzPath.getPaint();
const SkPath& clip = fuzzPath.getClip();
const SkMatrix& matrix = fuzzPath.getMatrix();
if (!contains_only_moveTo(clip)) {
canvas->clipPath(clip);
}
canvas->setMatrix(matrix);
canvas->drawPath(path, paint);
} else {
path_fuzz_stroker(&offscreen, fIndex += 100);
canvas->drawBitmap(offscreen, 0, 0);
}
this->inval(nullptr);
}
private:
int fIndex;
SkBitmap offscreen;
FuzzPath fuzzPath;
bool fOneDraw;
typedef SkView INHERITED;
};
static SkView* MyFactory() { return new PathFuzzView; }
static SkViewRegister reg(MyFactory);
|