blob: f20352436c423108f7a7ff0513efc77e9c84e230 [file] [log] [blame]
kjlubicke5654502016-07-19 16:50:03 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Fuzz.h"
9#include "SkPath.h"
10#include "SkPathOps.h"
11
12const int kLastOp = SkPathOp::kReverseDifference_SkPathOp;
13
14void BuildPath(Fuzz* fuzz,
15 SkPath* path,
16 int last_verb) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040017 while (!fuzz->exhausted()) {
Kevin Lubick416b2482016-11-10 16:17:49 -050018 // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint"
19 // smaller, which leads to more efficient fuzzing.
20 uint8_t operation;
21 fuzz->next(&operation);
22 SkScalar a,b,c,d,e,f;
kjlubicke5654502016-07-19 16:50:03 -070023
24 switch (operation % (last_verb + 1)) {
25 case SkPath::Verb::kMove_Verb:
Kevin Lubick416b2482016-11-10 16:17:49 -050026 fuzz->next(&a, &b);
27 path->moveTo(a, b);
kjlubicke5654502016-07-19 16:50:03 -070028 break;
29
30 case SkPath::Verb::kLine_Verb:
Kevin Lubick416b2482016-11-10 16:17:49 -050031 fuzz->next(&a, &b);
32 path->lineTo(a, b);
kjlubicke5654502016-07-19 16:50:03 -070033 break;
34
35 case SkPath::Verb::kQuad_Verb:
Kevin Lubick416b2482016-11-10 16:17:49 -050036 fuzz->next(&a, &b, &c, &d);
37 path->quadTo(a, b, c, d);
kjlubicke5654502016-07-19 16:50:03 -070038 break;
39
40 case SkPath::Verb::kConic_Verb:
Kevin Lubick416b2482016-11-10 16:17:49 -050041 fuzz->next(&a, &b, &c, &d, &e);
42 path->conicTo(a, b, c, d, e);
kjlubicke5654502016-07-19 16:50:03 -070043 break;
44
45 case SkPath::Verb::kCubic_Verb:
Kevin Lubick416b2482016-11-10 16:17:49 -050046 fuzz->next(&a, &b, &c, &d, &e, &f);
47 path->cubicTo(a, b, c, d, e, f);
kjlubicke5654502016-07-19 16:50:03 -070048 break;
49
50 case SkPath::Verb::kClose_Verb:
51 path->close();
52 break;
53
54 case SkPath::Verb::kDone_Verb:
55 // In this case, simply exit.
56 return;
57 }
58 }
59}
60
61DEF_FUZZ(Pathop, fuzz) {
62 SkOpBuilder builder;
kjlubicke5654502016-07-19 16:50:03 -070063
Kevin Lubick416b2482016-11-10 16:17:49 -050064 uint8_t stragglerOp;
65 fuzz->next(&stragglerOp);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040066 SkPath path;
67
68 BuildPath(fuzz, &path, SkPath::Verb::kDone_Verb);
69 builder.add(path, static_cast<SkPathOp>(stragglerOp % (kLastOp + 1)));
kjlubicke5654502016-07-19 16:50:03 -070070
71 SkPath result;
72 builder.resolve(&result);
73}