• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkPathBuilder.h"
12 #include "include/core/SkRRect.h"
13 #include "src/core/SkRectPriv.h"
14 #include "src/gpu/GrColorInfo.h"
15 #include "src/gpu/GrProcessorUnitTest.h"
16 #include "src/gpu/GrStyle.h"
17 #include "src/utils/SkDashPathPriv.h"
18 
19 #if GR_TEST_UTILS
20 
test_matrix(SkRandom * random,bool includeNonPerspective,bool includePerspective)21 static const SkMatrix& test_matrix(SkRandom* random,
22                                    bool includeNonPerspective,
23                                    bool includePerspective) {
24     static SkMatrix gMatrices[5];
25     static const int kPerspectiveCount = 1;
26     static bool gOnce;
27     if (!gOnce) {
28         gOnce = true;
29         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);
35 
36         // Perspective matrices
37         gMatrices[4].setRotate(SkIntToScalar(215));
38         gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
39         gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
40     }
41 
42     uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
43     if (includeNonPerspective && includePerspective) {
44         return gMatrices[random->nextULessThan(count)];
45     } else if (!includeNonPerspective) {
46         return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
47     } else {
48         SkASSERT(includeNonPerspective && !includePerspective);
49         return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
50     }
51 }
52 
53 namespace GrTest {
TestMatrix(SkRandom * random)54 const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
55 
TestMatrixPreservesRightAngles(SkRandom * random)56 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
57     static SkMatrix gMatrices[5];
58     static bool gOnce;
59     if (!gOnce) {
60         gOnce = true;
61         // 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);
74 
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 
TestMatrixRectStaysRect(SkRandom * random)82 const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
83     static SkMatrix gMatrices[6];
84     static bool gOnce;
85     if (!gOnce) {
86         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 
TestMatrixInvertible(SkRandom * random)108 const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
TestMatrixPerspective(SkRandom * random)109 const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
110 
TestWrapModes(SkRandom * random,GrSamplerState::WrapMode wrapModes[2])111 void 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 }
TestRect(SkRandom * random)120 const SkRect& TestRect(SkRandom* random) {
121     static SkRect gRects[7];
122     static bool gOnce;
123     if (!gOnce) {
124         gOnce = true;
125         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);
128         gRects[3] = SkRectPriv::MakeLargest();
129         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);
131     }
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
TestSquare(SkRandom * random)136 const 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);
143     }
144     return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
145 }
146 
TestRRectSimple(SkRandom * random)147 const SkRRect& TestRRectSimple(SkRandom* random) {
148     static SkRRect gRRect[2];
149     static bool gOnce;
150     if (!gOnce) {
151         gOnce = true;
152         SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
153         // true round rect with circular corners
154         gRRect[0].setRectXY(rectangle, 1.f, 1.f);
155         // true round rect with elliptical corners
156         gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
157 
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 
TestPath(SkRandom * random)165 const SkPath& TestPath(SkRandom* random) {
166     static SkPath gPath[7];
167     static bool gOnce;
168     if (!gOnce) {
169         gOnce = true;
170         // line
171         gPath[0] = SkPathBuilder().moveTo(0.f, 0.f)
172                                   .lineTo(10.f, 10.f)
173                                   .detach();
174         // quad
175         gPath[1] = SkPathBuilder().moveTo(0.f, 0.f)
176                                   .quadTo(10.f, 10.f, 20.f, 20.f)
177                                   .detach();
178         // conic
179         gPath[2] = SkPathBuilder().moveTo(0.f, 0.f)
180                                   .conicTo(10.f, 10.f, 20.f, 20.f, 1.f)
181                                   .detach();
182         // cubic
183         gPath[3] = SkPathBuilder().moveTo(0.f, 0.f)
184                                   .cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f)
185                                   .detach();
186         // all three
187         gPath[4] = SkPathBuilder().moveTo(0.f, 0.f)
188                                   .lineTo(10.f, 10.f)
189                                   .quadTo(10.f, 10.f, 20.f, 20.f)
190                                   .conicTo(10.f, 10.f, 20.f, 20.f, 1.f)
191                                   .cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f)
192                                   .detach();
193         // convex
194         gPath[5] = SkPathBuilder().moveTo(0.0f, 0.0f)
195                                   .lineTo(10.0f, 0.0f)
196                                   .lineTo(10.0f, 10.0f)
197                                   .lineTo(0.0f, 10.0f)
198                                   .close()
199                                   .detach();
200         // concave
201         gPath[6] = SkPathBuilder().moveTo(0.0f, 0.0f)
202                                   .lineTo(5.0f, 5.0f)
203                                   .lineTo(10.0f, 0.0f)
204                                   .lineTo(10.0f, 10.0f)
205                                   .lineTo(0.0f, 10.0f)
206                                   .close()
207                                   .detach();
208     }
209 
210     return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
211 }
212 
TestPathConvex(SkRandom * random)213 const SkPath& TestPathConvex(SkRandom* random) {
214     static SkPath gPath[3];
215     static bool gOnce;
216     if (!gOnce) {
217         gOnce = true;
218         // narrow rect
219         gPath[0] = SkPath::Polygon({{-1.5f, -50.0f},
220                                     {-1.5f, -50.0f},
221                                     { 1.5f, -50.0f},
222                                     { 1.5f,  50.0f},
223                                     {-1.5f,  50.0f}}, false);
224         // degenerate
225         gPath[1] = SkPath::Polygon({{-0.025f, -0.025f},
226                                     {-0.025f, -0.025f},
227                                     { 0.025f, -0.025f},
228                                     { 0.025f,  0.025f},
229                                     {-0.025f,  0.025f}}, false);
230         // clipped triangle
231         gPath[2] = SkPath::Polygon({{-10.0f, -50.0f},
232                                     {-10.0f, -50.0f},
233                                     { 10.0f, -50.0f},
234                                     { 50.0f,  31.0f},
235                                     { 40.0f,  50.0f},
236                                     {-40.0f,  50.0f},
237                                     {-50.0f,  31.0f}}, false);
238 
239         for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
240             SkASSERT(gPath[i].isConvex());
241         }
242     }
243 
244     return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
245 }
246 
randomize_stroke_rec(SkStrokeRec * rec,SkRandom * random)247 static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
248     bool strokeAndFill = random->nextBool();
249     SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
250     rec->setStrokeStyle(strokeWidth, strokeAndFill);
251 
252     SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
253     SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
254     SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
255     rec->setStrokeParams(cap, join, miterLimit);
256 }
257 
TestStrokeRec(SkRandom * random)258 SkStrokeRec TestStrokeRec(SkRandom* random) {
259     SkStrokeRec::InitStyle style =
260             SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
261     SkStrokeRec rec(style);
262     randomize_stroke_rec(&rec, random);
263     return rec;
264 }
265 
TestStyle(SkRandom * random,GrStyle * style)266 void TestStyle(SkRandom* random, GrStyle* style) {
267     SkStrokeRec::InitStyle initStyle =
268             SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
269     SkStrokeRec stroke(initStyle);
270     randomize_stroke_rec(&stroke, random);
271     sk_sp<SkPathEffect> pe;
272     if (random->nextBool()) {
273         int cnt = random->nextRangeU(1, 50) * 2;
274         std::unique_ptr<SkScalar[]> intervals(new SkScalar[cnt]);
275         SkScalar sum = 0;
276         for (int i = 0; i < cnt; i++) {
277             intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
278                                                    SkDoubleToScalar(10.0));
279             sum += intervals[i];
280         }
281         SkScalar phase = random->nextRangeScalar(0, sum);
282         pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
283     }
284     *style = GrStyle(stroke, std::move(pe));
285 }
286 
TestDashPathEffect(const SkScalar * intervals,int count,SkScalar phase)287 TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
288     fCount = count;
289     fIntervals.reset(count);
290     memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
291     SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
292                                    &fInitialDashIndex, &fIntervalLength, &fPhase);
293 }
294 
onFilterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect,const SkMatrix &) const295 bool TestDashPathEffect::onFilterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
296                                       const SkRect* cullRect, const SkMatrix&) const {
297     return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
298                                       fInitialDashLength, fInitialDashIndex, fIntervalLength);
299 }
300 
onAsADash(DashInfo * info) const301 SkPathEffect::DashType TestDashPathEffect::onAsADash(DashInfo* info) const {
302     if (info) {
303         if (info->fCount >= fCount && info->fIntervals) {
304             memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
305         }
306         info->fCount = fCount;
307         info->fPhase = fPhase;
308     }
309     return kDash_DashType;
310 }
311 
TestColorSpace(SkRandom * random)312 sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
313     static sk_sp<SkColorSpace> gColorSpaces[3];
314     static bool gOnce;
315     if (!gOnce) {
316         gOnce = true;
317         // No color space (legacy mode)
318         gColorSpaces[0] = nullptr;
319         // sRGB or color-spin sRGB
320         gColorSpaces[1] = SkColorSpace::MakeSRGB();
321         gColorSpaces[2] = SkColorSpace::MakeSRGB()->makeColorSpin();
322     }
323     return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
324 }
325 
TestColorXform(SkRandom * random)326 sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
327     // TODO: Add many more kinds of xforms here
328     static sk_sp<GrColorSpaceXform> gXforms[3];
329     static bool gOnce;
330     if (!gOnce) {
331         gOnce = true;
332         sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
333         sk_sp<SkColorSpace> spin = SkColorSpace::MakeSRGB()->makeColorSpin();
334         // No gamut change
335         gXforms[0] = nullptr;
336         gXforms[1] = GrColorSpaceXform::Make(srgb.get(), kPremul_SkAlphaType,
337                                              spin.get(), kPremul_SkAlphaType);
338         gXforms[2] = GrColorSpaceXform::Make(spin.get(), kPremul_SkAlphaType,
339                                              srgb.get(), kPremul_SkAlphaType);
340     }
341     return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
342 }
343 
TestAsFPArgs(GrProcessorTestData * d)344 TestAsFPArgs::TestAsFPArgs(GrProcessorTestData* d)
345         : fMatrixProvider(TestMatrix(d->fRandom))
346         , fColorInfoStorage(std::make_unique<GrColorInfo>(
347                   GrColorType::kRGBA_8888, kPremul_SkAlphaType, TestColorSpace(d->fRandom)))
348         , fArgs(d->context(),
349                 fMatrixProvider,
350                 fColorInfoStorage.get()) {}
351 
~TestAsFPArgs()352 TestAsFPArgs::~TestAsFPArgs() {}
353 
RandomColor(SkRandom * random)354 GrColor RandomColor(SkRandom* random) {
355     // There are only a few cases of random colors which interest us
356     enum ColorMode {
357         kAllOnes_ColorMode,
358         kAllZeros_ColorMode,
359         kAlphaOne_ColorMode,
360         kRandom_ColorMode,
361         kLast_ColorMode = kRandom_ColorMode
362     };
363 
364     ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
365     GrColor color SK_INIT_TO_AVOID_WARNING;
366     switch (colorMode) {
367         case kAllOnes_ColorMode:
368             color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
369             break;
370         case kAllZeros_ColorMode:
371             color = GrColorPackRGBA(0, 0, 0, 0);
372             break;
373         case kAlphaOne_ColorMode:
374             color = GrColorPackRGBA(random->nextULessThan(256),
375                                     random->nextULessThan(256),
376                                     random->nextULessThan(256),
377                                     0xFF);
378             break;
379         case kRandom_ColorMode: {
380                 uint8_t alpha = random->nextULessThan(256);
381                 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
382                                         random->nextRangeU(0, alpha),
383                                         random->nextRangeU(0, alpha),
384                                         alpha);
385             break;
386         }
387     }
388     return color;
389 }
390 
RandomCoverage(SkRandom * random)391 uint8_t RandomCoverage(SkRandom* random) {
392     enum CoverageMode {
393         kZero_CoverageMode,
394         kAllOnes_CoverageMode,
395         kRandom_CoverageMode,
396         kLast_CoverageMode = kRandom_CoverageMode
397     };
398 
399     CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
400     uint8_t coverage SK_INIT_TO_AVOID_WARNING;
401     switch (colorMode) {
402         case kZero_CoverageMode:
403             coverage = 0;
404             break;
405         case kAllOnes_CoverageMode:
406             coverage = 0xff;
407             break;
408         case kRandom_CoverageMode:
409             coverage = random->nextULessThan(256);
410             break;
411     }
412     return coverage;
413 }
414 
415 }  // namespace GrTest
416 
417 #endif
418