blob: 0bb4044eaaf1299a1ecb99e74c270b4397b7b9d2 [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);
109 gRects[6] = SkRect::MakeLTRB(10.0f, 10.0f, -10.0f, -10.0f);
joshualitt3f655f32015-04-29 10:01:22 -0700110 }
111 return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))];
112}
joshualitt3e708c52015-04-30 13:49:27 -0700113
114const SkRRect& TestRRectSimple(SkRandom* random) {
115 static SkRRect gRRect[4];
116 static bool gOnce;
117 if (!gOnce) {
118 gOnce = true;
119 SkRect square = SkRect::MakeWH(10.f, 10.f);
120 SkRect rectangle = SkRect::MakeWH(10.f, 20.f);
121 // rect
122 gRRect[0].setRect(square);
123 // oval
124 gRRect[1].setOval(rectangle);
125 // true round rect with circular corners
126 gRRect[2].setRectXY(rectangle, 1.f, 1.f);
127 // true round rect with elliptical corners
128 gRRect[3].setRectXY(rectangle, 2.0f, 1.0f);
129
130 for (size_t i = 0; i < SK_ARRAY_COUNT(gRRect); i++) {
131 SkASSERT(gRRect[i].isSimple());
132 }
133 }
134 return gRRect[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRRect)))];
135}
136
joshualitt40ded322015-05-02 07:07:17 -0700137const SkPath& TestPath(SkRandom* random) {
138 static SkPath gPath[7];
139 static bool gOnce;
140 if (!gOnce) {
141 gOnce = true;
142 // line
143 gPath[0].moveTo(0.f, 0.f);
144 gPath[0].lineTo(10.f, 10.f);
145 // quad
146 gPath[1].moveTo(0.f, 0.f);
147 gPath[1].quadTo(10.f, 10.f, 20.f, 20.f);
148 // conic
149 gPath[2].moveTo(0.f, 0.f);
150 gPath[2].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
151 // cubic
152 gPath[3].moveTo(0.f, 0.f);
153 gPath[3].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
154 // all three
155 gPath[4].moveTo(0.f, 0.f);
156 gPath[4].lineTo(10.f, 10.f);
157 gPath[4].quadTo(10.f, 10.f, 20.f, 20.f);
158 gPath[4].conicTo(10.f, 10.f, 20.f, 20.f, 1.f);
159 gPath[4].cubicTo(10.f, 10.f, 20.f, 20.f, 30.f, 30.f);
160 // convex
161 gPath[5].moveTo(0.0f, 0.0f);
162 gPath[5].lineTo(10.0f, 0.0f);
163 gPath[5].lineTo(10.0f, 10.0f);
164 gPath[5].lineTo(0.0f, 10.0f);
165 gPath[5].close();
166 // concave
167 gPath[6].moveTo(0.0f, 0.0f);
168 gPath[6].lineTo(5.0f, 5.0f);
169 gPath[6].lineTo(10.0f, 0.0f);
170 gPath[6].lineTo(10.0f, 10.0f);
171 gPath[6].lineTo(0.0f, 10.0f);
172 gPath[6].close();
173 }
174
175 return gPath[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gPath)))];
176}
177
joshualitt4eaf9ce2015-04-28 13:31:18 -0700178};
joshualitt3f655f32015-04-29 10:01:22 -0700179
180#endif