blob: 3eeed1c4a360d5e114c59113eb4592c1f2e75d49 [file] [log] [blame]
Kevin Lubickfec1dea2016-11-22 13:57:18 -05001/*
2 * Copyright 2016 Mozilla Foundation
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 "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkImage.h"
Kevin Lubickfec1dea2016-11-22 13:57:18 -050012#include "SkPath.h"
13#include "SkSurface.h"
14#include "SkTypeface.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050015#include "SkClipOpPriv.h"
Kevin Lubickfec1dea2016-11-22 13:57:18 -050016
17static const int kBmpSize = 24;
18static const int kMaxX = 250;
19static const int kMaxY = 250;
20static const int kPtsLen = 10;
21static const int kTxtLen = 5;
22
Kevin Lubickfec1dea2016-11-22 13:57:18 -050023static void init_string(Fuzz* fuzz, char* str, size_t bufSize) {
24 for (size_t i = 0; i < bufSize-1; ++i) {
25 fuzz->nextRange(&str[i], 0x20, 0x7E); // printable ASCII
26 }
27 str[bufSize-1] = '\0';
28}
29
30// make_paint mostly borrowed from FilterFuzz.cpp
31static void init_paint(Fuzz* fuzz, SkPaint* p) {
32 bool b;
33 fuzz->next(&b);
34 p->setAntiAlias(b);
35
36 uint8_t tmp_u8;
37 fuzz->nextRange(&tmp_u8, 0, (int)SkBlendMode::kLastMode);
38 p->setBlendMode(static_cast<SkBlendMode>(tmp_u8));
39
40 SkColor co;
41 fuzz->next(&co);
42 p->setColor(co);
43
44 fuzz->next(&b);
45 p->setDither(b);
46
47 fuzz->nextRange(&tmp_u8, 0, (int)kHigh_SkFilterQuality);
48 p->setFilterQuality(static_cast<SkFilterQuality>(tmp_u8));
49
50 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kFull_Hinting);
51 p->setHinting(static_cast<SkPaint::Hinting>(tmp_u8));
52
53 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Cap);
54 p->setStrokeCap(static_cast<SkPaint::Cap>(tmp_u8));
55
56 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Join);
57 p->setStrokeJoin(static_cast<SkPaint::Join>(tmp_u8));
58
59 SkScalar sc;
60 fuzz->next(&sc);
61 p->setStrokeMiter(sc);
62
63 fuzz->next(&sc);
64 p->setStrokeWidth(sc);
65
66 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kStrokeAndFill_Style);
67 p->setStyle(static_cast<SkPaint::Style>(tmp_u8));
68}
69
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050070static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) {
71 uint8_t colorType;
72 fuzz->nextRange(&colorType, 0, (int)kLastEnum_SkColorType);
Kevin Lubick5d5601c2017-02-21 16:06:19 -050073 // ColorType needs to match what the system configuration is.
74 if (colorType == kRGBA_8888_SkColorType || colorType == kBGRA_8888_SkColorType) {
75 colorType = kN32_SkColorType;
76 }
Kevin Lubick8c8b6182017-02-17 10:27:30 -050077 bool b;
78 fuzz->next(&b);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050079 SkImageInfo info = SkImageInfo::Make(kBmpSize,
80 kBmpSize,
81 (SkColorType)colorType,
Kevin Lubick8c8b6182017-02-17 10:27:30 -050082 b ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050083 if (!bmp->tryAllocPixels(info)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -040084 SkDEBUGF("Bitmap not allocated\n");
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050085 }
Kevin Lubick8c8b6182017-02-17 10:27:30 -050086 SkColor c;
87 fuzz->next(&c);
88 bmp->eraseColor(c);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050089
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050090 fuzz->next(&b);
91 SkPaint p;
92 if (b) {
93 init_paint(fuzz, &p);
94 }
95 else {
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050096 fuzz->next(&c);
97 p.setColor(c);
98 }
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050099}
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500100
101static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) {
102 uint8_t x, y;
103 fuzz->nextRange(&x, 1, kMaxX);
104 fuzz->nextRange(&y, 1, kMaxY);
105 *s = SkSurface::MakeRasterN32Premul(x, y);
Kevin Lubick1991f552018-02-27 10:59:10 -0500106
107 if (!*s) {
108 // Was possibly too big for the memory constrained fuzzing environments
109 *s = SkSurface::MakeNull(x, y);
110 }
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500111}
112
113
114static void fuzz_drawText(Fuzz* fuzz, sk_sp<SkTypeface> font) {
115 SkPaint p;
116 init_paint(fuzz, &p);
117 sk_sp<SkSurface> surface;
118 init_surface(fuzz, &surface);
119
120 char text[kTxtLen];
121 init_string(fuzz, text, kTxtLen);
122
123 SkScalar x, y;
124 fuzz->next(&x, &y);
125 // populate pts array
126 SkPoint pts[kPtsLen];
127 for (uint8_t i = 0; i < kPtsLen; ++i) {
128 pts[i].set(x, y);
129 x += p.getTextSize();
130 }
131
132 p.setTypeface(font);
133 // set text related attributes
134 bool b;
135 fuzz->next(&b);
136 p.setAutohinted(b);
137 fuzz->next(&b);
138 p.setDevKernText(b);
139 fuzz->next(&b);
140 p.setEmbeddedBitmapText(b);
141 fuzz->next(&b);
142 p.setFakeBoldText(b);
143 fuzz->next(&b);
144 p.setLCDRenderText(b);
145 fuzz->next(&b);
146 p.setLinearText(b);
147 fuzz->next(&b);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500148 p.setSubpixelText(b);
149 fuzz->next(&x);
150 p.setTextScaleX(x);
151 fuzz->next(&x);
152 p.setTextSkewX(x);
153 fuzz->next(&x);
154 p.setTextSize(x);
155 fuzz->next(&b);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500156 p.setVerticalText(b);
157
158 SkCanvas* cnv = surface->getCanvas();
159 cnv->drawPosText(text, (kTxtLen-1), pts, p);
160
161 fuzz->next(&x);
162 fuzz->next(&y);
163 cnv->drawText(text, (kTxtLen-1), x, y, p);
164}
165
166static void fuzz_drawCircle(Fuzz* fuzz) {
167 SkPaint p;
168 init_paint(fuzz, &p);
169 sk_sp<SkSurface> surface;
170 init_surface(fuzz, &surface);
171
172 SkScalar a, b, c;
173 fuzz->next(&a, &b, &c);
174 surface->getCanvas()->drawCircle(a, b, c, p);
175}
176
177static void fuzz_drawLine(Fuzz* fuzz) {
178 SkPaint p;
179 init_paint(fuzz, &p);
180 sk_sp<SkSurface> surface;
181 init_surface(fuzz, &surface);
182
183 SkScalar a, b, c, d;
184 fuzz->next(&a, &b, &c, &d);
185 surface->getCanvas()->drawLine(a, b, c, d, p);
186}
187
188static void fuzz_drawRect(Fuzz* fuzz) {
189 SkPaint p;
190 init_paint(fuzz, &p);
191 sk_sp<SkSurface> surface;
192 init_surface(fuzz, &surface);
193
194 SkScalar a, b, c, d;
195 fuzz->next(&a, &b, &c, &d);
196 SkRect r;
197 r = SkRect::MakeXYWH(a, b, c, d);
198
199 SkCanvas* cnv = surface->getCanvas();
200 cnv->drawRect(r, p);
201
202 bool bl;
203 fuzz->next(&bl);
204 fuzz->next(&a, &b, &c, &d);
205 r = SkRect::MakeXYWH(a, b, c, d);
Mike Reedc1f77742016-12-09 09:00:50 -0500206 cnv->clipRect(r, kIntersect_SkClipOp, bl);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500207}
208
209static void fuzz_drawPath(Fuzz* fuzz) {
210 SkPaint p;
211 init_paint(fuzz, &p);
212 sk_sp<SkSurface> surface;
213 init_surface(fuzz, &surface);
214
215 // TODO(kjlubick): put the ability to fuzz a path in shared file, with
216 // other common things (e.g. rects, lines)
217 uint8_t i, j;
218 fuzz->nextRange(&i, 0, 10); // set i to number of operations to perform
219 SkPath path;
220 SkScalar a, b, c, d, e, f;
221 for (int k = 0; k < i; ++k) {
222 fuzz->nextRange(&j, 0, 5); // set j to choose operation to perform
223 switch (j) {
224 case 0:
225 fuzz->next(&a, &b);
226 path.moveTo(a, b);
227 break;
228 case 1:
229 fuzz->next(&a, &b);
230 path.lineTo(a, b);
231 break;
232 case 2:
233 fuzz->next(&a, &b, &c, &d);
234 path.quadTo(a, b, c, d);
235 break;
236 case 3:
237 fuzz->next(&a, &b, &c, &d, &e);
238 path.conicTo(a, b, c, d, e);
239 break;
240 case 4:
241 fuzz->next(&a, &b, &c, &d, &e, &f);
242 path.cubicTo(a, b, c, d, e, f);
243 break;
244 case 5:
245 fuzz->next(&a, &b, &c, &d, &e);
246 path.arcTo(a, b, c, d, e);
247 break;
248 }
249 }
250 path.close();
251
252 SkCanvas* cnv = surface->getCanvas();
253 cnv->drawPath(path, p);
254
255 bool bl;
256 fuzz->next(&bl);
Mike Reedc1f77742016-12-09 09:00:50 -0500257 cnv->clipPath(path, kIntersect_SkClipOp, bl);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500258}
259
260static void fuzz_drawBitmap(Fuzz* fuzz) {
261 SkPaint p;
262 init_paint(fuzz, &p);
263 sk_sp<SkSurface> surface;
264 init_surface(fuzz, &surface);
265 SkBitmap bmp;
266 init_bitmap(fuzz, &bmp);
267
268 SkScalar a, b;
269 fuzz->next(&a, &b);
270 surface->getCanvas()->drawBitmap(bmp, a, b, &p);
271}
272
273static void fuzz_drawImage(Fuzz* fuzz) {
274 SkPaint p;
275 init_paint(fuzz, &p);
276 sk_sp<SkSurface> surface;
277 init_surface(fuzz, &surface);
278 SkBitmap bmp;
279 init_bitmap(fuzz, &bmp);
280
281 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bmp));
282
283 bool bl;
284 fuzz->next(&bl);
285 SkScalar a, b;
286 fuzz->next(&a, &b);
287 if (bl) {
288 surface->getCanvas()->drawImage(image, a, b, &p);
289 }
290 else {
291 SkRect dst = SkRect::MakeWH(a, b);
292 fuzz->next(&a, &b);
293 SkRect src = SkRect::MakeWH(a, b);
294 uint8_t x;
295 fuzz->nextRange(&x, 0, 1);
296 SkCanvas::SrcRectConstraint cst = (SkCanvas::SrcRectConstraint)x;
297 surface->getCanvas()->drawImageRect(image, src, dst, &p, cst);
298 }
299}
300
301static void fuzz_drawPaint(Fuzz* fuzz) {
302 SkPaint l, p;
303 init_paint(fuzz, &p);
304 sk_sp<SkSurface> surface;
305 init_surface(fuzz, &surface);
306
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500307 surface->getCanvas()->drawPaint(p);
308}
309
310DEF_FUZZ(DrawFunctions, fuzz) {
311 uint8_t i;
312 fuzz->next(&i);
313
314 switch(i) {
315 case 0: {
316 sk_sp<SkTypeface> f = SkTypeface::MakeDefault();
317 if (f == nullptr) {
318 SkDebugf("Could not initialize font.\n");
319 fuzz->signalBug();
320 }
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400321 SkDEBUGF("Fuzz DrawText\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500322 fuzz_drawText(fuzz, f);
323 return;
324 }
325 case 1:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400326 SkDEBUGF("Fuzz DrawRect\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500327 fuzz_drawRect(fuzz);
328 return;
329 case 2:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400330 SkDEBUGF("Fuzz DrawCircle\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500331 fuzz_drawCircle(fuzz);
332 return;
333 case 3:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400334 SkDEBUGF("Fuzz DrawLine\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500335 fuzz_drawLine(fuzz);
336 return;
337 case 4:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400338 SkDEBUGF("Fuzz DrawPath\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500339 fuzz_drawPath(fuzz);
340 return;
341 case 5:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400342 SkDEBUGF("Fuzz DrawImage/DrawImageRect\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500343 fuzz_drawImage(fuzz);
344 return;
345 case 6:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400346 SkDEBUGF("Fuzz DrawBitmap\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500347 fuzz_drawBitmap(fuzz);
348 return;
349 case 7:
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400350 SkDEBUGF("Fuzz DrawPaint\n");
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500351 fuzz_drawPaint(fuzz);
352 return;
353 }
354}