Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(426)

Side by Side Diff: fuzz/FuzzGradients.cpp

Issue 2478593003: Avoid params being initialized out of order in Fuzzer (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fuzz/Fuzz.h ('k') | fuzz/FuzzParsePath.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <vector> 15 #include <vector>
16 16
17 const int MAX_COUNT = 400; 17 const int MAX_COUNT = 400;
18 18
19 void makeMatrix(Fuzz* fuzz, SkMatrix* m) { 19 void makeMatrix(Fuzz* fuzz, SkMatrix* m) {
20 m->setAll(fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScala r>(), 20 SkScalar a, b, c, d, e, f, g, h, i;
21 fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScala r>(), 21 fuzz->next(&a, &b, &c, &d, &e, &f, &g, &h, &i);
22 fuzz->next<SkScalar>(), fuzz->next<SkScalar>(), fuzz->next<SkScala r>()); 22 m->setAll(a, b, c, d, e, f, g, h, i);
23 }
24
25 SkPoint makePoint(Fuzz* fuzz) {
26 SkScalar x, y; fuzz->next(&x, &y);
27 return SkPoint::Make(x, y);
23 } 28 }
24 29
25 void initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors, 30 void initGradientParams(Fuzz* fuzz, std::vector<SkColor>* colors,
26 std::vector<SkScalar>* pos, SkShader::TileMode* mode) { 31 std::vector<SkScalar>* pos, SkShader::TileMode* mode) {
27 int count = fuzz->nextRange(0, MAX_COUNT); 32 int count; fuzz->nextRange(&count, 0, MAX_COUNT);
28 33
29 *mode = static_cast<SkShader::TileMode>(fuzz->nextRange(0, 2)); 34 int m; fuzz->nextRange(&m, 0, 2);
35 *mode = static_cast<SkShader::TileMode>(m);
30 36
31 colors->clear(); 37 colors->clear();
32 pos ->clear(); 38 pos ->clear();
33 for (int i = 0; i < count; i++) { 39 for (int i = 0; i < count; i++) {
34 colors->push_back(fuzz->next<SkColor>()); 40 SkColor c;
35 pos ->push_back(fuzz->next<SkScalar>()); 41 SkScalar s;
42 fuzz->next(&c, &s);
43 colors->push_back(c);
44 pos ->push_back(s);
36 } 45 }
37 if (count) { 46 if (count) {
38 std::sort(pos->begin(), pos->end()); 47 std::sort(pos->begin(), pos->end());
39 // The order matters. If count == 1, we want pos == 0. 48 // The order matters. If count == 1, we want pos == 0.
40 (*pos)[count - 1] = 1; 49 (*pos)[count - 1] = 1;
41 (*pos)[0] = 0; 50 (*pos)[0] = 0;
42 } 51 }
43 } 52 }
44 53
45 void fuzzLinearGradient(Fuzz* fuzz) { 54 void fuzzLinearGradient(Fuzz* fuzz) {
46 SkPoint pts[2] = {SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar> ()), 55 // Hey, mtklein, is this deterministic?
47 SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar> ())}; 56 SkPoint pts[2] = {makePoint(fuzz), makePoint(fuzz)};
48 bool useLocalMatrix = fuzz->next<bool>(); 57 bool useLocalMatrix, useGlobalMatrix;
49 bool useGlobalMatrix = fuzz->next<bool>(); 58 fuzz->next(&useLocalMatrix, &useGlobalMatrix);
50 59
51 std::vector<SkColor> colors; 60 std::vector<SkColor> colors;
52 std::vector<SkScalar> pos; 61 std::vector<SkScalar> pos;
53 SkShader::TileMode mode; 62 SkShader::TileMode mode;
54 initGradientParams(fuzz, &colors, &pos, &mode); 63 initGradientParams(fuzz, &colors, &pos, &mode);
55 64
56 SkPaint p; 65 SkPaint p;
57 uint32_t flags = fuzz->next<uint32_t>(); 66 uint32_t flags; fuzz->next(&flags);
58 67
59 SkTLazy<SkMatrix> localMatrix; 68 SkTLazy<SkMatrix> localMatrix;
60 if (useLocalMatrix) { 69 if (useLocalMatrix) {
61 makeMatrix(fuzz, localMatrix.init()); 70 makeMatrix(fuzz, localMatrix.init());
62 } 71 }
63 p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(), 72 p.setShader(SkGradientShader::MakeLinear(pts, colors.data(), pos.data(),
64 colors.size(), mode, flags, localMatrix.getMaybeNull())); 73 colors.size(), mode, flags, localMatrix.getMaybeNull()));
65 74
66 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 75 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
67 if (useGlobalMatrix) { 76 if (useGlobalMatrix) {
68 SkMatrix gm; 77 SkMatrix gm;
69 makeMatrix(fuzz, &gm); 78 makeMatrix(fuzz, &gm);
70 SkCanvas* c = surface->getCanvas(); 79 SkCanvas* c = surface->getCanvas();
71 c->setMatrix(gm); 80 c->setMatrix(gm);
72 c->drawPaint(p); 81 c->drawPaint(p);
73 } else { 82 } else {
74 surface->getCanvas()->drawPaint(p); 83 surface->getCanvas()->drawPaint(p);
75 } 84 }
76 } 85 }
77 86
78 void fuzzRadialGradient(Fuzz* fuzz) { 87 void fuzzRadialGradient(Fuzz* fuzz) {
79 SkPoint center = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>( )); 88 SkPoint center = makePoint(fuzz);
80 SkScalar radius = fuzz->next<SkScalar>(); 89 SkScalar radius;
81 bool useLocalMatrix = fuzz->next<bool>(); 90 bool useLocalMatrix, useGlobalMatrix;
82 bool useGlobalMatrix = fuzz->next<bool>(); 91 fuzz->next(&radius, &useLocalMatrix, &useGlobalMatrix);
83 92
84 93
85 std::vector<SkColor> colors; 94 std::vector<SkColor> colors;
86 std::vector<SkScalar> pos; 95 std::vector<SkScalar> pos;
87 SkShader::TileMode mode; 96 SkShader::TileMode mode;
88 initGradientParams(fuzz, &colors, &pos, &mode); 97 initGradientParams(fuzz, &colors, &pos, &mode);
89 98
90 SkPaint p; 99 SkPaint p;
91 uint32_t flags = fuzz->next<uint32_t>(); 100 uint32_t flags; fuzz->next(&flags);
92 101
93 SkTLazy<SkMatrix> localMatrix; 102 SkTLazy<SkMatrix> localMatrix;
94 if (useLocalMatrix) { 103 if (useLocalMatrix) {
95 makeMatrix(fuzz, localMatrix.init()); 104 makeMatrix(fuzz, localMatrix.init());
96 } 105 }
97 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(), 106 p.setShader(SkGradientShader::MakeRadial(center, radius, colors.data(),
98 pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull())); 107 pos.data(), colors.size(), mode, flags, localMatrix.getMaybeNull()));
99 108
100 109
101 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 110 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
102 if (useGlobalMatrix) { 111 if (useGlobalMatrix) {
103 SkMatrix gm; 112 SkMatrix gm;
104 makeMatrix(fuzz, &gm); 113 makeMatrix(fuzz, &gm);
105 SkCanvas* c = surface->getCanvas(); 114 SkCanvas* c = surface->getCanvas();
106 c->setMatrix(gm); 115 c->setMatrix(gm);
107 c->drawPaint(p); 116 c->drawPaint(p);
108 } else { 117 } else {
109 surface->getCanvas()->drawPaint(p); 118 surface->getCanvas()->drawPaint(p);
110 } 119 }
111 } 120 }
112 121
113 void fuzzTwoPointConicalGradient(Fuzz* fuzz) { 122 void fuzzTwoPointConicalGradient(Fuzz* fuzz) {
114 SkPoint start = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>() ); 123 SkPoint start = makePoint(fuzz);
115 SkPoint end = SkPoint::Make(fuzz->next<SkScalar>(), fuzz->next<SkScalar>()); 124 SkPoint end = makePoint(fuzz);
116 SkScalar startRadius = fuzz->next<SkScalar>(); 125 SkScalar startRadius, endRadius;
117 SkScalar endRadius = fuzz->next<SkScalar>(); 126 bool useLocalMatrix, useGlobalMatrix;
118 bool useLocalMatrix = fuzz->next<bool>(); 127 fuzz->next(&startRadius, &endRadius, &useLocalMatrix, &useGlobalMatrix);
119 bool useGlobalMatrix = fuzz->next<bool>();
120 128
121 std::vector<SkColor> colors; 129 std::vector<SkColor> colors;
122 std::vector<SkScalar> pos; 130 std::vector<SkScalar> pos;
123 SkShader::TileMode mode; 131 SkShader::TileMode mode;
124 initGradientParams(fuzz, &colors, &pos, &mode); 132 initGradientParams(fuzz, &colors, &pos, &mode);
125 133
126 SkPaint p; 134 SkPaint p;
127 uint32_t flags = fuzz->next<uint32_t>(); 135 uint32_t flags; fuzz->next(&flags);
128 136
129 SkTLazy<SkMatrix> localMatrix; 137 SkTLazy<SkMatrix> localMatrix;
130 if (useLocalMatrix) { 138 if (useLocalMatrix) {
131 makeMatrix(fuzz, localMatrix.init()); 139 makeMatrix(fuzz, localMatrix.init());
132 } 140 }
133 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius, 141 p.setShader(SkGradientShader::MakeTwoPointConical(start, startRadius,
134 end, endRadius, colors.data(), pos.data(), colors.size(), mode, 142 end, endRadius, colors.data(), pos.data(), colors.size(), mode,
135 flags, localMatrix.getMaybeNull())); 143 flags, localMatrix.getMaybeNull()));
136 144
137 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 145 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
138 if (useGlobalMatrix) { 146 if (useGlobalMatrix) {
139 SkMatrix gm; 147 SkMatrix gm;
140 makeMatrix(fuzz, &gm); 148 makeMatrix(fuzz, &gm);
141 SkCanvas* c = surface->getCanvas(); 149 SkCanvas* c = surface->getCanvas();
142 c->setMatrix(gm); 150 c->setMatrix(gm);
143 c->drawPaint(p); 151 c->drawPaint(p);
144 } else { 152 } else {
145 surface->getCanvas()->drawPaint(p); 153 surface->getCanvas()->drawPaint(p);
146 } 154 }
147 } 155 }
148 156
149 void fuzzSweepGradient(Fuzz* fuzz) { 157 void fuzzSweepGradient(Fuzz* fuzz) {
150 SkScalar cx = fuzz->next<SkScalar>(); 158 SkScalar cx, cy;
151 SkScalar cy = fuzz->next<SkScalar>(); 159 bool useLocalMatrix, useGlobalMatrix;
152 bool useLocalMatrix = fuzz->next<bool>(); 160 fuzz->next(&cx, &cy, &useLocalMatrix, &useGlobalMatrix);
153 bool useGlobalMatrix = fuzz->next<bool>();
154 161
155 std::vector<SkColor> colors; 162 std::vector<SkColor> colors;
156 std::vector<SkScalar> pos; 163 std::vector<SkScalar> pos;
157 SkShader::TileMode mode; 164 SkShader::TileMode mode;
158 initGradientParams(fuzz, &colors, &pos, &mode); 165 initGradientParams(fuzz, &colors, &pos, &mode);
159 166
160 SkPaint p; 167 SkPaint p;
161 if (useLocalMatrix) { 168 if (useLocalMatrix) {
162 SkMatrix m; 169 SkMatrix m;
163 makeMatrix(fuzz, &m); 170 makeMatrix(fuzz, &m);
164 uint32_t flags = fuzz->next<uint32_t>(); 171 uint32_t flags; fuzz->next(&flags);
165 172
166 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), 173 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
167 pos.data(), colors.size(), flags, &m)); 174 pos.data(), colors.size(), flags, &m));
168 } else { 175 } else {
169 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(), 176 p.setShader(SkGradientShader::MakeSweep(cx, cy, colors.data(),
170 pos.data(), colors.size())); 177 pos.data(), colors.size()));
171 } 178 }
172 179
173 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50)); 180 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(50, 50));
174 if (useGlobalMatrix) { 181 if (useGlobalMatrix) {
175 SkMatrix gm; 182 SkMatrix gm;
176 makeMatrix(fuzz, &gm); 183 makeMatrix(fuzz, &gm);
177 SkCanvas* c = surface->getCanvas(); 184 SkCanvas* c = surface->getCanvas();
178 c->setMatrix(gm); 185 c->setMatrix(gm);
179 c->drawPaint(p); 186 c->drawPaint(p);
180 } else { 187 } else {
181 surface->getCanvas()->drawPaint(p); 188 surface->getCanvas()->drawPaint(p);
182 } 189 }
183 } 190 }
184 191
185 DEF_FUZZ(Gradients, fuzz) { 192 DEF_FUZZ(Gradients, fuzz) {
186 uint8_t i = fuzz->next<uint8_t>(); 193 uint8_t i; fuzz->next(&i);
187 194
188 switch(i) { 195 switch(i) {
189 case 0: 196 case 0:
190 SkDebugf("LinearGradient\n"); 197 SkDebugf("LinearGradient\n");
191 fuzzLinearGradient(fuzz); 198 fuzzLinearGradient(fuzz);
192 return; 199 return;
193 case 1: 200 case 1:
194 SkDebugf("RadialGradient\n"); 201 SkDebugf("RadialGradient\n");
195 fuzzRadialGradient(fuzz); 202 fuzzRadialGradient(fuzz);
196 return; 203 return;
197 case 2: 204 case 2:
198 SkDebugf("TwoPointConicalGradient\n"); 205 SkDebugf("TwoPointConicalGradient\n");
199 fuzzTwoPointConicalGradient(fuzz); 206 fuzzTwoPointConicalGradient(fuzz);
200 return; 207 return;
201 } 208 }
202 SkDebugf("SweepGradient\n"); 209 SkDebugf("SweepGradient\n");
203 fuzzSweepGradient(fuzz); 210 fuzzSweepGradient(fuzz);
204 return; 211 return;
205 } 212 }
OLDNEW
« no previous file with comments | « fuzz/Fuzz.h ('k') | fuzz/FuzzParsePath.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698