blob: 521e44be2f93044392aecd0b99cf9f2b32bb71cf [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"
12#include "SkLayerRasterizer.h"
13#include "SkPath.h"
14#include "SkSurface.h"
15#include "SkTypeface.h"
Mike Reedebfce6d2016-12-12 10:02:12 -050016#include "SkClipOpPriv.h"
Kevin Lubickfec1dea2016-11-22 13:57:18 -050017
18static const int kBmpSize = 24;
19static const int kMaxX = 250;
20static const int kMaxY = 250;
21static const int kPtsLen = 10;
22static const int kTxtLen = 5;
23
Kevin Lubickfec1dea2016-11-22 13:57:18 -050024static void init_string(Fuzz* fuzz, char* str, size_t bufSize) {
25 for (size_t i = 0; i < bufSize-1; ++i) {
26 fuzz->nextRange(&str[i], 0x20, 0x7E); // printable ASCII
27 }
28 str[bufSize-1] = '\0';
29}
30
31// make_paint mostly borrowed from FilterFuzz.cpp
32static void init_paint(Fuzz* fuzz, SkPaint* p) {
33 bool b;
34 fuzz->next(&b);
35 p->setAntiAlias(b);
36
37 uint8_t tmp_u8;
38 fuzz->nextRange(&tmp_u8, 0, (int)SkBlendMode::kLastMode);
39 p->setBlendMode(static_cast<SkBlendMode>(tmp_u8));
40
41 SkColor co;
42 fuzz->next(&co);
43 p->setColor(co);
44
45 fuzz->next(&b);
46 p->setDither(b);
47
48 fuzz->nextRange(&tmp_u8, 0, (int)kHigh_SkFilterQuality);
49 p->setFilterQuality(static_cast<SkFilterQuality>(tmp_u8));
50
51 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kFull_Hinting);
52 p->setHinting(static_cast<SkPaint::Hinting>(tmp_u8));
53
54 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Cap);
55 p->setStrokeCap(static_cast<SkPaint::Cap>(tmp_u8));
56
57 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Join);
58 p->setStrokeJoin(static_cast<SkPaint::Join>(tmp_u8));
59
60 SkScalar sc;
61 fuzz->next(&sc);
62 p->setStrokeMiter(sc);
63
64 fuzz->next(&sc);
65 p->setStrokeWidth(sc);
66
67 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kStrokeAndFill_Style);
68 p->setStyle(static_cast<SkPaint::Style>(tmp_u8));
69}
70
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050071static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) {
72 uint8_t colorType;
73 fuzz->nextRange(&colorType, 0, (int)kLastEnum_SkColorType);
Kevin Lubick8c8b6182017-02-17 10:27:30 -050074 bool b;
75 fuzz->next(&b);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050076 SkImageInfo info = SkImageInfo::Make(kBmpSize,
77 kBmpSize,
78 (SkColorType)colorType,
Kevin Lubick8c8b6182017-02-17 10:27:30 -050079 b ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050080 if (!bmp->tryAllocPixels(info)) {
81 SkDebugf("Bitmap not allocated\n");
82 }
Kevin Lubick8c8b6182017-02-17 10:27:30 -050083 SkColor c;
84 fuzz->next(&c);
85 bmp->eraseColor(c);
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050086
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050087 fuzz->next(&b);
88 SkPaint p;
89 if (b) {
90 init_paint(fuzz, &p);
91 }
92 else {
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050093 fuzz->next(&c);
94 p.setColor(c);
95 }
Kevin Lubickcdb4d3c2016-11-29 11:14:39 -050096}
Kevin Lubickfec1dea2016-11-22 13:57:18 -050097
98static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) {
99 uint8_t x, y;
100 fuzz->nextRange(&x, 1, kMaxX);
101 fuzz->nextRange(&y, 1, kMaxY);
102 *s = SkSurface::MakeRasterN32Premul(x, y);
103}
104
105
106static void fuzz_drawText(Fuzz* fuzz, sk_sp<SkTypeface> font) {
107 SkPaint p;
108 init_paint(fuzz, &p);
109 sk_sp<SkSurface> surface;
110 init_surface(fuzz, &surface);
111
112 char text[kTxtLen];
113 init_string(fuzz, text, kTxtLen);
114
115 SkScalar x, y;
116 fuzz->next(&x, &y);
117 // populate pts array
118 SkPoint pts[kPtsLen];
119 for (uint8_t i = 0; i < kPtsLen; ++i) {
120 pts[i].set(x, y);
121 x += p.getTextSize();
122 }
123
124 p.setTypeface(font);
125 // set text related attributes
126 bool b;
127 fuzz->next(&b);
128 p.setAutohinted(b);
129 fuzz->next(&b);
130 p.setDevKernText(b);
131 fuzz->next(&b);
132 p.setEmbeddedBitmapText(b);
133 fuzz->next(&b);
134 p.setFakeBoldText(b);
135 fuzz->next(&b);
136 p.setLCDRenderText(b);
137 fuzz->next(&b);
138 p.setLinearText(b);
139 fuzz->next(&b);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500140 p.setSubpixelText(b);
141 fuzz->next(&x);
142 p.setTextScaleX(x);
143 fuzz->next(&x);
144 p.setTextSkewX(x);
145 fuzz->next(&x);
146 p.setTextSize(x);
147 fuzz->next(&b);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500148 p.setVerticalText(b);
149
150 SkCanvas* cnv = surface->getCanvas();
151 cnv->drawPosText(text, (kTxtLen-1), pts, p);
152
153 fuzz->next(&x);
154 fuzz->next(&y);
155 cnv->drawText(text, (kTxtLen-1), x, y, p);
156}
157
158static void fuzz_drawCircle(Fuzz* fuzz) {
159 SkPaint p;
160 init_paint(fuzz, &p);
161 sk_sp<SkSurface> surface;
162 init_surface(fuzz, &surface);
163
164 SkScalar a, b, c;
165 fuzz->next(&a, &b, &c);
166 surface->getCanvas()->drawCircle(a, b, c, p);
167}
168
169static void fuzz_drawLine(Fuzz* fuzz) {
170 SkPaint p;
171 init_paint(fuzz, &p);
172 sk_sp<SkSurface> surface;
173 init_surface(fuzz, &surface);
174
175 SkScalar a, b, c, d;
176 fuzz->next(&a, &b, &c, &d);
177 surface->getCanvas()->drawLine(a, b, c, d, p);
178}
179
180static void fuzz_drawRect(Fuzz* fuzz) {
181 SkPaint p;
182 init_paint(fuzz, &p);
183 sk_sp<SkSurface> surface;
184 init_surface(fuzz, &surface);
185
186 SkScalar a, b, c, d;
187 fuzz->next(&a, &b, &c, &d);
188 SkRect r;
189 r = SkRect::MakeXYWH(a, b, c, d);
190
191 SkCanvas* cnv = surface->getCanvas();
192 cnv->drawRect(r, p);
193
194 bool bl;
195 fuzz->next(&bl);
196 fuzz->next(&a, &b, &c, &d);
197 r = SkRect::MakeXYWH(a, b, c, d);
Mike Reedc1f77742016-12-09 09:00:50 -0500198 cnv->clipRect(r, kIntersect_SkClipOp, bl);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500199}
200
201static void fuzz_drawPath(Fuzz* fuzz) {
202 SkPaint p;
203 init_paint(fuzz, &p);
204 sk_sp<SkSurface> surface;
205 init_surface(fuzz, &surface);
206
207 // TODO(kjlubick): put the ability to fuzz a path in shared file, with
208 // other common things (e.g. rects, lines)
209 uint8_t i, j;
210 fuzz->nextRange(&i, 0, 10); // set i to number of operations to perform
211 SkPath path;
212 SkScalar a, b, c, d, e, f;
213 for (int k = 0; k < i; ++k) {
214 fuzz->nextRange(&j, 0, 5); // set j to choose operation to perform
215 switch (j) {
216 case 0:
217 fuzz->next(&a, &b);
218 path.moveTo(a, b);
219 break;
220 case 1:
221 fuzz->next(&a, &b);
222 path.lineTo(a, b);
223 break;
224 case 2:
225 fuzz->next(&a, &b, &c, &d);
226 path.quadTo(a, b, c, d);
227 break;
228 case 3:
229 fuzz->next(&a, &b, &c, &d, &e);
230 path.conicTo(a, b, c, d, e);
231 break;
232 case 4:
233 fuzz->next(&a, &b, &c, &d, &e, &f);
234 path.cubicTo(a, b, c, d, e, f);
235 break;
236 case 5:
237 fuzz->next(&a, &b, &c, &d, &e);
238 path.arcTo(a, b, c, d, e);
239 break;
240 }
241 }
242 path.close();
243
244 SkCanvas* cnv = surface->getCanvas();
245 cnv->drawPath(path, p);
246
247 bool bl;
248 fuzz->next(&bl);
Mike Reedc1f77742016-12-09 09:00:50 -0500249 cnv->clipPath(path, kIntersect_SkClipOp, bl);
Kevin Lubickfec1dea2016-11-22 13:57:18 -0500250}
251
252static void fuzz_drawBitmap(Fuzz* fuzz) {
253 SkPaint p;
254 init_paint(fuzz, &p);
255 sk_sp<SkSurface> surface;
256 init_surface(fuzz, &surface);
257 SkBitmap bmp;
258 init_bitmap(fuzz, &bmp);
259
260 SkScalar a, b;
261 fuzz->next(&a, &b);
262 surface->getCanvas()->drawBitmap(bmp, a, b, &p);
263}
264
265static void fuzz_drawImage(Fuzz* fuzz) {
266 SkPaint p;
267 init_paint(fuzz, &p);
268 sk_sp<SkSurface> surface;
269 init_surface(fuzz, &surface);
270 SkBitmap bmp;
271 init_bitmap(fuzz, &bmp);
272
273 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bmp));
274
275 bool bl;
276 fuzz->next(&bl);
277 SkScalar a, b;
278 fuzz->next(&a, &b);
279 if (bl) {
280 surface->getCanvas()->drawImage(image, a, b, &p);
281 }
282 else {
283 SkRect dst = SkRect::MakeWH(a, b);
284 fuzz->next(&a, &b);
285 SkRect src = SkRect::MakeWH(a, b);
286 uint8_t x;
287 fuzz->nextRange(&x, 0, 1);
288 SkCanvas::SrcRectConstraint cst = (SkCanvas::SrcRectConstraint)x;
289 surface->getCanvas()->drawImageRect(image, src, dst, &p, cst);
290 }
291}
292
293static void fuzz_drawPaint(Fuzz* fuzz) {
294 SkPaint l, p;
295 init_paint(fuzz, &p);
296 sk_sp<SkSurface> surface;
297 init_surface(fuzz, &surface);
298
299 // add layers
300 uint8_t x;
301 fuzz->nextRange(&x, 1, 3); // max 3 layers
302 SkLayerRasterizer::Builder builder;
303 for (int i = 0; i < x; i++) {
304 init_paint(fuzz, &l);
305 builder.addLayer(l);
306 }
307
308 sk_sp<SkLayerRasterizer> raster(builder.detach());
309 p.setRasterizer(raster);
310
311 surface->getCanvas()->drawPaint(p);
312}
313
314DEF_FUZZ(DrawFunctions, fuzz) {
315 uint8_t i;
316 fuzz->next(&i);
317
318 switch(i) {
319 case 0: {
320 sk_sp<SkTypeface> f = SkTypeface::MakeDefault();
321 if (f == nullptr) {
322 SkDebugf("Could not initialize font.\n");
323 fuzz->signalBug();
324 }
325 SkDebugf("Fuzz DrawText\n");
326 fuzz_drawText(fuzz, f);
327 return;
328 }
329 case 1:
330 SkDebugf("Fuzz DrawRect\n");
331 fuzz_drawRect(fuzz);
332 return;
333 case 2:
334 SkDebugf("Fuzz DrawCircle\n");
335 fuzz_drawCircle(fuzz);
336 return;
337 case 3:
338 SkDebugf("Fuzz DrawLine\n");
339 fuzz_drawLine(fuzz);
340 return;
341 case 4:
342 SkDebugf("Fuzz DrawPath\n");
343 fuzz_drawPath(fuzz);
344 return;
345 case 5:
346 SkDebugf("Fuzz DrawImage/DrawImageRect\n");
347 fuzz_drawImage(fuzz);
348 return;
349 case 6:
350 SkDebugf("Fuzz DrawBitmap\n");
351 fuzz_drawBitmap(fuzz);
352 return;
353 case 7:
354 SkDebugf("Fuzz DrawPaint\n");
355 fuzz_drawPaint(fuzz);
356 return;
357 }
358}