kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 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 "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkSurface.h" |
| 12 | #include "SkTLazy.h" |
| 13 | |
| 14 | #include <algorithm> |
| 15 | |
| 16 | const int MAX_COUNT = 400; |
| 17 | |
| 18 | bool makeMatrix(Fuzz* fuzz, SkMatrix* m) { |
| 19 | SkScalar scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2; |
| 20 | if (!fuzz->next<SkScalar>(&scaleX) || |
| 21 | !fuzz->next<SkScalar>(&skewX) || |
| 22 | !fuzz->next<SkScalar>(&transX) || |
| 23 | !fuzz->next<SkScalar>(&skewY) || |
| 24 | !fuzz->next<SkScalar>(&scaleY) || |
| 25 | !fuzz->next<SkScalar>(&transY) || |
| 26 | !fuzz->next<SkScalar>(&persp0) || |
| 27 | !fuzz->next<SkScalar>(&persp1) || |
| 28 | !fuzz->next<SkScalar>(&persp2)) { |
| 29 | return false; |
| 30 | } |
| 31 | m->setAll(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2); |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool initGradientParams(Fuzz* fuzz, uint32_t* count, SkColor** colors, SkScalar** pos, |
| 36 | SkShader::TileMode* mode) { |
| 37 | if (fuzz->remaining() < sizeof(uint32_t)) { |
| 38 | return false; |
| 39 | } |
| 40 | uint32_t t_count; |
| 41 | SkColor* t_colors; |
| 42 | SkScalar* t_pos; |
| 43 | |
| 44 | t_count = fuzz->nextRangeU(0, MAX_COUNT); |
| 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; |
| 51 | } |
| 52 | t_colors = new SkColor[t_count]; |
| 53 | t_pos = new SkScalar[t_count]; |
| 54 | for (uint32_t i = 0; i < t_count; i++) { |
| 55 | fuzz->next<SkColor>(&t_colors[i]); |
| 56 | fuzz->next<SkScalar>(&t_pos[i]); |
| 57 | } |
| 58 | |
| 59 | if (t_count == 0) { |
| 60 | *count = 0; |
| 61 | *colors = NULL; |
| 62 | *pos = NULL; |
| 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 | } |
| 71 | |
| 72 | *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void fuzzLinearGradient(Fuzz* fuzz) { |
| 77 | SkScalar a, b, c, d; |
| 78 | bool useLocalMatrix, useGlobalMatrix; |
| 79 | if (!fuzz->next<SkScalar>(&a) || |
| 80 | !fuzz->next<SkScalar>(&b) || |
| 81 | !fuzz->next<SkScalar>(&c) || |
| 82 | !fuzz->next<SkScalar>(&d) || |
| 83 | !fuzz->next<bool>(&useLocalMatrix) || |
| 84 | !fuzz->next<bool>(&useGlobalMatrix)) { |
| 85 | return; |
| 86 | } |
| 87 | SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; |
| 88 | |
| 89 | uint32_t count; |
| 90 | SkColor* colors; |
| 91 | SkScalar* pos; |
| 92 | SkShader::TileMode mode; |
| 93 | if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | SkPaint p; |
| 98 | uint32_t flags; |
| 99 | if (!fuzz->next(&flags)) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | SkTLazy<SkMatrix> localMatrix; |
| 104 | if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 105 | return; |
| 106 | } |
| 107 | p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, mode, |
| 108 | flags, localMatrix.getMaybeNull())); |
| 109 | |
| 110 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 111 | if (useGlobalMatrix) { |
| 112 | SkMatrix gm; |
| 113 | if (!makeMatrix(fuzz, &gm)) { |
| 114 | return; |
| 115 | } |
| 116 | SkCanvas* c = surface->getCanvas(); |
| 117 | c->setMatrix(gm); |
| 118 | c->drawPaint(p); |
| 119 | } else { |
| 120 | surface->getCanvas()->drawPaint(p); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void fuzzRadialGradient(Fuzz* fuzz) { |
| 125 | SkScalar a, b, radius; |
| 126 | bool useLocalMatrix, useGlobalMatrix; |
| 127 | if (!fuzz->next<SkScalar>(&a) || |
| 128 | !fuzz->next<SkScalar>(&b) || |
| 129 | !fuzz->next<SkScalar>(&radius) || |
| 130 | !fuzz->next<bool>(&useLocalMatrix) || |
| 131 | !fuzz->next<bool>(&useGlobalMatrix)) { |
| 132 | return; |
| 133 | } |
| 134 | SkPoint center = SkPoint::Make(a,b); |
| 135 | |
| 136 | uint32_t count; |
| 137 | SkColor* colors; |
| 138 | SkScalar* pos; |
| 139 | SkShader::TileMode mode; |
| 140 | if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | SkPaint p; |
| 145 | uint32_t flags; |
| 146 | if (!fuzz->next(&flags)) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | SkTLazy<SkMatrix> localMatrix; |
| 151 | if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 152 | return; |
| 153 | } |
| 154 | p.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, |
| 155 | count, mode, flags, localMatrix.getMaybeNull())); |
| 156 | |
| 157 | |
| 158 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 159 | if (useGlobalMatrix) { |
| 160 | SkMatrix gm; |
| 161 | if (!makeMatrix(fuzz, &gm)) { |
| 162 | return; |
| 163 | } |
| 164 | SkCanvas* c = surface->getCanvas(); |
| 165 | c->setMatrix(gm); |
| 166 | c->drawPaint(p); |
| 167 | } else { |
| 168 | surface->getCanvas()->drawPaint(p); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void fuzzTwoPointConicalGradient(Fuzz* fuzz) { |
| 173 | SkScalar a, b, startRadius, c, d, endRadius; |
| 174 | bool useLocalMatrix, useGlobalMatrix; |
| 175 | if (!fuzz->next<SkScalar>(&a) || |
| 176 | !fuzz->next<SkScalar>(&b) || |
| 177 | !fuzz->next<SkScalar>(&startRadius) || |
| 178 | !fuzz->next<SkScalar>(&c) || |
| 179 | !fuzz->next<SkScalar>(&d) || |
| 180 | !fuzz->next<SkScalar>(&endRadius) || |
| 181 | !fuzz->next<bool>(&useLocalMatrix) || |
| 182 | !fuzz->next<bool>(&useGlobalMatrix)) { |
| 183 | return; |
| 184 | } |
| 185 | SkPoint start = SkPoint::Make(a, b); |
| 186 | SkPoint end = SkPoint::Make(c, d); |
| 187 | |
| 188 | uint32_t count; |
| 189 | SkColor* colors; |
| 190 | SkScalar* pos; |
| 191 | SkShader::TileMode mode; |
| 192 | if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | SkPaint p; |
| 197 | uint32_t flags; |
| 198 | if (!fuzz->next(&flags)) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | SkTLazy<SkMatrix> localMatrix; |
| 203 | if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 204 | return; |
| 205 | } |
| 206 | p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, end, |
| 207 | endRadius, colors, pos, count, mode, flags, localMatrix.getMaybeNull())); |
| 208 | |
| 209 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 210 | if (useGlobalMatrix) { |
| 211 | SkMatrix gm; |
| 212 | if (!makeMatrix(fuzz, &gm)) { |
| 213 | return; |
| 214 | } |
| 215 | SkCanvas* c = surface->getCanvas(); |
| 216 | c->setMatrix(gm); |
| 217 | c->drawPaint(p); |
| 218 | } else { |
| 219 | surface->getCanvas()->drawPaint(p); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void fuzzSweepGradient(Fuzz* fuzz) { |
| 224 | SkScalar cx, cy; |
| 225 | bool useLocalMatrix, useGlobalMatrix; |
| 226 | if (!fuzz->next<SkScalar>(&cx) || |
| 227 | !fuzz->next<SkScalar>(&cy) || |
| 228 | !fuzz->next<bool>(&useLocalMatrix) || |
| 229 | !fuzz->next<bool>(&useGlobalMatrix)) { |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | uint32_t count; |
| 234 | SkColor* colors; |
| 235 | SkScalar* pos; |
| 236 | SkShader::TileMode mode; |
| 237 | if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | SkPaint p; |
| 242 | if (useLocalMatrix) { |
| 243 | SkMatrix m; |
| 244 | if (!makeMatrix(fuzz, &m)) { |
| 245 | return; |
| 246 | } |
| 247 | uint32_t flags; |
| 248 | if (!fuzz->next(&flags)) { |
| 249 | return; |
| 250 | } |
| 251 | p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count, flags, &m)); |
| 252 | } else { |
| 253 | p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count)); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 258 | if (useGlobalMatrix) { |
| 259 | SkMatrix gm; |
| 260 | if (!makeMatrix(fuzz, &gm)) { |
| 261 | return; |
| 262 | } |
| 263 | SkCanvas* c = surface->getCanvas(); |
| 264 | c->setMatrix(gm); |
| 265 | c->drawPaint(p); |
| 266 | } else { |
| 267 | surface->getCanvas()->drawPaint(p); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | DEF_FUZZ(Gradients, fuzz) { |
| 272 | uint8_t i; |
| 273 | if (!fuzz->next<uint8_t>(&i)) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | switch(i) { |
| 278 | case 0: |
| 279 | SkDebugf("LinearGradient\n"); |
| 280 | fuzzLinearGradient(fuzz); |
| 281 | return; |
| 282 | case 1: |
| 283 | SkDebugf("RadialGradient\n"); |
| 284 | fuzzRadialGradient(fuzz); |
| 285 | return; |
| 286 | case 2: |
| 287 | SkDebugf("TwoPointConicalGradient\n"); |
| 288 | fuzzTwoPointConicalGradient(fuzz); |
| 289 | return; |
| 290 | } |
| 291 | SkDebugf("SweepGradient\n"); |
| 292 | fuzzSweepGradient(fuzz); |
| 293 | return; |
| 294 | } |