OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Fuzz.h" | 8 #include "Fuzz.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 !fuzz->next(&transY) || | 25 !fuzz->next(&transY) || |
26 !fuzz->next(&persp0) || | 26 !fuzz->next(&persp0) || |
27 !fuzz->next(&persp1) || | 27 !fuzz->next(&persp1) || |
28 !fuzz->next(&persp2)) { | 28 !fuzz->next(&persp2)) { |
29 return false; | 29 return false; |
30 } | 30 } |
31 m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, pers
p2); | 31 m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, pers
p2); |
32 return true; | 32 return true; |
33 } | 33 } |
34 | 34 |
35 bool initGradientParams(Fuzz* fuzz, uint32_t* count, SkColor** colors, SkScalar*
* pos, | 35 bool initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors, |
36 SkShader::TileMode* mode) { | 36 std::vector<SkScalar>* pos, SkShader::TileMode* mode) { |
37 if (fuzz->remaining() < sizeof(uint32_t)) { | 37 if (fuzz->remaining() < sizeof(uint32_t)) { |
38 return false; | 38 return false; |
39 } | 39 } |
40 uint32_t t_count; | 40 uint32_t count = fuzz->nextRangeU(0, MAX_COUNT); |
41 SkColor* t_colors; | |
42 SkScalar* t_pos; | |
43 | 41 |
44 t_count = fuzz->nextRangeU(0, MAX_COUNT); | 42 if (fuzz->remaining() < sizeof(uint32_t)) { |
45 if (t_count == 1) { | |
46 t_count = 2; | |
47 } | |
48 | |
49 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar)))
) { | |
50 return false; | 43 return false; |
51 } | 44 } |
52 t_colors = new SkColor[t_count]; | 45 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); |
53 t_pos = new SkScalar[t_count]; | 46 |
54 for (uint32_t i = 0; i < t_count; i++) { | 47 colors->clear(); |
55 fuzz->next(&t_colors[i]); | 48 pos ->clear(); |
56 fuzz->next(&t_pos[i]); | 49 for (uint32_t i = 0; i < count; i++) { |
| 50 SkColor c; |
| 51 SkScalar s; |
| 52 if (!fuzz->next(&c) || !fuzz->next(&s)) { |
| 53 return false; |
| 54 } |
| 55 colors->push_back(c); |
| 56 pos ->push_back(s); |
57 } | 57 } |
58 | 58 if (count) { |
59 if (t_count == 0) { | 59 std::sort(pos->begin(), pos->end()); |
60 *count = 0; | 60 // The order matters. If count == 1, we want pos == 0. |
61 *colors = NULL; | 61 (*pos)[count - 1] = 1; |
62 *pos = NULL; | 62 (*pos)[0] = 0; |
63 } else { | |
64 std::sort(t_pos, t_pos + t_count); | |
65 t_pos[0] = 0; | |
66 t_pos[t_count - 1] = 1; | |
67 *count = t_count; | |
68 *colors = t_colors; | |
69 *pos = t_pos; | |
70 } | 63 } |
71 | |
72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); | |
73 return true; | 64 return true; |
74 } | 65 } |
75 | 66 |
76 void fuzzLinearGradient(Fuzz* fuzz) { | 67 void fuzzLinearGradient(Fuzz* fuzz) { |
77 SkScalar a, b, c, d; | 68 SkScalar a, b, c, d; |
78 bool useLocalMatrix, useGlobalMatrix; | 69 bool useLocalMatrix, useGlobalMatrix; |
79 if (!fuzz->next(&a) || | 70 if (!fuzz->next(&a) || |
80 !fuzz->next(&b) || | 71 !fuzz->next(&b) || |
81 !fuzz->next(&c) || | 72 !fuzz->next(&c) || |
82 !fuzz->next(&d) || | 73 !fuzz->next(&d) || |
83 !fuzz->next(&useLocalMatrix) || | 74 !fuzz->next(&useLocalMatrix) || |
84 !fuzz->next(&useGlobalMatrix)) { | 75 !fuzz->next(&useGlobalMatrix)) { |
85 return; | 76 return; |
86 } | 77 } |
87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; | 78 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; |
88 | 79 |
89 uint32_t count; | 80 std::vector<SkColor> colors; |
90 SkColor* colors; | 81 std::vector<SkScalar> pos; |
91 SkScalar* pos; | |
92 SkShader::TileMode mode; | 82 SkShader::TileMode mode; |
93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 83 if (!initGradientParams(fuzz, &colors, &pos, &mode)) { |
94 return; | 84 return; |
95 } | 85 } |
96 | 86 |
97 SkPaint p; | 87 SkPaint p; |
98 uint32_t flags; | 88 uint32_t flags; |
99 if (!fuzz->next(&flags)) { | 89 if (!fuzz->next(&flags)) { |
100 return; | 90 return; |
101 } | 91 } |
102 | 92 |
103 SkTLazy<SkMatrix> localMatrix; | 93 SkTLazy<SkMatrix> localMatrix; |
104 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 94 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
105 return; | 95 return; |
106 } | 96 } |
107 p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, mode, | 97 p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), |
108 flags, localMatrix.getMaybeNull())); | 98 colors.size(), mode, flags, localMatrix.getMaybeNull())); |
109 | 99 |
110 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 100 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
111 if (useGlobalMatrix) { | 101 if (useGlobalMatrix) { |
112 SkMatrix gm; | 102 SkMatrix gm; |
113 if (!makeMatrix(fuzz, &gm)) { | 103 if (!makeMatrix(fuzz, &gm)) { |
114 return; | 104 return; |
115 } | 105 } |
116 SkCanvas* c = surface->getCanvas(); | 106 SkCanvas* c = surface->getCanvas(); |
117 c->setMatrix(gm); | 107 c->setMatrix(gm); |
118 c->drawPaint(p); | 108 c->drawPaint(p); |
119 } else { | 109 } else { |
120 surface->getCanvas()->drawPaint(p); | 110 surface->getCanvas()->drawPaint(p); |
121 } | 111 } |
122 } | 112 } |
123 | 113 |
124 void fuzzRadialGradient(Fuzz* fuzz) { | 114 void fuzzRadialGradient(Fuzz* fuzz) { |
125 SkScalar a, b, radius; | 115 SkScalar a, b, radius; |
126 bool useLocalMatrix, useGlobalMatrix; | 116 bool useLocalMatrix, useGlobalMatrix; |
127 if (!fuzz->next(&a) || | 117 if (!fuzz->next(&a) || |
128 !fuzz->next(&b) || | 118 !fuzz->next(&b) || |
129 !fuzz->next(&radius) || | 119 !fuzz->next(&radius) || |
130 !fuzz->next(&useLocalMatrix) || | 120 !fuzz->next(&useLocalMatrix) || |
131 !fuzz->next(&useGlobalMatrix)) { | 121 !fuzz->next(&useGlobalMatrix)) { |
132 return; | 122 return; |
133 } | 123 } |
134 SkPoint center = SkPoint::Make(a,b); | 124 SkPoint center = SkPoint::Make(a,b); |
135 | 125 |
136 uint32_t count; | 126 std::vector<SkColor> colors; |
137 SkColor* colors; | 127 std::vector<SkScalar> pos; |
138 SkScalar* pos; | |
139 SkShader::TileMode mode; | 128 SkShader::TileMode mode; |
140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 129 if (!initGradientParams(fuzz, &colors, &pos, &mode)) { |
141 return; | 130 return; |
142 } | 131 } |
143 | 132 |
144 SkPaint p; | 133 SkPaint p; |
145 uint32_t flags; | 134 uint32_t flags; |
146 if (!fuzz->next(&flags)) { | 135 if (!fuzz->next(&flags)) { |
147 return; | 136 return; |
148 } | 137 } |
149 | 138 |
150 SkTLazy<SkMatrix> localMatrix; | 139 SkTLazy<SkMatrix> localMatrix; |
151 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 140 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
152 return; | 141 return; |
153 } | 142 } |
154 p.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, | 143 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(), |
155 count, mode, flags, localMatrix.getMaybeNull())); | 144 pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull()))
; |
156 | 145 |
157 | 146 |
158 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 147 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
159 if (useGlobalMatrix) { | 148 if (useGlobalMatrix) { |
160 SkMatrix gm; | 149 SkMatrix gm; |
161 if (!makeMatrix(fuzz, &gm)) { | 150 if (!makeMatrix(fuzz, &gm)) { |
162 return; | 151 return; |
163 } | 152 } |
164 SkCanvas* c = surface->getCanvas(); | 153 SkCanvas* c = surface->getCanvas(); |
165 c->setMatrix(gm); | 154 c->setMatrix(gm); |
(...skipping 12 matching lines...) Expand all Loading... |
178 !fuzz->next(&c) || | 167 !fuzz->next(&c) || |
179 !fuzz->next(&d) || | 168 !fuzz->next(&d) || |
180 !fuzz->next(&endRadius) || | 169 !fuzz->next(&endRadius) || |
181 !fuzz->next(&useLocalMatrix) || | 170 !fuzz->next(&useLocalMatrix) || |
182 !fuzz->next(&useGlobalMatrix)) { | 171 !fuzz->next(&useGlobalMatrix)) { |
183 return; | 172 return; |
184 } | 173 } |
185 SkPoint start = SkPoint::Make(a, b); | 174 SkPoint start = SkPoint::Make(a, b); |
186 SkPoint end = SkPoint::Make(c, d); | 175 SkPoint end = SkPoint::Make(c, d); |
187 | 176 |
188 uint32_t count; | 177 std::vector<SkColor> colors; |
189 SkColor* colors; | 178 std::vector<SkScalar> pos; |
190 SkScalar* pos; | |
191 SkShader::TileMode mode; | 179 SkShader::TileMode mode; |
192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 180 if (!initGradientParams(fuzz, &colors, &pos, &mode)) { |
193 return; | 181 return; |
194 } | 182 } |
195 | 183 |
196 SkPaint p; | 184 SkPaint p; |
197 uint32_t flags; | 185 uint32_t flags; |
198 if (!fuzz->next(&flags)) { | 186 if (!fuzz->next(&flags)) { |
199 return; | 187 return; |
200 } | 188 } |
201 | 189 |
202 SkTLazy<SkMatrix> localMatrix; | 190 SkTLazy<SkMatrix> localMatrix; |
203 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 191 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
204 return; | 192 return; |
205 } | 193 } |
206 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, en
d, | 194 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, |
207 endRadius, colors, pos, count, mode, flags, localMatrix.getMaybeNull
())); | 195 end, endRadius, colors.data(), pos.data(), colors.size(), mode, |
| 196 flags, localMatrix.getMaybeNull())); |
208 | 197 |
209 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 198 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
210 if (useGlobalMatrix) { | 199 if (useGlobalMatrix) { |
211 SkMatrix gm; | 200 SkMatrix gm; |
212 if (!makeMatrix(fuzz, &gm)) { | 201 if (!makeMatrix(fuzz, &gm)) { |
213 return; | 202 return; |
214 } | 203 } |
215 SkCanvas* c = surface->getCanvas(); | 204 SkCanvas* c = surface->getCanvas(); |
216 c->setMatrix(gm); | 205 c->setMatrix(gm); |
217 c->drawPaint(p); | 206 c->drawPaint(p); |
218 } else { | 207 } else { |
219 surface->getCanvas()->drawPaint(p); | 208 surface->getCanvas()->drawPaint(p); |
220 } | 209 } |
221 } | 210 } |
222 | 211 |
223 void fuzzSweepGradient(Fuzz* fuzz) { | 212 void fuzzSweepGradient(Fuzz* fuzz) { |
224 SkScalar cx, cy; | 213 SkScalar cx, cy; |
225 bool useLocalMatrix, useGlobalMatrix; | 214 bool useLocalMatrix, useGlobalMatrix; |
226 if (!fuzz->next(&cx) || | 215 if (!fuzz->next(&cx) || |
227 !fuzz->next(&cy) || | 216 !fuzz->next(&cy) || |
228 !fuzz->next(&useLocalMatrix) || | 217 !fuzz->next(&useLocalMatrix) || |
229 !fuzz->next(&useGlobalMatrix)) { | 218 !fuzz->next(&useGlobalMatrix)) { |
230 return; | 219 return; |
231 } | 220 } |
232 | 221 |
233 uint32_t count; | 222 std::vector<SkColor> colors; |
234 SkColor* colors; | 223 std::vector<SkScalar> pos; |
235 SkScalar* pos; | |
236 SkShader::TileMode mode; | 224 SkShader::TileMode mode; |
237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 225 if (!initGradientParams(fuzz, &colors, &pos, &mode)) { |
238 return; | 226 return; |
239 } | 227 } |
240 | 228 |
241 SkPaint p; | 229 SkPaint p; |
242 if (useLocalMatrix) { | 230 if (useLocalMatrix) { |
243 SkMatrix m; | 231 SkMatrix m; |
244 if (!makeMatrix(fuzz, &m)) { | 232 if (!makeMatrix(fuzz, &m)) { |
245 return; | 233 return; |
246 } | 234 } |
247 uint32_t flags; | 235 uint32_t flags; |
248 if (!fuzz->next(&flags)) { | 236 if (!fuzz->next(&flags)) { |
249 return; | 237 return; |
250 } | 238 } |
251 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count,
flags, &m)); | 239 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 240 pos.data(), colors.size(), flags, &m)); |
252 } else { | 241 } else { |
253 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count))
; | 242 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 243 pos.data(), colors.size())); |
254 } | 244 } |
255 | 245 |
256 | 246 |
257 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 247 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
258 if (useGlobalMatrix) { | 248 if (useGlobalMatrix) { |
259 SkMatrix gm; | 249 SkMatrix gm; |
260 if (!makeMatrix(fuzz, &gm)) { | 250 if (!makeMatrix(fuzz, &gm)) { |
261 return; | 251 return; |
262 } | 252 } |
263 SkCanvas* c = surface->getCanvas(); | 253 SkCanvas* c = surface->getCanvas(); |
(...skipping 21 matching lines...) Expand all Loading... |
285 return; | 275 return; |
286 case 2: | 276 case 2: |
287 SkDebugf("TwoPointConicalGradient\n"); | 277 SkDebugf("TwoPointConicalGradient\n"); |
288 fuzzTwoPointConicalGradient(fuzz); | 278 fuzzTwoPointConicalGradient(fuzz); |
289 return; | 279 return; |
290 } | 280 } |
291 SkDebugf("SweepGradient\n"); | 281 SkDebugf("SweepGradient\n"); |
292 fuzzSweepGradient(fuzz); | 282 fuzzSweepGradient(fuzz); |
293 return; | 283 return; |
294 } | 284 } |
OLD | NEW |