blob: 0c15e6b839fb5186e3992a319dd19b802d8a6e12 [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"
14#include "src/core/SkMakeUnique.h"
15#include "src/core/SkRectPriv.h"
16#include "src/gpu/GrColorSpaceInfo.h"
17#include "src/gpu/GrProcessorUnitTest.h"
18#include "src/gpu/GrStyle.h"
19#include "src/utils/SkDashPathPriv.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070020
Hal Canary6f6961e2017-01-31 13:50:44 -050021#if GR_TEST_UTILS
joshualitt3f655f32015-04-29 10:01:22 -070022
robertphillips01a19502016-07-06 09:58:57 -070023static const SkMatrix& test_matrix(SkRandom* random,
24 bool includeNonPerspective,
25 bool includePerspective) {
joshualitt4eaf9ce2015-04-28 13:31:18 -070026 static SkMatrix gMatrices[5];
joshualitt2fbd4062015-05-07 13:06:41 -070027 static const int kPerspectiveCount = 1;
joshualitt4eaf9ce2015-04-28 13:31:18 -070028 static bool gOnce;
29 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070030 gOnce = true;
joshualitt4eaf9ce2015-04-28 13:31:18 -070031 gMatrices[0].reset();
32 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
33 gMatrices[2].setRotate(SkIntToScalar(17));
34 gMatrices[3].setRotate(SkIntToScalar(185));
35 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
36 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
joshualitt2fbd4062015-05-07 13:06:41 -070037
38 // Perspective matrices
joshualitt4eaf9ce2015-04-28 13:31:18 -070039 gMatrices[4].setRotate(SkIntToScalar(215));
40 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
41 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
joshualitt4eaf9ce2015-04-28 13:31:18 -070042 }
joshualitt2fbd4062015-05-07 13:06:41 -070043
44 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
robertphillips01a19502016-07-06 09:58:57 -070045 if (includeNonPerspective && includePerspective) {
joshualitt2fbd4062015-05-07 13:06:41 -070046 return gMatrices[random->nextULessThan(count)];
robertphillips01a19502016-07-06 09:58:57 -070047 } else if (!includeNonPerspective) {
48 return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
joshualitt2fbd4062015-05-07 13:06:41 -070049 } else {
robertphillips01a19502016-07-06 09:58:57 -070050 SkASSERT(includeNonPerspective && !includePerspective);
joshualitt2fbd4062015-05-07 13:06:41 -070051 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
52 }
joshualitt4eaf9ce2015-04-28 13:31:18 -070053}
joshualitt3f655f32015-04-29 10:01:22 -070054
joshualitt2fbd4062015-05-07 13:06:41 -070055namespace GrTest {
robertphillips01a19502016-07-06 09:58:57 -070056const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
joshualitt2fbd4062015-05-07 13:06:41 -070057
joshualittfa2008f2015-04-29 11:32:05 -070058const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
joshualitt3e708c52015-04-30 13:49:27 -070059 static SkMatrix gMatrices[5];
joshualittfa2008f2015-04-29 11:32:05 -070060 static bool gOnce;
61 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070062 gOnce = true;
joshualittfa2008f2015-04-29 11:32:05 -070063 // identity
64 gMatrices[0].reset();
65 // translation
66 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
67 // scale
68 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
69 // scale + translation
70 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
71 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
72 // orthogonal basis vectors
73 gMatrices[4].reset();
74 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
75 gMatrices[4].setRotate(47);
joshualittfa2008f2015-04-29 11:32:05 -070076
77 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
78 SkASSERT(gMatrices[i].preservesRightAngles());
79 }
80 }
81 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
82}
83
joshualitt3e708c52015-04-30 13:49:27 -070084const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
85 static SkMatrix gMatrices[6];
joshualitt3f655f32015-04-29 10:01:22 -070086 static bool gOnce;
87 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070088 gOnce = true;
89 // identity
90 gMatrices[0].reset();
91 // translation
92 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
93 // scale
94 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
95 // scale + translation
96 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
97 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
98 // reflection
99 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
100 // 90 degress rotation
101 gMatrices[5].setRotate(90);
102
103 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
104 SkASSERT(gMatrices[i].rectStaysRect());
105 }
106 }
107 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
108}
109
robertphillips01a19502016-07-06 09:58:57 -0700110const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
111const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
joshualitt2fbd4062015-05-07 13:06:41 -0700112
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400113void TestWrapModes(SkRandom* random, GrSamplerState::WrapMode wrapModes[2]) {
114 static const GrSamplerState::WrapMode kWrapModes[] = {
115 GrSamplerState::WrapMode::kClamp,
116 GrSamplerState::WrapMode::kRepeat,
117 GrSamplerState::WrapMode::kMirrorRepeat,
118 };
119 wrapModes[0] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
120 wrapModes[1] = kWrapModes[random->nextULessThan(SK_ARRAY_COUNT(kWrapModes))];
121}
joshualitt3e708c52015-04-30 13:49:27 -0700122const SkRect& TestRect(SkRandom* random) {
123 static SkRect gRects[7];
124 static bool gOnce;
125 if (!gOnce) {
126 gOnce = true;
joshualitt3f655f32015-04-29 10:01:22 -0700127 gRects[0] = SkRect::MakeWH(1.f, 1.f);
128 gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
129 gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
Mike Reed274218e2018-01-08 15:05:02 -0500130 gRects[3] = SkRectPriv::MakeLargest();
joshualitt11edad92015-09-22 10:32:28 -0700131 gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
132 gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
joshualitt6c891102015-05-13 08:51:49 -0700133 }
134 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
135}
136
137// Just some simple rects for code which expects its input very sanitized
138const SkRect& TestSquare(SkRandom* random) {
139 static SkRect gRects[2];
140 static bool gOnce;
141 if (!gOnce) {
142 gOnce = true;
143 gRects[0] = SkRect::MakeWH(128.f, 128.f);
144 gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
joshualitt3f655f32015-04-29 10:01:22 -0700145 }
146 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
147}
joshualitt3e708c52015-04-30 13:49:27 -0700148
149const SkRRect& TestRRectSimple(SkRandom* random) {
joshualitt6c891102015-05-13 08:51:49 -0700150 static SkRRect gRRect[2];
joshualitt3e708c52015-04-30 13:49:27 -0700151 static bool gOnce;
152 if (!gOnce) {
153 gOnce = true;
joshualitt3e708c52015-04-30 13:49:27 -0700154 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
joshualitt3e708c52015-04-30 13:49:27 -0700155 // true round rect with circular corners
joshualitt6c891102015-05-13 08:51:49 -0700156 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
joshualitt3e708c52015-04-30 13:49:27 -0700157 // true round rect with elliptical corners
joshualitt6c891102015-05-13 08:51:49 -0700158 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
joshualitt3e708c52015-04-30 13:49:27 -0700159
160 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
161 SkASSERT(gRRect[i].isSimple());
162 }
163 }
164 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
165}
166
joshualitt40ded322015-05-02 07:07:17 -0700167const SkPath& TestPath(SkRandom* random) {
168 static SkPath gPath[7];
169 static bool gOnce;
170 if (!gOnce) {
171 gOnce = true;
172 // line
173 gPath[0].moveTo(0.f, 0.f);
174 gPath[0].lineTo(10.f, 10.f);
175 // quad
176 gPath[1].moveTo(0.f, 0.f);
177 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
178 // conic
179 gPath[2].moveTo(0.f, 0.f);
180 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
181 // cubic
182 gPath[3].moveTo(0.f, 0.f);
183 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
184 // all three
185 gPath[4].moveTo(0.f, 0.f);
186 gPath[4].lineTo(10.f, 10.f);
187 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
188 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
189 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
190 // convex
191 gPath[5].moveTo(0.0f, 0.0f);
192 gPath[5].lineTo(10.0f, 0.0f);
193 gPath[5].lineTo(10.0f, 10.0f);
194 gPath[5].lineTo(0.0f, 10.0f);
195 gPath[5].close();
196 // concave
197 gPath[6].moveTo(0.0f, 0.0f);
198 gPath[6].lineTo(5.0f, 5.0f);
199 gPath[6].lineTo(10.0f, 0.0f);
200 gPath[6].lineTo(10.0f, 10.0f);
201 gPath[6].lineTo(0.0f, 10.0f);
202 gPath[6].close();
203 }
204
205 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
206}
207
joshualitt8e5c1772015-05-11 08:58:52 -0700208const SkPath& TestPathConvex(SkRandom* random) {
209 static SkPath gPath[3];
210 static bool gOnce;
211 if (!gOnce) {
212 gOnce = true;
213 // narrow rect
joshualitt6c891102015-05-13 08:51:49 -0700214 gPath[0].moveTo(-1.5f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700215 gPath[0].lineTo(-1.5f, -50.0f);
216 gPath[0].lineTo( 1.5f, -50.0f);
217 gPath[0].lineTo( 1.5f, 50.0f);
218 gPath[0].lineTo(-1.5f, 50.0f);
219 // degenerate
joshualitt6c891102015-05-13 08:51:49 -0700220 gPath[1].moveTo(-0.025f, -0.025f);
joshualitt8e5c1772015-05-11 08:58:52 -0700221 gPath[1].lineTo(-0.025f, -0.025f);
222 gPath[1].lineTo( 0.025f, -0.025f);
223 gPath[1].lineTo( 0.025f, 0.025f);
224 gPath[1].lineTo(-0.025f, 0.025f);
225 // clipped triangle
joshualitt6c891102015-05-13 08:51:49 -0700226 gPath[2].moveTo(-10.0f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700227 gPath[2].lineTo(-10.0f, -50.0f);
228 gPath[2].lineTo( 10.0f, -50.0f);
229 gPath[2].lineTo( 50.0f, 31.0f);
230 gPath[2].lineTo( 40.0f, 50.0f);
231 gPath[2].lineTo(-40.0f, 50.0f);
232 gPath[2].lineTo(-50.0f, 31.0f);
joshualitt6c891102015-05-13 08:51:49 -0700233
234 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
235 SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
236 }
joshualitt8e5c1772015-05-11 08:58:52 -0700237 }
238
239 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
240}
241
senorblancob4f9d0e2015-08-06 10:28:55 -0700242static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
senorblanco59cd3672015-08-05 13:37:49 -0700243 bool strokeAndFill = random->nextBool();
244 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
senorblancob4f9d0e2015-08-06 10:28:55 -0700245 rec->setStrokeStyle(strokeWidth, strokeAndFill);
joshualitt21279c72015-05-11 07:21:37 -0700246
senorblanco59cd3672015-08-05 13:37:49 -0700247 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
248 SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
249 SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
senorblancob4f9d0e2015-08-06 10:28:55 -0700250 rec->setStrokeParams(cap, join, miterLimit);
251}
252
253SkStrokeRec TestStrokeRec(SkRandom* random) {
254 SkStrokeRec::InitStyle style =
255 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
256 SkStrokeRec rec(style);
257 randomize_stroke_rec(&rec, random);
senorblanco59cd3672015-08-05 13:37:49 -0700258 return rec;
senorblanco29e0d3f2015-08-05 13:18:03 -0700259}
260
bsalomon6663acf2016-05-10 09:14:17 -0700261void TestStyle(SkRandom* random, GrStyle* style) {
262 SkStrokeRec::InitStyle initStyle =
senorblancob4f9d0e2015-08-06 10:28:55 -0700263 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
bsalomon6663acf2016-05-10 09:14:17 -0700264 SkStrokeRec stroke(initStyle);
265 randomize_stroke_rec(&stroke, random);
266 sk_sp<SkPathEffect> pe;
267 if (random->nextBool()) {
268 int cnt = random->nextRangeU(1, 50) * 2;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400269 std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
bsalomon6663acf2016-05-10 09:14:17 -0700270 SkScalar sum = 0;
271 for (int i = 0; i < cnt; i++) {
272 intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
273 SkDoubleToScalar(10.0));
274 sum += intervals[i];
275 }
276 SkScalar phase = random->nextRangeScalar(0, sum);
277 pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
senorblancob4f9d0e2015-08-06 10:28:55 -0700278 }
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500279 *style = GrStyle(stroke, std::move(pe));
senorblancob4f9d0e2015-08-06 10:28:55 -0700280}
281
bsalomon6663acf2016-05-10 09:14:17 -0700282TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
283 fCount = count;
284 fIntervals.reset(count);
285 memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
286 SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
287 &fInitialDashIndex, &fIntervalLength, &fPhase);
288}
289
Mike Reed6d10f8b2018-08-16 13:22:16 -0400290 bool TestDashPathEffect::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
291 const SkRect* cullRect) const {
bsalomon6663acf2016-05-10 09:14:17 -0700292 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
293 fInitialDashLength, fInitialDashIndex, fIntervalLength);
294}
295
Mike Reed6d10f8b2018-08-16 13:22:16 -0400296SkPathEffect::DashType TestDashPathEffect::onAsADash(DashInfo* info) const {
bsalomon6663acf2016-05-10 09:14:17 -0700297 if (info) {
298 if (info->fCount >= fCount && info->fIntervals) {
299 memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
300 }
301 info->fCount = fCount;
302 info->fPhase = fPhase;
303 }
304 return kDash_DashType;
305}
306
Brian Osman0d9dfe92016-10-03 15:24:44 -0400307sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
308 static sk_sp<SkColorSpace> gColorSpaces[3];
309 static bool gOnce;
310 if (!gOnce) {
311 gOnce = true;
312 // No color space (legacy mode)
313 gColorSpaces[0] = nullptr;
Brian Osman10b81422017-12-14 10:27:21 -0500314 // sRGB or color-spin sRGB
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500315 gColorSpaces[1] = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500316 gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osman0d9dfe92016-10-03 15:24:44 -0400317 }
318 return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
319}
320
Brian Osmane2f732f2016-10-03 14:23:50 -0400321sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
Brian Osman653f34d2018-06-14 11:44:02 -0400322 // TODO: Add many more kinds of xforms here
Brian Osmane2f732f2016-10-03 14:23:50 -0400323 static sk_sp<GrColorSpaceXform> gXforms[3];
324 static bool gOnce;
325 if (!gOnce) {
326 gOnce = true;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500327 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Brian Osman10b81422017-12-14 10:27:21 -0500328 sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin();
Brian Osmane2f732f2016-10-03 14:23:50 -0400329 // No gamut change
330 gXforms[0] = nullptr;
Mike Kleine03a1762018-08-22 11:52:16 -0400331 gXforms[1] = GrColorSpaceXform::Make(srgb.get(), kPremul_SkAlphaType,
332 spin.get(), kPremul_SkAlphaType);
333 gXforms[2] = GrColorSpaceXform::Make(spin.get(), kPremul_SkAlphaType,
334 srgb.get(), kPremul_SkAlphaType);
Brian Osmane2f732f2016-10-03 14:23:50 -0400335 }
336 return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
337}
bsalomon6663acf2016-05-10 09:14:17 -0700338
Mike Reed3bc266b2018-01-20 22:24:41 +0000339TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d)
Brian Salomone7499c72019-06-24 12:12:36 -0400340 : fViewMatrixStorage(TestMatrix(d->fRandom))
341 , fColorSpaceInfoStorage(skstd::make_unique<GrColorSpaceInfo>(
Brian Salomond6287472019-06-24 15:50:07 -0400342 GrColorType::kRGBA_8888, kPremul_SkAlphaType, TestColorSpace(d->fRandom),
343 kRGBA_8888_GrPixelConfig))
Brian Salomone7499c72019-06-24 12:12:36 -0400344 , fArgs(d->context(), &fViewMatrixStorage, kNone_SkFilterQuality,
345 fColorSpaceInfoStorage.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