blob: b690d7e3cd4bc8f0e33b1dd31b27f0d8ed641196 [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
8#include "GrTestUtils.h"
9#include "SkMatrix.h"
joshualitt40ded322015-05-02 07:07:17 -070010#include "SkPath.h"
joshualitt3e708c52015-04-30 13:49:27 -070011#include "SkRRect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070012
joshualitt3f655f32015-04-29 10:01:22 -070013#ifdef GR_TEST_UTILS
14
joshualitt2fbd4062015-05-07 13:06:41 -070015static const SkMatrix& test_matrix(SkRandom* random, bool includePerspective) {
joshualitt4eaf9ce2015-04-28 13:31:18 -070016 static SkMatrix gMatrices[5];
joshualitt2fbd4062015-05-07 13:06:41 -070017 static const int kPerspectiveCount = 1;
joshualitt4eaf9ce2015-04-28 13:31:18 -070018 static bool gOnce;
19 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070020 gOnce = true;
joshualitt4eaf9ce2015-04-28 13:31:18 -070021 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);
joshualitt2fbd4062015-05-07 13:06:41 -070027
28 // Perspective matrices
joshualitt4eaf9ce2015-04-28 13:31:18 -070029 gMatrices[4].setRotate(SkIntToScalar(215));
30 gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f);
31 gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f);
joshualitt4eaf9ce2015-04-28 13:31:18 -070032 }
joshualitt2fbd4062015-05-07 13:06:41 -070033
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 }
joshualitt4eaf9ce2015-04-28 13:31:18 -070040}
joshualitt3f655f32015-04-29 10:01:22 -070041
joshualitt2fbd4062015-05-07 13:06:41 -070042namespace GrTest {
43const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true); }
44
joshualittfa2008f2015-04-29 11:32:05 -070045const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) {
joshualitt3e708c52015-04-30 13:49:27 -070046 static SkMatrix gMatrices[5];
joshualittfa2008f2015-04-29 11:32:05 -070047 static bool gOnce;
48 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070049 gOnce = true;
joshualittfa2008f2015-04-29 11:32:05 -070050 // 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);
joshualittfa2008f2015-04-29 11:32:05 -070063
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
joshualitt3e708c52015-04-30 13:49:27 -070071const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) {
72 static SkMatrix gMatrices[6];
joshualitt3f655f32015-04-29 10:01:22 -070073 static bool gOnce;
74 if (!gOnce) {
joshualitt3e708c52015-04-30 13:49:27 -070075 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
joshualitt2fbd4062015-05-07 13:06:41 -070097const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, false); }
98
joshualitt3e708c52015-04-30 13:49:27 -070099const SkRect& TestRect(SkRandom* random) {
100 static SkRect gRects[7];
101 static bool gOnce;
102 if (!gOnce) {
103 gOnce = true;
joshualitt3f655f32015-04-29 10:01:22 -0700104 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);
joshualitt6c891102015-05-13 08:51:49 -0700109 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
115const 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);
joshualitt3f655f32015-04-29 10:01:22 -0700122 }
123 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
124}
joshualitt3e708c52015-04-30 13:49:27 -0700125
126const SkRRect& TestRRectSimple(SkRandom* random) {
joshualitt6c891102015-05-13 08:51:49 -0700127 static SkRRect gRRect[2];
joshualitt3e708c52015-04-30 13:49:27 -0700128 static bool gOnce;
129 if (!gOnce) {
130 gOnce = true;
joshualitt3e708c52015-04-30 13:49:27 -0700131 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
joshualitt3e708c52015-04-30 13:49:27 -0700132 // true round rect with circular corners
joshualitt6c891102015-05-13 08:51:49 -0700133 gRRect[0].setRectXY(rectangle, 1.f, 1.f);
joshualitt3e708c52015-04-30 13:49:27 -0700134 // true round rect with elliptical corners
joshualitt6c891102015-05-13 08:51:49 -0700135 gRRect[1].setRectXY(rectangle, 2.0f, 1.0f);
joshualitt3e708c52015-04-30 13:49:27 -0700136
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
joshualitt40ded322015-05-02 07:07:17 -0700144const 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
joshualitt8e5c1772015-05-11 08:58:52 -0700185const SkPath& TestPathConvex(SkRandom* random) {
186 static SkPath gPath[3];
187 static bool gOnce;
188 if (!gOnce) {
189 gOnce = true;
190 // narrow rect
joshualitt6c891102015-05-13 08:51:49 -0700191 gPath[0].moveTo(-1.5f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700192 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
joshualitt6c891102015-05-13 08:51:49 -0700197 gPath[1].moveTo(-0.025f, -0.025f);
joshualitt8e5c1772015-05-11 08:58:52 -0700198 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
joshualitt6c891102015-05-13 08:51:49 -0700203 gPath[2].moveTo(-10.0f, -50.0f);
joshualitt8e5c1772015-05-11 08:58:52 -0700204 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);
joshualitt6c891102015-05-13 08:51:49 -0700210
211 for (size_t i = 0; i < SK_ARRAY_COUNT(gPath); i++) {
212 SkASSERT(SkPath::kConvex_Convexity == gPath[i].getConvexity());
213 }
joshualitt8e5c1772015-05-11 08:58:52 -0700214 }
215
216 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
217}
218
joshualitt21279c72015-05-11 07:21:37 -0700219SkStrokeRec 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
joshualitt4eaf9ce2015-04-28 13:31:18 -0700234};
joshualitt3f655f32015-04-29 10:01:22 -0700235
236#endif