| 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" |
| 11 #include "SkSurface.h" | 11 #include "SkSurface.h" |
| 12 #include "SkTLazy.h" | 12 #include "SkTLazy.h" |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 | 15 |
| 16 const int MAX_COUNT = 400; | 16 const int MAX_COUNT = 400; |
| 17 | 17 |
| 18 bool makeMatrix(Fuzz* fuzz, SkMatrix* m) { | 18 bool makeMatrix(Fuzz* fuzz, SkMatrix* m) { |
| 19 SkScalar scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp
2; | 19 SkScalar scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp
2; |
| 20 if (!fuzz->next<SkScalar>(&scaleX) || | 20 if (!fuzz->next(&scaleX) || |
| 21 !fuzz->next<SkScalar>(&skewX) || | 21 !fuzz->next(&skewX) || |
| 22 !fuzz->next<SkScalar>(&transX) || | 22 !fuzz->next(&transX) || |
| 23 !fuzz->next<SkScalar>(&skewY) || | 23 !fuzz->next(&skewY) || |
| 24 !fuzz->next<SkScalar>(&scaleY) || | 24 !fuzz->next(&scaleY) || |
| 25 !fuzz->next<SkScalar>(&transY) || | 25 !fuzz->next(&transY) || |
| 26 !fuzz->next<SkScalar>(&persp0) || | 26 !fuzz->next(&persp0) || |
| 27 !fuzz->next<SkScalar>(&persp1) || | 27 !fuzz->next(&persp1) || |
| 28 !fuzz->next<SkScalar>(&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, uint32_t* count, SkColor** colors, SkScalar*
* pos, |
| 36 SkShader::TileMode* mode) { | 36 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; |
| 41 SkColor* t_colors; | 41 SkColor* t_colors; |
| 42 SkScalar* t_pos; | 42 SkScalar* t_pos; |
| 43 | 43 |
| 44 t_count = fuzz->nextRangeU(0, MAX_COUNT); | 44 t_count = fuzz->nextRangeU(0, MAX_COUNT); |
| 45 if (t_count == 1) { | 45 if (t_count == 1) { |
| 46 t_count = 2; | 46 t_count = 2; |
| 47 } | 47 } |
| 48 | 48 |
| 49 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar)))
) { | 49 if (fuzz->remaining() < (1 + t_count * (sizeof(SkColor) + sizeof(SkScalar)))
) { |
| 50 return false; | 50 return false; |
| 51 } | 51 } |
| 52 t_colors = new SkColor[t_count]; | 52 t_colors = new SkColor[t_count]; |
| 53 t_pos = new SkScalar[t_count]; | 53 t_pos = new SkScalar[t_count]; |
| 54 for (uint32_t i = 0; i < t_count; i++) { | 54 for (uint32_t i = 0; i < t_count; i++) { |
| 55 fuzz->next<SkColor>(&t_colors[i]); | 55 fuzz->next(&t_colors[i]); |
| 56 fuzz->next<SkScalar>(&t_pos[i]); | 56 fuzz->next(&t_pos[i]); |
| 57 } | 57 } |
| 58 | 58 |
| 59 if (t_count == 0) { | 59 if (t_count == 0) { |
| 60 *count = 0; | 60 *count = 0; |
| 61 *colors = NULL; | 61 *colors = NULL; |
| 62 *pos = NULL; | 62 *pos = NULL; |
| 63 } else { | 63 } else { |
| 64 std::sort(t_pos, t_pos + t_count); | 64 std::sort(t_pos, t_pos + t_count); |
| 65 t_pos[0] = 0; | 65 t_pos[0] = 0; |
| 66 t_pos[t_count - 1] = 1; | 66 t_pos[t_count - 1] = 1; |
| 67 *count = t_count; | 67 *count = t_count; |
| 68 *colors = t_colors; | 68 *colors = t_colors; |
| 69 *pos = t_pos; | 69 *pos = t_pos; |
| 70 } | 70 } |
| 71 | 71 |
| 72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); | 72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); |
| 73 return true; | 73 return true; |
| 74 } | 74 } |
| 75 | 75 |
| 76 void fuzzLinearGradient(Fuzz* fuzz) { | 76 void fuzzLinearGradient(Fuzz* fuzz) { |
| 77 SkScalar a, b, c, d; | 77 SkScalar a, b, c, d; |
| 78 bool useLocalMatrix, useGlobalMatrix; | 78 bool useLocalMatrix, useGlobalMatrix; |
| 79 if (!fuzz->next<SkScalar>(&a) || | 79 if (!fuzz->next(&a) || |
| 80 !fuzz->next<SkScalar>(&b) || | 80 !fuzz->next(&b) || |
| 81 !fuzz->next<SkScalar>(&c) || | 81 !fuzz->next(&c) || |
| 82 !fuzz->next<SkScalar>(&d) || | 82 !fuzz->next(&d) || |
| 83 !fuzz->next<bool>(&useLocalMatrix) || | 83 !fuzz->next(&useLocalMatrix) || |
| 84 !fuzz->next<bool>(&useGlobalMatrix)) { | 84 !fuzz->next(&useGlobalMatrix)) { |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; | 87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; |
| 88 | 88 |
| 89 uint32_t count; | 89 uint32_t count; |
| 90 SkColor* colors; | 90 SkColor* colors; |
| 91 SkScalar* pos; | 91 SkScalar* pos; |
| 92 SkShader::TileMode mode; | 92 SkShader::TileMode mode; |
| 93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 94 return; | 94 return; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 117 c->setMatrix(gm); | 117 c->setMatrix(gm); |
| 118 c->drawPaint(p); | 118 c->drawPaint(p); |
| 119 } else { | 119 } else { |
| 120 surface->getCanvas()->drawPaint(p); | 120 surface->getCanvas()->drawPaint(p); |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 void fuzzRadialGradient(Fuzz* fuzz) { | 124 void fuzzRadialGradient(Fuzz* fuzz) { |
| 125 SkScalar a, b, radius; | 125 SkScalar a, b, radius; |
| 126 bool useLocalMatrix, useGlobalMatrix; | 126 bool useLocalMatrix, useGlobalMatrix; |
| 127 if (!fuzz->next<SkScalar>(&a) || | 127 if (!fuzz->next(&a) || |
| 128 !fuzz->next<SkScalar>(&b) || | 128 !fuzz->next(&b) || |
| 129 !fuzz->next<SkScalar>(&radius) || | 129 !fuzz->next(&radius) || |
| 130 !fuzz->next<bool>(&useLocalMatrix) || | 130 !fuzz->next(&useLocalMatrix) || |
| 131 !fuzz->next<bool>(&useGlobalMatrix)) { | 131 !fuzz->next(&useGlobalMatrix)) { |
| 132 return; | 132 return; |
| 133 } | 133 } |
| 134 SkPoint center = SkPoint::Make(a,b); | 134 SkPoint center = SkPoint::Make(a,b); |
| 135 | 135 |
| 136 uint32_t count; | 136 uint32_t count; |
| 137 SkColor* colors; | 137 SkColor* colors; |
| 138 SkScalar* pos; | 138 SkScalar* pos; |
| 139 SkShader::TileMode mode; | 139 SkShader::TileMode mode; |
| 140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 141 return; | 141 return; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 165 c->setMatrix(gm); | 165 c->setMatrix(gm); |
| 166 c->drawPaint(p); | 166 c->drawPaint(p); |
| 167 } else { | 167 } else { |
| 168 surface->getCanvas()->drawPaint(p); | 168 surface->getCanvas()->drawPaint(p); |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 void fuzzTwoPointConicalGradient(Fuzz* fuzz) { | 172 void fuzzTwoPointConicalGradient(Fuzz* fuzz) { |
| 173 SkScalar a, b, startRadius, c, d, endRadius; | 173 SkScalar a, b, startRadius, c, d, endRadius; |
| 174 bool useLocalMatrix, useGlobalMatrix; | 174 bool useLocalMatrix, useGlobalMatrix; |
| 175 if (!fuzz->next<SkScalar>(&a) || | 175 if (!fuzz->next(&a) || |
| 176 !fuzz->next<SkScalar>(&b) || | 176 !fuzz->next(&b) || |
| 177 !fuzz->next<SkScalar>(&startRadius) || | 177 !fuzz->next(&startRadius) || |
| 178 !fuzz->next<SkScalar>(&c) || | 178 !fuzz->next(&c) || |
| 179 !fuzz->next<SkScalar>(&d) || | 179 !fuzz->next(&d) || |
| 180 !fuzz->next<SkScalar>(&endRadius) || | 180 !fuzz->next(&endRadius) || |
| 181 !fuzz->next<bool>(&useLocalMatrix) || | 181 !fuzz->next(&useLocalMatrix) || |
| 182 !fuzz->next<bool>(&useGlobalMatrix)) { | 182 !fuzz->next(&useGlobalMatrix)) { |
| 183 return; | 183 return; |
| 184 } | 184 } |
| 185 SkPoint start = SkPoint::Make(a, b); | 185 SkPoint start = SkPoint::Make(a, b); |
| 186 SkPoint end = SkPoint::Make(c, d); | 186 SkPoint end = SkPoint::Make(c, d); |
| 187 | 187 |
| 188 uint32_t count; | 188 uint32_t count; |
| 189 SkColor* colors; | 189 SkColor* colors; |
| 190 SkScalar* pos; | 190 SkScalar* pos; |
| 191 SkShader::TileMode mode; | 191 SkShader::TileMode mode; |
| 192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 216 c->setMatrix(gm); | 216 c->setMatrix(gm); |
| 217 c->drawPaint(p); | 217 c->drawPaint(p); |
| 218 } else { | 218 } else { |
| 219 surface->getCanvas()->drawPaint(p); | 219 surface->getCanvas()->drawPaint(p); |
| 220 } | 220 } |
| 221 } | 221 } |
| 222 | 222 |
| 223 void fuzzSweepGradient(Fuzz* fuzz) { | 223 void fuzzSweepGradient(Fuzz* fuzz) { |
| 224 SkScalar cx, cy; | 224 SkScalar cx, cy; |
| 225 bool useLocalMatrix, useGlobalMatrix; | 225 bool useLocalMatrix, useGlobalMatrix; |
| 226 if (!fuzz->next<SkScalar>(&cx) || | 226 if (!fuzz->next(&cx) || |
| 227 !fuzz->next<SkScalar>(&cy) || | 227 !fuzz->next(&cy) || |
| 228 !fuzz->next<bool>(&useLocalMatrix) || | 228 !fuzz->next(&useLocalMatrix) || |
| 229 !fuzz->next<bool>(&useGlobalMatrix)) { | 229 !fuzz->next(&useGlobalMatrix)) { |
| 230 return; | 230 return; |
| 231 } | 231 } |
| 232 | 232 |
| 233 uint32_t count; | 233 uint32_t count; |
| 234 SkColor* colors; | 234 SkColor* colors; |
| 235 SkScalar* pos; | 235 SkScalar* pos; |
| 236 SkShader::TileMode mode; | 236 SkShader::TileMode mode; |
| 237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 238 return; | 238 return; |
| 239 } | 239 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 263 SkCanvas* c = surface->getCanvas(); | 263 SkCanvas* c = surface->getCanvas(); |
| 264 c->setMatrix(gm); | 264 c->setMatrix(gm); |
| 265 c->drawPaint(p); | 265 c->drawPaint(p); |
| 266 } else { | 266 } else { |
| 267 surface->getCanvas()->drawPaint(p); | 267 surface->getCanvas()->drawPaint(p); |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 | 270 |
| 271 DEF_FUZZ(Gradients, fuzz) { | 271 DEF_FUZZ(Gradients, fuzz) { |
| 272 uint8_t i; | 272 uint8_t i; |
| 273 if (!fuzz->next<uint8_t>(&i)) { | 273 if (!fuzz->next(&i)) { |
| 274 return; | 274 return; |
| 275 } | 275 } |
| 276 | 276 |
| 277 switch(i) { | 277 switch(i) { |
| 278 case 0: | 278 case 0: |
| 279 SkDebugf("LinearGradient\n"); | 279 SkDebugf("LinearGradient\n"); |
| 280 fuzzLinearGradient(fuzz); | 280 fuzzLinearGradient(fuzz); |
| 281 return; | 281 return; |
| 282 case 1: | 282 case 1: |
| 283 SkDebugf("RadialGradient\n"); | 283 SkDebugf("RadialGradient\n"); |
| 284 fuzzRadialGradient(fuzz); | 284 fuzzRadialGradient(fuzz); |
| 285 return; | 285 return; |
| 286 case 2: | 286 case 2: |
| 287 SkDebugf("TwoPointConicalGradient\n"); | 287 SkDebugf("TwoPointConicalGradient\n"); |
| 288 fuzzTwoPointConicalGradient(fuzz); | 288 fuzzTwoPointConicalGradient(fuzz); |
| 289 return; | 289 return; |
| 290 } | 290 } |
| 291 SkDebugf("SweepGradient\n"); | 291 SkDebugf("SweepGradient\n"); |
| 292 fuzzSweepGradient(fuzz); | 292 fuzzSweepGradient(fuzz); |
| 293 return; | 293 return; |
| 294 } | 294 } |
| OLD | NEW |