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> |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 15 | #include <vector> |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 16 | |
| 17 | const int MAX_COUNT = 400; |
| 18 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 19 | void makeMatrix(Fuzz* fuzz, SkMatrix* m) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 20 | SkScalar mat[9]; |
| 21 | fuzz->nextN(mat, 9); |
| 22 | m->set9(mat); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 25 | void initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors, |
| 26 | std::vector<SkScalar>* pos, SkShader::TileMode* mode) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 27 | int count; |
| 28 | fuzz->nextRange(&count, 0, MAX_COUNT); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 29 | |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 30 | // Use a uint8_t to conserve bytes. This makes our "fuzzed bytes footprint" |
| 31 | // smaller, which leads to more efficient fuzzing. |
| 32 | uint8_t m; |
| 33 | fuzz->nextRange(&m, 0, 2); |
| 34 | *mode = static_cast<SkShader::TileMode>(m); |
kjlubick | 840f12a | 2016-10-25 06:11:05 -0700 | [diff] [blame] | 35 | |
| 36 | colors->clear(); |
| 37 | pos ->clear(); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 38 | for (int i = 0; i < count; i++) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 39 | SkColor c; |
| 40 | SkScalar s; |
| 41 | fuzz->next(&c, &s); |
| 42 | colors->push_back(c); |
| 43 | pos ->push_back(s); |
kjlubick | 840f12a | 2016-10-25 06:11:05 -0700 | [diff] [blame] | 44 | } |
| 45 | if (count) { |
| 46 | std::sort(pos->begin(), pos->end()); |
| 47 | // The order matters. If count == 1, we want pos == 0. |
| 48 | (*pos)[count - 1] = 1; |
| 49 | (*pos)[0] = 0; |
| 50 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void fuzzLinearGradient(Fuzz* fuzz) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 54 | SkPoint pts[2]; |
| 55 | fuzz->next(&pts[0].fX, &pts[0].fY, &pts[1].fX, &pts[1].fY); |
| 56 | bool useLocalMatrix, useGlobalMatrix; |
| 57 | fuzz->next(&useLocalMatrix, &useGlobalMatrix); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 58 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 59 | std::vector<SkColor> colors; |
| 60 | std::vector<SkScalar> pos; |
| 61 | SkShader::TileMode mode; |
| 62 | initGradientParams(fuzz, &colors, &pos, &mode); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 63 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 64 | SkPaint p; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 65 | uint32_t flags; |
| 66 | fuzz->next(&flags); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 67 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 68 | SkTLazy<SkMatrix> localMatrix; |
| 69 | if (useLocalMatrix) { |
| 70 | makeMatrix(fuzz, localMatrix.init()); |
| 71 | } |
| 72 | p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), |
| 73 | colors.size(), mode, flags, localMatrix.getMaybeNull())); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 74 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 75 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 76 | if (useGlobalMatrix) { |
| 77 | SkMatrix gm; |
| 78 | makeMatrix(fuzz, &gm); |
| 79 | SkCanvas* c = surface->getCanvas(); |
| 80 | c->setMatrix(gm); |
| 81 | c->drawPaint(p); |
| 82 | } else { |
| 83 | surface->getCanvas()->drawPaint(p); |
| 84 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void fuzzRadialGradient(Fuzz* fuzz) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 88 | SkPoint center; |
| 89 | fuzz->next(¢er.fX, ¢er.fY); |
| 90 | SkScalar radius; |
| 91 | bool useLocalMatrix, useGlobalMatrix; |
| 92 | fuzz->next(&radius, &useLocalMatrix, &useGlobalMatrix); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 93 | |
| 94 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 95 | std::vector<SkColor> colors; |
| 96 | std::vector<SkScalar> pos; |
| 97 | SkShader::TileMode mode; |
| 98 | initGradientParams(fuzz, &colors, &pos, &mode); |
| 99 | |
| 100 | SkPaint p; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 101 | uint32_t flags; |
| 102 | fuzz->next(&flags); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 103 | |
| 104 | SkTLazy<SkMatrix> localMatrix; |
| 105 | if (useLocalMatrix) { |
| 106 | makeMatrix(fuzz, localMatrix.init()); |
| 107 | } |
| 108 | p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(), |
| 109 | pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull())); |
| 110 | |
| 111 | |
| 112 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 113 | if (useGlobalMatrix) { |
| 114 | SkMatrix gm; |
| 115 | makeMatrix(fuzz, &gm); |
| 116 | SkCanvas* c = surface->getCanvas(); |
| 117 | c->setMatrix(gm); |
| 118 | c->drawPaint(p); |
| 119 | } else { |
| 120 | surface->getCanvas()->drawPaint(p); |
| 121 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void fuzzTwoPointConicalGradient(Fuzz* fuzz) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 125 | SkPoint start; |
| 126 | fuzz->next(&start.fX, &start.fY); |
| 127 | SkPoint end; |
| 128 | fuzz->next(&end.fX, &end.fY); |
| 129 | SkScalar startRadius, endRadius; |
| 130 | bool useLocalMatrix, useGlobalMatrix; |
| 131 | fuzz->next(&startRadius, &endRadius, &useLocalMatrix, &useGlobalMatrix); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 132 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 133 | std::vector<SkColor> colors; |
| 134 | std::vector<SkScalar> pos; |
| 135 | SkShader::TileMode mode; |
| 136 | initGradientParams(fuzz, &colors, &pos, &mode); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 137 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 138 | SkPaint p; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 139 | uint32_t flags; |
| 140 | fuzz->next(&flags); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 141 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 142 | SkTLazy<SkMatrix> localMatrix; |
| 143 | if (useLocalMatrix) { |
| 144 | makeMatrix(fuzz, localMatrix.init()); |
| 145 | } |
| 146 | p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, |
| 147 | end, endRadius, colors.data(), pos.data(), colors.size(), mode, |
| 148 | flags, localMatrix.getMaybeNull())); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 149 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 150 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 151 | if (useGlobalMatrix) { |
| 152 | SkMatrix gm; |
| 153 | makeMatrix(fuzz, &gm); |
| 154 | SkCanvas* c = surface->getCanvas(); |
| 155 | c->setMatrix(gm); |
| 156 | c->drawPaint(p); |
| 157 | } else { |
| 158 | surface->getCanvas()->drawPaint(p); |
| 159 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void fuzzSweepGradient(Fuzz* fuzz) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 163 | SkScalar cx, cy; |
| 164 | bool useLocalMatrix, useGlobalMatrix; |
| 165 | fuzz->next(&cx, &cy, &useLocalMatrix, &useGlobalMatrix); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 166 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 167 | std::vector<SkColor> colors; |
| 168 | std::vector<SkScalar> pos; |
| 169 | SkShader::TileMode mode; |
| 170 | initGradientParams(fuzz, &colors, &pos, &mode); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 171 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 172 | SkPaint p; |
| 173 | if (useLocalMatrix) { |
| 174 | SkMatrix m; |
| 175 | makeMatrix(fuzz, &m); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 176 | uint32_t flags; |
| 177 | fuzz->next(&flags); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 178 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 179 | p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 180 | pos.data(), colors.size(), flags, &m)); |
| 181 | } else { |
| 182 | p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 183 | pos.data(), colors.size())); |
| 184 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 185 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 186 | sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 187 | if (useGlobalMatrix) { |
| 188 | SkMatrix gm; |
| 189 | makeMatrix(fuzz, &gm); |
| 190 | SkCanvas* c = surface->getCanvas(); |
| 191 | c->setMatrix(gm); |
| 192 | c->drawPaint(p); |
| 193 | } else { |
| 194 | surface->getCanvas()->drawPaint(p); |
| 195 | } |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | DEF_FUZZ(Gradients, fuzz) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 199 | uint8_t i; |
| 200 | fuzz->next(&i); |
kjlubick | 1eda1eb | 2016-08-12 06:26:03 -0700 | [diff] [blame] | 201 | |
| 202 | switch(i) { |
| 203 | case 0: |
| 204 | SkDebugf("LinearGradient\n"); |
| 205 | fuzzLinearGradient(fuzz); |
| 206 | return; |
| 207 | case 1: |
| 208 | SkDebugf("RadialGradient\n"); |
| 209 | fuzzRadialGradient(fuzz); |
| 210 | return; |
| 211 | case 2: |
| 212 | SkDebugf("TwoPointConicalGradient\n"); |
| 213 | fuzzTwoPointConicalGradient(fuzz); |
| 214 | return; |
| 215 | } |
| 216 | SkDebugf("SweepGradient\n"); |
| 217 | fuzzSweepGradient(fuzz); |
| 218 | return; |
| 219 | } |