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<SkScalar>(&transY) || | 25 !fuzz->next<SkScalar>(&transY) || |
26 !fuzz->next<SkScalar>(&persp0) || | 26 !fuzz->next<SkScalar>(&persp0) || |
27 !fuzz->next<SkScalar>(&persp1) || | 27 !fuzz->next<SkScalar>(&persp1) || |
28 !fuzz->next<SkScalar>(&persp2)) { | 28 !fuzz->next<SkScalar>(&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, uint32_t* count, std::unique_ptr<SkColor[]>* colors, |
36 SkShader::TileMode* mode) { | 36 std::unique_ptr<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 t_count; |
mtklein_C
2016/10/24 17:51:11
Out of curiosity, what is the t_ prefix for?
kjlubick
2016/10/24 19:11:46
I don't recall. It's been awhile since I wrote th
| |
41 SkColor* t_colors; | |
42 SkScalar* t_pos; | |
43 | |
44 t_count = fuzz->nextRangeU(0, MAX_COUNT); | 41 t_count = fuzz->nextRangeU(0, MAX_COUNT); |
45 if (t_count == 1) { | 42 if (t_count == 1) { |
46 t_count = 2; | 43 t_count = 2; |
47 } | 44 } |
48 | 45 |
49 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar))) ) { | 46 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar))) ) { |
50 return false; | 47 return false; |
51 } | 48 } |
52 t_colors = new SkColor[t_count]; | 49 colors->reset(new SkColor[t_count]); |
53 t_pos = new SkScalar[t_count]; | 50 pos->reset(new SkScalar[t_count]); |
51 | |
54 for (uint32_t i = 0; i < t_count; i++) { | 52 for (uint32_t i = 0; i < t_count; i++) { |
55 fuzz->next<SkColor>(&t_colors[i]); | 53 fuzz->next<SkColor>(&(*colors)[i]); |
56 fuzz->next<SkScalar>(&t_pos[i]); | 54 fuzz->next<SkScalar>(&(*pos)[i]); |
57 } | 55 } |
58 | 56 |
59 if (t_count == 0) { | 57 if (t_count == 0) { |
60 *count = 0; | 58 *count = 0; |
61 *colors = NULL; | 59 colors->reset(); |
62 *pos = NULL; | 60 pos->reset(); |
63 } else { | 61 } else { |
64 std::sort(t_pos, t_pos + t_count); | 62 std::sort(pos->get(), pos->get() + t_count); |
65 t_pos[0] = 0; | 63 (*pos)[0] = 0; |
66 t_pos[t_count - 1] = 1; | 64 (*pos)[t_count - 1] = 1; |
67 *count = t_count; | 65 *count = t_count; |
68 *colors = t_colors; | |
69 *pos = t_pos; | |
70 } | 66 } |
71 | 67 |
72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); | 68 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); |
73 return true; | 69 return true; |
74 } | 70 } |
75 | 71 |
76 void fuzzLinearGradient(Fuzz* fuzz) { | 72 void fuzzLinearGradient(Fuzz* fuzz) { |
77 SkScalar a, b, c, d; | 73 SkScalar a, b, c, d; |
78 bool useLocalMatrix, useGlobalMatrix; | 74 bool useLocalMatrix, useGlobalMatrix; |
79 if (!fuzz->next<SkScalar>(&a) || | 75 if (!fuzz->next<SkScalar>(&a) || |
80 !fuzz->next<SkScalar>(&b) || | 76 !fuzz->next<SkScalar>(&b) || |
81 !fuzz->next<SkScalar>(&c) || | 77 !fuzz->next<SkScalar>(&c) || |
82 !fuzz->next<SkScalar>(&d) || | 78 !fuzz->next<SkScalar>(&d) || |
83 !fuzz->next(&useLocalMatrix) || | 79 !fuzz->next(&useLocalMatrix) || |
84 !fuzz->next(&useGlobalMatrix)) { | 80 !fuzz->next(&useGlobalMatrix)) { |
85 return; | 81 return; |
86 } | 82 } |
87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; | 83 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; |
88 | 84 |
89 uint32_t count; | 85 uint32_t count; |
90 SkColor* colors; | 86 std::unique_ptr<SkColor[]> colors; |
mtklein_C
2016/10/24 17:51:11
This seems fine, but we'd normally use an SkTDArra
kjlubick
2016/10/24 19:11:46
Done. Vector used. I don't care about the perfor
| |
91 SkScalar* pos; | 87 std::unique_ptr<SkScalar[]> pos; |
92 SkShader::TileMode mode; | 88 SkShader::TileMode mode; |
93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 89 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
94 return; | 90 return; |
95 } | 91 } |
96 | 92 |
97 SkPaint p; | 93 SkPaint p; |
98 uint32_t flags; | 94 uint32_t flags; |
99 if (!fuzz->next(&flags)) { | 95 if (!fuzz->next(&flags)) { |
100 return; | 96 return; |
101 } | 97 } |
102 | 98 |
103 SkTLazy<SkMatrix> localMatrix; | 99 SkTLazy<SkMatrix> localMatrix; |
104 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 100 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
105 return; | 101 return; |
106 } | 102 } |
107 p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, mode, | 103 p.setShader(SkGradientShader::MakeLinear(pts, colors.get(), pos.get(), |
108 flags, localMatrix.getMaybeNull())); | 104 count, mode, flags, localMatrix.getMaybeNull())); |
109 | 105 |
110 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 106 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
111 if (useGlobalMatrix) { | 107 if (useGlobalMatrix) { |
112 SkMatrix gm; | 108 SkMatrix gm; |
113 if (!makeMatrix(fuzz, &gm)) { | 109 if (!makeMatrix(fuzz, &gm)) { |
114 return; | 110 return; |
115 } | 111 } |
116 SkCanvas* c = surface->getCanvas(); | 112 SkCanvas* c = surface->getCanvas(); |
117 c->setMatrix(gm); | 113 c->setMatrix(gm); |
118 c->drawPaint(p); | 114 c->drawPaint(p); |
119 } else { | 115 } else { |
120 surface->getCanvas()->drawPaint(p); | 116 surface->getCanvas()->drawPaint(p); |
121 } | 117 } |
122 } | 118 } |
123 | 119 |
124 void fuzzRadialGradient(Fuzz* fuzz) { | 120 void fuzzRadialGradient(Fuzz* fuzz) { |
125 SkScalar a, b, radius; | 121 SkScalar a, b, radius; |
126 bool useLocalMatrix, useGlobalMatrix; | 122 bool useLocalMatrix, useGlobalMatrix; |
127 if (!fuzz->next<SkScalar>(&a) || | 123 if (!fuzz->next<SkScalar>(&a) || |
128 !fuzz->next<SkScalar>(&b) || | 124 !fuzz->next<SkScalar>(&b) || |
129 !fuzz->next<SkScalar>(&radius) || | 125 !fuzz->next<SkScalar>(&radius) || |
130 !fuzz->next(&useLocalMatrix) || | 126 !fuzz->next(&useLocalMatrix) || |
131 !fuzz->next(&useGlobalMatrix)) { | 127 !fuzz->next(&useGlobalMatrix)) { |
132 return; | 128 return; |
133 } | 129 } |
134 SkPoint center = SkPoint::Make(a,b); | 130 SkPoint center = SkPoint::Make(a,b); |
135 | 131 |
136 uint32_t count; | 132 uint32_t count; |
137 SkColor* colors; | 133 std::unique_ptr<SkColor[]> colors; |
138 SkScalar* pos; | 134 std::unique_ptr<SkScalar[]> pos; |
139 SkShader::TileMode mode; | 135 SkShader::TileMode mode; |
140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 136 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
141 return; | 137 return; |
142 } | 138 } |
143 | 139 |
144 SkPaint p; | 140 SkPaint p; |
145 uint32_t flags; | 141 uint32_t flags; |
146 if (!fuzz->next(&flags)) { | 142 if (!fuzz->next(&flags)) { |
147 return; | 143 return; |
148 } | 144 } |
149 | 145 |
150 SkTLazy<SkMatrix> localMatrix; | 146 SkTLazy<SkMatrix> localMatrix; |
151 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 147 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
152 return; | 148 return; |
153 } | 149 } |
154 p.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, | 150 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.get(), |
155 count, mode, flags, localMatrix.getMaybeNull())); | 151 pos.get(), count, mode, flags, localMatrix.getMaybeNull())); |
156 | 152 |
157 | 153 |
158 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 154 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
159 if (useGlobalMatrix) { | 155 if (useGlobalMatrix) { |
160 SkMatrix gm; | 156 SkMatrix gm; |
161 if (!makeMatrix(fuzz, &gm)) { | 157 if (!makeMatrix(fuzz, &gm)) { |
162 return; | 158 return; |
163 } | 159 } |
164 SkCanvas* c = surface->getCanvas(); | 160 SkCanvas* c = surface->getCanvas(); |
165 c->setMatrix(gm); | 161 c->setMatrix(gm); |
(...skipping 13 matching lines...) Expand all Loading... | |
179 !fuzz->next<SkScalar>(&d) || | 175 !fuzz->next<SkScalar>(&d) || |
180 !fuzz->next<SkScalar>(&endRadius) || | 176 !fuzz->next<SkScalar>(&endRadius) || |
181 !fuzz->next(&useLocalMatrix) || | 177 !fuzz->next(&useLocalMatrix) || |
182 !fuzz->next(&useGlobalMatrix)) { | 178 !fuzz->next(&useGlobalMatrix)) { |
183 return; | 179 return; |
184 } | 180 } |
185 SkPoint start = SkPoint::Make(a, b); | 181 SkPoint start = SkPoint::Make(a, b); |
186 SkPoint end = SkPoint::Make(c, d); | 182 SkPoint end = SkPoint::Make(c, d); |
187 | 183 |
188 uint32_t count; | 184 uint32_t count; |
189 SkColor* colors; | 185 std::unique_ptr<SkColor[]> colors; |
190 SkScalar* pos; | 186 std::unique_ptr<SkScalar[]> pos; |
191 SkShader::TileMode mode; | 187 SkShader::TileMode mode; |
192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 188 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
193 return; | 189 return; |
194 } | 190 } |
195 | 191 |
196 SkPaint p; | 192 SkPaint p; |
197 uint32_t flags; | 193 uint32_t flags; |
198 if (!fuzz->next(&flags)) { | 194 if (!fuzz->next(&flags)) { |
199 return; | 195 return; |
200 } | 196 } |
201 | 197 |
202 SkTLazy<SkMatrix> localMatrix; | 198 SkTLazy<SkMatrix> localMatrix; |
203 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 199 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
204 return; | 200 return; |
205 } | 201 } |
206 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, en d, | 202 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, en d, |
207 endRadius, colors, pos, count, mode, flags, localMatrix.getMaybeNull ())); | 203 endRadius, colors.get(), pos.get(), count, mode, flags, |
204 localMatrix.getMaybeNull())); | |
208 | 205 |
209 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 206 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
210 if (useGlobalMatrix) { | 207 if (useGlobalMatrix) { |
211 SkMatrix gm; | 208 SkMatrix gm; |
212 if (!makeMatrix(fuzz, &gm)) { | 209 if (!makeMatrix(fuzz, &gm)) { |
213 return; | 210 return; |
214 } | 211 } |
215 SkCanvas* c = surface->getCanvas(); | 212 SkCanvas* c = surface->getCanvas(); |
216 c->setMatrix(gm); | 213 c->setMatrix(gm); |
217 c->drawPaint(p); | 214 c->drawPaint(p); |
218 } else { | 215 } else { |
219 surface->getCanvas()->drawPaint(p); | 216 surface->getCanvas()->drawPaint(p); |
220 } | 217 } |
221 } | 218 } |
222 | 219 |
223 void fuzzSweepGradient(Fuzz* fuzz) { | 220 void fuzzSweepGradient(Fuzz* fuzz) { |
224 SkScalar cx, cy; | 221 SkScalar cx, cy; |
225 bool useLocalMatrix, useGlobalMatrix; | 222 bool useLocalMatrix, useGlobalMatrix; |
226 if (!fuzz->next<SkScalar>(&cx) || | 223 if (!fuzz->next<SkScalar>(&cx) || |
227 !fuzz->next<SkScalar>(&cy) || | 224 !fuzz->next<SkScalar>(&cy) || |
228 !fuzz->next(&useLocalMatrix) || | 225 !fuzz->next(&useLocalMatrix) || |
229 !fuzz->next(&useGlobalMatrix)) { | 226 !fuzz->next(&useGlobalMatrix)) { |
230 return; | 227 return; |
231 } | 228 } |
232 | 229 |
233 uint32_t count; | 230 uint32_t count; |
234 SkColor* colors; | 231 std::unique_ptr<SkColor[]> colors; |
235 SkScalar* pos; | 232 std::unique_ptr<SkScalar[]> pos; |
236 SkShader::TileMode mode; | 233 SkShader::TileMode mode; |
237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 234 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
238 return; | 235 return; |
239 } | 236 } |
240 | 237 |
241 SkPaint p; | 238 SkPaint p; |
242 if (useLocalMatrix) { | 239 if (useLocalMatrix) { |
243 SkMatrix m; | 240 SkMatrix m; |
244 if (!makeMatrix(fuzz, &m)) { | 241 if (!makeMatrix(fuzz, &m)) { |
245 return; | 242 return; |
246 } | 243 } |
247 uint32_t flags; | 244 uint32_t flags; |
248 if (!fuzz->next(&flags)) { | 245 if (!fuzz->next(&flags)) { |
249 return; | 246 return; |
250 } | 247 } |
251 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count, flags, &m)); | 248 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.get(), |
249 pos.get(), count, flags, &m)); | |
252 } else { | 250 } else { |
253 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count)) ; | 251 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.get(), |
252 pos.get(), count)); | |
254 } | 253 } |
255 | 254 |
256 | 255 |
257 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 256 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
258 if (useGlobalMatrix) { | 257 if (useGlobalMatrix) { |
259 SkMatrix gm; | 258 SkMatrix gm; |
260 if (!makeMatrix(fuzz, &gm)) { | 259 if (!makeMatrix(fuzz, &gm)) { |
261 return; | 260 return; |
262 } | 261 } |
263 SkCanvas* c = surface->getCanvas(); | 262 SkCanvas* c = surface->getCanvas(); |
(...skipping 21 matching lines...) Expand all Loading... | |
285 return; | 284 return; |
286 case 2: | 285 case 2: |
287 SkDebugf("TwoPointConicalGradient\n"); | 286 SkDebugf("TwoPointConicalGradient\n"); |
288 fuzzTwoPointConicalGradient(fuzz); | 287 fuzzTwoPointConicalGradient(fuzz); |
289 return; | 288 return; |
290 } | 289 } |
291 SkDebugf("SweepGradient\n"); | 290 SkDebugf("SweepGradient\n"); |
292 fuzzSweepGradient(fuzz); | 291 fuzzSweepGradient(fuzz); |
293 return; | 292 return; |
294 } | 293 } |
OLD | NEW |