blob: b73bbf9217415839b5797b2f183a4fbecc4547bf [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"
16
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
23static void init_bitmap(Fuzz* fuzz, SkBitmap* bmp) {
24 uint8_t colorType;
25 fuzz->nextRange(&colorType, 0, (int)kLastEnum_SkColorType);
26 SkImageInfo info = SkImageInfo::Make(kBmpSize,
27 kBmpSize,
28 (SkColorType)colorType,
29 kPremul_SkAlphaType);
30 if (!bmp->tryAllocPixels(info)) {
31 SkDebugf("Bitmap not allocated\n");
32 }
33 bool b;
34 fuzz->next(&b);
35 if (b) { // initialize
36 SkCanvas canvas(*bmp);
37 canvas.clear(0);
38 SkColor c;
39 fuzz->next(&c);
40 SkPaint p; // TODO: maybe init_paint?
41 p.setColor(c);
42 canvas.drawRect(SkRect::MakeXYWH(0, 0, kBmpSize, kBmpSize), p);
43 }
44}
45
46static void init_string(Fuzz* fuzz, char* str, size_t bufSize) {
47 for (size_t i = 0; i < bufSize-1; ++i) {
48 fuzz->nextRange(&str[i], 0x20, 0x7E); // printable ASCII
49 }
50 str[bufSize-1] = '\0';
51}
52
53// make_paint mostly borrowed from FilterFuzz.cpp
54static void init_paint(Fuzz* fuzz, SkPaint* p) {
55 bool b;
56 fuzz->next(&b);
57 p->setAntiAlias(b);
58
59 uint8_t tmp_u8;
60 fuzz->nextRange(&tmp_u8, 0, (int)SkBlendMode::kLastMode);
61 p->setBlendMode(static_cast<SkBlendMode>(tmp_u8));
62
63 SkColor co;
64 fuzz->next(&co);
65 p->setColor(co);
66
67 fuzz->next(&b);
68 p->setDither(b);
69
70 fuzz->nextRange(&tmp_u8, 0, (int)kHigh_SkFilterQuality);
71 p->setFilterQuality(static_cast<SkFilterQuality>(tmp_u8));
72
73 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kFull_Hinting);
74 p->setHinting(static_cast<SkPaint::Hinting>(tmp_u8));
75
76 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Cap);
77 p->setStrokeCap(static_cast<SkPaint::Cap>(tmp_u8));
78
79 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kLast_Join);
80 p->setStrokeJoin(static_cast<SkPaint::Join>(tmp_u8));
81
82 SkScalar sc;
83 fuzz->next(&sc);
84 p->setStrokeMiter(sc);
85
86 fuzz->next(&sc);
87 p->setStrokeWidth(sc);
88
89 fuzz->nextRange(&tmp_u8, 0, (int)SkPaint::kStrokeAndFill_Style);
90 p->setStyle(static_cast<SkPaint::Style>(tmp_u8));
91}
92
93
94static void init_surface(Fuzz* fuzz, sk_sp<SkSurface>* s) {
95 uint8_t x, y;
96 fuzz->nextRange(&x, 1, kMaxX);
97 fuzz->nextRange(&y, 1, kMaxY);
98 *s = SkSurface::MakeRasterN32Premul(x, y);
99}
100
101
102static void fuzz_drawText(Fuzz* fuzz, sk_sp<SkTypeface> font) {
103 SkPaint p;
104 init_paint(fuzz, &p);
105 sk_sp<SkSurface> surface;
106 init_surface(fuzz, &surface);
107
108 char text[kTxtLen];
109 init_string(fuzz, text, kTxtLen);
110
111 SkScalar x, y;
112 fuzz->next(&x, &y);
113 // populate pts array
114 SkPoint pts[kPtsLen];
115 for (uint8_t i = 0; i < kPtsLen; ++i) {
116 pts[i].set(x, y);
117 x += p.getTextSize();
118 }
119
120 p.setTypeface(font);
121 // set text related attributes
122 bool b;
123 fuzz->next(&b);
124 p.setAutohinted(b);
125 fuzz->next(&b);
126 p.setDevKernText(b);
127 fuzz->next(&b);
128 p.setEmbeddedBitmapText(b);
129 fuzz->next(&b);
130 p.setFakeBoldText(b);
131 fuzz->next(&b);
132 p.setLCDRenderText(b);
133 fuzz->next(&b);
134 p.setLinearText(b);
135 fuzz->next(&b);
136 p.setStrikeThruText(b);
137 fuzz->next(&b);
138 p.setSubpixelText(b);
139 fuzz->next(&x);
140 p.setTextScaleX(x);
141 fuzz->next(&x);
142 p.setTextSkewX(x);
143 fuzz->next(&x);
144 p.setTextSize(x);
145 fuzz->next(&b);
146 p.setUnderlineText(b);
147 fuzz->next(&b);
148 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);
198 cnv->clipRect(r, SkCanvas::kIntersect_Op, bl);
199}
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);
249 cnv->clipPath(path, SkCanvas::kIntersect_Op, bl);
250}
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}