1/*
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 "src/gpu/GrTestUtils.h"
9
10#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/SkRectPriv.h"
15#include "src/gpu/GrColorInfo.h"
16#include "src/gpu/GrProcessorUnitTest.h"
17#include "src/gpu/GrStyle.h"
18#include "src/utils/SkDashPathPriv.h"
19
20#if GR_TEST_UTILS
21
22static const SkMatrix& test_matrix(SkRandom* random,
23 bool includeNonPerspective,
24 bool includePerspective) {
25 static SkMatrix gMatrices[5];
26 static const int kPerspectiveCount = 1;
27 static bool gOnce;
28 if (!gOnce) {
29 gOnce = true;
30 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);
36
37 // Perspective matrices
38 gMatrices[4].setRotate(SkIntToScalar(215));
39 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
40 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
41 }
42
43 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
44 if (includeNonPerspective && includePerspective) {
45 return gMatrices[random->nextULessThan(count)];
46 } else if (!includeNonPerspective) {
47 return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
48 } else {
49 SkASSERT(includeNonPerspective && !includePerspective);
50 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
51 }
52}
53
54namespace GrTest {
55const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
56
57const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
58 static SkMatrix gMatrices[5];
59 static bool gOnce;
60 if (!gOnce) {
61 gOnce = true;
62 // 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);
75
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
83const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
84 static SkMatrix gMatrices[6];
85 static bool gOnce;
86 if (!gOnce) {
87 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
109const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
110const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
111
112void 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}
121const SkRect& TestRect(SkRandom* random) {
122 static SkRect gRects[7];
123 static bool gOnce;
124 if (!gOnce) {
125 gOnce = true;
126 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);
129 gRects[3] = SkRectPriv::MakeLargest();
130 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);
132 }
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);
144 }
145 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
146}
147
148const SkRRect& TestRRectSimple(SkRandom* random) {
149 static SkRRect gRRect[2];
150 static bool gOnce;
151 if (!gOnce) {
152 gOnce = true;
153 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
154 // true round rect with circular corners
155 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
156 // true round rect with elliptical corners
157 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
158
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
166const 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
207const SkPath& TestPathConvex(SkRandom* random) {
208 static SkPath gPath[3];
209 static bool gOnce;
210 if (!gOnce) {
211 gOnce = true;
212 // narrow rect
213 gPath[0].moveTo(-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 gPath[0].lineTo(-1.5f, 50.0f);
218 // degenerate
219 gPath[1].moveTo(-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 gPath[1].lineTo(-0.025f, 0.025f);
224 // clipped triangle
225 gPath[2].moveTo(-10.0f, -50.0f);
226 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);
232
233 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
234 SkASSERT(SkPathConvexityType::kConvex == gPath[i].getConvexityType());
235 }
236 }
237
238 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
239}
240
241static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
242 bool strokeAndFill = random->nextBool();
243 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
244 rec->setStrokeStyle(strokeWidth, strokeAndFill);
245
246 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);
249 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);
257 return rec;
258}
259
260void TestStyle(SkRandom* random, GrStyle* style) {
261 SkStrokeRec::InitStyle initStyle =
262 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
263 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;
268 std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
269 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);
277 }
278 *style = GrStyle(stroke, std::move(pe));
279}
280
281TestDashPathEffect::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
289 bool TestDashPathEffect::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
290 const SkRect* cullRect) const {
291 return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
292 fInitialDashLength, fInitialDashIndex, fIntervalLength);
293}
294
295SkPathEffect::DashType TestDashPathEffect::onAsADash(DashInfo* info) const {
296 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
306sk_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;
313 // sRGB or color-spin sRGB
314 gColorSpaces[1] = SkColorSpace::MakeSRGB();
315 gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin();
316 }
317 return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
318}
319
320sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
321 // TODO: Add many more kinds of xforms here
322 static sk_sp<GrColorSpaceXform> gXforms[3];
323 static bool gOnce;
324 if (!gOnce) {
325 gOnce = true;
326 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
327 sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin();
328 // No gamut change
329 gXforms[0] = nullptr;
330 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);
334 }
335 return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
336}
337
338TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d)
339 : fViewMatrixStorage(TestMatrix(d->fRandom))
340 , fColorInfoStorage(std::make_unique<GrColorInfo>(
341 GrColorType::kRGBA_8888, kPremul_SkAlphaType, TestColorSpace(d->fRandom)))
342 , fArgs(d->context(), &fViewMatrixStorage, kNone_SkFilterQuality, fColorInfoStorage.get()) {
343}
344
345TestAsFPArgs::~TestAsFPArgs() {}
346
347} // namespace GrTest
348
349#endif
350