Chromium Code Reviews| 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, uint32_t* count, std::vector<SkColor>* color s, |
| 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 t_count; |
| 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) { |
|
mtklein_C
2016/10/24 19:26:35
Why don't we fuzz count == 1?
kjlubick
2016/10/24 20:22:30
The API said >=2, so I followed that rule. Lookin
| |
| 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))) ) { |
|
mtklein_C
2016/10/24 19:26:35
What's the 1 extra byte here for?
It's probably a
kjlubick
2016/10/24 20:22:30
FWIW, the extra 1 byte is for static_cast<SkShader
| |
| 50 return false; | 47 return false; |
| 51 } | 48 } |
| 52 t_colors = new SkColor[t_count]; | 49 |
| 53 t_pos = new SkScalar[t_count]; | 50 SkColor c; |
|
mtklein_C
2016/10/24 19:26:35
It doesn't make things any faster or slower to put
kjlubick
2016/10/24 20:22:30
Done.
| |
| 51 SkScalar s; | |
| 54 for (uint32_t i = 0; i < t_count; i++) { | 52 for (uint32_t i = 0; i < t_count; i++) { |
|
mtklein_C
2016/10/24 19:26:35
ordinarily we'd write int instead of uint32_t here
kjlubick
2016/10/24 20:22:30
Well, then I get a warning because t_count is uint
mtklein_C
2016/10/24 20:54:38
This is why we always make things like this ints.
| |
| 55 fuzz->next(&t_colors[i]); | 53 fuzz->next(&c); |
|
mtklein_C
2016/10/24 19:26:35
Don't we need to check that calls to next() succee
kjlubick
2016/10/24 20:22:30
They only wouldn't succeed if there isn't enough "
| |
| 56 fuzz->next(&t_pos[i]); | 54 colors->push_back(c); |
| 55 fuzz->next(&s); | |
| 56 pos->push_back(s); | |
| 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->clear(); |
| 62 *pos = NULL; | 62 pos->clear(); |
| 63 } else { | 63 } else { |
| 64 std::sort(t_pos, t_pos + t_count); | 64 std::sort(pos->begin(), pos->begin() + t_count); |
|
mtklein_C
2016/10/24 19:26:35
pos->begin() + t_count is also known as pos->end()
kjlubick
2016/10/24 20:22:30
Done.
| |
| 65 t_pos[0] = 0; | 65 (*pos)[0] = 0; |
| 66 t_pos[t_count - 1] = 1; | 66 (*pos)[t_count - 1] = 1; |
| 67 *count = t_count; | 67 *count = t_count; |
| 68 *colors = t_colors; | |
| 69 *pos = t_pos; | |
| 70 } | 68 } |
| 71 | 69 |
| 72 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); | 70 *mode = static_cast<SkShader::TileMode>(fuzz->nextRangeU(0, 3)); |
| 73 return true; | 71 return true; |
| 74 } | 72 } |
| 75 | 73 |
| 76 void fuzzLinearGradient(Fuzz* fuzz) { | 74 void fuzzLinearGradient(Fuzz* fuzz) { |
| 77 SkScalar a, b, c, d; | 75 SkScalar a, b, c, d; |
| 78 bool useLocalMatrix, useGlobalMatrix; | 76 bool useLocalMatrix, useGlobalMatrix; |
| 79 if (!fuzz->next(&a) || | 77 if (!fuzz->next(&a) || |
| 80 !fuzz->next(&b) || | 78 !fuzz->next(&b) || |
| 81 !fuzz->next(&c) || | 79 !fuzz->next(&c) || |
| 82 !fuzz->next(&d) || | 80 !fuzz->next(&d) || |
| 83 !fuzz->next(&useLocalMatrix) || | 81 !fuzz->next(&useLocalMatrix) || |
| 84 !fuzz->next(&useGlobalMatrix)) { | 82 !fuzz->next(&useGlobalMatrix)) { |
| 85 return; | 83 return; |
| 86 } | 84 } |
| 87 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; | 85 SkPoint pts[2] = {SkPoint::Make(a,b), SkPoint::Make(c, d)}; |
| 88 | 86 |
| 89 uint32_t count; | 87 uint32_t count; |
| 90 SkColor* colors; | 88 std::vector<SkColor> colors; |
| 91 SkScalar* pos; | 89 std::vector<SkScalar> pos; |
| 92 SkShader::TileMode mode; | 90 SkShader::TileMode mode; |
| 93 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 91 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 94 return; | 92 return; |
| 95 } | 93 } |
| 96 | 94 |
| 97 SkPaint p; | 95 SkPaint p; |
| 98 uint32_t flags; | 96 uint32_t flags; |
| 99 if (!fuzz->next(&flags)) { | 97 if (!fuzz->next(&flags)) { |
| 100 return; | 98 return; |
| 101 } | 99 } |
| 102 | 100 |
| 103 SkTLazy<SkMatrix> localMatrix; | 101 SkTLazy<SkMatrix> localMatrix; |
| 104 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 102 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 105 return; | 103 return; |
| 106 } | 104 } |
| 107 p.setShader(SkGradientShader::MakeLinear(pts, colors, pos, count, mode, | 105 p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), |
| 108 flags, localMatrix.getMaybeNull())); | 106 pos.data(), count, mode, flags, localMatrix.getMaybeNull())); |
| 109 | 107 |
| 110 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 108 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 111 if (useGlobalMatrix) { | 109 if (useGlobalMatrix) { |
| 112 SkMatrix gm; | 110 SkMatrix gm; |
| 113 if (!makeMatrix(fuzz, &gm)) { | 111 if (!makeMatrix(fuzz, &gm)) { |
| 114 return; | 112 return; |
| 115 } | 113 } |
| 116 SkCanvas* c = surface->getCanvas(); | 114 SkCanvas* c = surface->getCanvas(); |
| 117 c->setMatrix(gm); | 115 c->setMatrix(gm); |
| 118 c->drawPaint(p); | 116 c->drawPaint(p); |
| 119 } else { | 117 } else { |
| 120 surface->getCanvas()->drawPaint(p); | 118 surface->getCanvas()->drawPaint(p); |
| 121 } | 119 } |
| 122 } | 120 } |
| 123 | 121 |
| 124 void fuzzRadialGradient(Fuzz* fuzz) { | 122 void fuzzRadialGradient(Fuzz* fuzz) { |
| 125 SkScalar a, b, radius; | 123 SkScalar a, b, radius; |
| 126 bool useLocalMatrix, useGlobalMatrix; | 124 bool useLocalMatrix, useGlobalMatrix; |
| 127 if (!fuzz->next(&a) || | 125 if (!fuzz->next(&a) || |
| 128 !fuzz->next(&b) || | 126 !fuzz->next(&b) || |
| 129 !fuzz->next(&radius) || | 127 !fuzz->next(&radius) || |
| 130 !fuzz->next(&useLocalMatrix) || | 128 !fuzz->next(&useLocalMatrix) || |
| 131 !fuzz->next(&useGlobalMatrix)) { | 129 !fuzz->next(&useGlobalMatrix)) { |
| 132 return; | 130 return; |
| 133 } | 131 } |
| 134 SkPoint center = SkPoint::Make(a,b); | 132 SkPoint center = SkPoint::Make(a,b); |
| 135 | 133 |
| 136 uint32_t count; | 134 uint32_t count; |
| 137 SkColor* colors; | 135 std::vector<SkColor> colors; |
| 138 SkScalar* pos; | 136 std::vector<SkScalar> pos; |
| 139 SkShader::TileMode mode; | 137 SkShader::TileMode mode; |
| 140 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 138 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 141 return; | 139 return; |
| 142 } | 140 } |
| 143 | 141 |
| 144 SkPaint p; | 142 SkPaint p; |
| 145 uint32_t flags; | 143 uint32_t flags; |
| 146 if (!fuzz->next(&flags)) { | 144 if (!fuzz->next(&flags)) { |
| 147 return; | 145 return; |
| 148 } | 146 } |
| 149 | 147 |
| 150 SkTLazy<SkMatrix> localMatrix; | 148 SkTLazy<SkMatrix> localMatrix; |
| 151 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 149 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 152 return; | 150 return; |
| 153 } | 151 } |
| 154 p.setShader(SkGradientShader::MakeRadial(center, radius, colors, pos, | 152 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(), |
| 155 count, mode, flags, localMatrix.getMaybeNull())); | 153 pos.data(), count, mode, flags, localMatrix.getMaybeNull())); |
| 156 | 154 |
| 157 | 155 |
| 158 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 156 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 159 if (useGlobalMatrix) { | 157 if (useGlobalMatrix) { |
| 160 SkMatrix gm; | 158 SkMatrix gm; |
| 161 if (!makeMatrix(fuzz, &gm)) { | 159 if (!makeMatrix(fuzz, &gm)) { |
| 162 return; | 160 return; |
| 163 } | 161 } |
| 164 SkCanvas* c = surface->getCanvas(); | 162 SkCanvas* c = surface->getCanvas(); |
| 165 c->setMatrix(gm); | 163 c->setMatrix(gm); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 179 !fuzz->next(&d) || | 177 !fuzz->next(&d) || |
| 180 !fuzz->next(&endRadius) || | 178 !fuzz->next(&endRadius) || |
| 181 !fuzz->next(&useLocalMatrix) || | 179 !fuzz->next(&useLocalMatrix) || |
| 182 !fuzz->next(&useGlobalMatrix)) { | 180 !fuzz->next(&useGlobalMatrix)) { |
| 183 return; | 181 return; |
| 184 } | 182 } |
| 185 SkPoint start = SkPoint::Make(a, b); | 183 SkPoint start = SkPoint::Make(a, b); |
| 186 SkPoint end = SkPoint::Make(c, d); | 184 SkPoint end = SkPoint::Make(c, d); |
| 187 | 185 |
| 188 uint32_t count; | 186 uint32_t count; |
| 189 SkColor* colors; | 187 std::vector<SkColor> colors; |
| 190 SkScalar* pos; | 188 std::vector<SkScalar> pos; |
| 191 SkShader::TileMode mode; | 189 SkShader::TileMode mode; |
| 192 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 190 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 193 return; | 191 return; |
| 194 } | 192 } |
| 195 | 193 |
| 196 SkPaint p; | 194 SkPaint p; |
| 197 uint32_t flags; | 195 uint32_t flags; |
| 198 if (!fuzz->next(&flags)) { | 196 if (!fuzz->next(&flags)) { |
| 199 return; | 197 return; |
| 200 } | 198 } |
| 201 | 199 |
| 202 SkTLazy<SkMatrix> localMatrix; | 200 SkTLazy<SkMatrix> localMatrix; |
| 203 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { | 201 if (useLocalMatrix && !makeMatrix(fuzz, localMatrix.init())) { |
| 204 return; | 202 return; |
| 205 } | 203 } |
| 206 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, en d, | 204 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, |
| 207 endRadius, colors, pos, count, mode, flags, localMatrix.getMaybeNull ())); | 205 end, endRadius, colors.data(), pos.data(), count, mode, flags, |
| 206 localMatrix.getMaybeNull())); | |
| 208 | 207 |
| 209 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 208 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 210 if (useGlobalMatrix) { | 209 if (useGlobalMatrix) { |
| 211 SkMatrix gm; | 210 SkMatrix gm; |
| 212 if (!makeMatrix(fuzz, &gm)) { | 211 if (!makeMatrix(fuzz, &gm)) { |
| 213 return; | 212 return; |
| 214 } | 213 } |
| 215 SkCanvas* c = surface->getCanvas(); | 214 SkCanvas* c = surface->getCanvas(); |
| 216 c->setMatrix(gm); | 215 c->setMatrix(gm); |
| 217 c->drawPaint(p); | 216 c->drawPaint(p); |
| 218 } else { | 217 } else { |
| 219 surface->getCanvas()->drawPaint(p); | 218 surface->getCanvas()->drawPaint(p); |
| 220 } | 219 } |
| 221 } | 220 } |
| 222 | 221 |
| 223 void fuzzSweepGradient(Fuzz* fuzz) { | 222 void fuzzSweepGradient(Fuzz* fuzz) { |
| 224 SkScalar cx, cy; | 223 SkScalar cx, cy; |
| 225 bool useLocalMatrix, useGlobalMatrix; | 224 bool useLocalMatrix, useGlobalMatrix; |
| 226 if (!fuzz->next(&cx) || | 225 if (!fuzz->next(&cx) || |
| 227 !fuzz->next(&cy) || | 226 !fuzz->next(&cy) || |
| 228 !fuzz->next(&useLocalMatrix) || | 227 !fuzz->next(&useLocalMatrix) || |
| 229 !fuzz->next(&useGlobalMatrix)) { | 228 !fuzz->next(&useGlobalMatrix)) { |
| 230 return; | 229 return; |
| 231 } | 230 } |
| 232 | 231 |
| 233 uint32_t count; | 232 uint32_t count; |
| 234 SkColor* colors; | 233 std::vector<SkColor> colors; |
| 235 SkScalar* pos; | 234 std::vector<SkScalar> pos; |
| 236 SkShader::TileMode mode; | 235 SkShader::TileMode mode; |
| 237 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { | 236 if (!initGradientParams(fuzz, &count, &colors, &pos, &mode)) { |
| 238 return; | 237 return; |
| 239 } | 238 } |
| 240 | 239 |
| 241 SkPaint p; | 240 SkPaint p; |
| 242 if (useLocalMatrix) { | 241 if (useLocalMatrix) { |
| 243 SkMatrix m; | 242 SkMatrix m; |
| 244 if (!makeMatrix(fuzz, &m)) { | 243 if (!makeMatrix(fuzz, &m)) { |
| 245 return; | 244 return; |
| 246 } | 245 } |
| 247 uint32_t flags; | 246 uint32_t flags; |
| 248 if (!fuzz->next(&flags)) { | 247 if (!fuzz->next(&flags)) { |
| 249 return; | 248 return; |
| 250 } | 249 } |
| 251 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count, flags, &m)); | 250 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 251 pos.data(), count, flags, &m)); | |
| 252 } else { | 252 } else { |
| 253 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors, pos, count)) ; | 253 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), |
| 254 pos.data(), count)); | |
| 254 } | 255 } |
| 255 | 256 |
| 256 | 257 |
| 257 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); | 258 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); |
| 258 if (useGlobalMatrix) { | 259 if (useGlobalMatrix) { |
| 259 SkMatrix gm; | 260 SkMatrix gm; |
| 260 if (!makeMatrix(fuzz, &gm)) { | 261 if (!makeMatrix(fuzz, &gm)) { |
| 261 return; | 262 return; |
| 262 } | 263 } |
| 263 SkCanvas* c = surface->getCanvas(); | 264 SkCanvas* c = surface->getCanvas(); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 285 return; | 286 return; |
| 286 case 2: | 287 case 2: |
| 287 SkDebugf("TwoPointConicalGradient\n"); | 288 SkDebugf("TwoPointConicalGradient\n"); |
| 288 fuzzTwoPointConicalGradient(fuzz); | 289 fuzzTwoPointConicalGradient(fuzz); |
| 289 return; | 290 return; |
| 290 } | 291 } |
| 291 SkDebugf("SweepGradient\n"); | 292 SkDebugf("SweepGradient\n"); |
| 292 fuzzSweepGradient(fuzz); | 293 fuzzSweepGradient(fuzz); |
| 293 return; | 294 return; |
| 294 } | 295 } |
| OLD | NEW |