blob: e4516dd6203169f544ba235c6052868a6a3c576b [file] [log] [blame]
joshualitt4eaf9ce2015-04-28 13:31:18 -07001/*
2 * Copyright 2015 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 "GrTestUtils.h"
Brian Osman9f532a32016-10-19 11:12:09 -04009#include "GrProcessorUnitTest.h"
bsalomon6663acf2016-05-10 09:14:17 -070010#include "GrStyle.h"
Matt Sarett77a7a1b2017-02-07 13:56:11 -050011#include "SkColorSpace_Base.h"
bsalomon6663acf2016-05-10 09:14:17 -070012#include "SkDashPathPriv.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070013#include "SkMatrix.h"
joshualitt40ded322015-05-02 07:07:17 -070014#include "SkPath.h"
joshualitt3e708c52015-04-30 13:49:27 -070015#include "SkRRect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070016
Hal Canary6f6961e2017-01-31 13:50:44 -050017#if GR_TEST_UTILS
joshualitt3f655f32015-04-29 10:01:22 -070018
robertphillips01a19502016-07-06 09:58:57 -070019static const SkMatrix& test_matrix(SkRandom* random,
20 bool includeNonPerspective,
21 bool includePerspective) {
joshualitt4eaf9ce2015-04-28 13:31:18 -070022 static SkMatrix gMatrices[5];
joshualitt2fbd4062015-05-07 13:06:41 -070023 static const int kPerspectiveCount = 1;
joshualitt4eaf9ce2015-04-28 13:31:18 -070024 static bool gOnce;
25 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070026 gOnce = true;
joshualitt4eaf9ce2015-04-28 13:31:18 -070027 gMatrices[0].reset();
28 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
29 gMatrices[2].setRotate(SkIntToScalar(17));
30 gMatrices[3].setRotate(SkIntToScalar(185));
31 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
32 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
joshualitt2fbd4062015-05-07 13:06:41 -070033
34 // Perspective matrices
joshualitt4eaf9ce2015-04-28 13:31:18 -070035 gMatrices[4].setRotate(SkIntToScalar(215));
36 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
37 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
joshualitt4eaf9ce2015-04-28 13:31:18 -070038 }
joshualitt2fbd4062015-05-07 13:06:41 -070039
40 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
robertphillips01a19502016-07-06 09:58:57 -070041 if (includeNonPerspective && includePerspective) {
joshualitt2fbd4062015-05-07 13:06:41 -070042 return gMatrices[random->nextULessThan(count)];
robertphillips01a19502016-07-06 09:58:57 -070043 } else if (!includeNonPerspective) {
44 return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
joshualitt2fbd4062015-05-07 13:06:41 -070045 } else {
robertphillips01a19502016-07-06 09:58:57 -070046 SkASSERT(includeNonPerspective && !includePerspective);
joshualitt2fbd4062015-05-07 13:06:41 -070047 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
48 }
joshualitt4eaf9ce2015-04-28 13:31:18 -070049}
joshualitt3f655f32015-04-29 10:01:22 -070050
joshualitt2fbd4062015-05-07 13:06:41 -070051namespace GrTest {
robertphillips01a19502016-07-06 09:58:57 -070052const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
joshualitt2fbd4062015-05-07 13:06:41 -070053
joshualittfa2008f2015-04-29 11:32:05 -070054const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
joshualitt3e708c52015-04-30 13:49:27 -070055 static SkMatrix gMatrices[5];
joshualittfa2008f2015-04-29 11:32:05 -070056 static bool gOnce;
57 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070058 gOnce = true;
joshualittfa2008f2015-04-29 11:32:05 -070059 // identity
60 gMatrices[0].reset();
61 // translation
62 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
63 // scale
64 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
65 // scale + translation
66 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
67 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
68 // orthogonal basis vectors
69 gMatrices[4].reset();
70 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
71 gMatrices[4].setRotate(47);
joshualittfa2008f2015-04-29 11:32:05 -070072
73 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
74 SkASSERT(gMatrices[i].preservesRightAngles());
75 }
76 }
77 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
78}
79
joshualitt3e708c52015-04-30 13:49:27 -070080const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
81 static SkMatrix gMatrices[6];
joshualitt3f655f32015-04-29 10:01:22 -070082 static bool gOnce;
83 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070084 gOnce = true;
85 // identity
86 gMatrices[0].reset();
87 // translation
88 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
89 // scale
90 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
91 // scale + translation
92 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
93 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
94 // reflection
95 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
96 // 90 degress rotation
97 gMatrices[5].setRotate(90);
98
99 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
100 SkASSERT(gMatrices[i].rectStaysRect());
101 }
102 }
103 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
104}
105
robertphillips01a19502016-07-06 09:58:57 -0700106const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
107const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
joshualitt2fbd4062015-05-07 13:06:41 -0700108
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400109void TestWrapModes(SkRandom* random, GrSamplerState::WrapMode wrapModes[2]) {
110 static const GrSamplerState::WrapMode kWrapModes[] = {
111 GrSamplerState::WrapMode::kClamp,
112 GrSamplerState::WrapMode::kRepeat,
113 GrSamplerState::WrapMode::kMirrorRepeat,
114 };
115 wrapModes[0] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
116 wrapModes[1] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
117}
joshualitt3e708c52015-04-30 13:49:27 -0700118const SkRect& TestRect(SkRandom* random) {
119 static SkRect gRects[7];
120 static bool gOnce;
121 if (!gOnce) {
122 gOnce = true;
joshualitt3f655f32015-04-29 10:01:22 -0700123 gRects[0] = SkRect::MakeWH(1.f, 1.f);
124 gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
125 gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
joshualitt11edad92015-09-22 10:32:28 -0700126 gRects[3] = SkRect::MakeLargest();
127 gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
128 gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
joshualitt6c891102015-05-13 08:51:49 -0700129 }
130 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
131}
132
133// Just some simple rects for code which expects its input very sanitized
134const SkRect& TestSquare(SkRandom* random) {
135 static SkRect gRects[2];
136 static bool gOnce;
137 if (!gOnce) {
138 gOnce = true;
139 gRects[0] = SkRect::MakeWH(128.f, 128.f);
140 gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
joshualitt3f655f32015-04-29 10:01:22 -0700141 }
142 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
143}
joshualitt3e708c52015-04-30 13:49:27 -0700144
145const SkRRect& TestRRectSimple(SkRandom* random) {
joshualitt6c891102015-05-13 08:51:49 -0700146 static SkRRect gRRect[2];
joshualitt3e708c52015-04-30 13:49:27 -0700147 static bool gOnce;
148 if (!gOnce) {
149 gOnce = true;
joshualitt3e708c52015-04-30 13:49:27 -0700150 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
joshualitt3e708c52015-04-30 13:49:27 -0700151 // true round rect with circular corners
joshualitt6c891102015-05-13 08:51:49 -0700152 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
joshualitt3e708c52015-04-30 13:49:27 -0700153 // true round rect with elliptical corners
joshualitt6c891102015-05-13 08:51:49 -0700154 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
joshualitt3e708c52015-04-30 13:49:27 -0700155
156 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
157 SkASSERT(gRRect[i].isSimple());
158 }
159 }
160 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
161}
162
joshualitt40ded322015-05-02 07:07:17 -0700163const SkPath& TestPath(SkRandom* random) {
164 static SkPath gPath[7];
165 static bool gOnce;
166 if (!gOnce) {
167 gOnce = true;
168 // line
169 gPath[0].moveTo(0.f, 0.f);
170 gPath[0].lineTo(10.f, 10.f);
171 // quad
172 gPath[1].moveTo(0.f, 0.f);
173 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
174 // conic
175 gPath[2].moveTo(0.f, 0.f);
176 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
177 // cubic
178 gPath[3].moveTo(0.f, 0.f);
179 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
180 // all three
181 gPath[4].moveTo(0.f, 0.f);
182 gPath[4].lineTo(10.f, 10.f);
183 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
184 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
185 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
186 // convex
187 gPath[5].moveTo(0.0f, 0.0f);
188 gPath[5].lineTo(10.0f, 0.0f);
189 gPath[5].lineTo(10.0f, 10.0f);
190 gPath[5].lineTo(0.0f, 10.0f);
191 gPath[5].close();
192 // concave
193 gPath[6].moveTo(0.0f, 0.0f);
194 gPath[6].lineTo(5.0f, 5.0f);
195 gPath[6].lineTo(10.0f, 0.0f);
196 gPath[6].lineTo(10.0f, 10.0f);
197 gPath[6].lineTo(0.0f, 10.0f);
198 gPath[6].close();
199 }
200
201 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
202}
203
joshualitt8e5c1772015-05-11 08:58:52 -0700204const SkPath& TestPathConvex(SkRandom* random) {
205 static SkPath gPath[3];
206 static bool gOnce;
207 if (!gOnce) {
208 gOnce = true;
209 // narrow rect
joshualitt6c891102015-05-13 08:51:49 -0700210 gPath[0].moveTo(-1.5f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700211 gPath[0].lineTo(-1.5f, -50.0f);
212 gPath[0].lineTo( 1.5f, -50.0f);
213 gPath[0].lineTo( 1.5f, 50.0f);
214 gPath[0].lineTo(-1.5f, 50.0f);
215 // degenerate
joshualitt6c891102015-05-13 08:51:49 -0700216 gPath[1].moveTo(-0.025f, -0.025f);
joshualitt8e5c1772015-05-11 08:58:52 -0700217 gPath[1].lineTo(-0.025f, -0.025f);
218 gPath[1].lineTo( 0.025f, -0.025f);
219 gPath[1].lineTo( 0.025f, 0.025f);
220 gPath[1].lineTo(-0.025f, 0.025f);
221 // clipped triangle
joshualitt6c891102015-05-13 08:51:49 -0700222 gPath[2].moveTo(-10.0f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700223 gPath[2].lineTo(-10.0f, -50.0f);
224 gPath[2].lineTo( 10.0f, -50.0f);
225 gPath[2].lineTo( 50.0f, 31.0f);
226 gPath[2].lineTo( 40.0f, 50.0f);
227 gPath[2].lineTo(-40.0f, 50.0f);
228 gPath[2].lineTo(-50.0f, 31.0f);
joshualitt6c891102015-05-13 08:51:49 -0700229
230 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
231 SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
232 }
joshualitt8e5c1772015-05-11 08:58:52 -0700233 }
234
235 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
236}
237
senorblancob4f9d0e2015-08-06 10:28:55 -0700238static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
senorblanco59cd3672015-08-05 13:37:49 -0700239 bool strokeAndFill = random->nextBool();
240 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
senorblancob4f9d0e2015-08-06 10:28:55 -0700241 rec->setStrokeStyle(strokeWidth, strokeAndFill);
joshualitt21279c72015-05-11 07:21:37 -0700242
senorblanco59cd3672015-08-05 13:37:49 -0700243 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
244 SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
245 SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
senorblancob4f9d0e2015-08-06 10:28:55 -0700246 rec->setStrokeParams(cap, join, miterLimit);
247}
248
249SkStrokeRec TestStrokeRec(SkRandom* random) {
250 SkStrokeRec::InitStyle style =
251 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
252 SkStrokeRec rec(style);
253 randomize_stroke_rec(&rec, random);
senorblanco59cd3672015-08-05 13:37:49 -0700254 return rec;
senorblanco29e0d3f2015-08-05 13:18:03 -0700255}
256
bsalomon6663acf2016-05-10 09:14:17 -0700257void TestStyle(SkRandom* random, GrStyle* style) {
258 SkStrokeRec::InitStyle initStyle =
senorblancob4f9d0e2015-08-06 10:28:55 -0700259 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
bsalomon6663acf2016-05-10 09:14:17 -0700260 SkStrokeRec stroke(initStyle);
261 randomize_stroke_rec(&stroke, random);
262 sk_sp<SkPathEffect> pe;
263 if (random->nextBool()) {
264 int cnt = random->nextRangeU(1, 50) * 2;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400265 std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
bsalomon6663acf2016-05-10 09:14:17 -0700266 SkScalar sum = 0;
267 for (int i = 0; i < cnt; i++) {
268 intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
269 SkDoubleToScalar(10.0));
270 sum += intervals[i];
271 }
272 SkScalar phase = random->nextRangeScalar(0, sum);
273 pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
senorblancob4f9d0e2015-08-06 10:28:55 -0700274 }
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500275 *style = GrStyle(stroke, std::move(pe));
senorblancob4f9d0e2015-08-06 10:28:55 -0700276}
277
bsalomon6663acf2016-05-10 09:14:17 -0700278TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
279 fCount = count;
280 fIntervals.reset(count);
281 memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
282 SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
283 &fInitialDashIndex, &fIntervalLength, &fPhase);
284}
285
286 bool TestDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
287 const SkRect* cullRect) const {
288 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
289 fInitialDashLength, fInitialDashIndex, fIntervalLength);
290}
291
292SkPathEffect::DashType TestDashPathEffect::asADash(DashInfo* info) const {
293 if (info) {
294 if (info->fCount >= fCount && info->fIntervals) {
295 memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
296 }
297 info->fCount = fCount;
298 info->fPhase = fPhase;
299 }
300 return kDash_DashType;
301}
302
Brian Osman0d9dfe92016-10-03 15:24:44 -0400303sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
304 static sk_sp<SkColorSpace> gColorSpaces[3];
305 static bool gOnce;
306 if (!gOnce) {
307 gOnce = true;
308 // No color space (legacy mode)
309 gColorSpaces[0] = nullptr;
310 // sRGB or Adobe
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500311 gColorSpaces[1] = SkColorSpace::MakeSRGB();
312 gColorSpaces[2] = SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named);
Brian Osman0d9dfe92016-10-03 15:24:44 -0400313 }
314 return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
315}
316
Brian Osmane2f732f2016-10-03 14:23:50 -0400317sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
318 static sk_sp<GrColorSpaceXform> gXforms[3];
319 static bool gOnce;
320 if (!gOnce) {
321 gOnce = true;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500322 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
323 sk_sp<SkColorSpace> adobe = SkColorSpace_Base::MakeNamed(SkColorSpace_Base::kAdobeRGB_Named);
Brian Osmane2f732f2016-10-03 14:23:50 -0400324 // No gamut change
325 gXforms[0] = nullptr;
326 // To larger gamut
327 gXforms[1] = GrColorSpaceXform::Make(srgb.get(), adobe.get());
328 // To smaller gamut
329 gXforms[2] = GrColorSpaceXform::Make(adobe.get(), srgb.get());
330 }
331 return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
332}
bsalomon6663acf2016-05-10 09:14:17 -0700333
Brian Osman9f532a32016-10-19 11:12:09 -0400334TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d) {
335 fViewMatrixStorage = TestMatrix(d->fRandom);
336 fColorSpaceStorage = TestColorSpace(d->fRandom);
337
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500338 fArgs.fContext = d->context();
Brian Osman9f532a32016-10-19 11:12:09 -0400339 fArgs.fViewMatrix = &fViewMatrixStorage;
340 fArgs.fLocalMatrix = nullptr;
341 fArgs.fFilterQuality = kNone_SkFilterQuality;
342 fArgs.fDstColorSpace = fColorSpaceStorage.get();
Brian Osman9f532a32016-10-19 11:12:09 -0400343}
344
bsalomon6663acf2016-05-10 09:14:17 -0700345} // namespace GrTest
joshualitt3f655f32015-04-29 10:01:22 -0700346
347#endif