blob: bba88c04d42a37f4b4b1369267a7e5d30270f634 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrTestUtils.h"
Robert Phillips1efecea2019-02-15 16:58:40 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkMatrix.h"
11#include "include/core/SkPath.h"
12#include "include/core/SkRRect.h"
13#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/core/SkRectPriv.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040015#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrProcessorUnitTest.h"
17#include "src/gpu/GrStyle.h"
18#include "src/utils/SkDashPathPriv.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070019
Hal Canary6f6961e2017-01-31 13:50:44 -050020#if GR_TEST_UTILS
joshualitt3f655f32015-04-29 10:01:22 -070021
robertphillips01a19502016-07-06 09:58:57 -070022static const SkMatrix& test_matrix(SkRandom* random,
23 bool includeNonPerspective,
24 bool includePerspective) {
joshualitt4eaf9ce2015-04-28 13:31:18 -070025 static SkMatrix gMatrices[5];
joshualitt2fbd4062015-05-07 13:06:41 -070026 static const int kPerspectiveCount = 1;
joshualitt4eaf9ce2015-04-28 13:31:18 -070027 static bool gOnce;
28 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070029 gOnce = true;
joshualitt4eaf9ce2015-04-28 13:31:18 -070030 gMatrices[0].reset();
31 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
32 gMatrices[2].setRotate(SkIntToScalar(17));
33 gMatrices[3].setRotate(SkIntToScalar(185));
34 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
35 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
joshualitt2fbd4062015-05-07 13:06:41 -070036
37 // Perspective matrices
joshualitt4eaf9ce2015-04-28 13:31:18 -070038 gMatrices[4].setRotate(SkIntToScalar(215));
39 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
40 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
joshualitt4eaf9ce2015-04-28 13:31:18 -070041 }
joshualitt2fbd4062015-05-07 13:06:41 -070042
43 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
robertphillips01a19502016-07-06 09:58:57 -070044 if (includeNonPerspective && includePerspective) {
joshualitt2fbd4062015-05-07 13:06:41 -070045 return gMatrices[random->nextULessThan(count)];
robertphillips01a19502016-07-06 09:58:57 -070046 } else if (!includeNonPerspective) {
47 return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
joshualitt2fbd4062015-05-07 13:06:41 -070048 } else {
robertphillips01a19502016-07-06 09:58:57 -070049 SkASSERT(includeNonPerspective && !includePerspective);
joshualitt2fbd4062015-05-07 13:06:41 -070050 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
51 }
joshualitt4eaf9ce2015-04-28 13:31:18 -070052}
joshualitt3f655f32015-04-29 10:01:22 -070053
joshualitt2fbd4062015-05-07 13:06:41 -070054namespace GrTest {
robertphillips01a19502016-07-06 09:58:57 -070055const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
joshualitt2fbd4062015-05-07 13:06:41 -070056
joshualittfa2008f2015-04-29 11:32:05 -070057const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
joshualitt3e708c52015-04-30 13:49:27 -070058 static SkMatrix gMatrices[5];
joshualittfa2008f2015-04-29 11:32:05 -070059 static bool gOnce;
60 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070061 gOnce = true;
joshualittfa2008f2015-04-29 11:32:05 -070062 // identity
63 gMatrices[0].reset();
64 // translation
65 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
66 // scale
67 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
68 // scale + translation
69 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
70 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
71 // orthogonal basis vectors
72 gMatrices[4].reset();
73 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
74 gMatrices[4].setRotate(47);
joshualittfa2008f2015-04-29 11:32:05 -070075
76 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
77 SkASSERT(gMatrices[i].preservesRightAngles());
78 }
79 }
80 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
81}
82
joshualitt3e708c52015-04-30 13:49:27 -070083const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
84 static SkMatrix gMatrices[6];
joshualitt3f655f32015-04-29 10:01:22 -070085 static bool gOnce;
86 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070087 gOnce = true;
88 // identity
89 gMatrices[0].reset();
90 // translation
91 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
92 // scale
93 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
94 // scale + translation
95 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
96 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
97 // reflection
98 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
99 // 90 degress rotation
100 gMatrices[5].setRotate(90);
101
102 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
103 SkASSERT(gMatrices[i].rectStaysRect());
104 }
105 }
106 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
107}
108
robertphillips01a19502016-07-06 09:58:57 -0700109const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
110const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
joshualitt2fbd4062015-05-07 13:06:41 -0700111
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400112void TestWrapModes(SkRandom* random, GrSamplerState::WrapMode wrapModes[2]) {
113 static const GrSamplerState::WrapMode kWrapModes[] = {
114 GrSamplerState::WrapMode::kClamp,
115 GrSamplerState::WrapMode::kRepeat,
116 GrSamplerState::WrapMode::kMirrorRepeat,
117 };
118 wrapModes[0] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
119 wrapModes[1] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
120}
joshualitt3e708c52015-04-30 13:49:27 -0700121const SkRect& TestRect(SkRandom* random) {
122 static SkRect gRects[7];
123 static bool gOnce;
124 if (!gOnce) {
125 gOnce = true;
joshualitt3f655f32015-04-29 10:01:22 -0700126 gRects[0] = SkRect::MakeWH(1.f, 1.f);
127 gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
128 gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
Mike Reed274218e2018-01-08 15:05:02 -0500129 gRects[3] = SkRectPriv::MakeLargest();
joshualitt11edad92015-09-22 10:32:28 -0700130 gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
131 gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
joshualitt6c891102015-05-13 08:51:49 -0700132 }
133 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
134}
135
136// Just some simple rects for code which expects its input very sanitized
137const SkRect& TestSquare(SkRandom* random) {
138 static SkRect gRects[2];
139 static bool gOnce;
140 if (!gOnce) {
141 gOnce = true;
142 gRects[0] = SkRect::MakeWH(128.f, 128.f);
143 gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
joshualitt3f655f32015-04-29 10:01:22 -0700144 }
145 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
146}
joshualitt3e708c52015-04-30 13:49:27 -0700147
148const SkRRect& TestRRectSimple(SkRandom* random) {
joshualitt6c891102015-05-13 08:51:49 -0700149 static SkRRect gRRect[2];
joshualitt3e708c52015-04-30 13:49:27 -0700150 static bool gOnce;
151 if (!gOnce) {
152 gOnce = true;
joshualitt3e708c52015-04-30 13:49:27 -0700153 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
joshualitt3e708c52015-04-30 13:49:27 -0700154 // true round rect with circular corners
joshualitt6c891102015-05-13 08:51:49 -0700155 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
joshualitt3e708c52015-04-30 13:49:27 -0700156 // true round rect with elliptical corners
joshualitt6c891102015-05-13 08:51:49 -0700157 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
joshualitt3e708c52015-04-30 13:49:27 -0700158
159 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
160 SkASSERT(gRRect[i].isSimple());
161 }
162 }
163 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
164}
165
joshualitt40ded322015-05-02 07:07:17 -0700166const SkPath& TestPath(SkRandom* random) {
167 static SkPath gPath[7];
168 static bool gOnce;
169 if (!gOnce) {
170 gOnce = true;
171 // line
172 gPath[0].moveTo(0.f, 0.f);
173 gPath[0].lineTo(10.f, 10.f);
174 // quad
175 gPath[1].moveTo(0.f, 0.f);
176 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
177 // conic
178 gPath[2].moveTo(0.f, 0.f);
179 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
180 // cubic
181 gPath[3].moveTo(0.f, 0.f);
182 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
183 // all three
184 gPath[4].moveTo(0.f, 0.f);
185 gPath[4].lineTo(10.f, 10.f);
186 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
187 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
188 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
189 // convex
190 gPath[5].moveTo(0.0f, 0.0f);
191 gPath[5].lineTo(10.0f, 0.0f);
192 gPath[5].lineTo(10.0f, 10.0f);
193 gPath[5].lineTo(0.0f, 10.0f);
194 gPath[5].close();
195 // concave
196 gPath[6].moveTo(0.0f, 0.0f);
197 gPath[6].lineTo(5.0f, 5.0f);
198 gPath[6].lineTo(10.0f, 0.0f);
199 gPath[6].lineTo(10.0f, 10.0f);
200 gPath[6].lineTo(0.0f, 10.0f);
201 gPath[6].close();
202 }
203
204 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
205}
206
joshualitt8e5c1772015-05-11 08:58:52 -0700207const SkPath& TestPathConvex(SkRandom* random) {
208 static SkPath gPath[3];
209 static bool gOnce;
210 if (!gOnce) {
211 gOnce = true;
212 // narrow rect
joshualitt6c891102015-05-13 08:51:49 -0700213 gPath[0].moveTo(-1.5f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700214 gPath[0].lineTo(-1.5f, -50.0f);
215 gPath[0].lineTo( 1.5f, -50.0f);
216 gPath[0].lineTo( 1.5f, 50.0f);
217 gPath[0].lineTo(-1.5f, 50.0f);
218 // degenerate
joshualitt6c891102015-05-13 08:51:49 -0700219 gPath[1].moveTo(-0.025f, -0.025f);
joshualitt8e5c1772015-05-11 08:58:52 -0700220 gPath[1].lineTo(-0.025f, -0.025f);
221 gPath[1].lineTo( 0.025f, -0.025f);
222 gPath[1].lineTo( 0.025f, 0.025f);
223 gPath[1].lineTo(-0.025f, 0.025f);
224 // clipped triangle
joshualitt6c891102015-05-13 08:51:49 -0700225 gPath[2].moveTo(-10.0f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700226 gPath[2].lineTo(-10.0f, -50.0f);
227 gPath[2].lineTo( 10.0f, -50.0f);
228 gPath[2].lineTo( 50.0f, 31.0f);
229 gPath[2].lineTo( 40.0f, 50.0f);
230 gPath[2].lineTo(-40.0f, 50.0f);
231 gPath[2].lineTo(-50.0f, 31.0f);
joshualitt6c891102015-05-13 08:51:49 -0700232
233 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
Mike Reed30bc5272019-11-22 18:34:02 +0000234 SkASSERT(SkPathConvexityType::kConvex == gPath[i].getConvexityType());
joshualitt6c891102015-05-13 08:51:49 -0700235 }
joshualitt8e5c1772015-05-11 08:58:52 -0700236 }
237
238 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
239}
240
senorblancob4f9d0e2015-08-06 10:28:55 -0700241static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
senorblanco59cd3672015-08-05 13:37:49 -0700242 bool strokeAndFill = random->nextBool();
243 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
senorblancob4f9d0e2015-08-06 10:28:55 -0700244 rec->setStrokeStyle(strokeWidth, strokeAndFill);
joshualitt21279c72015-05-11 07:21:37 -0700245
senorblanco59cd3672015-08-05 13:37:49 -0700246 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
247 SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
248 SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
senorblancob4f9d0e2015-08-06 10:28:55 -0700249 rec->setStrokeParams(cap, join, miterLimit);
250}
251
252SkStrokeRec TestStrokeRec(SkRandom* random) {
253 SkStrokeRec::InitStyle style =
254 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
255 SkStrokeRec rec(style);
256 randomize_stroke_rec(&rec, random);
senorblanco59cd3672015-08-05 13:37:49 -0700257 return rec;
senorblanco29e0d3f2015-08-05 13:18:03 -0700258}
259
bsalomon6663acf2016-05-10 09:14:17 -0700260void TestStyle(SkRandom* random, GrStyle* style) {
261 SkStrokeRec::InitStyle initStyle =
senorblancob4f9d0e2015-08-06 10:28:55 -0700262 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
bsalomon6663acf2016-05-10 09:14:17 -0700263 SkStrokeRec stroke(initStyle);
264 randomize_stroke_rec(&stroke, random);
265 sk_sp<SkPathEffect> pe;
266 if (random->nextBool()) {
267 int cnt = random->nextRangeU(1, 50) * 2;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400268 std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
bsalomon6663acf2016-05-10 09:14:17 -0700269 SkScalar sum = 0;
270 for (int i = 0; i < cnt; i++) {
271 intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
272 SkDoubleToScalar(10.0));
273 sum += intervals[i];
274 }
275 SkScalar phase = random->nextRangeScalar(0, sum);
276 pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
senorblancob4f9d0e2015-08-06 10:28:55 -0700277 }
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500278 *style = GrStyle(stroke, std::move(pe));
senorblancob4f9d0e2015-08-06 10:28:55 -0700279}
280
bsalomon6663acf2016-05-10 09:14:17 -0700281TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
282 fCount = count;
283 fIntervals.reset(count);
284 memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
285 SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
286 &fInitialDashIndex, &fIntervalLength, &fPhase);
287}
288
Mike Reed6d10f8b2018-08-16 13:22:16 -0400289 bool TestDashPathEffect::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
290 const SkRect* cullRect) const {
bsalomon6663acf2016-05-10 09:14:17 -0700291 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
292 fInitialDashLength, fInitialDashIndex, fIntervalLength);
293}
294
Mike Reed6d10f8b2018-08-16 13:22:16 -0400295SkPathEffect::DashType TestDashPathEffect::onAsADash(DashInfo* info) const {
bsalomon6663acf2016-05-10 09:14:17 -0700296 if (info) {
297 if (info->fCount >= fCount && info->fIntervals) {
298 memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
299 }
300 info->fCount = fCount;
301 info->fPhase = fPhase;
302 }
303 return kDash_DashType;
304}
305
Brian Osman0d9dfe92016-10-03 15:24:44 -0400306sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
307 static sk_sp<SkColorSpace> gColorSpaces[3];
308 static bool gOnce;
309 if (!gOnce) {
310 gOnce = true;
311 // No color space (legacy mode)
312 gColorSpaces[0] = nullptr;
Brian Osman10b81422017-12-14 10:27:21 -0500313 // sRGB or color-spin sRGB
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500314 gColorSpaces[1] = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500315 gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osman0d9dfe92016-10-03 15:24:44 -0400316 }
317 return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
318}
319
Brian Osmane2f732f2016-10-03 14:23:50 -0400320sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
Brian Osman653f34d2018-06-14 11:44:02 -0400321 // TODO: Add many more kinds of xforms here
Brian Osmane2f732f2016-10-03 14:23:50 -0400322 static sk_sp<GrColorSpaceXform> gXforms[3];
323 static bool gOnce;
324 if (!gOnce) {
325 gOnce = true;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500326 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500327 sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osmane2f732f2016-10-03 14:23:50 -0400328 // No gamut change
329 gXforms[0] = nullptr;
Mike Kleine03a1762018-08-22 11:52:16 -0400330 gXforms[1] = GrColorSpaceXform::Make(srgb.get(), kPremul_SkAlphaType,
331 spin.get(), kPremul_SkAlphaType);
332 gXforms[2] = GrColorSpaceXform::Make(spin.get(), kPremul_SkAlphaType,
333 srgb.get(), kPremul_SkAlphaType);
Brian Osmane2f732f2016-10-03 14:23:50 -0400334 }
335 return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
336}
bsalomon6663acf2016-05-10 09:14:17 -0700337
Mike Reed3bc266b2018-01-20 22:24:41 +0000338TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d)
Brian Osman449b1152020-04-15 16:43:00 -0400339 : fMatrixProvider(TestMatrix(d->fRandom))
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500340 , fColorInfoStorage(std::make_unique<GrColorInfo>(
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400341 GrColorType::kRGBA_8888, kPremul_SkAlphaType, TestColorSpace(d->fRandom)))
Brian Osman449b1152020-04-15 16:43:00 -0400342 , fArgs(d->context(),
343 fMatrixProvider,
344 kNone_SkFilterQuality,
345 fColorInfoStorage.get()) {}
Brian Osman9f532a32016-10-19 11:12:09 -0400346
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400347TestAsFPArgs::~TestAsFPArgs() {}
348
bsalomon6663acf2016-05-10 09:14:17 -0700349} // namespace GrTest
joshualitt3f655f32015-04-29 10:01:22 -0700350
351#endif