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 "GrTestUtils.h"
9 #include "GrStyle.h"
10 #include "SkColorSpace.h"
11 #include "SkDashPathPriv.h"
12 #include "SkMatrix.h"
13 #include "SkPath.h"
14 #include "SkRRect.h"
15 
16 #ifdef GR_TEST_UTILS
17 
test_matrix(SkRandom * random,bool includeNonPerspective,bool includePerspective)18 static const SkMatrix& test_matrix(SkRandom* random,
19                                    bool includeNonPerspective,
20                                    bool includePerspective) {
21     static SkMatrix gMatrices[5];
22     static const int kPerspectiveCount = 1;
23     static bool gOnce;
24     if (!gOnce) {
25         gOnce = true;
26         gMatrices[0].reset();
27         gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
28         gMatrices[2].setRotate(SkIntToScalar(17));
29         gMatrices[3].setRotate(SkIntToScalar(185));
30         gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
31         gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
32 
33         // Perspective matrices
34         gMatrices[4].setRotate(SkIntToScalar(215));
35         gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
36         gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
37     }
38 
39     uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
40     if (includeNonPerspective && includePerspective) {
41         return gMatrices[random->nextULessThan(count)];
42     } else if (!includeNonPerspective) {
43         return gMatrices[count - 1 - random->nextULessThan(kPerspectiveCount)];
44     } else {
45         SkASSERT(includeNonPerspective && !includePerspective);
46         return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
47     }
48 }
49 
50 namespace GrTest {
TestMatrix(SkRandom * random)51 const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true, true); }
52 
TestMatrixPreservesRightAngles(SkRandom * random)53 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
54     static SkMatrix gMatrices[5];
55     static bool gOnce;
56     if (!gOnce) {
57         gOnce = true;
58         // identity
59         gMatrices[0].reset();
60         // translation
61         gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
62         // scale
63         gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
64         // scale + translation
65         gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
66         gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
67         // orthogonal basis vectors
68         gMatrices[4].reset();
69         gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
70         gMatrices[4].setRotate(47);
71 
72         for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
73             SkASSERT(gMatrices[i].preservesRightAngles());
74         }
75     }
76     return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
77 }
78 
TestMatrixRectStaysRect(SkRandom * random)79 const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
80     static SkMatrix gMatrices[6];
81     static bool gOnce;
82     if (!gOnce) {
83         gOnce = true;
84         // identity
85         gMatrices[0].reset();
86         // translation
87         gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
88         // scale
89         gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
90         // scale + translation
91         gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
92         gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
93         // reflection
94         gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
95         // 90 degress rotation
96         gMatrices[5].setRotate(90);
97 
98         for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
99             SkASSERT(gMatrices[i].rectStaysRect());
100         }
101     }
102     return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
103 }
104 
TestMatrixInvertible(SkRandom * random)105 const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, true, false); }
TestMatrixPerspective(SkRandom * random)106 const SkMatrix& TestMatrixPerspective(SkRandom* random) { return test_matrix(random, false, true); }
107 
TestRect(SkRandom * random)108 const SkRect& TestRect(SkRandom* random) {
109     static SkRect gRects[7];
110     static bool gOnce;
111     if (!gOnce) {
112         gOnce = true;
113         gRects[0] = SkRect::MakeWH(1.f, 1.f);
114         gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
115         gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
116         gRects[3] = SkRect::MakeLargest();
117         gRects[4] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
118         gRects[5] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
119     }
120     return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
121 }
122 
123 // Just some simple rects for code which expects its input very sanitized
TestSquare(SkRandom * random)124 const SkRect& TestSquare(SkRandom* random) {
125     static SkRect gRects[2];
126     static bool gOnce;
127     if (!gOnce) {
128         gOnce = true;
129         gRects[0] = SkRect::MakeWH(128.f, 128.f);
130         gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
131     }
132     return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
133 }
134 
TestRRectSimple(SkRandom * random)135 const SkRRect& TestRRectSimple(SkRandom* random) {
136     static SkRRect gRRect[2];
137     static bool gOnce;
138     if (!gOnce) {
139         gOnce = true;
140         SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
141         // true round rect with circular corners
142         gRRect[0].setRectXY(rectangle, 1.f, 1.f);
143         // true round rect with elliptical corners
144         gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
145 
146         for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
147             SkASSERT(gRRect[i].isSimple());
148         }
149     }
150     return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
151 }
152 
TestPath(SkRandom * random)153 const SkPath& TestPath(SkRandom* random) {
154     static SkPath gPath[7];
155     static bool gOnce;
156     if (!gOnce) {
157         gOnce = true;
158         // line
159         gPath[0].moveTo(0.f, 0.f);
160         gPath[0].lineTo(10.f, 10.f);
161         // quad
162         gPath[1].moveTo(0.f, 0.f);
163         gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
164         // conic
165         gPath[2].moveTo(0.f, 0.f);
166         gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
167         // cubic
168         gPath[3].moveTo(0.f, 0.f);
169         gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
170         // all three
171         gPath[4].moveTo(0.f, 0.f);
172         gPath[4].lineTo(10.f, 10.f);
173         gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
174         gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
175         gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
176         // convex
177         gPath[5].moveTo(0.0f, 0.0f);
178         gPath[5].lineTo(10.0f, 0.0f);
179         gPath[5].lineTo(10.0f, 10.0f);
180         gPath[5].lineTo(0.0f, 10.0f);
181         gPath[5].close();
182         // concave
183         gPath[6].moveTo(0.0f, 0.0f);
184         gPath[6].lineTo(5.0f, 5.0f);
185         gPath[6].lineTo(10.0f, 0.0f);
186         gPath[6].lineTo(10.0f, 10.0f);
187         gPath[6].lineTo(0.0f, 10.0f);
188         gPath[6].close();
189     }
190 
191     return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
192 }
193 
TestPathConvex(SkRandom * random)194 const SkPath& TestPathConvex(SkRandom* random) {
195     static SkPath gPath[3];
196     static bool gOnce;
197     if (!gOnce) {
198         gOnce = true;
199         // narrow rect
200         gPath[0].moveTo(-1.5f, -50.0f);
201         gPath[0].lineTo(-1.5f, -50.0f);
202         gPath[0].lineTo( 1.5f, -50.0f);
203         gPath[0].lineTo( 1.5f,  50.0f);
204         gPath[0].lineTo(-1.5f,  50.0f);
205         // degenerate
206         gPath[1].moveTo(-0.025f, -0.025f);
207         gPath[1].lineTo(-0.025f, -0.025f);
208         gPath[1].lineTo( 0.025f, -0.025f);
209         gPath[1].lineTo( 0.025f,  0.025f);
210         gPath[1].lineTo(-0.025f,  0.025f);
211         // clipped triangle
212         gPath[2].moveTo(-10.0f, -50.0f);
213         gPath[2].lineTo(-10.0f, -50.0f);
214         gPath[2].lineTo( 10.0f, -50.0f);
215         gPath[2].lineTo( 50.0f,  31.0f);
216         gPath[2].lineTo( 40.0f,  50.0f);
217         gPath[2].lineTo(-40.0f,  50.0f);
218         gPath[2].lineTo(-50.0f,  31.0f);
219 
220         for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
221             SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
222         }
223     }
224 
225     return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
226 }
227 
randomize_stroke_rec(SkStrokeRec * rec,SkRandom * random)228 static void randomize_stroke_rec(SkStrokeRec* rec, SkRandom* random) {
229     bool strokeAndFill = random->nextBool();
230     SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
231     rec->setStrokeStyle(strokeWidth, strokeAndFill);
232 
233     SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
234     SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
235     SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
236     rec->setStrokeParams(cap, join, miterLimit);
237 }
238 
TestStrokeRec(SkRandom * random)239 SkStrokeRec TestStrokeRec(SkRandom* random) {
240     SkStrokeRec::InitStyle style =
241             SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
242     SkStrokeRec rec(style);
243     randomize_stroke_rec(&rec, random);
244     return rec;
245 }
246 
TestStyle(SkRandom * random,GrStyle * style)247 void TestStyle(SkRandom* random, GrStyle* style) {
248     SkStrokeRec::InitStyle initStyle =
249             SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
250     SkStrokeRec stroke(initStyle);
251     randomize_stroke_rec(&stroke, random);
252     sk_sp<SkPathEffect> pe;
253     if (random->nextBool()) {
254         int cnt = random->nextRangeU(1, 50) * 2;
255         SkAutoTDeleteArray<SkScalar> intervals(new SkScalar[cnt]);
256         SkScalar sum = 0;
257         for (int i = 0; i < cnt; i++) {
258             intervals[i] = random->nextRangeScalar(SkDoubleToScalar(0.01),
259                                                    SkDoubleToScalar(10.0));
260             sum += intervals[i];
261         }
262         SkScalar phase = random->nextRangeScalar(0, sum);
263         pe = TestDashPathEffect::Make(intervals.get(), cnt, phase);
264     }
265     *style = GrStyle(stroke, pe.get());
266 }
267 
TestDashPathEffect(const SkScalar * intervals,int count,SkScalar phase)268 TestDashPathEffect::TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase) {
269     fCount = count;
270     fIntervals.reset(count);
271     memcpy(fIntervals.get(), intervals, count * sizeof(SkScalar));
272     SkDashPath::CalcDashParameters(phase, intervals, count, &fInitialDashLength,
273                                    &fInitialDashIndex, &fIntervalLength, &fPhase);
274 }
275 
filterPath(SkPath * dst,const SkPath & src,SkStrokeRec * rec,const SkRect * cullRect) const276     bool TestDashPathEffect::filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* rec,
277                                      const SkRect* cullRect) const {
278     return SkDashPath::InternalFilter(dst, src, rec, cullRect, fIntervals.get(), fCount,
279                                       fInitialDashLength, fInitialDashIndex, fIntervalLength);
280 }
281 
asADash(DashInfo * info) const282 SkPathEffect::DashType TestDashPathEffect::asADash(DashInfo* info) const {
283     if (info) {
284         if (info->fCount >= fCount && info->fIntervals) {
285             memcpy(info->fIntervals, fIntervals.get(), fCount * sizeof(SkScalar));
286         }
287         info->fCount = fCount;
288         info->fPhase = fPhase;
289     }
290     return kDash_DashType;
291 }
292 
TestColorSpace(SkRandom * random)293 sk_sp<SkColorSpace> TestColorSpace(SkRandom* random) {
294     static sk_sp<SkColorSpace> gColorSpaces[3];
295     static bool gOnce;
296     if (!gOnce) {
297         gOnce = true;
298         // No color space (legacy mode)
299         gColorSpaces[0] = nullptr;
300         // sRGB or Adobe
301         gColorSpaces[1] = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
302         gColorSpaces[2] = SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named);
303     }
304     return gColorSpaces[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gColorSpaces)))];
305 }
306 
TestColorXform(SkRandom * random)307 sk_sp<GrColorSpaceXform> TestColorXform(SkRandom* random) {
308     static sk_sp<GrColorSpaceXform> gXforms[3];
309     static bool gOnce;
310     if (!gOnce) {
311         gOnce = true;
312         sk_sp<SkColorSpace> srgb = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
313         sk_sp<SkColorSpace> adobe = SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named);
314         // No gamut change
315         gXforms[0] = nullptr;
316         // To larger gamut
317         gXforms[1] = GrColorSpaceXform::Make(srgb.get(), adobe.get());
318         // To smaller gamut
319         gXforms[2] = GrColorSpaceXform::Make(adobe.get(), srgb.get());
320     }
321     return gXforms[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gXforms)))];
322 }
323 
324 }  // namespace GrTest
325 
326 #endif
327