joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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" |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 11 | #include "SkRRect.h" |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 12 | |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 13 | #ifdef GR_TEST_UTILS |
| 14 | |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 15 | static const SkMatrix& test_matrix(SkRandom* random, bool includePerspective) { |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 16 | static SkMatrix gMatrices[5]; |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 17 | static const int kPerspectiveCount = 1; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 18 | static bool gOnce; |
| 19 | if (!gOnce) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 20 | gOnce = true; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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); |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 27 | |
| 28 | // Perspective matrices |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 29 | gMatrices[4].setRotate(SkIntToScalar(215)); |
| 30 | gMatrices[4].set(SkMatrix::kMPersp0, 0.00013f); |
| 31 | gMatrices[4].set(SkMatrix::kMPersp1, -0.000039f); |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 32 | } |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 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 | } |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 40 | } |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 41 | |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 42 | namespace GrTest { |
| 43 | const SkMatrix& TestMatrix(SkRandom* random) { return test_matrix(random, true); } |
| 44 | |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 45 | const SkMatrix& TestMatrixPreservesRightAngles(SkRandom* random) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 46 | static SkMatrix gMatrices[5]; |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 47 | static bool gOnce; |
| 48 | if (!gOnce) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 49 | gOnce = true; |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 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); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 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 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 71 | const SkMatrix& TestMatrixRectStaysRect(SkRandom* random) { |
| 72 | static SkMatrix gMatrices[6]; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 73 | static bool gOnce; |
| 74 | if (!gOnce) { |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 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 | |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame^] | 97 | const SkMatrix& TestMatrixInvertible(SkRandom* random) { return test_matrix(random, false); } |
| 98 | |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 99 | const SkRect& TestRect(SkRandom* random) { |
| 100 | static SkRect gRects[7]; |
| 101 | static bool gOnce; |
| 102 | if (!gOnce) { |
| 103 | gOnce = true; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 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); |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 110 | } |
| 111 | return gRects[random->nextULessThan(static_cast<uint32_t>(SK_ARRAY_COUNT(gRects)))]; |
| 112 | } |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 113 | |
| 114 | const 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 | |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 137 | const 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 | |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 178 | }; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 179 | |
| 180 | #endif |