blob: ded7383580d492f7a09814dc305efa67aa8eb141 [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 Salomon4cbb6e62017-10-25 15:12:19 -04009#include "GrColorSpaceInfo.h"
Brian Osman9f532a32016-10-19 11:12:09 -040010#include "GrProcessorUnitTest.h"
bsalomon6663acf2016-05-10 09:14:17 -070011#include "GrStyle.h"
12#include "SkDashPathPriv.h"
Brian Salomon4cbb6e62017-10-25 15:12:19 -040013#include "SkMakeUnique.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070014#include "SkMatrix.h"
joshualitt40ded322015-05-02 07:07:17 -070015#include "SkPath.h"
Mike Reed274218e2018-01-08 15:05:02 -050016#include "SkRectPriv.h"
joshualitt3e708c52015-04-30 13:49:27 -070017#include "SkRRect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070018
Hal Canary6f6961e2017-01-31 13:50:44 -050019#if GR_TEST_UTILS
joshualitt3f655f32015-04-29 10:01:22 -070020
robertphillips01a19502016-07-06 09:58:57 -070021static const SkMatrix& test_matrix(SkRandom* random,
22 bool includeNonPerspective,
23 bool includePerspective) {
joshualitt4eaf9ce2015-04-28 13:31:18 -070024 static SkMatrix gMatrices[5];
joshualitt2fbd4062015-05-07 13:06:41 -070025 static const int kPerspectiveCount = 1;
joshualitt4eaf9ce2015-04-28 13:31:18 -070026 static bool gOnce;
27 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070028 gOnce = true;
joshualitt4eaf9ce2015-04-28 13:31:18 -070029 gMatrices[0].reset();
30 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
31 gMatrices[2].setRotate(SkIntToScalar(17));
32 gMatrices[3].setRotate(SkIntToScalar(185));
33 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
34 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
joshualitt2fbd4062015-05-07 13:06:41 -070035
36 // Perspective matrices
joshualitt4eaf9ce2015-04-28 13:31:18 -070037 gMatrices[4].setRotate(SkIntToScalar(215));
38 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
39 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
joshualitt4eaf9ce2015-04-28 13:31:18 -070040 }
joshualitt2fbd4062015-05-07 13:06:41 -070041
42 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
robertphillips01a19502016-07-06 09:58:57 -070043 if (includeNonPerspective && includePerspective) {
joshualitt2fbd4062015-05-07 13:06:41 -070044 return gMatrices[random->nextULessThan(count)];
robertphillips01a19502016-07-06 09:58:57 -070045 } else if (!includeNonPerspective) {
46 return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
joshualitt2fbd4062015-05-07 13:06:41 -070047 } else {
robertphillips01a19502016-07-06 09:58:57 -070048 SkASSERT(includeNonPerspective && !includePerspective);
joshualitt2fbd4062015-05-07 13:06:41 -070049 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
50 }
joshualitt4eaf9ce2015-04-28 13:31:18 -070051}
joshualitt3f655f32015-04-29 10:01:22 -070052
joshualitt2fbd4062015-05-07 13:06:41 -070053namespace GrTest {
robertphillips01a19502016-07-06 09:58:57 -070054const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
joshualitt2fbd4062015-05-07 13:06:41 -070055
joshualittfa2008f2015-04-29 11:32:05 -070056const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
joshualitt3e708c52015-04-30 13:49:27 -070057 static SkMatrix gMatrices[5];
joshualittfa2008f2015-04-29 11:32:05 -070058 static bool gOnce;
59 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070060 gOnce = true;
joshualittfa2008f2015-04-29 11:32:05 -070061 // identity
62 gMatrices[0].reset();
63 // translation
64 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
65 // scale
66 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
67 // scale + translation
68 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
69 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
70 // orthogonal basis vectors
71 gMatrices[4].reset();
72 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
73 gMatrices[4].setRotate(47);
joshualittfa2008f2015-04-29 11:32:05 -070074
75 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
76 SkASSERT(gMatrices[i].preservesRightAngles());
77 }
78 }
79 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
80}
81
joshualitt3e708c52015-04-30 13:49:27 -070082const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
83 static SkMatrix gMatrices[6];
joshualitt3f655f32015-04-29 10:01:22 -070084 static bool gOnce;
85 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070086 gOnce = true;
87 // identity
88 gMatrices[0].reset();
89 // translation
90 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
91 // scale
92 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
93 // scale + translation
94 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
95 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
96 // reflection
97 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
98 // 90 degress rotation
99 gMatrices[5].setRotate(90);
100
101 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
102 SkASSERT(gMatrices[i].rectStaysRect());
103 }
104 }
105 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
106}
107
robertphillips01a19502016-07-06 09:58:57 -0700108const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
109const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
joshualitt2fbd4062015-05-07 13:06:41 -0700110
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400111void TestWrapModes(SkRandom* random, GrSamplerState::WrapMode wrapModes[2]) {
112 static const GrSamplerState::WrapMode kWrapModes[] = {
113 GrSamplerState::WrapMode::kClamp,
114 GrSamplerState::WrapMode::kRepeat,
115 GrSamplerState::WrapMode::kMirrorRepeat,
116 };
117 wrapModes[0] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
118 wrapModes[1] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
119}
joshualitt3e708c52015-04-30 13:49:27 -0700120const SkRect& TestRect(SkRandom* random) {
121 static SkRect gRects[7];
122 static bool gOnce;
123 if (!gOnce) {
124 gOnce = true;
joshualitt3f655f32015-04-29 10:01:22 -0700125 gRects[0] = SkRect::MakeWH(1.f, 1.f);
126 gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
127 gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
Mike Reed274218e2018-01-08 15:05:02 -0500128 gRects[3] = SkRectPriv::MakeLargest();
joshualitt11edad92015-09-22 10:32:28 -0700129 gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
130 gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
joshualitt6c891102015-05-13 08:51:49 -0700131 }
132 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
133}
134
135// Just some simple rects for code which expects its input very sanitized
136const SkRect& TestSquare(SkRandom* random) {
137 static SkRect gRects[2];
138 static bool gOnce;
139 if (!gOnce) {
140 gOnce = true;
141 gRects[0] = SkRect::MakeWH(128.f, 128.f);
142 gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
joshualitt3f655f32015-04-29 10:01:22 -0700143 }
144 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
145}
joshualitt3e708c52015-04-30 13:49:27 -0700146
147const SkRRect& TestRRectSimple(SkRandom* random) {
joshualitt6c891102015-05-13 08:51:49 -0700148 static SkRRect gRRect[2];
joshualitt3e708c52015-04-30 13:49:27 -0700149 static bool gOnce;
150 if (!gOnce) {
151 gOnce = true;
joshualitt3e708c52015-04-30 13:49:27 -0700152 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
joshualitt3e708c52015-04-30 13:49:27 -0700153 // true round rect with circular corners
joshualitt6c891102015-05-13 08:51:49 -0700154 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
joshualitt3e708c52015-04-30 13:49:27 -0700155 // true round rect with elliptical corners
joshualitt6c891102015-05-13 08:51:49 -0700156 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
joshualitt3e708c52015-04-30 13:49:27 -0700157
158 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
159 SkASSERT(gRRect[i].isSimple());
160 }
161 }
162 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
163}
164
joshualitt40ded322015-05-02 07:07:17 -0700165const SkPath& TestPath(SkRandom* random) {
166 static SkPath gPath[7];
167 static bool gOnce;
168 if (!gOnce) {
169 gOnce = true;
170 // line
171 gPath[0].moveTo(0.f, 0.f);
172 gPath[0].lineTo(10.f, 10.f);
173 // quad
174 gPath[1].moveTo(0.f, 0.f);
175 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
176 // conic
177 gPath[2].moveTo(0.f, 0.f);
178 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
179 // cubic
180 gPath[3].moveTo(0.f, 0.f);
181 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
182 // all three
183 gPath[4].moveTo(0.f, 0.f);
184 gPath[4].lineTo(10.f, 10.f);
185 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
186 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
187 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
188 // convex
189 gPath[5].moveTo(0.0f, 0.0f);
190 gPath[5].lineTo(10.0f, 0.0f);
191 gPath[5].lineTo(10.0f, 10.0f);
192 gPath[5].lineTo(0.0f, 10.0f);
193 gPath[5].close();
194 // concave
195 gPath[6].moveTo(0.0f, 0.0f);
196 gPath[6].lineTo(5.0f, 5.0f);
197 gPath[6].lineTo(10.0f, 0.0f);
198 gPath[6].lineTo(10.0f, 10.0f);
199 gPath[6].lineTo(0.0f, 10.0f);
200 gPath[6].close();
201 }
202
203 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
204}
205
joshualitt8e5c1772015-05-11 08:58:52 -0700206const SkPath& TestPathConvex(SkRandom* random) {
207 static SkPath gPath[3];
208 static bool gOnce;
209 if (!gOnce) {
210 gOnce = true;
211 // narrow rect
joshualitt6c891102015-05-13 08:51:49 -0700212 gPath[0].moveTo(-1.5f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700213 gPath[0].lineTo(-1.5f, -50.0f);
214 gPath[0].lineTo( 1.5f, -50.0f);
215 gPath[0].lineTo( 1.5f, 50.0f);
216 gPath[0].lineTo(-1.5f, 50.0f);
217 // degenerate
joshualitt6c891102015-05-13 08:51:49 -0700218 gPath[1].moveTo(-0.025f, -0.025f);
joshualitt8e5c1772015-05-11 08:58:52 -0700219 gPath[1].lineTo(-0.025f, -0.025f);
220 gPath[1].lineTo( 0.025f, -0.025f);
221 gPath[1].lineTo( 0.025f, 0.025f);
222 gPath[1].lineTo(-0.025f, 0.025f);
223 // clipped triangle
joshualitt6c891102015-05-13 08:51:49 -0700224 gPath[2].moveTo(-10.0f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700225 gPath[2].lineTo(-10.0f, -50.0f);
226 gPath[2].lineTo( 10.0f, -50.0f);
227 gPath[2].lineTo( 50.0f, 31.0f);
228 gPath[2].lineTo( 40.0f, 50.0f);
229 gPath[2].lineTo(-40.0f, 50.0f);
230 gPath[2].lineTo(-50.0f, 31.0f);
joshualitt6c891102015-05-13 08:51:49 -0700231
232 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
233 SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
234 }
joshualitt8e5c1772015-05-11 08:58:52 -0700235 }
236
237 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
238}
239
senorblancob4f9d0e2015-08-06 10:28:55 -0700240static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
senorblanco59cd3672015-08-05 13:37:49 -0700241 bool strokeAndFill = random->nextBool();
242 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
senorblancob4f9d0e2015-08-06 10:28:55 -0700243 rec->setStrokeStyle(strokeWidth, strokeAndFill);
joshualitt21279c72015-05-11 07:21:37 -0700244
senorblanco59cd3672015-08-05 13:37:49 -0700245 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
246 SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
247 SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
senorblancob4f9d0e2015-08-06 10:28:55 -0700248 rec->setStrokeParams(cap, join, miterLimit);
249}
250
251SkStrokeRec TestStrokeRec(SkRandom* random) {
252 SkStrokeRec::InitStyle style =
253 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
254 SkStrokeRec rec(style);
255 randomize_stroke_rec(&rec, random);
senorblanco59cd3672015-08-05 13:37:49 -0700256 return rec;
senorblanco29e0d3f2015-08-05 13:18:03 -0700257}
258
bsalomon6663acf2016-05-10 09:14:17 -0700259void TestStyle(SkRandom* random, GrStyle* style) {
260 SkStrokeRec::InitStyle initStyle =
senorblancob4f9d0e2015-08-06 10:28:55 -0700261 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
bsalomon6663acf2016-05-10 09:14:17 -0700262 SkStrokeRec stroke(initStyle);
263 randomize_stroke_rec(&stroke, random);
264 sk_sp<SkPathEffect> pe;
265 if (random->nextBool()) {
266 int cnt = random->nextRangeU(1, 50) * 2;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400267 std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
bsalomon6663acf2016-05-10 09:14:17 -0700268 SkScalar sum = 0;
269 for (int i = 0; i < cnt; i++) {
270 intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
271 SkDoubleToScalar(10.0));
272 sum += intervals[i];
273 }
274 SkScalar phase = random->nextRangeScalar(0, sum);
275 pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
senorblancob4f9d0e2015-08-06 10:28:55 -0700276 }
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500277 *style = GrStyle(stroke, std::move(pe));
senorblancob4f9d0e2015-08-06 10:28:55 -0700278}
279
bsalomon6663acf2016-05-10 09:14:17 -0700280TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
281 fCount = count;
282 fIntervals.reset(count);
283 memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
284 SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
285 &fInitialDashIndex, &fIntervalLength, &fPhase);
286}
287
Mike Reed6d10f8b2018-08-16 13:22:16 -0400288 bool TestDashPathEffect::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
289 const SkRect* cullRect) const {
bsalomon6663acf2016-05-10 09:14:17 -0700290 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
291 fInitialDashLength, fInitialDashIndex, fIntervalLength);
292}
293
Mike Reed6d10f8b2018-08-16 13:22:16 -0400294SkPathEffect::DashType TestDashPathEffect::onAsADash(DashInfo* info) const {
bsalomon6663acf2016-05-10 09:14:17 -0700295 if (info) {
296 if (info->fCount >= fCount && info->fIntervals) {
297 memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
298 }
299 info->fCount = fCount;
300 info->fPhase = fPhase;
301 }
302 return kDash_DashType;
303}
304
Brian Osman0d9dfe92016-10-03 15:24:44 -0400305sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
306 static sk_sp<SkColorSpace> gColorSpaces[3];
307 static bool gOnce;
308 if (!gOnce) {
309 gOnce = true;
310 // No color space (legacy mode)
311 gColorSpaces[0] = nullptr;
Brian Osman10b81422017-12-14 10:27:21 -0500312 // sRGB or color-spin sRGB
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500313 gColorSpaces[1] = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500314 gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osman0d9dfe92016-10-03 15:24:44 -0400315 }
316 return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
317}
318
Brian Osmane2f732f2016-10-03 14:23:50 -0400319sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
Brian Osman653f34d2018-06-14 11:44:02 -0400320 // TODO: Add many more kinds of xforms here
Brian Osmane2f732f2016-10-03 14:23:50 -0400321 static sk_sp<GrColorSpaceXform> gXforms[3];
322 static bool gOnce;
323 if (!gOnce) {
324 gOnce = true;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500325 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500326 sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osmane2f732f2016-10-03 14:23:50 -0400327 // No gamut change
328 gXforms[0] = nullptr;
Mike Kleine03a1762018-08-22 11:52:16 -0400329 gXforms[1] = GrColorSpaceXform::Make(srgb.get(), kPremul_SkAlphaType,
330 spin.get(), kPremul_SkAlphaType);
331 gXforms[2] = GrColorSpaceXform::Make(spin.get(), kPremul_SkAlphaType,
332 srgb.get(), kPremul_SkAlphaType);
Brian Osmane2f732f2016-10-03 14:23:50 -0400333 }
334 return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
335}
bsalomon6663acf2016-05-10 09:14:17 -0700336
Mike Reed3bc266b2018-01-20 22:24:41 +0000337TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d)
338 : fViewMatrixStorage(TestMatrix(d->fRandom))
339 , fColorSpaceInfoStorage(skstd::make_unique<GrColorSpaceInfo>(TestColorSpace(d->fRandom),
340 kRGBA_8888_GrPixelConfig))
Florin Malitac6c5ead2018-04-11 15:33:40 -0400341 , fArgs(d->context(), &fViewMatrixStorage, kNone_SkFilterQuality, fColorSpaceInfoStorage.get())
Mike Reed3bc266b2018-01-20 22:24:41 +0000342{}
Brian Osman9f532a32016-10-19 11:12:09 -0400343
Brian Salomon4cbb6e62017-10-25 15:12:19 -0400344TestAsFPArgs::~TestAsFPArgs() {}
345
bsalomon6663acf2016-05-10 09:14:17 -0700346} // namespace GrTest
joshualitt3f655f32015-04-29 10:01:22 -0700347
348#endif