epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 7 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 12 | #include "SkParse.h" |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 13 | #include "SkParsePath.h" |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 14 | #include "SkPathEffect.h" |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 15 | #include "SkRandom.h" |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 16 | #include "SkReader32.h" |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 17 | #include "SkRRect.h" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 18 | #include "SkSize.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 19 | #include "SkSurface.h" |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 20 | #include "SkTypes.h" |
| 21 | #include "SkWriter32.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 22 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 23 | static void make_path0(SkPath* path) { |
| 24 | // from * https://code.google.com/p/skia/issues/detail?id=1706 |
| 25 | |
| 26 | path->moveTo(146.939f, 1012.84f); |
| 27 | path->lineTo(181.747f, 1009.18f); |
| 28 | path->lineTo(182.165f, 1013.16f); |
| 29 | path->lineTo(147.357f, 1016.82f); |
| 30 | path->lineTo(146.939f, 1012.84f); |
| 31 | path->close(); |
| 32 | } |
| 33 | |
| 34 | static void make_path1(SkPath* path) { |
| 35 | path->addRect(SkRect::MakeXYWH(10, 10, 10, 1)); |
| 36 | } |
| 37 | |
| 38 | typedef void (*PathProc)(SkPath*); |
| 39 | |
| 40 | /* |
| 41 | * Regression test: we used to crash (overwrite internal storage) during |
| 42 | * construction of the region when the path was INVERSE. That is now fixed, |
| 43 | * so test these regions (which used to assert/crash). |
| 44 | * |
| 45 | * https://code.google.com/p/skia/issues/detail?id=1706 |
| 46 | */ |
| 47 | static void test_path_to_region(skiatest::Reporter* reporter) { |
| 48 | PathProc procs[] = { |
| 49 | make_path0, |
| 50 | make_path1, |
| 51 | }; |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 52 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 53 | SkRegion clip; |
| 54 | clip.setRect(0, 0, 1255, 1925); |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 55 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 56 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
| 57 | SkPath path; |
| 58 | procs[i](&path); |
skia.committer@gmail.com | 4726291 | 2013-10-16 07:02:24 +0000 | [diff] [blame] | 59 | |
reed@google.com | cc8be77 | 2013-10-15 15:35:29 +0000 | [diff] [blame] | 60 | SkRegion rgn; |
| 61 | rgn.setPath(path, clip); |
| 62 | path.toggleInverseFillType(); |
| 63 | rgn.setPath(path, clip); |
| 64 | } |
| 65 | } |
| 66 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 67 | #if defined(WIN32) |
| 68 | #define SUPPRESS_VISIBILITY_WARNING |
| 69 | #else |
| 70 | #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden"))) |
| 71 | #endif |
| 72 | |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 73 | static void test_path_close_issue1474(skiatest::Reporter* reporter) { |
| 74 | // This test checks that r{Line,Quad,Conic,Cubic}To following a close() |
| 75 | // are relative to the point we close to, not relative to the point we close from. |
| 76 | SkPath path; |
| 77 | SkPoint last; |
| 78 | |
| 79 | // Test rLineTo(). |
| 80 | path.rLineTo(0, 100); |
| 81 | path.rLineTo(100, 0); |
| 82 | path.close(); // Returns us back to 0,0. |
| 83 | path.rLineTo(50, 50); // This should go to 50,50. |
| 84 | |
| 85 | path.getLastPt(&last); |
| 86 | REPORTER_ASSERT(reporter, 50 == last.fX); |
| 87 | REPORTER_ASSERT(reporter, 50 == last.fY); |
| 88 | |
| 89 | // Test rQuadTo(). |
| 90 | path.rewind(); |
| 91 | path.rLineTo(0, 100); |
| 92 | path.rLineTo(100, 0); |
| 93 | path.close(); |
| 94 | path.rQuadTo(50, 50, 75, 75); |
| 95 | |
| 96 | path.getLastPt(&last); |
| 97 | REPORTER_ASSERT(reporter, 75 == last.fX); |
| 98 | REPORTER_ASSERT(reporter, 75 == last.fY); |
| 99 | |
| 100 | // Test rConicTo(). |
| 101 | path.rewind(); |
| 102 | path.rLineTo(0, 100); |
| 103 | path.rLineTo(100, 0); |
| 104 | path.close(); |
| 105 | path.rConicTo(50, 50, 85, 85, 2); |
| 106 | |
| 107 | path.getLastPt(&last); |
| 108 | REPORTER_ASSERT(reporter, 85 == last.fX); |
| 109 | REPORTER_ASSERT(reporter, 85 == last.fY); |
| 110 | |
| 111 | // Test rCubicTo(). |
| 112 | path.rewind(); |
| 113 | path.rLineTo(0, 100); |
| 114 | path.rLineTo(100, 0); |
| 115 | path.close(); |
| 116 | path.rCubicTo(50, 50, 85, 85, 95, 95); |
| 117 | |
| 118 | path.getLastPt(&last); |
| 119 | REPORTER_ASSERT(reporter, 95 == last.fX); |
| 120 | REPORTER_ASSERT(reporter, 95 == last.fY); |
| 121 | } |
| 122 | |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 123 | static void test_android_specific_behavior(skiatest::Reporter* reporter) { |
| 124 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 125 | // Make sure we treat fGenerationID and fSourcePath correctly for each of |
| 126 | // copy, assign, rewind, reset, and swap. |
| 127 | SkPath original, source, anotherSource; |
| 128 | original.setSourcePath(&source); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 129 | original.moveTo(0, 0); |
| 130 | original.lineTo(1, 1); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 131 | REPORTER_ASSERT(reporter, original.getSourcePath() == &source); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 132 | |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 133 | uint32_t copyID, assignID; |
| 134 | |
| 135 | // Test copy constructor. Copy generation ID, copy source path. |
| 136 | SkPath copy(original); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 137 | REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID()); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 138 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 139 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 140 | // Test assigment operator. Change generation ID, copy source path. |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 141 | SkPath assign; |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 142 | assignID = assign.getGenerationID(); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 143 | assign = original; |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 144 | REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 145 | REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath()); |
| 146 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 147 | // Test rewind. Change generation ID, don't touch source path. |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 148 | copyID = copy.getGenerationID(); |
| 149 | copy.rewind(); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
| 152 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 153 | // Test reset. Change generation ID, don't touch source path. |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 154 | assignID = assign.getGenerationID(); |
| 155 | assign.reset(); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath()); |
| 158 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 159 | // Test swap. Swap the generation IDs, swap source paths. |
| 160 | copy.reset(); |
| 161 | copy.moveTo(2, 2); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 162 | copy.setSourcePath(&anotherSource); |
| 163 | copyID = copy.getGenerationID(); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 164 | assign.moveTo(3, 3); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 165 | assignID = assign.getGenerationID(); |
| 166 | copy.swap(assign); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 167 | REPORTER_ASSERT(reporter, copy.getGenerationID() != copyID); |
| 168 | REPORTER_ASSERT(reporter, assign.getGenerationID() != assignID); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
| 170 | REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 171 | #endif |
| 172 | } |
| 173 | |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 174 | static void test_gen_id(skiatest::Reporter* reporter) { |
| 175 | SkPath a, b; |
| 176 | REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); |
| 177 | |
| 178 | a.moveTo(0, 0); |
| 179 | const uint32_t z = a.getGenerationID(); |
| 180 | REPORTER_ASSERT(reporter, z != b.getGenerationID()); |
| 181 | |
| 182 | a.reset(); |
| 183 | REPORTER_ASSERT(reporter, a.getGenerationID() == b.getGenerationID()); |
| 184 | |
| 185 | a.moveTo(1, 1); |
| 186 | const uint32_t y = a.getGenerationID(); |
| 187 | REPORTER_ASSERT(reporter, z != y); |
| 188 | |
| 189 | b.moveTo(2, 2); |
| 190 | const uint32_t x = b.getGenerationID(); |
| 191 | REPORTER_ASSERT(reporter, x != y && x != z); |
| 192 | |
| 193 | a.swap(b); |
| 194 | REPORTER_ASSERT(reporter, b.getGenerationID() == y && a.getGenerationID() == x); |
| 195 | |
| 196 | b = a; |
| 197 | REPORTER_ASSERT(reporter, b.getGenerationID() == x); |
| 198 | |
| 199 | SkPath c(a); |
| 200 | REPORTER_ASSERT(reporter, c.getGenerationID() == x); |
| 201 | |
| 202 | c.lineTo(3, 3); |
| 203 | const uint32_t w = c.getGenerationID(); |
| 204 | REPORTER_ASSERT(reporter, b.getGenerationID() == x); |
| 205 | REPORTER_ASSERT(reporter, a.getGenerationID() == x); |
| 206 | REPORTER_ASSERT(reporter, w != x); |
| 207 | |
| 208 | #ifdef SK_BUILD_FOR_ANDROID |
| 209 | static bool kExpectGenIDToIgnoreFill = false; |
| 210 | #else |
| 211 | static bool kExpectGenIDToIgnoreFill = true; |
| 212 | #endif |
| 213 | |
| 214 | c.toggleInverseFillType(); |
| 215 | const uint32_t v = c.getGenerationID(); |
| 216 | REPORTER_ASSERT(reporter, (v == w) == kExpectGenIDToIgnoreFill); |
| 217 | |
| 218 | c.rewind(); |
| 219 | REPORTER_ASSERT(reporter, v != c.getGenerationID()); |
| 220 | } |
| 221 | |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 222 | // This used to assert in the debug build, as the edges did not all line-up. |
| 223 | static void test_bad_cubic_crbug234190() { |
| 224 | SkPath path; |
| 225 | path.moveTo(13.8509f, 3.16858f); |
| 226 | path.cubicTo(-2.35893e+08f, -4.21044e+08f, |
| 227 | -2.38991e+08f, -4.26573e+08f, |
| 228 | -2.41016e+08f, -4.30188e+08f); |
| 229 | |
| 230 | SkPaint paint; |
| 231 | paint.setAntiAlias(true); |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 232 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(84, 88)); |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 233 | surface->getCanvas()->drawPath(path, paint); |
| 234 | } |
| 235 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 236 | static void test_bad_cubic_crbug229478() { |
| 237 | const SkPoint pts[] = { |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 238 | { 4595.91064f, -11596.9873f }, |
| 239 | { 4597.2168f, -11595.9414f }, |
| 240 | { 4598.52344f, -11594.8955f }, |
| 241 | { 4599.83008f, -11593.8496f }, |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 242 | }; |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 243 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 244 | SkPath path; |
| 245 | path.moveTo(pts[0]); |
| 246 | path.cubicTo(pts[1], pts[2], pts[3]); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 247 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 248 | SkPaint paint; |
| 249 | paint.setStyle(SkPaint::kStroke_Style); |
| 250 | paint.setStrokeWidth(20); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 251 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 252 | SkPath dst; |
| 253 | // Before the fix, this would infinite-recurse, and run out of stack |
| 254 | // because we would keep trying to subdivide a degenerate cubic segment. |
| 255 | paint.getFillPath(path, &dst, NULL); |
| 256 | } |
| 257 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 258 | static void build_path_170666(SkPath& path) { |
| 259 | path.moveTo(17.9459f, 21.6344f); |
| 260 | path.lineTo(139.545f, -47.8105f); |
| 261 | path.lineTo(139.545f, -47.8105f); |
| 262 | path.lineTo(131.07f, -47.3888f); |
| 263 | path.lineTo(131.07f, -47.3888f); |
| 264 | path.lineTo(122.586f, -46.9532f); |
| 265 | path.lineTo(122.586f, -46.9532f); |
| 266 | path.lineTo(18076.6f, 31390.9f); |
| 267 | path.lineTo(18076.6f, 31390.9f); |
| 268 | path.lineTo(18085.1f, 31390.5f); |
| 269 | path.lineTo(18085.1f, 31390.5f); |
| 270 | path.lineTo(18076.6f, 31390.9f); |
| 271 | path.lineTo(18076.6f, 31390.9f); |
| 272 | path.lineTo(17955, 31460.3f); |
| 273 | path.lineTo(17955, 31460.3f); |
| 274 | path.lineTo(17963.5f, 31459.9f); |
| 275 | path.lineTo(17963.5f, 31459.9f); |
| 276 | path.lineTo(17971.9f, 31459.5f); |
| 277 | path.lineTo(17971.9f, 31459.5f); |
| 278 | path.lineTo(17.9551f, 21.6205f); |
| 279 | path.lineTo(17.9551f, 21.6205f); |
| 280 | path.lineTo(9.47091f, 22.0561f); |
| 281 | path.lineTo(9.47091f, 22.0561f); |
| 282 | path.lineTo(17.9459f, 21.6344f); |
| 283 | path.lineTo(17.9459f, 21.6344f); |
| 284 | path.close();path.moveTo(0.995934f, 22.4779f); |
| 285 | path.lineTo(0.986725f, 22.4918f); |
| 286 | path.lineTo(0.986725f, 22.4918f); |
| 287 | path.lineTo(17955, 31460.4f); |
| 288 | path.lineTo(17955, 31460.4f); |
| 289 | path.lineTo(17971.9f, 31459.5f); |
| 290 | path.lineTo(17971.9f, 31459.5f); |
| 291 | path.lineTo(18093.6f, 31390.1f); |
| 292 | path.lineTo(18093.6f, 31390.1f); |
| 293 | path.lineTo(18093.6f, 31390); |
| 294 | path.lineTo(18093.6f, 31390); |
| 295 | path.lineTo(139.555f, -47.8244f); |
| 296 | path.lineTo(139.555f, -47.8244f); |
| 297 | path.lineTo(122.595f, -46.9671f); |
| 298 | path.lineTo(122.595f, -46.9671f); |
| 299 | path.lineTo(0.995934f, 22.4779f); |
| 300 | path.lineTo(0.995934f, 22.4779f); |
| 301 | path.close(); |
| 302 | path.moveTo(5.43941f, 25.5223f); |
| 303 | path.lineTo(798267, -28871.1f); |
| 304 | path.lineTo(798267, -28871.1f); |
| 305 | path.lineTo(3.12512e+06f, -113102); |
| 306 | path.lineTo(3.12512e+06f, -113102); |
| 307 | path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813); |
| 308 | path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f); |
| 309 | path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f); |
| 310 | path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f); |
| 311 | path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f); |
| 312 | path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f); |
| 313 | path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f); |
| 314 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 315 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 316 | path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f); |
| 317 | path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f); |
| 318 | path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f); |
| 319 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 320 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 321 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 322 | path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f); |
| 323 | path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f); |
| 324 | path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f); |
| 325 | path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f); |
| 326 | path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f); |
| 327 | path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814); |
| 328 | path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103); |
| 329 | path.lineTo(798284, -28872); |
| 330 | path.lineTo(798284, -28872); |
| 331 | path.lineTo(22.4044f, 24.6677f); |
| 332 | path.lineTo(22.4044f, 24.6677f); |
| 333 | path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f); |
| 334 | path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f); |
| 335 | path.close(); |
| 336 | } |
| 337 | |
| 338 | static void build_path_simple_170666(SkPath& path) { |
| 339 | path.moveTo(126.677f, 24.1591f); |
| 340 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 341 | } |
| 342 | |
| 343 | // This used to assert in the SK_DEBUG build, as the clip step would fail with |
| 344 | // too-few interations in our cubic-line intersection code. That code now runs |
| 345 | // 24 interations (instead of 16). |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 346 | static void test_crbug_170666() { |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 347 | SkPath path; |
| 348 | SkPaint paint; |
| 349 | paint.setAntiAlias(true); |
| 350 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 351 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(1000, 1000)); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 352 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 353 | build_path_simple_170666(path); |
| 354 | surface->getCanvas()->drawPath(path, paint); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 355 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 356 | build_path_170666(path); |
| 357 | surface->getCanvas()->drawPath(path, paint); |
| 358 | } |
| 359 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 360 | static void test_addrect(skiatest::Reporter* reporter) { |
| 361 | SkPath path; |
| 362 | path.lineTo(0, 0); |
| 363 | path.addRect(SkRect::MakeWH(50, 100)); |
| 364 | REPORTER_ASSERT(reporter, path.isRect(NULL)); |
| 365 | |
| 366 | path.reset(); |
| 367 | path.lineTo(FLT_EPSILON, FLT_EPSILON); |
| 368 | path.addRect(SkRect::MakeWH(50, 100)); |
| 369 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 370 | |
| 371 | path.reset(); |
| 372 | path.quadTo(0, 0, 0, 0); |
| 373 | path.addRect(SkRect::MakeWH(50, 100)); |
| 374 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 375 | |
| 376 | path.reset(); |
| 377 | path.conicTo(0, 0, 0, 0, 0.5f); |
| 378 | path.addRect(SkRect::MakeWH(50, 100)); |
| 379 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 380 | |
| 381 | path.reset(); |
| 382 | path.cubicTo(0, 0, 0, 0, 0, 0); |
| 383 | path.addRect(SkRect::MakeWH(50, 100)); |
| 384 | REPORTER_ASSERT(reporter, !path.isRect(NULL)); |
| 385 | } |
| 386 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 387 | // Make sure we stay non-finite once we get there (unless we reset or rewind). |
| 388 | static void test_addrect_isfinite(skiatest::Reporter* reporter) { |
| 389 | SkPath path; |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 390 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 391 | path.addRect(SkRect::MakeWH(50, 100)); |
| 392 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 393 | |
| 394 | path.moveTo(0, 0); |
| 395 | path.lineTo(SK_ScalarInfinity, 42); |
| 396 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 397 | |
| 398 | path.addRect(SkRect::MakeWH(50, 100)); |
| 399 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 400 | |
| 401 | path.reset(); |
| 402 | REPORTER_ASSERT(reporter, path.isFinite()); |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 403 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 404 | path.addRect(SkRect::MakeWH(50, 100)); |
| 405 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 406 | } |
| 407 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 408 | static void build_big_path(SkPath* path, bool reducedCase) { |
| 409 | if (reducedCase) { |
| 410 | path->moveTo(577330, 1971.72f); |
| 411 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 412 | } else { |
| 413 | path->moveTo(60.1631f, 7.70567f); |
| 414 | path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f); |
| 415 | path->lineTo(577379, 1977.77f); |
| 416 | path->quadTo(577364, 1979.57f, 577325, 1980.26f); |
| 417 | path->quadTo(577286, 1980.95f, 577245, 1980.13f); |
| 418 | path->quadTo(577205, 1979.3f, 577187, 1977.45f); |
| 419 | path->quadTo(577168, 1975.6f, 577183, 1973.8f); |
| 420 | path->quadTo(577198, 1972, 577238, 1971.31f); |
| 421 | path->quadTo(577277, 1970.62f, 577317, 1971.45f); |
| 422 | path->quadTo(577330, 1971.72f, 577341, 1972.11f); |
| 423 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 424 | path->moveTo(306.718f, -32.912f); |
| 425 | path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f); |
| 426 | } |
| 427 | } |
| 428 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 429 | static void test_clipped_cubic() { |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 430 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterPMColor(640, 480)); |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 431 | |
| 432 | // This path used to assert, because our cubic-chopping code incorrectly |
| 433 | // moved control points after the chop. This test should be run in SK_DEBUG |
| 434 | // mode to ensure that we no long assert. |
| 435 | SkPath path; |
| 436 | for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) { |
| 437 | build_big_path(&path, SkToBool(doReducedCase)); |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 438 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 439 | SkPaint paint; |
| 440 | for (int doAA = 0; doAA <= 1; ++doAA) { |
| 441 | paint.setAntiAlias(SkToBool(doAA)); |
| 442 | surface->getCanvas()->drawPath(path, paint); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 447 | // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/ |
| 448 | // which triggered an assert, from a tricky cubic. This test replicates that |
| 449 | // example, so we can ensure that we handle it (in SkEdge.cpp), and don't |
| 450 | // assert in the SK_DEBUG build. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 451 | static void test_tricky_cubic() { |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 452 | const SkPoint pts[] = { |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 453 | { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) }, |
| 454 | { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) }, |
| 455 | { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) }, |
| 456 | { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) }, |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 457 | }; |
| 458 | |
| 459 | SkPath path; |
| 460 | path.moveTo(pts[0]); |
| 461 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 462 | |
| 463 | SkPaint paint; |
| 464 | paint.setAntiAlias(true); |
| 465 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 466 | SkSurface* surface = SkSurface::NewRasterPMColor(19, 130); |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 467 | surface->getCanvas()->drawPath(path, paint); |
| 468 | surface->unref(); |
| 469 | } |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 470 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 471 | // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651 |
| 472 | // |
| 473 | static void test_isfinite_after_transform(skiatest::Reporter* reporter) { |
| 474 | SkPath path; |
| 475 | path.quadTo(157, 366, 286, 208); |
| 476 | path.arcTo(37, 442, 315, 163, 957494590897113.0f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 477 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 478 | SkMatrix matrix; |
| 479 | matrix.setScale(1000*1000, 1000*1000); |
| 480 | |
| 481 | // Be sure that path::transform correctly updates isFinite and the bounds |
| 482 | // if the transformation overflows. The previous bug was that isFinite was |
| 483 | // set to true in this case, but the bounds were not set to empty (which |
| 484 | // they should be). |
| 485 | while (path.isFinite()) { |
| 486 | REPORTER_ASSERT(reporter, path.getBounds().isFinite()); |
| 487 | REPORTER_ASSERT(reporter, !path.getBounds().isEmpty()); |
| 488 | path.transform(matrix); |
| 489 | } |
| 490 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 491 | |
| 492 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 493 | path.transform(matrix); |
| 494 | // we need to still be non-finite |
| 495 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 496 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 497 | } |
| 498 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 499 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 500 | SkScalar xIn, SkScalar yIn, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 501 | int startAngle) |
| 502 | { |
| 503 | |
| 504 | SkScalar rx = SkMinScalar(rect.width(), xIn); |
| 505 | SkScalar ry = SkMinScalar(rect.height(), yIn); |
| 506 | |
| 507 | SkRect arcRect; |
| 508 | arcRect.set(-rx, -ry, rx, ry); |
| 509 | switch (startAngle) { |
| 510 | case 0: |
| 511 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); |
| 512 | break; |
| 513 | case 90: |
| 514 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); |
| 515 | break; |
| 516 | case 180: |
| 517 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); |
| 518 | break; |
| 519 | case 270: |
| 520 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); |
| 521 | break; |
| 522 | default: |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); |
| 527 | } |
| 528 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 529 | static void make_arb_round_rect(SkPath* path, const SkRect& r, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 530 | SkScalar xCorner, SkScalar yCorner) { |
| 531 | // we are lazy here and use the same x & y for each corner |
| 532 | add_corner_arc(path, r, xCorner, yCorner, 270); |
| 533 | add_corner_arc(path, r, xCorner, yCorner, 0); |
| 534 | add_corner_arc(path, r, xCorner, yCorner, 90); |
| 535 | add_corner_arc(path, r, xCorner, yCorner, 180); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 536 | path->close(); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // Chrome creates its own round rects with each corner possibly being different. |
| 540 | // Performance will suffer if they are not convex. |
| 541 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 542 | // the same test (but with drawing) |
| 543 | static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 544 | SkRandom rand; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 545 | SkRect r; |
| 546 | |
| 547 | for (int i = 0; i < 5000; ++i) { |
| 548 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 549 | SkScalar size = rand.nextUScalar1() * 30; |
| 550 | if (size < SK_Scalar1) { |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 551 | continue; |
| 552 | } |
| 553 | r.fLeft = rand.nextUScalar1() * 300; |
| 554 | r.fTop = rand.nextUScalar1() * 300; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 555 | r.fRight = r.fLeft + 2 * size; |
| 556 | r.fBottom = r.fTop + 2 * size; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 557 | |
| 558 | SkPath temp; |
| 559 | |
| 560 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 561 | |
| 562 | REPORTER_ASSERT(reporter, temp.isConvex()); |
| 563 | } |
| 564 | } |
| 565 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 566 | // Chrome will sometimes create a 0 radius round rect. The degenerate |
| 567 | // quads prevent the path from being converted to a rect |
| 568 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 569 | // the same test (but with drawing) |
| 570 | static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 571 | SkRandom rand; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 572 | SkRect r; |
| 573 | |
| 574 | for (int i = 0; i < 5000; ++i) { |
| 575 | |
| 576 | SkScalar size = rand.nextUScalar1() * 30; |
| 577 | if (size < SK_Scalar1) { |
| 578 | continue; |
| 579 | } |
| 580 | r.fLeft = rand.nextUScalar1() * 300; |
| 581 | r.fTop = rand.nextUScalar1() * 300; |
| 582 | r.fRight = r.fLeft + 2 * size; |
| 583 | r.fBottom = r.fTop + 2 * size; |
| 584 | |
| 585 | SkPath temp; |
| 586 | |
| 587 | make_arb_round_rect(&temp, r, 0, 0); |
| 588 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 589 | SkRect result; |
| 590 | REPORTER_ASSERT(reporter, temp.isRect(&result)); |
| 591 | REPORTER_ASSERT(reporter, r == result); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 595 | static void test_rect_isfinite(skiatest::Reporter* reporter) { |
| 596 | const SkScalar inf = SK_ScalarInfinity; |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 597 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 598 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 599 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 600 | SkRect r; |
| 601 | r.setEmpty(); |
| 602 | REPORTER_ASSERT(reporter, r.isFinite()); |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 603 | r.set(0, 0, inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 604 | REPORTER_ASSERT(reporter, !r.isFinite()); |
| 605 | r.set(0, 0, nan, 0); |
| 606 | REPORTER_ASSERT(reporter, !r.isFinite()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 607 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 608 | SkPoint pts[] = { |
| 609 | { 0, 0 }, |
| 610 | { SK_Scalar1, 0 }, |
| 611 | { 0, SK_Scalar1 }, |
| 612 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 613 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 614 | bool isFine = r.setBoundsCheck(pts, 3); |
| 615 | REPORTER_ASSERT(reporter, isFine); |
| 616 | REPORTER_ASSERT(reporter, !r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 617 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 618 | pts[1].set(inf, 0); |
| 619 | isFine = r.setBoundsCheck(pts, 3); |
| 620 | REPORTER_ASSERT(reporter, !isFine); |
| 621 | REPORTER_ASSERT(reporter, r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 622 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 623 | pts[1].set(nan, 0); |
| 624 | isFine = r.setBoundsCheck(pts, 3); |
| 625 | REPORTER_ASSERT(reporter, !isFine); |
| 626 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 627 | } |
| 628 | |
| 629 | static void test_path_isfinite(skiatest::Reporter* reporter) { |
| 630 | const SkScalar inf = SK_ScalarInfinity; |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 631 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 632 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 633 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 634 | SkPath path; |
| 635 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 636 | |
| 637 | path.reset(); |
| 638 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 639 | |
| 640 | path.reset(); |
| 641 | path.moveTo(SK_Scalar1, 0); |
| 642 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 643 | |
| 644 | path.reset(); |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 645 | path.moveTo(inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 646 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 647 | |
| 648 | path.reset(); |
| 649 | path.moveTo(nan, 0); |
| 650 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 651 | } |
| 652 | |
| 653 | static void test_isfinite(skiatest::Reporter* reporter) { |
| 654 | test_rect_isfinite(reporter); |
| 655 | test_path_isfinite(reporter); |
| 656 | } |
| 657 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 658 | // assert that we always |
| 659 | // start with a moveTo |
| 660 | // only have 1 moveTo |
| 661 | // only have Lines after that |
| 662 | // end with a single close |
| 663 | // only have (at most) 1 close |
| 664 | // |
| 665 | static void test_poly(skiatest::Reporter* reporter, const SkPath& path, |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 666 | const SkPoint srcPts[], bool expectClose) { |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 667 | SkPath::RawIter iter(path); |
| 668 | SkPoint pts[4]; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 669 | |
| 670 | bool firstTime = true; |
| 671 | bool foundClose = false; |
| 672 | for (;;) { |
| 673 | switch (iter.next(pts)) { |
| 674 | case SkPath::kMove_Verb: |
| 675 | REPORTER_ASSERT(reporter, firstTime); |
| 676 | REPORTER_ASSERT(reporter, pts[0] == srcPts[0]); |
| 677 | srcPts++; |
| 678 | firstTime = false; |
| 679 | break; |
| 680 | case SkPath::kLine_Verb: |
| 681 | REPORTER_ASSERT(reporter, !firstTime); |
| 682 | REPORTER_ASSERT(reporter, pts[1] == srcPts[0]); |
| 683 | srcPts++; |
| 684 | break; |
| 685 | case SkPath::kQuad_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 686 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 687 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 688 | case SkPath::kConic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 689 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 690 | break; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 691 | case SkPath::kCubic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 692 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 693 | break; |
| 694 | case SkPath::kClose_Verb: |
| 695 | REPORTER_ASSERT(reporter, !firstTime); |
| 696 | REPORTER_ASSERT(reporter, !foundClose); |
| 697 | REPORTER_ASSERT(reporter, expectClose); |
| 698 | foundClose = true; |
| 699 | break; |
| 700 | case SkPath::kDone_Verb: |
| 701 | goto DONE; |
| 702 | } |
| 703 | } |
| 704 | DONE: |
| 705 | REPORTER_ASSERT(reporter, foundClose == expectClose); |
| 706 | } |
| 707 | |
| 708 | static void test_addPoly(skiatest::Reporter* reporter) { |
| 709 | SkPoint pts[32]; |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 710 | SkRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 711 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 712 | for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) { |
| 713 | pts[i].fX = rand.nextSScalar1(); |
| 714 | pts[i].fY = rand.nextSScalar1(); |
| 715 | } |
| 716 | |
| 717 | for (int doClose = 0; doClose <= 1; ++doClose) { |
| 718 | for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) { |
| 719 | SkPath path; |
| 720 | path.addPoly(pts, count, SkToBool(doClose)); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 721 | test_poly(reporter, path, pts, SkToBool(doClose)); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 726 | static void test_strokerec(skiatest::Reporter* reporter) { |
| 727 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 728 | REPORTER_ASSERT(reporter, rec.isFillStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 729 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 730 | rec.setHairlineStyle(); |
| 731 | REPORTER_ASSERT(reporter, rec.isHairlineStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 732 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 733 | rec.setStrokeStyle(SK_Scalar1, false); |
| 734 | REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 735 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 736 | rec.setStrokeStyle(SK_Scalar1, true); |
| 737 | REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 738 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 739 | rec.setStrokeStyle(0, false); |
| 740 | REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 741 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 742 | rec.setStrokeStyle(0, true); |
| 743 | REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle()); |
| 744 | } |
| 745 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 746 | // Set this for paths that don't have a consistent direction such as a bowtie. |
| 747 | // (cheapComputeDirection is not expected to catch these.) |
| 748 | static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1); |
| 749 | |
| 750 | static void check_direction(skiatest::Reporter* reporter, const SkPath& path, |
| 751 | SkPath::Direction expected) { |
| 752 | if (expected == kDontCheckDir) { |
| 753 | return; |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 754 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 755 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 756 | |
| 757 | SkPath::Direction dir; |
| 758 | if (copy.cheapComputeDirection(&dir)) { |
| 759 | REPORTER_ASSERT(reporter, dir == expected); |
| 760 | } else { |
| 761 | REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected); |
| 762 | } |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 763 | } |
| 764 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 765 | static void test_direction(skiatest::Reporter* reporter) { |
| 766 | size_t i; |
| 767 | SkPath path; |
| 768 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 769 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction)); |
| 770 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction)); |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 771 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction)); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 772 | |
| 773 | static const char* gDegen[] = { |
| 774 | "M 10 10", |
| 775 | "M 10 10 M 20 20", |
| 776 | "M 10 10 L 20 20", |
| 777 | "M 10 10 L 10 10 L 10 10", |
| 778 | "M 10 10 Q 10 10 10 10", |
| 779 | "M 10 10 C 10 10 10 10 10 10", |
| 780 | }; |
| 781 | for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) { |
| 782 | path.reset(); |
| 783 | bool valid = SkParsePath::FromSVGString(gDegen[i], &path); |
| 784 | REPORTER_ASSERT(reporter, valid); |
| 785 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 786 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 787 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 788 | static const char* gCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 789 | "M 10 10 L 10 10 Q 20 10 20 20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 790 | "M 10 10 C 20 10 20 20 20 20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 791 | "M 20 10 Q 20 20 30 20 L 10 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 792 | // rect with top two corners replaced by cubics with identical middle |
| 793 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 794 | "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10", |
| 795 | "M 20 10 L 0 10 Q 10 10 20 0", // left, degenerate serif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 796 | }; |
| 797 | for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) { |
| 798 | path.reset(); |
| 799 | bool valid = SkParsePath::FromSVGString(gCW[i], &path); |
| 800 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 801 | check_direction(reporter, path, SkPath::kCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 802 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 803 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 804 | static const char* gCCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 805 | "M 10 10 L 10 10 Q 20 10 20 -20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 806 | "M 10 10 C 20 10 20 -20 20 -20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 807 | "M 20 10 Q 20 20 10 20 L 30 20", // test double-back at y-max |
bsalomon@google.com | 4eefe61 | 2012-07-10 18:28:12 +0000 | [diff] [blame] | 808 | // rect with top two corners replaced by cubics with identical middle |
| 809 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 810 | "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10", |
| 811 | "M 10 10 L 30 10 Q 20 10 10 0", // right, degenerate serif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 812 | }; |
| 813 | for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) { |
| 814 | path.reset(); |
| 815 | bool valid = SkParsePath::FromSVGString(gCCW[i], &path); |
| 816 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 817 | check_direction(reporter, path, SkPath::kCCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 818 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 819 | |
| 820 | // Test two donuts, each wound a different direction. Only the outer contour |
| 821 | // determines the cheap direction |
| 822 | path.reset(); |
| 823 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction); |
| 824 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 825 | check_direction(reporter, path, SkPath::kCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 826 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 827 | path.reset(); |
| 828 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction); |
| 829 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 830 | check_direction(reporter, path, SkPath::kCCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 831 | |
| 832 | // triangle with one point really far from the origin. |
| 833 | path.reset(); |
| 834 | // the first point is roughly 1.05e10, 1.05e10 |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 835 | path.moveTo(SkBits2Float(0x501c7652), SkBits2Float(0x501c7652)); |
bsalomon@google.com | 53aab78 | 2012-02-23 14:54:49 +0000 | [diff] [blame] | 836 | path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1); |
| 837 | path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 838 | check_direction(reporter, path, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 839 | |
| 840 | path.reset(); |
| 841 | path.conicTo(20, 0, 20, 20, 0.5f); |
| 842 | path.close(); |
| 843 | check_direction(reporter, path, SkPath::kCW_Direction); |
| 844 | |
| 845 | path.reset(); |
| 846 | path.lineTo(1, 1e7f); |
| 847 | path.lineTo(1e7f, 2e7f); |
| 848 | path.close(); |
| 849 | REPORTER_ASSERT(reporter, SkPath::kConvex_Convexity == path.getConvexity()); |
| 850 | check_direction(reporter, path, SkPath::kCCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 851 | } |
| 852 | |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 853 | static void add_rect(SkPath* path, const SkRect& r) { |
| 854 | path->moveTo(r.fLeft, r.fTop); |
| 855 | path->lineTo(r.fRight, r.fTop); |
| 856 | path->lineTo(r.fRight, r.fBottom); |
| 857 | path->lineTo(r.fLeft, r.fBottom); |
| 858 | path->close(); |
| 859 | } |
| 860 | |
| 861 | static void test_bounds(skiatest::Reporter* reporter) { |
| 862 | static const SkRect rects[] = { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 863 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, |
| 864 | { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, |
| 865 | { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, |
| 866 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 867 | }; |
| 868 | |
| 869 | SkPath path0, path1; |
| 870 | for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { |
| 871 | path0.addRect(rects[i]); |
| 872 | add_rect(&path1, rects[i]); |
| 873 | } |
| 874 | |
| 875 | REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); |
| 876 | } |
| 877 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 878 | static void stroke_cubic(const SkPoint pts[4]) { |
| 879 | SkPath path; |
| 880 | path.moveTo(pts[0]); |
| 881 | path.cubicTo(pts[1], pts[2], pts[3]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 882 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 883 | SkPaint paint; |
| 884 | paint.setStyle(SkPaint::kStroke_Style); |
| 885 | paint.setStrokeWidth(SK_Scalar1 * 2); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 886 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 887 | SkPath fill; |
| 888 | paint.getFillPath(path, &fill); |
| 889 | } |
| 890 | |
| 891 | // just ensure this can run w/o any SkASSERTS firing in the debug build |
| 892 | // we used to assert due to differences in how we determine a degenerate vector |
| 893 | // but that was fixed with the introduction of SkPoint::CanNormalize |
| 894 | static void stroke_tiny_cubic() { |
| 895 | SkPoint p0[] = { |
| 896 | { 372.0f, 92.0f }, |
| 897 | { 372.0f, 92.0f }, |
| 898 | { 372.0f, 92.0f }, |
| 899 | { 372.0f, 92.0f }, |
| 900 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 901 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 902 | stroke_cubic(p0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 903 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 904 | SkPoint p1[] = { |
| 905 | { 372.0f, 92.0f }, |
| 906 | { 372.0007f, 92.000755f }, |
| 907 | { 371.99927f, 92.003922f }, |
| 908 | { 371.99826f, 92.003899f }, |
| 909 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 910 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 911 | stroke_cubic(p1); |
| 912 | } |
| 913 | |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 914 | static void check_close(skiatest::Reporter* reporter, const SkPath& path) { |
| 915 | for (int i = 0; i < 2; ++i) { |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 916 | SkPath::Iter iter(path, SkToBool(i)); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 917 | SkPoint mv; |
| 918 | SkPoint pts[4]; |
| 919 | SkPath::Verb v; |
| 920 | int nMT = 0; |
| 921 | int nCL = 0; |
tomhudson@google.com | 221db3c | 2011-07-28 21:10:29 +0000 | [diff] [blame] | 922 | mv.set(0, 0); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 923 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 924 | switch (v) { |
| 925 | case SkPath::kMove_Verb: |
| 926 | mv = pts[0]; |
| 927 | ++nMT; |
| 928 | break; |
| 929 | case SkPath::kClose_Verb: |
| 930 | REPORTER_ASSERT(reporter, mv == pts[0]); |
| 931 | ++nCL; |
| 932 | break; |
| 933 | default: |
| 934 | break; |
| 935 | } |
| 936 | } |
| 937 | // if we force a close on the interator we should have a close |
| 938 | // for every moveTo |
| 939 | REPORTER_ASSERT(reporter, !i || nMT == nCL); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | static void test_close(skiatest::Reporter* reporter) { |
| 944 | SkPath closePt; |
| 945 | closePt.moveTo(0, 0); |
| 946 | closePt.close(); |
| 947 | check_close(reporter, closePt); |
| 948 | |
| 949 | SkPath openPt; |
| 950 | openPt.moveTo(0, 0); |
| 951 | check_close(reporter, openPt); |
| 952 | |
| 953 | SkPath empty; |
| 954 | check_close(reporter, empty); |
| 955 | empty.close(); |
| 956 | check_close(reporter, empty); |
| 957 | |
| 958 | SkPath rect; |
| 959 | rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 960 | check_close(reporter, rect); |
| 961 | rect.close(); |
| 962 | check_close(reporter, rect); |
| 963 | |
| 964 | SkPath quad; |
| 965 | quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 966 | check_close(reporter, quad); |
| 967 | quad.close(); |
| 968 | check_close(reporter, quad); |
| 969 | |
| 970 | SkPath cubic; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 971 | quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 972 | 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1); |
| 973 | check_close(reporter, cubic); |
| 974 | cubic.close(); |
| 975 | check_close(reporter, cubic); |
| 976 | |
| 977 | SkPath line; |
| 978 | line.moveTo(SK_Scalar1, SK_Scalar1); |
| 979 | line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1); |
| 980 | check_close(reporter, line); |
| 981 | line.close(); |
| 982 | check_close(reporter, line); |
| 983 | |
| 984 | SkPath rect2; |
| 985 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 986 | rect2.close(); |
| 987 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 988 | check_close(reporter, rect2); |
| 989 | rect2.close(); |
| 990 | check_close(reporter, rect2); |
| 991 | |
| 992 | SkPath oval3; |
| 993 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100)); |
| 994 | oval3.close(); |
| 995 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200)); |
| 996 | check_close(reporter, oval3); |
| 997 | oval3.close(); |
| 998 | check_close(reporter, oval3); |
| 999 | |
| 1000 | SkPath moves; |
| 1001 | moves.moveTo(SK_Scalar1, SK_Scalar1); |
| 1002 | moves.moveTo(5 * SK_Scalar1, SK_Scalar1); |
| 1003 | moves.moveTo(SK_Scalar1, 10 * SK_Scalar1); |
| 1004 | moves.moveTo(10 *SK_Scalar1, SK_Scalar1); |
| 1005 | check_close(reporter, moves); |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 1006 | |
| 1007 | stroke_tiny_cubic(); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1010 | static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, |
| 1011 | SkPath::Convexity expected) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1012 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 1013 | SkPath::Convexity c = copy.getConvexity(); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1014 | REPORTER_ASSERT(reporter, c == expected); |
| 1015 | } |
| 1016 | |
| 1017 | static void test_convexity2(skiatest::Reporter* reporter) { |
| 1018 | SkPath pt; |
| 1019 | pt.moveTo(0, 0); |
| 1020 | pt.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 1021 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1022 | check_direction(reporter, pt, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1023 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1024 | SkPath line; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1025 | line.moveTo(12*SK_Scalar1, 20*SK_Scalar1); |
| 1026 | line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1027 | line.close(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1028 | check_convexity(reporter, line, SkPath::kConvex_Convexity); |
| 1029 | check_direction(reporter, line, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1030 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1031 | SkPath triLeft; |
| 1032 | triLeft.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1033 | triLeft.lineTo(SK_Scalar1, 0); |
| 1034 | triLeft.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1035 | triLeft.close(); |
| 1036 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1037 | check_direction(reporter, triLeft, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1038 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1039 | SkPath triRight; |
| 1040 | triRight.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1041 | triRight.lineTo(-SK_Scalar1, 0); |
| 1042 | triRight.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1043 | triRight.close(); |
| 1044 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1045 | check_direction(reporter, triRight, SkPath::kCCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1046 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1047 | SkPath square; |
| 1048 | square.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1049 | square.lineTo(SK_Scalar1, 0); |
| 1050 | square.lineTo(SK_Scalar1, SK_Scalar1); |
| 1051 | square.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1052 | square.close(); |
| 1053 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1054 | check_direction(reporter, square, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1055 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1056 | SkPath redundantSquare; |
| 1057 | redundantSquare.moveTo(0, 0); |
| 1058 | redundantSquare.lineTo(0, 0); |
| 1059 | redundantSquare.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1060 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1061 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1062 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 1063 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1064 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1065 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 1066 | redundantSquare.lineTo(0, SK_Scalar1); |
| 1067 | redundantSquare.lineTo(0, SK_Scalar1); |
| 1068 | redundantSquare.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1069 | redundantSquare.close(); |
| 1070 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1071 | check_direction(reporter, redundantSquare, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1072 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1073 | SkPath bowTie; |
| 1074 | bowTie.moveTo(0, 0); |
| 1075 | bowTie.lineTo(0, 0); |
| 1076 | bowTie.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1077 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1078 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1079 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 1080 | bowTie.lineTo(SK_Scalar1, 0); |
| 1081 | bowTie.lineTo(SK_Scalar1, 0); |
| 1082 | bowTie.lineTo(SK_Scalar1, 0); |
| 1083 | bowTie.lineTo(0, SK_Scalar1); |
| 1084 | bowTie.lineTo(0, SK_Scalar1); |
| 1085 | bowTie.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1086 | bowTie.close(); |
| 1087 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1088 | check_direction(reporter, bowTie, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1089 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1090 | SkPath spiral; |
| 1091 | spiral.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1092 | spiral.lineTo(100*SK_Scalar1, 0); |
| 1093 | spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 1094 | spiral.lineTo(0, 100*SK_Scalar1); |
| 1095 | spiral.lineTo(0, 50*SK_Scalar1); |
| 1096 | spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1); |
| 1097 | spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1098 | spiral.close(); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 1099 | check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1100 | check_direction(reporter, spiral, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1101 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1102 | SkPath dent; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 1103 | dent.moveTo(0, 0); |
| 1104 | dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 1105 | dent.lineTo(0, 100*SK_Scalar1); |
| 1106 | dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1); |
| 1107 | dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1108 | dent.close(); |
| 1109 | check_convexity(reporter, dent, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1110 | check_direction(reporter, dent, SkPath::kCW_Direction); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1113 | static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, |
| 1114 | const SkRect& bounds) { |
| 1115 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 1116 | REPORTER_ASSERT(reporter, p.getBounds() == bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 1117 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 1118 | SkPath p2(p); |
| 1119 | REPORTER_ASSERT(reporter, p2.isConvex()); |
| 1120 | REPORTER_ASSERT(reporter, p2.getBounds() == bounds); |
| 1121 | |
| 1122 | SkPath other; |
| 1123 | other.swap(p2); |
| 1124 | REPORTER_ASSERT(reporter, other.isConvex()); |
| 1125 | REPORTER_ASSERT(reporter, other.getBounds() == bounds); |
| 1126 | } |
| 1127 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1128 | static void setFromString(SkPath* path, const char str[]) { |
| 1129 | bool first = true; |
| 1130 | while (str) { |
| 1131 | SkScalar x, y; |
| 1132 | str = SkParse::FindScalar(str, &x); |
| 1133 | if (NULL == str) { |
| 1134 | break; |
| 1135 | } |
| 1136 | str = SkParse::FindScalar(str, &y); |
| 1137 | SkASSERT(str); |
| 1138 | if (first) { |
| 1139 | path->moveTo(x, y); |
| 1140 | first = false; |
| 1141 | } else { |
| 1142 | path->lineTo(x, y); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | static void test_convexity(skiatest::Reporter* reporter) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1148 | SkPath path; |
| 1149 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1150 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1151 | path.addCircle(0, 0, SkIntToScalar(10)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1152 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1153 | path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1154 | check_convexity(reporter, path, SkPath::kConcave_Convexity); |
| 1155 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1156 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1157 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1158 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1159 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1160 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1161 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1162 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1163 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1164 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1165 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1166 | static const struct { |
| 1167 | const char* fPathStr; |
| 1168 | SkPath::Convexity fExpectedConvexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1169 | SkPath::Direction fExpectedDirection; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1170 | } gRec[] = { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1171 | { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1172 | { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1173 | { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1174 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction }, |
| 1175 | { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction }, |
| 1176 | { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction }, |
| 1177 | { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir }, |
| 1178 | { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity, SkPath::kCW_Direction }, |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1179 | }; |
| 1180 | |
| 1181 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 1182 | SkPath path; |
| 1183 | setFromString(&path, gRec[i].fPathStr); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1184 | check_convexity(reporter, path, gRec[i].fExpectedConvexity); |
| 1185 | check_direction(reporter, path, gRec[i].fExpectedDirection); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1186 | // check after setting the initial convex and direction |
| 1187 | if (kDontCheckDir != gRec[i].fExpectedDirection) { |
| 1188 | SkPath copy(path); |
| 1189 | SkPath::Direction dir; |
| 1190 | bool foundDir = copy.cheapComputeDirection(&dir); |
| 1191 | REPORTER_ASSERT(reporter, (gRec[i].fExpectedDirection == SkPath::kUnknown_Direction) |
| 1192 | ^ foundDir); |
| 1193 | REPORTER_ASSERT(reporter, !foundDir || gRec[i].fExpectedDirection == dir); |
| 1194 | check_convexity(reporter, copy, gRec[i].fExpectedConvexity); |
| 1195 | } |
| 1196 | REPORTER_ASSERT(reporter, gRec[i].fExpectedConvexity == path.getConvexity()); |
| 1197 | check_direction(reporter, path, gRec[i].fExpectedDirection); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1201 | static void test_isLine(skiatest::Reporter* reporter) { |
| 1202 | SkPath path; |
| 1203 | SkPoint pts[2]; |
| 1204 | const SkScalar value = SkIntToScalar(5); |
| 1205 | |
| 1206 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1207 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1208 | // set some non-zero values |
| 1209 | pts[0].set(value, value); |
| 1210 | pts[1].set(value, value); |
| 1211 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1212 | // check that pts was untouched |
| 1213 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1214 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1215 | |
| 1216 | const SkScalar moveX = SkIntToScalar(1); |
| 1217 | const SkScalar moveY = SkIntToScalar(2); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1218 | REPORTER_ASSERT(reporter, value != moveX && value != moveY); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1219 | |
| 1220 | path.moveTo(moveX, moveY); |
| 1221 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1222 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1223 | // check that pts was untouched |
| 1224 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1225 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1226 | |
| 1227 | const SkScalar lineX = SkIntToScalar(2); |
| 1228 | const SkScalar lineY = SkIntToScalar(2); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1229 | REPORTER_ASSERT(reporter, value != lineX && value != lineY); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1230 | |
| 1231 | path.lineTo(lineX, lineY); |
| 1232 | REPORTER_ASSERT(reporter, path.isLine(NULL)); |
| 1233 | |
| 1234 | REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY)); |
| 1235 | REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY)); |
| 1236 | REPORTER_ASSERT(reporter, path.isLine(pts)); |
| 1237 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1238 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 1239 | |
| 1240 | path.lineTo(0, 0); // too many points/verbs |
| 1241 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1242 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1243 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1244 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1245 | |
| 1246 | path.reset(); |
| 1247 | path.quadTo(1, 1, 2, 2); |
| 1248 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1251 | static void test_conservativelyContains(skiatest::Reporter* reporter) { |
| 1252 | SkPath path; |
| 1253 | |
| 1254 | // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect. |
| 1255 | static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 1256 | |
| 1257 | // A circle that bounds kBaseRect (with a significant amount of slop) |
| 1258 | SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height()); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1259 | circleR = SkScalarMul(circleR, 1.75f) / 2; |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1260 | static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()}; |
| 1261 | |
| 1262 | // round-rect radii |
| 1263 | static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)}; |
skia.committer@gmail.com | cec8de6 | 2012-11-14 02:01:22 +0000 | [diff] [blame] | 1264 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1265 | static const struct SUPPRESS_VISIBILITY_WARNING { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1266 | SkRect fQueryRect; |
| 1267 | bool fInRect; |
| 1268 | bool fInCircle; |
| 1269 | bool fInRR; |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1270 | bool fInCubicRR; |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1271 | } kQueries[] = { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1272 | {kBaseRect, true, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1273 | |
| 1274 | // rect well inside of kBaseRect |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1275 | {SkRect::MakeLTRB(kBaseRect.fLeft + 0.25f*kBaseRect.width(), |
| 1276 | kBaseRect.fTop + 0.25f*kBaseRect.height(), |
| 1277 | kBaseRect.fRight - 0.25f*kBaseRect.width(), |
| 1278 | kBaseRect.fBottom - 0.25f*kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1279 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1280 | |
| 1281 | // rects with edges off by one from kBaseRect's edges |
| 1282 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1283 | kBaseRect.width(), kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1284 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1285 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1286 | kBaseRect.width() + 1, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1287 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1288 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1289 | kBaseRect.width() + 1, kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1290 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1291 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1292 | kBaseRect.width(), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1293 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1294 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1295 | kBaseRect.width(), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1296 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1297 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1298 | kBaseRect.width() + 2, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1299 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1300 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1301 | kBaseRect.width() + 2, kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1302 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1303 | |
| 1304 | // zero-w/h rects at each corner of kBaseRect |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1305 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false, false}, |
| 1306 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false, true}, |
| 1307 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false, true}, |
| 1308 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1309 | |
| 1310 | // far away rect |
| 1311 | {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom, |
| 1312 | SkIntToScalar(10), SkIntToScalar(10)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1313 | false, false, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1314 | |
| 1315 | // very large rect containing kBaseRect |
| 1316 | {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(), |
| 1317 | kBaseRect.fTop - 5 * kBaseRect.height(), |
| 1318 | 11 * kBaseRect.width(), 11 * kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1319 | false, false, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1320 | |
| 1321 | // skinny rect that spans same y-range as kBaseRect |
| 1322 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1323 | SkIntToScalar(1), kBaseRect.height()), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1324 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1325 | |
| 1326 | // short rect that spans same x-range as kBaseRect |
| 1327 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1328 | true, true, true, true}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1329 | |
| 1330 | // skinny rect that spans slightly larger y-range than kBaseRect |
| 1331 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1332 | SkIntToScalar(1), kBaseRect.height() + 1), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1333 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1334 | |
| 1335 | // short rect that spans slightly larger x-range than kBaseRect |
| 1336 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), |
| 1337 | kBaseRect.width() + 1, SkScalar(1)), |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1338 | false, true, false, false}, |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1339 | }; |
| 1340 | |
| 1341 | for (int inv = 0; inv < 4; ++inv) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1342 | for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1343 | SkRect qRect = kQueries[q].fQueryRect; |
| 1344 | if (inv & 0x1) { |
| 1345 | SkTSwap(qRect.fLeft, qRect.fRight); |
| 1346 | } |
| 1347 | if (inv & 0x2) { |
| 1348 | SkTSwap(qRect.fTop, qRect.fBottom); |
| 1349 | } |
| 1350 | for (int d = 0; d < 2; ++d) { |
| 1351 | SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction; |
| 1352 | path.reset(); |
| 1353 | path.addRect(kBaseRect, dir); |
| 1354 | REPORTER_ASSERT(reporter, kQueries[q].fInRect == |
| 1355 | path.conservativelyContainsRect(qRect)); |
| 1356 | |
| 1357 | path.reset(); |
| 1358 | path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir); |
| 1359 | REPORTER_ASSERT(reporter, kQueries[q].fInCircle == |
| 1360 | path.conservativelyContainsRect(qRect)); |
| 1361 | |
| 1362 | path.reset(); |
| 1363 | path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir); |
| 1364 | REPORTER_ASSERT(reporter, kQueries[q].fInRR == |
| 1365 | path.conservativelyContainsRect(qRect)); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1366 | |
| 1367 | path.reset(); |
| 1368 | path.moveTo(kBaseRect.fLeft + kRRRadii[0], kBaseRect.fTop); |
| 1369 | path.cubicTo(kBaseRect.fLeft + kRRRadii[0] / 2, kBaseRect.fTop, |
| 1370 | kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1] / 2, |
| 1371 | kBaseRect.fLeft, kBaseRect.fTop + kRRRadii[1]); |
| 1372 | path.lineTo(kBaseRect.fLeft, kBaseRect.fBottom); |
| 1373 | path.lineTo(kBaseRect.fRight, kBaseRect.fBottom); |
| 1374 | path.lineTo(kBaseRect.fRight, kBaseRect.fTop); |
| 1375 | path.close(); |
| 1376 | REPORTER_ASSERT(reporter, kQueries[q].fInCubicRR == |
| 1377 | path.conservativelyContainsRect(qRect)); |
| 1378 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1379 | } |
| 1380 | // Slightly non-convex shape, shouldn't contain any rects. |
| 1381 | path.reset(); |
| 1382 | path.moveTo(0, 0); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 1383 | path.lineTo(SkIntToScalar(50), 0.05f); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1384 | path.lineTo(SkIntToScalar(100), 0); |
| 1385 | path.lineTo(SkIntToScalar(100), SkIntToScalar(100)); |
| 1386 | path.lineTo(0, SkIntToScalar(100)); |
| 1387 | path.close(); |
| 1388 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect)); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | // make sure a minimal convex shape works, a right tri with edges along pos x and y axes. |
| 1393 | path.reset(); |
| 1394 | path.moveTo(0, 0); |
| 1395 | path.lineTo(SkIntToScalar(100), 0); |
| 1396 | path.lineTo(0, SkIntToScalar(100)); |
| 1397 | |
| 1398 | // inside, on along top edge |
| 1399 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1400 | SkIntToScalar(10), |
| 1401 | SkIntToScalar(10)))); |
| 1402 | // above |
| 1403 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( |
| 1404 | SkRect::MakeXYWH(SkIntToScalar(50), |
| 1405 | SkIntToScalar(-10), |
| 1406 | SkIntToScalar(10), |
| 1407 | SkIntToScalar(10)))); |
| 1408 | // to the left |
| 1409 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10), |
| 1410 | SkIntToScalar(5), |
| 1411 | SkIntToScalar(5), |
| 1412 | SkIntToScalar(5)))); |
| 1413 | |
| 1414 | // outside the diagonal edge |
| 1415 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10), |
| 1416 | SkIntToScalar(200), |
| 1417 | SkIntToScalar(20), |
| 1418 | SkIntToScalar(5)))); |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 1419 | |
| 1420 | // same as above path and first test but with an extra moveTo. |
| 1421 | path.reset(); |
| 1422 | path.moveTo(100, 100); |
| 1423 | path.moveTo(0, 0); |
| 1424 | path.lineTo(SkIntToScalar(100), 0); |
| 1425 | path.lineTo(0, SkIntToScalar(100)); |
| 1426 | |
| 1427 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1428 | SkIntToScalar(10), |
| 1429 | SkIntToScalar(10)))); |
| 1430 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1431 | path.reset(); |
| 1432 | path.lineTo(100, 100); |
| 1433 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(0, 0, 1, 1))); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1436 | static void test_isRect_open_close(skiatest::Reporter* reporter) { |
| 1437 | SkPath path; |
| 1438 | bool isClosed; |
| 1439 | |
| 1440 | path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(1, 1); path.lineTo(0, 1); |
| 1441 | |
| 1442 | if (false) { |
| 1443 | // I think these should pass, but isRect() doesn't behave |
| 1444 | // this way... yet |
| 1445 | REPORTER_ASSERT(reporter, path.isRect(NULL, NULL)); |
| 1446 | REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL)); |
| 1447 | REPORTER_ASSERT(reporter, !isClosed); |
| 1448 | } |
| 1449 | |
| 1450 | path.close(); |
| 1451 | REPORTER_ASSERT(reporter, path.isRect(NULL, NULL)); |
| 1452 | REPORTER_ASSERT(reporter, path.isRect(&isClosed, NULL)); |
| 1453 | REPORTER_ASSERT(reporter, isClosed); |
| 1454 | } |
| 1455 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1456 | // Simple isRect test is inline TestPath, below. |
| 1457 | // test_isRect provides more extensive testing. |
| 1458 | static void test_isRect(skiatest::Reporter* reporter) { |
reed@google.com | f32322b | 2013-10-16 15:14:04 +0000 | [diff] [blame] | 1459 | test_isRect_open_close(reporter); |
| 1460 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1461 | // passing tests (all moveTo / lineTo... |
| 1462 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; |
| 1463 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1464 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1465 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
| 1466 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; |
| 1467 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1468 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1469 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1470 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1471 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; |
| 1472 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1473 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1474 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1475 | SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1476 | SkPoint rf[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}}; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1477 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1478 | // failing tests |
| 1479 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1480 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1481 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1482 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1483 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1484 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1485 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1486 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1487 | SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps |
| 1488 | SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap |
| 1489 | SkPoint fb[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 1}}; // falls short |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1490 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1491 | // failing, no close |
| 1492 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 1493 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 1494 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1495 | struct IsRectTest { |
| 1496 | SkPoint *fPoints; |
| 1497 | size_t fPointCount; |
| 1498 | bool fClose; |
| 1499 | bool fIsRect; |
| 1500 | } tests[] = { |
| 1501 | { r1, SK_ARRAY_COUNT(r1), true, true }, |
| 1502 | { r2, SK_ARRAY_COUNT(r2), true, true }, |
| 1503 | { r3, SK_ARRAY_COUNT(r3), true, true }, |
| 1504 | { r4, SK_ARRAY_COUNT(r4), true, true }, |
| 1505 | { r5, SK_ARRAY_COUNT(r5), true, true }, |
| 1506 | { r6, SK_ARRAY_COUNT(r6), true, true }, |
| 1507 | { r7, SK_ARRAY_COUNT(r7), true, true }, |
| 1508 | { r8, SK_ARRAY_COUNT(r8), true, true }, |
| 1509 | { r9, SK_ARRAY_COUNT(r9), true, true }, |
| 1510 | { ra, SK_ARRAY_COUNT(ra), true, true }, |
| 1511 | { rb, SK_ARRAY_COUNT(rb), true, true }, |
| 1512 | { rc, SK_ARRAY_COUNT(rc), true, true }, |
| 1513 | { rd, SK_ARRAY_COUNT(rd), true, true }, |
| 1514 | { re, SK_ARRAY_COUNT(re), true, true }, |
| 1515 | { rf, SK_ARRAY_COUNT(rf), true, true }, |
| 1516 | |
| 1517 | { f1, SK_ARRAY_COUNT(f1), true, false }, |
| 1518 | { f2, SK_ARRAY_COUNT(f2), true, false }, |
| 1519 | { f3, SK_ARRAY_COUNT(f3), true, false }, |
| 1520 | { f4, SK_ARRAY_COUNT(f4), true, false }, |
| 1521 | { f5, SK_ARRAY_COUNT(f5), true, false }, |
| 1522 | { f6, SK_ARRAY_COUNT(f6), true, false }, |
| 1523 | { f7, SK_ARRAY_COUNT(f7), true, false }, |
| 1524 | { f8, SK_ARRAY_COUNT(f8), true, false }, |
| 1525 | { f9, SK_ARRAY_COUNT(f9), true, false }, |
| 1526 | { fa, SK_ARRAY_COUNT(fa), true, false }, |
| 1527 | { fb, SK_ARRAY_COUNT(fb), true, false }, |
| 1528 | |
| 1529 | { c1, SK_ARRAY_COUNT(c1), false, false }, |
| 1530 | { c2, SK_ARRAY_COUNT(c2), false, false }, |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1531 | }; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1532 | |
| 1533 | const size_t testCount = SK_ARRAY_COUNT(tests); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1534 | size_t index; |
| 1535 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1536 | SkPath path; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1537 | path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); |
| 1538 | for (index = 1; index < tests[testIndex].fPointCount; ++index) { |
| 1539 | path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1540 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1541 | if (tests[testIndex].fClose) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1542 | path.close(); |
| 1543 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1544 | REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL)); |
| 1545 | REPORTER_ASSERT(reporter, tests[testIndex].fIsRect == path.isRect(NULL, NULL)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1546 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1547 | if (tests[testIndex].fIsRect) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1548 | SkRect computed, expected; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1549 | expected.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1550 | REPORTER_ASSERT(reporter, path.isRect(&computed)); |
| 1551 | REPORTER_ASSERT(reporter, expected == computed); |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 1552 | |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1553 | bool isClosed; |
| 1554 | SkPath::Direction direction, cheapDirection; |
| 1555 | REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection)); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1556 | REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction)); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1557 | REPORTER_ASSERT(reporter, isClosed == tests[testIndex].fClose); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1558 | REPORTER_ASSERT(reporter, direction == cheapDirection); |
| 1559 | } else { |
| 1560 | SkRect computed; |
| 1561 | computed.set(123, 456, 789, 1011); |
| 1562 | REPORTER_ASSERT(reporter, !path.isRect(&computed)); |
| 1563 | REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456); |
| 1564 | REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011); |
| 1565 | |
| 1566 | bool isClosed = (bool) -1; |
| 1567 | SkPath::Direction direction = (SkPath::Direction) -1; |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1568 | REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1569 | REPORTER_ASSERT(reporter, isClosed == (bool) -1); |
| 1570 | REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1571 | } |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1572 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1573 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1574 | // fail, close then line |
| 1575 | SkPath path1; |
| 1576 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1577 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1578 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1579 | } |
| 1580 | path1.close(); |
| 1581 | path1.lineTo(1, 0); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1582 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1583 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1584 | // fail, move in the middle |
| 1585 | path1.reset(); |
| 1586 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1587 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1588 | if (index == 2) { |
| 1589 | path1.moveTo(1, .5f); |
| 1590 | } |
| 1591 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1592 | } |
| 1593 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1594 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1595 | |
| 1596 | // fail, move on the edge |
| 1597 | path1.reset(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1598 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1599 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1600 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1601 | } |
| 1602 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1603 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1604 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1605 | // fail, quad |
| 1606 | path1.reset(); |
| 1607 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1608 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1609 | if (index == 2) { |
| 1610 | path1.quadTo(1, .5f, 1, .5f); |
| 1611 | } |
| 1612 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1613 | } |
| 1614 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1615 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1616 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1617 | // fail, cubic |
| 1618 | path1.reset(); |
| 1619 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1620 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1621 | if (index == 2) { |
| 1622 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 1623 | } |
| 1624 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1625 | } |
| 1626 | path1.close(); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1627 | REPORTER_ASSERT(reporter, !path1.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1630 | static void test_isNestedRects(skiatest::Reporter* reporter) { |
| 1631 | // passing tests (all moveTo / lineTo... |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1632 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1633 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1634 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1635 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1636 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1637 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1638 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1639 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1640 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1641 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, {1, 0}, {.5f, 0}}; // CCW |
| 1642 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, {0, 1}, {0, .5f}}; // CW |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1643 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW |
| 1644 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW |
| 1645 | SkPoint re[] = {{0, 0}, {1, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1646 | |
| 1647 | // failing tests |
| 1648 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1649 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1650 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1651 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1652 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1653 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1654 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1655 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
| 1656 | |
| 1657 | // failing, no close |
| 1658 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 1659 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 1660 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1661 | struct IsNestedRectTest { |
| 1662 | SkPoint *fPoints; |
| 1663 | size_t fPointCount; |
| 1664 | SkPath::Direction fDirection; |
| 1665 | bool fClose; |
| 1666 | bool fIsNestedRect; // nests with path.addRect(-1, -1, 2, 2); |
| 1667 | } tests[] = { |
| 1668 | { r1, SK_ARRAY_COUNT(r1), SkPath::kCW_Direction , true, true }, |
| 1669 | { r2, SK_ARRAY_COUNT(r2), SkPath::kCW_Direction , true, true }, |
| 1670 | { r3, SK_ARRAY_COUNT(r3), SkPath::kCW_Direction , true, true }, |
| 1671 | { r4, SK_ARRAY_COUNT(r4), SkPath::kCW_Direction , true, true }, |
| 1672 | { r5, SK_ARRAY_COUNT(r5), SkPath::kCCW_Direction, true, true }, |
| 1673 | { r6, SK_ARRAY_COUNT(r6), SkPath::kCCW_Direction, true, true }, |
| 1674 | { r7, SK_ARRAY_COUNT(r7), SkPath::kCCW_Direction, true, true }, |
| 1675 | { r8, SK_ARRAY_COUNT(r8), SkPath::kCCW_Direction, true, true }, |
| 1676 | { r9, SK_ARRAY_COUNT(r9), SkPath::kCCW_Direction, true, true }, |
| 1677 | { ra, SK_ARRAY_COUNT(ra), SkPath::kCCW_Direction, true, true }, |
| 1678 | { rb, SK_ARRAY_COUNT(rb), SkPath::kCW_Direction, true, true }, |
| 1679 | { rc, SK_ARRAY_COUNT(rc), SkPath::kCW_Direction, true, true }, |
| 1680 | { rd, SK_ARRAY_COUNT(rd), SkPath::kCCW_Direction, true, true }, |
| 1681 | { re, SK_ARRAY_COUNT(re), SkPath::kCW_Direction, true, true }, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1682 | |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1683 | { f1, SK_ARRAY_COUNT(f1), SkPath::kUnknown_Direction, true, false }, |
| 1684 | { f2, SK_ARRAY_COUNT(f2), SkPath::kUnknown_Direction, true, false }, |
| 1685 | { f3, SK_ARRAY_COUNT(f3), SkPath::kUnknown_Direction, true, false }, |
| 1686 | { f4, SK_ARRAY_COUNT(f4), SkPath::kUnknown_Direction, true, false }, |
| 1687 | { f5, SK_ARRAY_COUNT(f5), SkPath::kUnknown_Direction, true, false }, |
| 1688 | { f6, SK_ARRAY_COUNT(f6), SkPath::kUnknown_Direction, true, false }, |
| 1689 | { f7, SK_ARRAY_COUNT(f7), SkPath::kUnknown_Direction, true, false }, |
| 1690 | { f8, SK_ARRAY_COUNT(f8), SkPath::kUnknown_Direction, true, false }, |
| 1691 | |
| 1692 | { c1, SK_ARRAY_COUNT(c1), SkPath::kUnknown_Direction, false, false }, |
| 1693 | { c2, SK_ARRAY_COUNT(c2), SkPath::kUnknown_Direction, false, false }, |
| 1694 | }; |
| 1695 | |
| 1696 | const size_t testCount = SK_ARRAY_COUNT(tests); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1697 | size_t index; |
| 1698 | for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1699 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1700 | SkPath path; |
| 1701 | if (rectFirst) { |
| 1702 | path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1703 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1704 | path.moveTo(tests[testIndex].fPoints[0].fX, tests[testIndex].fPoints[0].fY); |
| 1705 | for (index = 1; index < tests[testIndex].fPointCount; ++index) { |
| 1706 | path.lineTo(tests[testIndex].fPoints[index].fX, tests[testIndex].fPoints[index].fY); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1707 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1708 | if (tests[testIndex].fClose) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1709 | path.close(); |
| 1710 | } |
| 1711 | if (!rectFirst) { |
| 1712 | path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1713 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1714 | REPORTER_ASSERT(reporter, tests[testIndex].fIsNestedRect == path.isNestedRects(NULL)); |
| 1715 | if (tests[testIndex].fIsNestedRect) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1716 | SkRect expected[2], computed[2]; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1717 | SkPath::Direction expectedDirs[2], computedDirs[2]; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1718 | SkRect testBounds; |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1719 | testBounds.set(tests[testIndex].fPoints, tests[testIndex].fPointCount); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1720 | expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2); |
| 1721 | expected[1] = testBounds; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1722 | if (rectFirst) { |
| 1723 | expectedDirs[0] = SkPath::kCW_Direction; |
| 1724 | } else { |
| 1725 | expectedDirs[0] = SkPath::kCCW_Direction; |
| 1726 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1727 | expectedDirs[1] = tests[testIndex].fDirection; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1728 | REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1729 | REPORTER_ASSERT(reporter, expected[0] == computed[0]); |
| 1730 | REPORTER_ASSERT(reporter, expected[1] == computed[1]); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1731 | REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]); |
| 1732 | REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1733 | } |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | // fail, close then line |
| 1737 | SkPath path1; |
| 1738 | if (rectFirst) { |
| 1739 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1740 | } |
| 1741 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1742 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1743 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1744 | } |
| 1745 | path1.close(); |
| 1746 | path1.lineTo(1, 0); |
| 1747 | if (!rectFirst) { |
| 1748 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1749 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1750 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1751 | |
| 1752 | // fail, move in the middle |
| 1753 | path1.reset(); |
| 1754 | if (rectFirst) { |
| 1755 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1756 | } |
| 1757 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1758 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1759 | if (index == 2) { |
| 1760 | path1.moveTo(1, .5f); |
| 1761 | } |
| 1762 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1763 | } |
| 1764 | path1.close(); |
| 1765 | if (!rectFirst) { |
| 1766 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1767 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1768 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1769 | |
| 1770 | // fail, move on the edge |
| 1771 | path1.reset(); |
| 1772 | if (rectFirst) { |
| 1773 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1774 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1775 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1776 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1777 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1778 | } |
| 1779 | path1.close(); |
| 1780 | if (!rectFirst) { |
| 1781 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1782 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1783 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1784 | |
| 1785 | // fail, quad |
| 1786 | path1.reset(); |
| 1787 | if (rectFirst) { |
| 1788 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1789 | } |
| 1790 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1791 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1792 | if (index == 2) { |
| 1793 | path1.quadTo(1, .5f, 1, .5f); |
| 1794 | } |
| 1795 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1796 | } |
| 1797 | path1.close(); |
| 1798 | if (!rectFirst) { |
| 1799 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1800 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1801 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1802 | |
| 1803 | // fail, cubic |
| 1804 | path1.reset(); |
| 1805 | if (rectFirst) { |
| 1806 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1807 | } |
| 1808 | path1.moveTo(r1[0].fX, r1[0].fY); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1809 | for (index = 1; index < SK_ARRAY_COUNT(r1); ++index) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1810 | if (index == 2) { |
| 1811 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 1812 | } |
| 1813 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1814 | } |
| 1815 | path1.close(); |
| 1816 | if (!rectFirst) { |
| 1817 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1818 | } |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1819 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 1820 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1821 | // fail, not nested |
| 1822 | path1.reset(); |
| 1823 | path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction); |
| 1824 | path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1825 | REPORTER_ASSERT(reporter, !path1.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1826 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1827 | |
| 1828 | // pass, stroke rect |
| 1829 | SkPath src, dst; |
| 1830 | src.addRect(1, 1, 7, 7, SkPath::kCW_Direction); |
| 1831 | SkPaint strokePaint; |
| 1832 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 1833 | strokePaint.setStrokeWidth(2); |
| 1834 | strokePaint.getFillPath(src, &dst); |
bungeman@google.com | b8d9d5b | 2013-09-25 18:21:39 +0000 | [diff] [blame] | 1835 | REPORTER_ASSERT(reporter, dst.isNestedRects(NULL)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1838 | static void write_and_read_back(skiatest::Reporter* reporter, |
| 1839 | const SkPath& p) { |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 1840 | SkWriter32 writer; |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1841 | writer.writePath(p); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1842 | size_t size = writer.bytesWritten(); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1843 | SkAutoMalloc storage(size); |
| 1844 | writer.flatten(storage.get()); |
| 1845 | SkReader32 reader(storage.get(), size); |
| 1846 | |
| 1847 | SkPath readBack; |
| 1848 | REPORTER_ASSERT(reporter, readBack != p); |
| 1849 | reader.readPath(&readBack); |
| 1850 | REPORTER_ASSERT(reporter, readBack == p); |
| 1851 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1852 | REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() == |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1853 | p.getConvexityOrUnknown()); |
| 1854 | |
| 1855 | REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL)); |
| 1856 | |
| 1857 | const SkRect& origBounds = p.getBounds(); |
| 1858 | const SkRect& readBackBounds = readBack.getBounds(); |
| 1859 | |
| 1860 | REPORTER_ASSERT(reporter, origBounds == readBackBounds); |
| 1861 | } |
| 1862 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1863 | static void test_flattening(skiatest::Reporter* reporter) { |
| 1864 | SkPath p; |
| 1865 | |
| 1866 | static const SkPoint pts[] = { |
| 1867 | { 0, 0 }, |
| 1868 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 1869 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 1870 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 1871 | }; |
| 1872 | p.moveTo(pts[0]); |
| 1873 | p.lineTo(pts[1]); |
| 1874 | p.quadTo(pts[2], pts[3]); |
| 1875 | p.cubicTo(pts[4], pts[5], pts[6]); |
| 1876 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1877 | write_and_read_back(reporter, p); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1878 | |
| 1879 | // create a buffer that should be much larger than the path so we don't |
| 1880 | // kill our stack if writer goes too far. |
| 1881 | char buffer[1024]; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 1882 | size_t size1 = p.writeToMemory(NULL); |
| 1883 | size_t size2 = p.writeToMemory(buffer); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1884 | REPORTER_ASSERT(reporter, size1 == size2); |
| 1885 | |
| 1886 | SkPath p2; |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 1887 | size_t size3 = p2.readFromMemory(buffer, 1024); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1888 | REPORTER_ASSERT(reporter, size1 == size3); |
| 1889 | REPORTER_ASSERT(reporter, p == p2); |
| 1890 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1891 | size3 = p2.readFromMemory(buffer, 0); |
| 1892 | REPORTER_ASSERT(reporter, !size3); |
| 1893 | |
| 1894 | SkPath tooShort; |
| 1895 | size3 = tooShort.readFromMemory(buffer, size1 - 1); |
| 1896 | REPORTER_ASSERT(reporter, tooShort.isEmpty()); |
| 1897 | |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1898 | char buffer2[1024]; |
| 1899 | size3 = p2.writeToMemory(buffer2); |
| 1900 | REPORTER_ASSERT(reporter, size1 == size3); |
| 1901 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1902 | |
| 1903 | // test persistence of the oval flag & convexity |
| 1904 | { |
| 1905 | SkPath oval; |
| 1906 | SkRect rect = SkRect::MakeWH(10, 10); |
| 1907 | oval.addOval(rect); |
| 1908 | |
| 1909 | write_and_read_back(reporter, oval); |
| 1910 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | static void test_transform(skiatest::Reporter* reporter) { |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1914 | SkPath p; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1915 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1916 | #define CONIC_PERSPECTIVE_BUG_FIXED 0 |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1917 | static const SkPoint pts[] = { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1918 | { 0, 0 }, // move |
| 1919 | { SkIntToScalar(10), SkIntToScalar(10) }, // line |
| 1920 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, // quad |
| 1921 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) }, // cubic |
| 1922 | #if CONIC_PERSPECTIVE_BUG_FIXED |
| 1923 | { 0, 0 }, { SkIntToScalar(20), SkIntToScalar(10) }, // conic |
| 1924 | #endif |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1925 | }; |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1926 | const int kPtCount = SK_ARRAY_COUNT(pts); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1927 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1928 | p.moveTo(pts[0]); |
| 1929 | p.lineTo(pts[1]); |
| 1930 | p.quadTo(pts[2], pts[3]); |
| 1931 | p.cubicTo(pts[4], pts[5], pts[6]); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1932 | #if CONIC_PERSPECTIVE_BUG_FIXED |
| 1933 | p.conicTo(pts[4], pts[5], 0.5f); |
| 1934 | #endif |
| 1935 | p.close(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1936 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1937 | { |
| 1938 | SkMatrix matrix; |
| 1939 | matrix.reset(); |
| 1940 | SkPath p1; |
| 1941 | p.transform(matrix, &p1); |
| 1942 | REPORTER_ASSERT(reporter, p == p1); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1943 | } |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1944 | |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1945 | |
| 1946 | { |
| 1947 | SkMatrix matrix; |
| 1948 | matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3); |
| 1949 | |
| 1950 | SkPath p1; // Leave p1 non-unique (i.e., the empty path) |
| 1951 | |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 1952 | p.transform(matrix, &p1); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1953 | SkPoint pts1[kPtCount]; |
skia.committer@gmail.com | 6e515d6 | 2013-12-04 07:02:26 +0000 | [diff] [blame] | 1954 | int count = p1.getPoints(pts1, kPtCount); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1955 | REPORTER_ASSERT(reporter, kPtCount == count); |
| 1956 | for (int i = 0; i < count; ++i) { |
| 1957 | SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3); |
| 1958 | REPORTER_ASSERT(reporter, newPt == pts1[i]); |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | { |
| 1963 | SkMatrix matrix; |
| 1964 | matrix.reset(); |
| 1965 | matrix.setPerspX(SkScalarToPersp(4)); |
| 1966 | |
| 1967 | SkPath p1; |
| 1968 | p1.moveTo(SkPoint::Make(0, 0)); |
| 1969 | |
| 1970 | p.transform(matrix, &p1); |
| 1971 | REPORTER_ASSERT(reporter, matrix.invert(&matrix)); |
| 1972 | p1.transform(matrix, NULL); |
| 1973 | SkRect pBounds = p.getBounds(); |
| 1974 | SkRect p1Bounds = p1.getBounds(); |
| 1975 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fLeft, p1Bounds.fLeft)); |
| 1976 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fTop, p1Bounds.fTop)); |
| 1977 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fRight, p1Bounds.fRight)); |
| 1978 | REPORTER_ASSERT(reporter, SkScalarNearlyEqual(pBounds.fBottom, p1Bounds.fBottom)); |
| 1979 | } |
| 1980 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 1981 | p.reset(); |
| 1982 | p.addCircle(0, 0, 1, SkPath::kCW_Direction); |
robertphillips@google.com | b06e88d | 2013-12-03 17:15:36 +0000 | [diff] [blame] | 1983 | |
| 1984 | { |
| 1985 | SkMatrix matrix; |
| 1986 | matrix.reset(); |
| 1987 | SkPath p1; |
| 1988 | p1.moveTo(SkPoint::Make(0, 0)); |
| 1989 | |
| 1990 | p.transform(matrix, &p1); |
| 1991 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCW_Direction)); |
| 1992 | } |
| 1993 | |
| 1994 | |
| 1995 | { |
| 1996 | SkMatrix matrix; |
| 1997 | matrix.reset(); |
| 1998 | matrix.setScaleX(-1); |
| 1999 | SkPath p1; |
| 2000 | p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) |
| 2001 | |
| 2002 | p.transform(matrix, &p1); |
| 2003 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kCCW_Direction)); |
| 2004 | } |
| 2005 | |
| 2006 | { |
| 2007 | SkMatrix matrix; |
| 2008 | matrix.setAll(1, 1, 0, 1, 1, 0, 0, 0, 1); |
| 2009 | SkPath p1; |
| 2010 | p1.moveTo(SkPoint::Make(0, 0)); // Make p1 unique (i.e., not empty path) |
| 2011 | |
| 2012 | p.transform(matrix, &p1); |
| 2013 | REPORTER_ASSERT(reporter, p1.cheapIsDirection(SkPath::kUnknown_Direction)); |
| 2014 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2017 | static void test_zero_length_paths(skiatest::Reporter* reporter) { |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2018 | SkPath p; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2019 | uint8_t verbs[32]; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2020 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2021 | struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2022 | const char* testPath; |
| 2023 | const size_t numResultPts; |
| 2024 | const SkRect resultBound; |
| 2025 | const SkPath::Verb* resultVerbs; |
| 2026 | const size_t numResultVerbs; |
| 2027 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2028 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2029 | static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb }; |
| 2030 | static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb }; |
| 2031 | static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 2032 | static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 2033 | static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 2034 | static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 2035 | static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb }; |
| 2036 | static const SkPath::Verb resultVerbs8[] = { |
| 2037 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb |
| 2038 | }; |
| 2039 | static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 2040 | static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 2041 | static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb }; |
| 2042 | static const SkPath::Verb resultVerbs12[] = { |
| 2043 | SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb |
| 2044 | }; |
| 2045 | static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 2046 | static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 2047 | static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb }; |
| 2048 | static const SkPath::Verb resultVerbs16[] = { |
| 2049 | SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb |
| 2050 | }; |
| 2051 | static const struct zeroPathTestData gZeroLengthTests[] = { |
| 2052 | { "M 1 1", 1, {0, 0, 0, 0}, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2053 | { "M 1 1 M 2 1", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2054 | { "M 1 1 z", 1, {0, 0, 0, 0}, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2055 | { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 2056 | { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }, |
| 2057 | { "M 1 1 L 1 1 M 2 1 L 2 1", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs6, SK_ARRAY_COUNT(resultVerbs6) }, |
| 2058 | { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) }, |
| 2059 | { "M 1 1 L 1 1 z M 2 1 L 2 1 z", 4, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs8, SK_ARRAY_COUNT(resultVerbs8) }, |
| 2060 | { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) }, |
| 2061 | { "M 1 1 Q 1 1 1 1 M 2 1 Q 2 1 2 1", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs10, SK_ARRAY_COUNT(resultVerbs10) }, |
| 2062 | { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) }, |
| 2063 | { "M 1 1 Q 1 1 1 1 z M 2 1 Q 2 1 2 1 z", 6, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs12, SK_ARRAY_COUNT(resultVerbs12) }, |
| 2064 | { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) }, |
| 2065 | { "M 1 1 C 1 1 1 1 1 1 M 2 1 C 2 1 2 1 2 1", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs14, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2066 | SK_ARRAY_COUNT(resultVerbs14) |
| 2067 | }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2068 | { "M 1 1 C 1 1 1 1 1 1 z", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs15, SK_ARRAY_COUNT(resultVerbs15) }, |
| 2069 | { "M 1 1 C 1 1 1 1 1 1 z M 2 1 C 2 1 2 1 2 1 z", 8, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs16, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2070 | SK_ARRAY_COUNT(resultVerbs16) |
| 2071 | } |
| 2072 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2073 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2074 | for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) { |
| 2075 | p.reset(); |
| 2076 | bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p); |
| 2077 | REPORTER_ASSERT(reporter, valid); |
| 2078 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 2079 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints()); |
| 2080 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds()); |
| 2081 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs))); |
| 2082 | for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) { |
| 2083 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]); |
| 2084 | } |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2085 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | struct SegmentInfo { |
| 2089 | SkPath fPath; |
| 2090 | int fPointCount; |
| 2091 | }; |
| 2092 | |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2093 | #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask) |
| 2094 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2095 | static void test_segment_masks(skiatest::Reporter* reporter) { |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2096 | SkPath p, p2; |
| 2097 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2098 | p.moveTo(0, 0); |
| 2099 | p.quadTo(100, 100, 200, 200); |
| 2100 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); |
| 2101 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2102 | p2 = p; |
| 2103 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2104 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 2105 | REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); |
| 2106 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2107 | p2 = p; |
| 2108 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
| 2109 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2110 | p.reset(); |
| 2111 | p.moveTo(0, 0); |
| 2112 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 2113 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 2114 | p2 = p; |
| 2115 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2116 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2117 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 2118 | } |
| 2119 | |
| 2120 | static void test_iter(skiatest::Reporter* reporter) { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2121 | SkPath p; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2122 | SkPoint pts[4]; |
| 2123 | |
| 2124 | // Test an iterator with no path |
| 2125 | SkPath::Iter noPathIter; |
| 2126 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2127 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2128 | // Test that setting an empty path works |
| 2129 | noPathIter.setPath(p, false); |
| 2130 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2131 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2132 | // Test that close path makes no difference for an empty path |
| 2133 | noPathIter.setPath(p, true); |
| 2134 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2135 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2136 | // Test an iterator with an initial empty path |
| 2137 | SkPath::Iter iter(p, false); |
| 2138 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2139 | |
| 2140 | // Test that close path makes no difference |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2141 | iter.setPath(p, true); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2142 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2143 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2144 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2145 | struct iterTestData { |
| 2146 | const char* testPath; |
| 2147 | const bool forceClose; |
| 2148 | const bool consumeDegenerates; |
| 2149 | const size_t* numResultPtsPerVerb; |
| 2150 | const SkPoint* resultPts; |
| 2151 | const SkPath::Verb* resultVerbs; |
| 2152 | const size_t numResultVerbs; |
| 2153 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2154 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2155 | static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb }; |
| 2156 | static const SkPath::Verb resultVerbs2[] = { |
| 2157 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb |
| 2158 | }; |
| 2159 | static const SkPath::Verb resultVerbs3[] = { |
| 2160 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2161 | }; |
| 2162 | static const SkPath::Verb resultVerbs4[] = { |
| 2163 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2164 | }; |
| 2165 | static const SkPath::Verb resultVerbs5[] = { |
| 2166 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 2167 | }; |
| 2168 | static const size_t resultPtsSizes1[] = { 0 }; |
schenney@chromium.org | fedd09b | 2012-06-13 18:29:20 +0000 | [diff] [blame] | 2169 | static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 }; |
| 2170 | static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 }; |
| 2171 | static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 }; |
| 2172 | static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 }; |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2173 | static const SkPoint* resultPts1 = 0; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2174 | static const SkPoint resultPts2[] = { |
| 2175 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 } |
| 2176 | }; |
| 2177 | static const SkPoint resultPts3[] = { |
| 2178 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }, |
| 2179 | { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 } |
| 2180 | }; |
| 2181 | static const SkPoint resultPts4[] = { |
| 2182 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 2183 | }; |
| 2184 | static const SkPoint resultPts5[] = { |
| 2185 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 2186 | }; |
| 2187 | static const struct iterTestData gIterTests[] = { |
| 2188 | { "M 1 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2189 | { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2190 | { "M 1 0 M 1 0 M 3 0 M 4 0 M 5 0", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2191 | { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2192 | { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2193 | { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2194 | { "z M 1 0 z z M 2 0 z M 3 0 M 4 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 2195 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
| 2196 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
| 2197 | { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2198 | { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 2199 | { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 2200 | { "M 1 0 L 1 0 M 0 0 z", true, false, resultPtsSizes5, resultPts5, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) } |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2201 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2202 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 2203 | for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) { |
| 2204 | p.reset(); |
| 2205 | bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p); |
| 2206 | REPORTER_ASSERT(reporter, valid); |
| 2207 | iter.setPath(p, gIterTests[i].forceClose); |
| 2208 | int j = 0, l = 0; |
| 2209 | do { |
| 2210 | REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]); |
| 2211 | for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) { |
| 2212 | REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]); |
| 2213 | } |
| 2214 | } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb); |
| 2215 | REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs); |
| 2216 | } |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2217 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2218 | p.reset(); |
| 2219 | iter.setPath(p, false); |
| 2220 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2221 | p.lineTo(1, 1); |
| 2222 | p.close(); |
| 2223 | iter.setPath(p, false); |
| 2224 | REPORTER_ASSERT(reporter, iter.isClosedContour()); |
| 2225 | p.reset(); |
| 2226 | iter.setPath(p, true); |
| 2227 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2228 | p.lineTo(1, 1); |
| 2229 | iter.setPath(p, true); |
| 2230 | REPORTER_ASSERT(reporter, iter.isClosedContour()); |
| 2231 | p.moveTo(0, 0); |
| 2232 | p.lineTo(2, 2); |
| 2233 | iter.setPath(p, false); |
| 2234 | REPORTER_ASSERT(reporter, !iter.isClosedContour()); |
| 2235 | |
| 2236 | // this checks to see if the NaN logic is executed in SkPath::autoClose(), but does not |
| 2237 | // check to see if the result is correct. |
| 2238 | for (int setNaN = 0; setNaN < 4; ++setNaN) { |
| 2239 | p.reset(); |
| 2240 | p.moveTo(setNaN == 0 ? SK_ScalarNaN : 0, setNaN == 1 ? SK_ScalarNaN : 0); |
| 2241 | p.lineTo(setNaN == 2 ? SK_ScalarNaN : 1, setNaN == 3 ? SK_ScalarNaN : 1); |
| 2242 | iter.setPath(p, true); |
| 2243 | iter.next(pts, false); |
| 2244 | iter.next(pts, false); |
| 2245 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == iter.next(pts, false)); |
| 2246 | } |
| 2247 | |
| 2248 | p.reset(); |
| 2249 | p.quadTo(0, 0, 0, 0); |
| 2250 | iter.setPath(p, false); |
| 2251 | iter.next(pts, false); |
| 2252 | REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == iter.next(pts, false)); |
| 2253 | iter.setPath(p, false); |
| 2254 | iter.next(pts, false); |
| 2255 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2256 | |
| 2257 | p.reset(); |
| 2258 | p.conicTo(0, 0, 0, 0, 0.5f); |
| 2259 | iter.setPath(p, false); |
| 2260 | iter.next(pts, false); |
| 2261 | REPORTER_ASSERT(reporter, SkPath::kConic_Verb == iter.next(pts, false)); |
| 2262 | iter.setPath(p, false); |
| 2263 | iter.next(pts, false); |
| 2264 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2265 | |
| 2266 | p.reset(); |
| 2267 | p.cubicTo(0, 0, 0, 0, 0, 0); |
| 2268 | iter.setPath(p, false); |
| 2269 | iter.next(pts, false); |
| 2270 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); |
| 2271 | iter.setPath(p, false); |
| 2272 | iter.next(pts, false); |
| 2273 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2274 | |
| 2275 | p.moveTo(1, 1); // add a trailing moveto |
| 2276 | iter.setPath(p, false); |
| 2277 | iter.next(pts, false); |
| 2278 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == iter.next(pts, false)); |
| 2279 | iter.setPath(p, false); |
| 2280 | iter.next(pts, false); |
| 2281 | REPORTER_ASSERT(reporter, SkPath::kDone_Verb == iter.next(pts, true)); |
| 2282 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2283 | // The GM degeneratesegments.cpp test is more extensive |
| 2284 | } |
| 2285 | |
| 2286 | static void test_raw_iter(skiatest::Reporter* reporter) { |
| 2287 | SkPath p; |
| 2288 | SkPoint pts[4]; |
| 2289 | |
| 2290 | // Test an iterator with no path |
| 2291 | SkPath::RawIter noPathIter; |
| 2292 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
| 2293 | // Test that setting an empty path works |
| 2294 | noPathIter.setPath(p); |
| 2295 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2296 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2297 | // Test an iterator with an initial empty path |
| 2298 | SkPath::RawIter iter(p); |
| 2299 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2300 | |
| 2301 | // Test that a move-only path returns the move. |
| 2302 | p.moveTo(SK_Scalar1, 0); |
| 2303 | iter.setPath(p); |
| 2304 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2305 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2306 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2307 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2308 | |
| 2309 | // No matter how many moves we add, we should get them all back |
| 2310 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 2311 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 2312 | iter.setPath(p); |
| 2313 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2314 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2315 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2316 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2317 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2318 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2319 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2320 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 2321 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 2322 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2323 | |
| 2324 | // Initial close is never ever stored |
| 2325 | p.reset(); |
| 2326 | p.close(); |
| 2327 | iter.setPath(p); |
| 2328 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2329 | |
| 2330 | // Move/close sequences |
| 2331 | p.reset(); |
| 2332 | p.close(); // Not stored, no purpose |
| 2333 | p.moveTo(SK_Scalar1, 0); |
| 2334 | p.close(); |
| 2335 | p.close(); // Not stored, no purpose |
| 2336 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 2337 | p.close(); |
| 2338 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 2339 | p.moveTo(SK_Scalar1*4, SK_Scalar1*3); |
| 2340 | p.close(); |
| 2341 | iter.setPath(p); |
| 2342 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2343 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2344 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2345 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2346 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2347 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2348 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2349 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2350 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2351 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2352 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2353 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2354 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2355 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 2356 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 2357 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2358 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2359 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2360 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2361 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2362 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2363 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2364 | |
| 2365 | // Generate random paths and verify |
| 2366 | SkPoint randomPts[25]; |
| 2367 | for (int i = 0; i < 5; ++i) { |
| 2368 | for (int j = 0; j < 5; ++j) { |
| 2369 | randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j); |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | // Max of 10 segments, max 3 points per segment |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 2374 | SkRandom rand(9876543); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2375 | SkPoint expectedPts[31]; // May have leading moveTo |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2376 | SkPath::Verb expectedVerbs[22]; // May have leading moveTo |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2377 | SkPath::Verb nextVerb; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2378 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2379 | for (int i = 0; i < 500; ++i) { |
| 2380 | p.reset(); |
| 2381 | bool lastWasClose = true; |
| 2382 | bool haveMoveTo = false; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2383 | SkPoint lastMoveToPt = { 0, 0 }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2384 | int numPoints = 0; |
| 2385 | int numVerbs = (rand.nextU() >> 16) % 10; |
| 2386 | int numIterVerbs = 0; |
| 2387 | for (int j = 0; j < numVerbs; ++j) { |
| 2388 | do { |
| 2389 | nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb); |
| 2390 | } while (lastWasClose && nextVerb == SkPath::kClose_Verb); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2391 | switch (nextVerb) { |
| 2392 | case SkPath::kMove_Verb: |
| 2393 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2394 | p.moveTo(expectedPts[numPoints]); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2395 | lastMoveToPt = expectedPts[numPoints]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2396 | numPoints += 1; |
| 2397 | lastWasClose = false; |
| 2398 | haveMoveTo = true; |
| 2399 | break; |
| 2400 | case SkPath::kLine_Verb: |
| 2401 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2402 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2403 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2404 | haveMoveTo = true; |
| 2405 | } |
| 2406 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2407 | p.lineTo(expectedPts[numPoints]); |
| 2408 | numPoints += 1; |
| 2409 | lastWasClose = false; |
| 2410 | break; |
| 2411 | case SkPath::kQuad_Verb: |
| 2412 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2413 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2414 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2415 | haveMoveTo = true; |
| 2416 | } |
| 2417 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2418 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2419 | p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]); |
| 2420 | numPoints += 2; |
| 2421 | lastWasClose = false; |
| 2422 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2423 | case SkPath::kConic_Verb: |
| 2424 | if (!haveMoveTo) { |
| 2425 | expectedPts[numPoints++] = lastMoveToPt; |
| 2426 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2427 | haveMoveTo = true; |
| 2428 | } |
| 2429 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2430 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2431 | p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2432 | rand.nextUScalar1() * 4); |
| 2433 | numPoints += 2; |
| 2434 | lastWasClose = false; |
| 2435 | break; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2436 | case SkPath::kCubic_Verb: |
| 2437 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2438 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2439 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2440 | haveMoveTo = true; |
| 2441 | } |
| 2442 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2443 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2444 | expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2445 | p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2446 | expectedPts[numPoints + 2]); |
| 2447 | numPoints += 3; |
| 2448 | lastWasClose = false; |
| 2449 | break; |
| 2450 | case SkPath::kClose_Verb: |
| 2451 | p.close(); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2452 | haveMoveTo = false; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2453 | lastWasClose = true; |
| 2454 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2455 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2456 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2457 | } |
| 2458 | expectedVerbs[numIterVerbs++] = nextVerb; |
| 2459 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2460 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2461 | iter.setPath(p); |
| 2462 | numVerbs = numIterVerbs; |
| 2463 | numIterVerbs = 0; |
| 2464 | int numIterPts = 0; |
| 2465 | SkPoint lastMoveTo; |
| 2466 | SkPoint lastPt; |
| 2467 | lastMoveTo.set(0, 0); |
| 2468 | lastPt.set(0, 0); |
| 2469 | while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2470 | REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]); |
| 2471 | numIterVerbs++; |
| 2472 | switch (nextVerb) { |
| 2473 | case SkPath::kMove_Verb: |
| 2474 | REPORTER_ASSERT(reporter, numIterPts < numPoints); |
| 2475 | REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]); |
| 2476 | lastPt = lastMoveTo = pts[0]; |
| 2477 | numIterPts += 1; |
| 2478 | break; |
| 2479 | case SkPath::kLine_Verb: |
| 2480 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 1); |
| 2481 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2482 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2483 | lastPt = pts[1]; |
| 2484 | numIterPts += 1; |
| 2485 | break; |
| 2486 | case SkPath::kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2487 | case SkPath::kConic_Verb: |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2488 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 2); |
| 2489 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2490 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2491 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2492 | lastPt = pts[2]; |
| 2493 | numIterPts += 2; |
| 2494 | break; |
| 2495 | case SkPath::kCubic_Verb: |
| 2496 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 3); |
| 2497 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2498 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2499 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2500 | REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]); |
| 2501 | lastPt = pts[3]; |
| 2502 | numIterPts += 3; |
| 2503 | break; |
| 2504 | case SkPath::kClose_Verb: |
| 2505 | REPORTER_ASSERT(reporter, pts[0] == lastMoveTo); |
| 2506 | lastPt = lastMoveTo; |
| 2507 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2508 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2509 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | REPORTER_ASSERT(reporter, numIterPts == numPoints); |
| 2513 | REPORTER_ASSERT(reporter, numIterVerbs == numVerbs); |
| 2514 | } |
| 2515 | } |
| 2516 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2517 | static void check_for_circle(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2518 | const SkPath& path, |
| 2519 | bool expectedCircle, |
| 2520 | SkPath::Direction expectedDir) { |
robertphillips@google.com | 466310d | 2013-12-03 16:43:54 +0000 | [diff] [blame] | 2521 | SkRect rect = SkRect::MakeEmpty(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2522 | REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle); |
| 2523 | REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir)); |
skia.committer@gmail.com | fbb0ed9 | 2012-11-13 21:46:06 +0000 | [diff] [blame] | 2524 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2525 | if (expectedCircle) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2526 | REPORTER_ASSERT(reporter, rect.height() == rect.width()); |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | static void test_circle_skew(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2531 | const SkPath& path, |
| 2532 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2533 | SkPath tmp; |
| 2534 | |
| 2535 | SkMatrix m; |
| 2536 | m.setSkew(SkIntToScalar(3), SkIntToScalar(5)); |
| 2537 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2538 | // this matrix reverses the direction. |
| 2539 | if (SkPath::kCCW_Direction == dir) { |
| 2540 | dir = SkPath::kCW_Direction; |
| 2541 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2542 | REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2543 | dir = SkPath::kCCW_Direction; |
| 2544 | } |
| 2545 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | static void test_circle_translate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2549 | const SkPath& path, |
| 2550 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2551 | SkPath tmp; |
| 2552 | |
| 2553 | // translate at small offset |
| 2554 | SkMatrix m; |
| 2555 | m.setTranslate(SkIntToScalar(15), SkIntToScalar(15)); |
| 2556 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2557 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2558 | |
| 2559 | tmp.reset(); |
| 2560 | m.reset(); |
| 2561 | |
| 2562 | // translate at a relatively big offset |
| 2563 | m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000)); |
| 2564 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2565 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | static void test_circle_rotate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2569 | const SkPath& path, |
| 2570 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2571 | for (int angle = 0; angle < 360; ++angle) { |
| 2572 | SkPath tmp; |
| 2573 | SkMatrix m; |
| 2574 | m.setRotate(SkIntToScalar(angle)); |
| 2575 | path.transform(m, &tmp); |
| 2576 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2577 | // TODO: a rotated circle whose rotated angle is not a multiple of 90 |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2578 | // degrees is not an oval anymore, this can be improved. we made this |
| 2579 | // for the simplicity of our implementation. |
| 2580 | if (angle % 90 == 0) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2581 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2582 | } else { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2583 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2588 | static void test_circle_mirror_x(skiatest::Reporter* reporter, |
| 2589 | const SkPath& path, |
| 2590 | SkPath::Direction dir) { |
| 2591 | SkPath tmp; |
| 2592 | SkMatrix m; |
| 2593 | m.reset(); |
| 2594 | m.setScaleX(-SK_Scalar1); |
| 2595 | path.transform(m, &tmp); |
| 2596 | |
| 2597 | if (SkPath::kCW_Direction == dir) { |
| 2598 | dir = SkPath::kCCW_Direction; |
| 2599 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2600 | REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2601 | dir = SkPath::kCW_Direction; |
| 2602 | } |
| 2603 | |
| 2604 | check_for_circle(reporter, tmp, true, dir); |
| 2605 | } |
| 2606 | |
| 2607 | static void test_circle_mirror_y(skiatest::Reporter* reporter, |
| 2608 | const SkPath& path, |
| 2609 | SkPath::Direction dir) { |
| 2610 | SkPath tmp; |
| 2611 | SkMatrix m; |
| 2612 | m.reset(); |
| 2613 | m.setScaleY(-SK_Scalar1); |
| 2614 | path.transform(m, &tmp); |
| 2615 | |
| 2616 | if (SkPath::kCW_Direction == dir) { |
| 2617 | dir = SkPath::kCCW_Direction; |
| 2618 | } else { |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2619 | REPORTER_ASSERT(reporter, SkPath::kCCW_Direction == dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2620 | dir = SkPath::kCW_Direction; |
| 2621 | } |
| 2622 | |
| 2623 | check_for_circle(reporter, tmp, true, dir); |
| 2624 | } |
| 2625 | |
| 2626 | static void test_circle_mirror_xy(skiatest::Reporter* reporter, |
| 2627 | const SkPath& path, |
| 2628 | SkPath::Direction dir) { |
| 2629 | SkPath tmp; |
| 2630 | SkMatrix m; |
| 2631 | m.reset(); |
| 2632 | m.setScaleX(-SK_Scalar1); |
| 2633 | m.setScaleY(-SK_Scalar1); |
| 2634 | path.transform(m, &tmp); |
| 2635 | |
| 2636 | check_for_circle(reporter, tmp, true, dir); |
| 2637 | } |
| 2638 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2639 | static void test_circle_with_direction(skiatest::Reporter* reporter, |
| 2640 | SkPath::Direction dir) { |
| 2641 | SkPath path; |
| 2642 | |
| 2643 | // circle at origin |
| 2644 | path.addCircle(0, 0, SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2645 | check_for_circle(reporter, path, true, dir); |
| 2646 | test_circle_rotate(reporter, path, dir); |
| 2647 | test_circle_translate(reporter, path, dir); |
| 2648 | test_circle_skew(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2649 | |
| 2650 | // circle at an offset at (10, 10) |
| 2651 | path.reset(); |
| 2652 | path.addCircle(SkIntToScalar(10), SkIntToScalar(10), |
| 2653 | SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2654 | check_for_circle(reporter, path, true, dir); |
| 2655 | test_circle_rotate(reporter, path, dir); |
| 2656 | test_circle_translate(reporter, path, dir); |
| 2657 | test_circle_skew(reporter, path, dir); |
| 2658 | test_circle_mirror_x(reporter, path, dir); |
| 2659 | test_circle_mirror_y(reporter, path, dir); |
| 2660 | test_circle_mirror_xy(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | static void test_circle_with_add_paths(skiatest::Reporter* reporter) { |
| 2664 | SkPath path; |
| 2665 | SkPath circle; |
| 2666 | SkPath rect; |
| 2667 | SkPath empty; |
| 2668 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2669 | static const SkPath::Direction kCircleDir = SkPath::kCW_Direction; |
| 2670 | static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction; |
| 2671 | |
| 2672 | circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2673 | rect.addRect(SkIntToScalar(5), SkIntToScalar(5), |
| 2674 | SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction); |
| 2675 | |
| 2676 | SkMatrix translate; |
| 2677 | translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12)); |
| 2678 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 2679 | // Although all the path concatenation related operations leave |
| 2680 | // the path a circle, most mark it as a non-circle for simplicity |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2681 | |
| 2682 | // empty + circle (translate) |
| 2683 | path = empty; |
| 2684 | path.addPath(circle, translate); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2685 | check_for_circle(reporter, path, false, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2686 | |
| 2687 | // circle + empty (translate) |
| 2688 | path = circle; |
| 2689 | path.addPath(empty, translate); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 2690 | check_for_circle(reporter, path, true, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2691 | |
| 2692 | // test reverseAddPath |
| 2693 | path = circle; |
| 2694 | path.reverseAddPath(rect); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2695 | check_for_circle(reporter, path, false, kCircleDirOpposite); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | static void test_circle(skiatest::Reporter* reporter) { |
| 2699 | test_circle_with_direction(reporter, SkPath::kCW_Direction); |
| 2700 | test_circle_with_direction(reporter, SkPath::kCCW_Direction); |
| 2701 | |
| 2702 | // multiple addCircle() |
| 2703 | SkPath path; |
| 2704 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2705 | path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2706 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2707 | |
| 2708 | // some extra lineTo() would make isOval() fail |
| 2709 | path.reset(); |
| 2710 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2711 | path.lineTo(0, 0); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2712 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2713 | |
| 2714 | // not back to the original point |
| 2715 | path.reset(); |
| 2716 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2717 | path.setLastPt(SkIntToScalar(5), SkIntToScalar(5)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2718 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2719 | |
| 2720 | test_circle_with_add_paths(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2721 | |
| 2722 | // test negative radius |
| 2723 | path.reset(); |
| 2724 | path.addCircle(0, 0, -1, SkPath::kCW_Direction); |
| 2725 | REPORTER_ASSERT(reporter, path.isEmpty()); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | static void test_oval(skiatest::Reporter* reporter) { |
| 2729 | SkRect rect; |
| 2730 | SkMatrix m; |
| 2731 | SkPath path; |
| 2732 | |
| 2733 | rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50)); |
| 2734 | path.addOval(rect); |
| 2735 | |
| 2736 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2737 | |
| 2738 | m.setRotate(SkIntToScalar(90)); |
| 2739 | SkPath tmp; |
| 2740 | path.transform(m, &tmp); |
| 2741 | // an oval rotated 90 degrees is still an oval. |
| 2742 | REPORTER_ASSERT(reporter, tmp.isOval(NULL)); |
| 2743 | |
| 2744 | m.reset(); |
| 2745 | m.setRotate(SkIntToScalar(30)); |
| 2746 | tmp.reset(); |
| 2747 | path.transform(m, &tmp); |
| 2748 | // an oval rotated 30 degrees is not an oval anymore. |
| 2749 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2750 | |
| 2751 | // since empty path being transformed. |
| 2752 | path.reset(); |
| 2753 | tmp.reset(); |
| 2754 | m.reset(); |
| 2755 | path.transform(m, &tmp); |
| 2756 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2757 | |
| 2758 | // empty path is not an oval |
| 2759 | tmp.reset(); |
| 2760 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2761 | |
| 2762 | // only has moveTo()s |
| 2763 | tmp.reset(); |
| 2764 | tmp.moveTo(0, 0); |
| 2765 | tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 2766 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2767 | |
| 2768 | // mimic WebKit's calling convention, |
| 2769 | // call moveTo() first and then call addOval() |
| 2770 | path.reset(); |
| 2771 | path.moveTo(0, 0); |
| 2772 | path.addOval(rect); |
| 2773 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2774 | |
| 2775 | // copy path |
| 2776 | path.reset(); |
| 2777 | tmp.reset(); |
| 2778 | tmp.addOval(rect); |
| 2779 | path = tmp; |
| 2780 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2781 | } |
| 2782 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2783 | static void test_empty(skiatest::Reporter* reporter, const SkPath& p) { |
| 2784 | SkPath empty; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2785 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2786 | REPORTER_ASSERT(reporter, p.isEmpty()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2787 | REPORTER_ASSERT(reporter, 0 == p.countPoints()); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2788 | REPORTER_ASSERT(reporter, 0 == p.countVerbs()); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2789 | REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2790 | REPORTER_ASSERT(reporter, p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2791 | REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); |
| 2792 | REPORTER_ASSERT(reporter, !p.isInverseFillType()); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2793 | REPORTER_ASSERT(reporter, p == empty); |
| 2794 | REPORTER_ASSERT(reporter, !(p != empty)); |
| 2795 | } |
| 2796 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2797 | static void test_rrect_is_convex(skiatest::Reporter* reporter, SkPath* path, |
| 2798 | SkPath::Direction dir) { |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2799 | REPORTER_ASSERT(reporter, path->isConvex()); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2800 | REPORTER_ASSERT(reporter, path->cheapIsDirection(dir)); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2801 | path->setConvexity(SkPath::kUnknown_Convexity); |
| 2802 | REPORTER_ASSERT(reporter, path->isConvex()); |
| 2803 | path->reset(); |
| 2804 | } |
| 2805 | |
| 2806 | static void test_rrect(skiatest::Reporter* reporter) { |
| 2807 | SkPath p; |
| 2808 | SkRRect rr; |
| 2809 | SkVector radii[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; |
| 2810 | SkRect r = {10, 20, 30, 40}; |
| 2811 | rr.setRectRadii(r, radii); |
| 2812 | p.addRRect(rr); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2813 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2814 | p.addRRect(rr, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2815 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2816 | p.addRoundRect(r, &radii[0].fX); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2817 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2818 | p.addRoundRect(r, &radii[0].fX, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2819 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2820 | p.addRoundRect(r, radii[1].fX, radii[1].fY); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2821 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2822 | p.addRoundRect(r, radii[1].fX, radii[1].fY, SkPath::kCCW_Direction); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2823 | test_rrect_is_convex(reporter, &p, SkPath::kCCW_Direction); |
| 2824 | for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) { |
| 2825 | SkVector save = radii[i]; |
| 2826 | radii[i].set(0, 0); |
| 2827 | rr.setRectRadii(r, radii); |
| 2828 | p.addRRect(rr); |
| 2829 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 2830 | radii[i] = save; |
| 2831 | } |
| 2832 | p.addRoundRect(r, 0, 0); |
| 2833 | SkRect returnedRect; |
| 2834 | REPORTER_ASSERT(reporter, p.isRect(&returnedRect)); |
| 2835 | REPORTER_ASSERT(reporter, returnedRect == r); |
| 2836 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 2837 | SkVector zeroRadii[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 2838 | rr.setRectRadii(r, zeroRadii); |
| 2839 | p.addRRect(rr); |
| 2840 | bool closed; |
| 2841 | SkPath::Direction dir; |
| 2842 | REPORTER_ASSERT(reporter, p.isRect(&closed, &dir)); |
| 2843 | REPORTER_ASSERT(reporter, closed); |
| 2844 | REPORTER_ASSERT(reporter, SkPath::kCW_Direction == dir); |
| 2845 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 2846 | p.addRRect(rr, SkPath::kCW_Direction); |
| 2847 | p.addRRect(rr, SkPath::kCW_Direction); |
| 2848 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 2849 | p.reset(); |
| 2850 | p.addRRect(rr, SkPath::kCCW_Direction); |
| 2851 | p.addRRect(rr, SkPath::kCCW_Direction); |
| 2852 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 2853 | p.reset(); |
| 2854 | SkRect emptyR = {10, 20, 10, 30}; |
| 2855 | rr.setRectRadii(emptyR, radii); |
| 2856 | p.addRRect(rr); |
| 2857 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 2858 | SkRect largeR = {0, 0, SK_ScalarMax, SK_ScalarMax}; |
| 2859 | rr.setRectRadii(largeR, radii); |
| 2860 | p.addRRect(rr); |
| 2861 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 2862 | SkRect infR = {0, 0, SK_ScalarMax, SK_ScalarInfinity}; |
| 2863 | rr.setRectRadii(infR, radii); |
| 2864 | p.addRRect(rr); |
| 2865 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
| 2866 | SkRect tinyR = {0, 0, 1e-9f, 1e-9f}; |
| 2867 | p.addRoundRect(tinyR, 5e-11f, 5e-11f); |
| 2868 | test_rrect_is_convex(reporter, &p, SkPath::kCW_Direction); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 2871 | static void test_arc(skiatest::Reporter* reporter) { |
| 2872 | SkPath p; |
| 2873 | SkRect emptyOval = {10, 20, 30, 20}; |
| 2874 | REPORTER_ASSERT(reporter, emptyOval.isEmpty()); |
| 2875 | p.addArc(emptyOval, 1, 2); |
| 2876 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 2877 | p.reset(); |
| 2878 | SkRect oval = {10, 20, 30, 40}; |
| 2879 | p.addArc(oval, 1, 0); |
| 2880 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 2881 | p.reset(); |
| 2882 | SkPath cwOval; |
| 2883 | cwOval.addOval(oval); |
| 2884 | p.addArc(oval, 1, 360); |
| 2885 | REPORTER_ASSERT(reporter, p == cwOval); |
| 2886 | p.reset(); |
| 2887 | SkPath ccwOval; |
| 2888 | ccwOval.addOval(oval, SkPath::kCCW_Direction); |
| 2889 | p.addArc(oval, 1, -360); |
| 2890 | REPORTER_ASSERT(reporter, p == ccwOval); |
| 2891 | p.reset(); |
| 2892 | p.addArc(oval, 1, 180); |
| 2893 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 2894 | REPORTER_ASSERT(reporter, p.cheapIsDirection(SkPath::kCW_Direction)); |
| 2895 | p.setConvexity(SkPath::kUnknown_Convexity); |
| 2896 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 2897 | } |
| 2898 | |
| 2899 | static void check_move(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 2900 | SkScalar x0, SkScalar y0) { |
| 2901 | SkPoint pts[4]; |
| 2902 | SkPath::Verb v = iter->next(pts); |
| 2903 | REPORTER_ASSERT(reporter, v == SkPath::kMove_Verb); |
| 2904 | REPORTER_ASSERT(reporter, pts[0].fX == x0); |
| 2905 | REPORTER_ASSERT(reporter, pts[0].fY == y0); |
| 2906 | } |
| 2907 | |
| 2908 | static void check_line(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 2909 | SkScalar x1, SkScalar y1) { |
| 2910 | SkPoint pts[4]; |
| 2911 | SkPath::Verb v = iter->next(pts); |
| 2912 | REPORTER_ASSERT(reporter, v == SkPath::kLine_Verb); |
| 2913 | REPORTER_ASSERT(reporter, pts[1].fX == x1); |
| 2914 | REPORTER_ASSERT(reporter, pts[1].fY == y1); |
| 2915 | } |
| 2916 | |
| 2917 | static void check_quad(skiatest::Reporter* reporter, SkPath::RawIter* iter, |
| 2918 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 2919 | SkPoint pts[4]; |
| 2920 | SkPath::Verb v = iter->next(pts); |
| 2921 | REPORTER_ASSERT(reporter, v == SkPath::kQuad_Verb); |
| 2922 | REPORTER_ASSERT(reporter, pts[1].fX == x1); |
| 2923 | REPORTER_ASSERT(reporter, pts[1].fY == y1); |
| 2924 | REPORTER_ASSERT(reporter, pts[2].fX == x2); |
| 2925 | REPORTER_ASSERT(reporter, pts[2].fY == y2); |
| 2926 | } |
| 2927 | |
| 2928 | static void check_done(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { |
| 2929 | SkPoint pts[4]; |
| 2930 | SkPath::Verb v = iter->next(pts); |
| 2931 | REPORTER_ASSERT(reporter, v == SkPath::kDone_Verb); |
| 2932 | } |
| 2933 | |
| 2934 | static void check_done_and_reset(skiatest::Reporter* reporter, SkPath* p, SkPath::RawIter* iter) { |
| 2935 | check_done(reporter, p, iter); |
| 2936 | p->reset(); |
| 2937 | } |
| 2938 | |
| 2939 | static void check_path_is_move_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 2940 | SkScalar x0, SkScalar y0) { |
| 2941 | SkPath::RawIter iter(*p); |
| 2942 | check_move(reporter, &iter, x0, y0); |
| 2943 | check_done_and_reset(reporter, p, &iter); |
| 2944 | } |
| 2945 | |
| 2946 | static void check_path_is_line_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 2947 | SkScalar x1, SkScalar y1) { |
| 2948 | SkPath::RawIter iter(*p); |
| 2949 | check_move(reporter, &iter, 0, 0); |
| 2950 | check_line(reporter, &iter, x1, y1); |
| 2951 | check_done_and_reset(reporter, p, &iter); |
| 2952 | } |
| 2953 | |
| 2954 | static void check_path_is_line(skiatest::Reporter* reporter, SkPath* p, |
| 2955 | SkScalar x1, SkScalar y1) { |
| 2956 | SkPath::RawIter iter(*p); |
| 2957 | check_move(reporter, &iter, 0, 0); |
| 2958 | check_line(reporter, &iter, x1, y1); |
| 2959 | check_done(reporter, p, &iter); |
| 2960 | } |
| 2961 | |
| 2962 | static void check_path_is_line_pair_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 2963 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 2964 | SkPath::RawIter iter(*p); |
| 2965 | check_move(reporter, &iter, 0, 0); |
| 2966 | check_line(reporter, &iter, x1, y1); |
| 2967 | check_line(reporter, &iter, x2, y2); |
| 2968 | check_done_and_reset(reporter, p, &iter); |
| 2969 | } |
| 2970 | |
| 2971 | static void check_path_is_quad_and_reset(skiatest::Reporter* reporter, SkPath* p, |
| 2972 | SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) { |
| 2973 | SkPath::RawIter iter(*p); |
| 2974 | check_move(reporter, &iter, 0, 0); |
| 2975 | check_quad(reporter, &iter, x1, y1, x2, y2); |
| 2976 | check_done_and_reset(reporter, p, &iter); |
| 2977 | } |
| 2978 | |
| 2979 | static void test_arcTo(skiatest::Reporter* reporter) { |
| 2980 | SkPath p; |
| 2981 | p.arcTo(0, 0, 1, 2, 1); |
| 2982 | check_path_is_line_and_reset(reporter, &p, 0, 0); |
| 2983 | p.arcTo(1, 2, 1, 2, 1); |
| 2984 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 2985 | p.arcTo(1, 2, 3, 4, 0); |
| 2986 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 2987 | p.arcTo(1, 2, 0, 0, 1); |
| 2988 | check_path_is_line_and_reset(reporter, &p, 1, 2); |
| 2989 | p.arcTo(1, 0, 1, 1, 1); |
| 2990 | SkPoint pt; |
| 2991 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == 1); |
| 2992 | p.reset(); |
| 2993 | p.arcTo(1, 0, 1, -1, 1); |
| 2994 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt.fX == 1 && pt.fY == -1); |
| 2995 | p.reset(); |
| 2996 | SkRect oval = {1, 2, 3, 4}; |
| 2997 | p.arcTo(oval, 0, 0, true); |
| 2998 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 2999 | p.arcTo(oval, 0, 0, false); |
| 3000 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3001 | p.arcTo(oval, 360, 0, true); |
| 3002 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3003 | p.arcTo(oval, 360, 0, false); |
| 3004 | check_path_is_move_and_reset(reporter, &p, oval.fRight, oval.centerY()); |
| 3005 | for (float sweep = 359, delta = 0.5f; sweep != (float) (sweep + delta); ) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 3006 | p.arcTo(oval, 0, sweep, false); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3007 | REPORTER_ASSERT(reporter, p.getBounds() == oval); |
| 3008 | sweep += delta; |
| 3009 | delta /= 2; |
| 3010 | } |
| 3011 | for (float sweep = 361, delta = 0.5f; sweep != (float) (sweep - delta);) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 3012 | p.arcTo(oval, 0, sweep, false); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3013 | REPORTER_ASSERT(reporter, p.getBounds() == oval); |
| 3014 | sweep -= delta; |
| 3015 | delta /= 2; |
| 3016 | } |
| 3017 | SkRect noOvalWidth = {1, 2, 0, 3}; |
| 3018 | p.reset(); |
| 3019 | p.arcTo(noOvalWidth, 0, 360, false); |
| 3020 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3021 | |
| 3022 | SkRect noOvalHeight = {1, 2, 3, 1}; |
| 3023 | p.reset(); |
| 3024 | p.arcTo(noOvalHeight, 0, 360, false); |
| 3025 | REPORTER_ASSERT(reporter, p.isEmpty()); |
| 3026 | } |
| 3027 | |
| 3028 | static void test_addPath(skiatest::Reporter* reporter) { |
| 3029 | SkPath p, q; |
| 3030 | p.lineTo(1, 2); |
| 3031 | q.moveTo(4, 4); |
| 3032 | q.lineTo(7, 8); |
| 3033 | q.conicTo(8, 7, 6, 5, 0.5f); |
| 3034 | q.quadTo(6, 7, 8, 6); |
| 3035 | q.cubicTo(5, 6, 7, 8, 7, 5); |
| 3036 | q.close(); |
| 3037 | p.addPath(q, -4, -4); |
| 3038 | SkRect expected = {0, 0, 4, 4}; |
| 3039 | REPORTER_ASSERT(reporter, p.getBounds() == expected); |
| 3040 | p.reset(); |
| 3041 | p.reverseAddPath(q); |
| 3042 | SkRect reverseExpected = {4, 4, 8, 8}; |
| 3043 | REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); |
| 3044 | } |
| 3045 | |
| 3046 | static void test_conicTo_special_case(skiatest::Reporter* reporter) { |
| 3047 | SkPath p; |
| 3048 | p.conicTo(1, 2, 3, 4, -1); |
| 3049 | check_path_is_line_and_reset(reporter, &p, 3, 4); |
| 3050 | p.conicTo(1, 2, 3, 4, SK_ScalarInfinity); |
| 3051 | check_path_is_line_pair_and_reset(reporter, &p, 1, 2, 3, 4); |
| 3052 | p.conicTo(1, 2, 3, 4, 1); |
| 3053 | check_path_is_quad_and_reset(reporter, &p, 1, 2, 3, 4); |
| 3054 | } |
| 3055 | |
| 3056 | static void test_get_point(skiatest::Reporter* reporter) { |
| 3057 | SkPath p; |
| 3058 | SkPoint pt = p.getPoint(0); |
| 3059 | REPORTER_ASSERT(reporter, pt == SkPoint::Make(0, 0)); |
| 3060 | REPORTER_ASSERT(reporter, !p.getLastPt(NULL)); |
| 3061 | REPORTER_ASSERT(reporter, !p.getLastPt(&pt) && pt == SkPoint::Make(0, 0)); |
| 3062 | p.setLastPt(10, 10); |
| 3063 | pt = p.getPoint(0); |
| 3064 | REPORTER_ASSERT(reporter, pt == SkPoint::Make(10, 10)); |
| 3065 | REPORTER_ASSERT(reporter, p.getLastPt(NULL)); |
| 3066 | p.rMoveTo(10, 10); |
| 3067 | REPORTER_ASSERT(reporter, p.getLastPt(&pt) && pt == SkPoint::Make(20, 20)); |
| 3068 | } |
| 3069 | |
| 3070 | static void test_contains(skiatest::Reporter* reporter) { |
| 3071 | SkPath p; |
| 3072 | p.setFillType(SkPath::kInverseWinding_FillType); |
| 3073 | REPORTER_ASSERT(reporter, p.contains(0, 0)); |
| 3074 | p.setFillType(SkPath::kWinding_FillType); |
| 3075 | REPORTER_ASSERT(reporter, !p.contains(0, 0)); |
| 3076 | p.moveTo(4, 4); |
| 3077 | p.lineTo(6, 8); |
| 3078 | p.lineTo(8, 4); |
| 3079 | // test quick reject |
| 3080 | REPORTER_ASSERT(reporter, !p.contains(4, 0)); |
| 3081 | REPORTER_ASSERT(reporter, !p.contains(0, 4)); |
| 3082 | REPORTER_ASSERT(reporter, !p.contains(4, 10)); |
| 3083 | REPORTER_ASSERT(reporter, !p.contains(10, 4)); |
| 3084 | // test various crossings in x |
| 3085 | REPORTER_ASSERT(reporter, !p.contains(5, 7)); |
| 3086 | REPORTER_ASSERT(reporter, p.contains(6, 7)); |
| 3087 | REPORTER_ASSERT(reporter, !p.contains(7, 7)); |
| 3088 | p.reset(); |
| 3089 | p.moveTo(4, 4); |
| 3090 | p.lineTo(8, 6); |
| 3091 | p.lineTo(4, 8); |
| 3092 | // test various crossings in y |
| 3093 | REPORTER_ASSERT(reporter, !p.contains(7, 5)); |
| 3094 | REPORTER_ASSERT(reporter, p.contains(7, 6)); |
| 3095 | REPORTER_ASSERT(reporter, !p.contains(7, 7)); |
| 3096 | // test quads |
| 3097 | p.reset(); |
| 3098 | p.moveTo(4, 4); |
| 3099 | p.quadTo(6, 6, 8, 8); |
| 3100 | p.quadTo(6, 8, 4, 8); |
| 3101 | p.quadTo(4, 6, 4, 4); |
| 3102 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3103 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3104 | |
| 3105 | p.reset(); |
| 3106 | p.moveTo(6, 6); |
| 3107 | p.quadTo(8, 8, 6, 8); |
| 3108 | p.quadTo(4, 8, 4, 6); |
| 3109 | p.quadTo(4, 4, 6, 6); |
| 3110 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3111 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3112 | |
| 3113 | #define CONIC_CONTAINS_BUG_FIXED 0 |
| 3114 | #if CONIC_CONTAINS_BUG_FIXED |
| 3115 | p.reset(); |
| 3116 | p.moveTo(4, 4); |
| 3117 | p.conicTo(6, 6, 8, 8, 0.5f); |
| 3118 | p.conicTo(6, 8, 4, 8, 0.5f); |
| 3119 | p.conicTo(4, 6, 4, 4, 0.5f); |
| 3120 | REPORTER_ASSERT(reporter, p.contains(5, 6)); |
| 3121 | REPORTER_ASSERT(reporter, !p.contains(6, 5)); |
| 3122 | #endif |
| 3123 | |
| 3124 | // test cubics |
| 3125 | SkPoint pts[] = {{5, 4}, {6, 5}, {7, 6}, {6, 6}, {4, 6}, {5, 7}, {5, 5}, {5, 4}, {6, 5}, {7, 6}}; |
| 3126 | for (int i = 0; i < 3; ++i) { |
| 3127 | p.reset(); |
| 3128 | p.setFillType(SkPath::kEvenOdd_FillType); |
| 3129 | p.moveTo(pts[i].fX, pts[i].fY); |
| 3130 | p.cubicTo(pts[i + 1].fX, pts[i + 1].fY, pts[i + 2].fX, pts[i + 2].fY, pts[i + 3].fX, pts[i + 3].fY); |
| 3131 | p.cubicTo(pts[i + 4].fX, pts[i + 4].fY, pts[i + 5].fX, pts[i + 5].fY, pts[i + 6].fX, pts[i + 6].fY); |
| 3132 | p.close(); |
| 3133 | REPORTER_ASSERT(reporter, p.contains(5.5f, 5.5f)); |
| 3134 | REPORTER_ASSERT(reporter, !p.contains(4.5f, 5.5f)); |
| 3135 | } |
| 3136 | } |
| 3137 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3138 | class PathRefTest_Private { |
| 3139 | public: |
| 3140 | static void TestPathRef(skiatest::Reporter* reporter) { |
| 3141 | static const int kRepeatCnt = 10; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3142 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3143 | SkAutoTUnref<SkPathRef> pathRef(SkNEW(SkPathRef)); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3144 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3145 | SkPathRef::Editor ed(&pathRef); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3146 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3147 | { |
| 3148 | ed.growForRepeatedVerb(SkPath::kMove_Verb, kRepeatCnt); |
| 3149 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3150 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); |
| 3151 | REPORTER_ASSERT(reporter, 0 == pathRef->getSegmentMasks()); |
| 3152 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3153 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == pathRef->atVerb(i)); |
| 3154 | } |
| 3155 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3156 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3157 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3158 | { |
| 3159 | ed.growForRepeatedVerb(SkPath::kLine_Verb, kRepeatCnt); |
| 3160 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3161 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countPoints()); |
| 3162 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == pathRef->getSegmentMasks()); |
| 3163 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3164 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == pathRef->atVerb(i)); |
| 3165 | } |
| 3166 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3167 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3168 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3169 | { |
| 3170 | ed.growForRepeatedVerb(SkPath::kQuad_Verb, kRepeatCnt); |
| 3171 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3172 | REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); |
| 3173 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == pathRef->getSegmentMasks()); |
| 3174 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3175 | REPORTER_ASSERT(reporter, SkPath::kQuad_Verb == pathRef->atVerb(i)); |
| 3176 | } |
| 3177 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3178 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3179 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3180 | { |
| 3181 | SkScalar* weights = NULL; |
| 3182 | ed.growForRepeatedVerb(SkPath::kConic_Verb, kRepeatCnt, &weights); |
| 3183 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3184 | REPORTER_ASSERT(reporter, 2*kRepeatCnt == pathRef->countPoints()); |
| 3185 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countWeights()); |
| 3186 | REPORTER_ASSERT(reporter, SkPath::kConic_SegmentMask == pathRef->getSegmentMasks()); |
| 3187 | REPORTER_ASSERT(reporter, NULL != weights); |
| 3188 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3189 | REPORTER_ASSERT(reporter, SkPath::kConic_Verb == pathRef->atVerb(i)); |
| 3190 | } |
| 3191 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3192 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3193 | |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3194 | { |
| 3195 | ed.growForRepeatedVerb(SkPath::kCubic_Verb, kRepeatCnt); |
| 3196 | REPORTER_ASSERT(reporter, kRepeatCnt == pathRef->countVerbs()); |
| 3197 | REPORTER_ASSERT(reporter, 3*kRepeatCnt == pathRef->countPoints()); |
| 3198 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == pathRef->getSegmentMasks()); |
| 3199 | for (int i = 0; i < kRepeatCnt; ++i) { |
| 3200 | REPORTER_ASSERT(reporter, SkPath::kCubic_Verb == pathRef->atVerb(i)); |
| 3201 | } |
| 3202 | ed.resetToSize(0, 0, 0); |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3203 | } |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3204 | } |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3205 | }; |
robertphillips@google.com | 6b8dbb6 | 2013-12-12 23:03:51 +0000 | [diff] [blame] | 3206 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3207 | static void test_operatorEqual(skiatest::Reporter* reporter) { |
| 3208 | SkPath a; |
| 3209 | SkPath b; |
| 3210 | REPORTER_ASSERT(reporter, a == a); |
| 3211 | REPORTER_ASSERT(reporter, a == b); |
| 3212 | a.setFillType(SkPath::kInverseWinding_FillType); |
| 3213 | REPORTER_ASSERT(reporter, a != b); |
| 3214 | a.reset(); |
| 3215 | REPORTER_ASSERT(reporter, a == b); |
| 3216 | a.lineTo(1, 1); |
| 3217 | REPORTER_ASSERT(reporter, a != b); |
| 3218 | a.reset(); |
| 3219 | REPORTER_ASSERT(reporter, a == b); |
| 3220 | a.lineTo(1, 1); |
| 3221 | b.lineTo(1, 2); |
| 3222 | REPORTER_ASSERT(reporter, a != b); |
| 3223 | a.reset(); |
| 3224 | a.lineTo(1, 2); |
| 3225 | REPORTER_ASSERT(reporter, a == b); |
| 3226 | } |
| 3227 | |
| 3228 | class PathTest_Private { |
| 3229 | public: |
| 3230 | static void TestPathTo(skiatest::Reporter* reporter) { |
| 3231 | SkPath p, q; |
| 3232 | p.lineTo(4, 4); |
| 3233 | p.reversePathTo(q); |
| 3234 | check_path_is_line(reporter, &p, 4, 4); |
| 3235 | q.moveTo(-4, -4); |
| 3236 | p.reversePathTo(q); |
| 3237 | check_path_is_line(reporter, &p, 4, 4); |
| 3238 | q.lineTo(7, 8); |
| 3239 | q.conicTo(8, 7, 6, 5, 0.5f); |
| 3240 | q.quadTo(6, 7, 8, 6); |
| 3241 | q.cubicTo(5, 6, 7, 8, 7, 5); |
| 3242 | q.close(); |
| 3243 | p.reversePathTo(q); |
| 3244 | SkRect reverseExpected = {-4, -4, 8, 8}; |
| 3245 | REPORTER_ASSERT(reporter, p.getBounds() == reverseExpected); |
| 3246 | } |
| 3247 | }; |
| 3248 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 3249 | DEF_TEST(Path, reporter) { |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3250 | SkTSize<SkScalar>::Make(3,4); |
| 3251 | |
| 3252 | SkPath p, empty; |
| 3253 | SkRect bounds, bounds2; |
| 3254 | test_empty(reporter, p); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3255 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 3256 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3257 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3258 | // this triggers a code path in SkPath::operator= which is otherwise unexercised |
| 3259 | SkPath& self = p; |
| 3260 | p = self; |
| 3261 | |
| 3262 | // this triggers a code path in SkPath::swap which is otherwise unexercised |
| 3263 | p.swap(self); |
| 3264 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3265 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3266 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3267 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 3268 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3269 | // we have quads or cubics |
| 3270 | REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3271 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 3272 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3273 | p.reset(); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3274 | test_empty(reporter, p); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3275 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3276 | p.addOval(bounds); |
| 3277 | check_convex_bounds(reporter, p, bounds); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3278 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 3279 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3280 | p.rewind(); |
| 3281 | test_empty(reporter, p); |
| 3282 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3283 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 3284 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 3285 | // we have only lines |
| 3286 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3287 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3288 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 3289 | REPORTER_ASSERT(reporter, p != empty); |
| 3290 | REPORTER_ASSERT(reporter, !(p == empty)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3291 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 3292 | // do getPoints and getVerbs return the right result |
| 3293 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4); |
| 3294 | REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3295 | SkPoint pts[4]; |
| 3296 | int count = p.getPoints(pts, 4); |
| 3297 | REPORTER_ASSERT(reporter, count == 4); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 3298 | uint8_t verbs[6]; |
| 3299 | verbs[5] = 0xff; |
| 3300 | p.getVerbs(verbs, 5); |
| 3301 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]); |
| 3302 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]); |
| 3303 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]); |
| 3304 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]); |
| 3305 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]); |
| 3306 | REPORTER_ASSERT(reporter, 0xff == verbs[5]); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3307 | bounds2.set(pts, 4); |
| 3308 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3309 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3310 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 3311 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 3312 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3313 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3314 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 3315 | bounds2.setEmpty(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3316 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 3317 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 3318 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3319 | // now force p to not be a rect |
| 3320 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 3321 | p.addRect(bounds); |
| 3322 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3323 | |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3324 | test_operatorEqual(reporter); |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 3325 | test_isLine(reporter); |
| 3326 | test_isRect(reporter); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 3327 | test_isNestedRects(reporter); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 3328 | test_zero_length_paths(reporter); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 3329 | test_direction(reporter); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 3330 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 3331 | test_convexity2(reporter); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 3332 | test_conservativelyContains(reporter); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 3333 | test_close(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 3334 | test_segment_masks(reporter); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 3335 | test_flattening(reporter); |
| 3336 | test_transform(reporter); |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 3337 | test_bounds(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 3338 | test_iter(reporter); |
| 3339 | test_raw_iter(reporter); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 3340 | test_circle(reporter); |
| 3341 | test_oval(reporter); |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 3342 | test_strokerec(reporter); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 3343 | test_addPoly(reporter); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 3344 | test_isfinite(reporter); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 3345 | test_isfinite_after_transform(reporter); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 3346 | test_arb_round_rect_is_convex(reporter); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 3347 | test_arb_zero_rad_round_rect_is_rect(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3348 | test_addrect(reporter); |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 3349 | test_addrect_isfinite(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 3350 | test_tricky_cubic(); |
| 3351 | test_clipped_cubic(); |
| 3352 | test_crbug_170666(); |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 3353 | test_bad_cubic_crbug229478(); |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 3354 | test_bad_cubic_crbug234190(); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 3355 | test_android_specific_behavior(reporter); |
commit-bot@chromium.org | 1ab9f73 | 2013-10-30 18:57:55 +0000 | [diff] [blame] | 3356 | test_gen_id(reporter); |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 3357 | test_path_close_issue1474(reporter); |
reed@google.com | b58ba89 | 2013-10-15 15:44:35 +0000 | [diff] [blame] | 3358 | test_path_to_region(reporter); |
commit-bot@chromium.org | 42feaaf | 2013-11-08 15:51:12 +0000 | [diff] [blame] | 3359 | test_rrect(reporter); |
commit-bot@chromium.org | a1a097e | 2013-11-14 16:53:22 +0000 | [diff] [blame] | 3360 | test_arc(reporter); |
| 3361 | test_arcTo(reporter); |
| 3362 | test_addPath(reporter); |
| 3363 | test_conicTo_special_case(reporter); |
| 3364 | test_get_point(reporter); |
| 3365 | test_contains(reporter); |
| 3366 | PathTest_Private::TestPathTo(reporter); |
robertphillips@google.com | 0efb21b | 2013-12-13 19:36:25 +0000 | [diff] [blame] | 3367 | PathRefTest_Private::TestPathRef(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 3368 | } |