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 "SkMatrix.h"
10 #include "SkPath.h"
11 #include "SkRRect.h"
12
13 #ifdef GR_TEST_UTILS
14
test_matrix(SkRandom * random,bool includePerspective)15 static const SkMatrix& test_matrix(SkRandom* random, bool includePerspective) {
16 static SkMatrix gMatrices[5];
17 static const int kPerspectiveCount = 1;
18 static bool gOnce;
19 if (!gOnce) {
20 gOnce = true;
21 gMatrices[0].reset();
22 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
23 gMatrices[2].setRotate(SkIntToScalar(17));
24 gMatrices[3].setRotate(SkIntToScalar(185));
25 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
26 gMatrices[3].postScale(SkIntToScalar(2), SK_ScalarHalf);
27
28 // Perspective matrices
29 gMatrices[4].setRotate(SkIntToScalar(215));
30 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
31 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
32 }
33
34 uint32_t count = static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices));
35 if (includePerspective) {
36 return gMatrices[random->nextULessThan(count)];
37 } else {
38 return gMatrices[random->nextULessThan(count - kPerspectiveCount)];
39 }
40 }
41
42 namespace GrTest {
TestMatrix(SkRandom * random)43 const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true); }
44
TestMatrixPreservesRightAngles(SkRandom * random)45 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
46 static SkMatrix gMatrices[5];
47 static bool gOnce;
48 if (!gOnce) {
49 gOnce = true;
50 // identity
51 gMatrices[0].reset();
52 // translation
53 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
54 // scale
55 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
56 // scale + translation
57 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
58 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
59 // orthogonal basis vectors
60 gMatrices[4].reset();
61 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
62 gMatrices[4].setRotate(47);
63
64 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
65 SkASSERT(gMatrices[i].preservesRightAngles());
66 }
67 }
68 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
69 }
70
TestMatrixRectStaysRect(SkRandom * random)71 const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
72 static SkMatrix gMatrices[6];
73 static bool gOnce;
74 if (!gOnce) {
75 gOnce = true;
76 // identity
77 gMatrices[0].reset();
78 // translation
79 gMatrices[1].setTranslate(SkIntToScalar(-100), SkIntToScalar(100));
80 // scale
81 gMatrices[2].setScale(SkIntToScalar(17), SkIntToScalar(17));
82 // scale + translation
83 gMatrices[3].setScale(SkIntToScalar(-17), SkIntToScalar(-17));
84 gMatrices[3].postTranslate(SkIntToScalar(66), SkIntToScalar(-33));
85 // reflection
86 gMatrices[4].setScale(SkIntToScalar(-1), SkIntToScalar(-1));
87 // 90 degress rotation
88 gMatrices[5].setRotate(90);
89
90 for (size_t i = 0; i < SK_ARRAY_COUNT(gMatrices); i++) {
91 SkASSERT(gMatrices[i].rectStaysRect());
92 }
93 }
94 return gMatrices[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gMatrices)))];
95 }
96
TestMatrixInvertible(SkRandom * random)97 const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, false); }
98
TestRect(SkRandom * random)99 const SkRect& TestRect(SkRandom* random) {
100 static SkRect gRects[7];
101 static bool gOnce;
102 if (!gOnce) {
103 gOnce = true;
104 gRects[0] = SkRect::MakeWH(1.f, 1.f);
105 gRects[1] = SkRect::MakeWH(1.0f, 256.0f);
106 gRects[2] = SkRect::MakeWH(256.0f, 1.0f);
107 gRects[4] = SkRect::MakeLargest();
108 gRects[5] = SkRect::MakeLTRB(-65535.0f, -65535.0f, 65535.0f, 65535.0f);
109 gRects[6] = SkRect::MakeLTRB(-10.0f, -10.0f, 10.0f, 10.0f);
110 }
111 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
112 }
113
114 // Just some simple rects for code which expects its input very sanitized
TestSquare(SkRandom * random)115 const SkRect& TestSquare(SkRandom* random) {
116 static SkRect gRects[2];
117 static bool gOnce;
118 if (!gOnce) {
119 gOnce = true;
120 gRects[0] = SkRect::MakeWH(128.f, 128.f);
121 gRects[1] = SkRect::MakeWH(256.0f, 256.0f);
122 }
123 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
124 }
125
TestRRectSimple(SkRandom * random)126 const SkRRect& TestRRectSimple(SkRandom* random) {
127 static SkRRect gRRect[2];
128 static bool gOnce;
129 if (!gOnce) {
130 gOnce = true;
131 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
132 // true round rect with circular corners
133 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
134 // true round rect with elliptical corners
135 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
136
137 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
138 SkASSERT(gRRect[i].isSimple());
139 }
140 }
141 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
142 }
143
TestPath(SkRandom * random)144 const SkPath& TestPath(SkRandom* random) {
145 static SkPath gPath[7];
146 static bool gOnce;
147 if (!gOnce) {
148 gOnce = true;
149 // line
150 gPath[0].moveTo(0.f, 0.f);
151 gPath[0].lineTo(10.f, 10.f);
152 // quad
153 gPath[1].moveTo(0.f, 0.f);
154 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
155 // conic
156 gPath[2].moveTo(0.f, 0.f);
157 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
158 // cubic
159 gPath[3].moveTo(0.f, 0.f);
160 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
161 // all three
162 gPath[4].moveTo(0.f, 0.f);
163 gPath[4].lineTo(10.f, 10.f);
164 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
165 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
166 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
167 // convex
168 gPath[5].moveTo(0.0f, 0.0f);
169 gPath[5].lineTo(10.0f, 0.0f);
170 gPath[5].lineTo(10.0f, 10.0f);
171 gPath[5].lineTo(0.0f, 10.0f);
172 gPath[5].close();
173 // concave
174 gPath[6].moveTo(0.0f, 0.0f);
175 gPath[6].lineTo(5.0f, 5.0f);
176 gPath[6].lineTo(10.0f, 0.0f);
177 gPath[6].lineTo(10.0f, 10.0f);
178 gPath[6].lineTo(0.0f, 10.0f);
179 gPath[6].close();
180 }
181
182 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
183 }
184
TestPathConvex(SkRandom * random)185 const SkPath& TestPathConvex(SkRandom* random) {
186 static SkPath gPath[3];
187 static bool gOnce;
188 if (!gOnce) {
189 gOnce = true;
190 // narrow rect
191 gPath[0].moveTo(-1.5f, -50.0f);
192 gPath[0].lineTo(-1.5f, -50.0f);
193 gPath[0].lineTo( 1.5f, -50.0f);
194 gPath[0].lineTo( 1.5f, 50.0f);
195 gPath[0].lineTo(-1.5f, 50.0f);
196 // degenerate
197 gPath[1].moveTo(-0.025f, -0.025f);
198 gPath[1].lineTo(-0.025f, -0.025f);
199 gPath[1].lineTo( 0.025f, -0.025f);
200 gPath[1].lineTo( 0.025f, 0.025f);
201 gPath[1].lineTo(-0.025f, 0.025f);
202 // clipped triangle
203 gPath[2].moveTo(-10.0f, -50.0f);
204 gPath[2].lineTo(-10.0f, -50.0f);
205 gPath[2].lineTo( 10.0f, -50.0f);
206 gPath[2].lineTo( 50.0f, 31.0f);
207 gPath[2].lineTo( 40.0f, 50.0f);
208 gPath[2].lineTo(-40.0f, 50.0f);
209 gPath[2].lineTo(-50.0f, 31.0f);
210
211 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
212 SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
213 }
214 }
215
216 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
217 }
218
TestStrokeRec(SkRandom * random)219 SkStrokeRec TestStrokeRec(SkRandom* random) {
220 SkStrokeRec::InitStyle style =
221 SkStrokeRec::InitStyle(random->nextULessThan(SkStrokeRec::kFill_InitStyle + 1));
222 SkStrokeRec rec(style);
223 bool strokeAndFill = random->nextBool();
224 SkScalar strokeWidth = random->nextBool() ? 0.f : 1.f;
225 rec.setStrokeStyle(strokeWidth, strokeAndFill);
226
227 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
228 SkPaint::Join join = SkPaint::Join(random->nextULessThan(SkPaint::kJoinCount));
229 SkScalar miterLimit = random->nextRangeScalar(1.f, 5.f);
230 rec.setStrokeParams(cap, join, miterLimit);
231 return rec;
232 }
233
234 };
235
236 #endif
237