epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 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" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 17 | #include "SkSize.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 18 | #include "SkSurface.h" |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
| 20 | #include "SkWriter32.h" |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 21 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 22 | #if defined(WIN32) |
| 23 | #define SUPPRESS_VISIBILITY_WARNING |
| 24 | #else |
| 25 | #define SUPPRESS_VISIBILITY_WARNING __attribute__((visibility("hidden"))) |
| 26 | #endif |
| 27 | |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 28 | static SkSurface* new_surface(int w, int h) { |
| 29 | SkImage::Info info = { |
| 30 | w, h, SkImage::kPMColor_ColorType, SkImage::kPremul_AlphaType |
| 31 | }; |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 32 | return SkSurface::NewRaster(info); |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 33 | } |
| 34 | |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 35 | static void test_path_close_issue1474(skiatest::Reporter* reporter) { |
| 36 | // This test checks that r{Line,Quad,Conic,Cubic}To following a close() |
| 37 | // are relative to the point we close to, not relative to the point we close from. |
| 38 | SkPath path; |
| 39 | SkPoint last; |
| 40 | |
| 41 | // Test rLineTo(). |
| 42 | path.rLineTo(0, 100); |
| 43 | path.rLineTo(100, 0); |
| 44 | path.close(); // Returns us back to 0,0. |
| 45 | path.rLineTo(50, 50); // This should go to 50,50. |
| 46 | |
| 47 | path.getLastPt(&last); |
| 48 | REPORTER_ASSERT(reporter, 50 == last.fX); |
| 49 | REPORTER_ASSERT(reporter, 50 == last.fY); |
| 50 | |
| 51 | // Test rQuadTo(). |
| 52 | path.rewind(); |
| 53 | path.rLineTo(0, 100); |
| 54 | path.rLineTo(100, 0); |
| 55 | path.close(); |
| 56 | path.rQuadTo(50, 50, 75, 75); |
| 57 | |
| 58 | path.getLastPt(&last); |
| 59 | REPORTER_ASSERT(reporter, 75 == last.fX); |
| 60 | REPORTER_ASSERT(reporter, 75 == last.fY); |
| 61 | |
| 62 | // Test rConicTo(). |
| 63 | path.rewind(); |
| 64 | path.rLineTo(0, 100); |
| 65 | path.rLineTo(100, 0); |
| 66 | path.close(); |
| 67 | path.rConicTo(50, 50, 85, 85, 2); |
| 68 | |
| 69 | path.getLastPt(&last); |
| 70 | REPORTER_ASSERT(reporter, 85 == last.fX); |
| 71 | REPORTER_ASSERT(reporter, 85 == last.fY); |
| 72 | |
| 73 | // Test rCubicTo(). |
| 74 | path.rewind(); |
| 75 | path.rLineTo(0, 100); |
| 76 | path.rLineTo(100, 0); |
| 77 | path.close(); |
| 78 | path.rCubicTo(50, 50, 85, 85, 95, 95); |
| 79 | |
| 80 | path.getLastPt(&last); |
| 81 | REPORTER_ASSERT(reporter, 95 == last.fX); |
| 82 | REPORTER_ASSERT(reporter, 95 == last.fY); |
| 83 | } |
| 84 | |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 85 | static void test_android_specific_behavior(skiatest::Reporter* reporter) { |
| 86 | #ifdef SK_BUILD_FOR_ANDROID |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 87 | // Make sure we treat fGenerationID and fSourcePath correctly for each of |
| 88 | // copy, assign, rewind, reset, and swap. |
| 89 | SkPath original, source, anotherSource; |
| 90 | original.setSourcePath(&source); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 91 | original.moveTo(0, 0); |
| 92 | original.lineTo(1, 1); |
| 93 | REPORTER_ASSERT(reporter, original.getGenerationID() > 0); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, original.getSourcePath() == &source); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 95 | |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 96 | uint32_t copyID, assignID; |
| 97 | |
| 98 | // Test copy constructor. Copy generation ID, copy source path. |
| 99 | SkPath copy(original); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, copy.getGenerationID() == original.getGenerationID()); |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 102 | |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 103 | // Test assigment operator. Increment generation ID, copy source path. |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 104 | SkPath assign; |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 105 | assignID = assign.getGenerationID(); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 106 | assign = original; |
mtklein@google.com | cb8b0ee | 2013-08-15 21:14:51 +0000 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID); |
| 108 | REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath()); |
| 109 | |
| 110 | // Test rewind. Increment generation ID, don't touch source path. |
| 111 | copyID = copy.getGenerationID(); |
| 112 | copy.rewind(); |
| 113 | REPORTER_ASSERT(reporter, copy.getGenerationID() > copyID); |
| 114 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
| 115 | |
| 116 | // Test reset. Increment generation ID, don't touch source path. |
| 117 | assignID = assign.getGenerationID(); |
| 118 | assign.reset(); |
| 119 | REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID); |
| 120 | REPORTER_ASSERT(reporter, assign.getSourcePath() == original.getSourcePath()); |
| 121 | |
| 122 | // Test swap. Increment both generation IDs, swap source paths. |
| 123 | copy.setSourcePath(&anotherSource); |
| 124 | copyID = copy.getGenerationID(); |
| 125 | assignID = assign.getGenerationID(); |
| 126 | copy.swap(assign); |
| 127 | REPORTER_ASSERT(reporter, copy.getGenerationID() > copyID); |
| 128 | REPORTER_ASSERT(reporter, assign.getGenerationID() > assignID); |
| 129 | REPORTER_ASSERT(reporter, copy.getSourcePath() == original.getSourcePath()); |
| 130 | REPORTER_ASSERT(reporter, assign.getSourcePath() == &anotherSource); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 134 | // This used to assert in the debug build, as the edges did not all line-up. |
| 135 | static void test_bad_cubic_crbug234190() { |
| 136 | SkPath path; |
| 137 | path.moveTo(13.8509f, 3.16858f); |
| 138 | path.cubicTo(-2.35893e+08f, -4.21044e+08f, |
| 139 | -2.38991e+08f, -4.26573e+08f, |
| 140 | -2.41016e+08f, -4.30188e+08f); |
| 141 | |
| 142 | SkPaint paint; |
| 143 | paint.setAntiAlias(true); |
| 144 | SkAutoTUnref<SkSurface> surface(new_surface(84, 88)); |
| 145 | surface->getCanvas()->drawPath(path, paint); |
| 146 | } |
| 147 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 148 | static void test_bad_cubic_crbug229478() { |
| 149 | const SkPoint pts[] = { |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 150 | { 4595.91064f, -11596.9873f }, |
| 151 | { 4597.2168f, -11595.9414f }, |
| 152 | { 4598.52344f, -11594.8955f }, |
| 153 | { 4599.83008f, -11593.8496f }, |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 154 | }; |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 155 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 156 | SkPath path; |
| 157 | path.moveTo(pts[0]); |
| 158 | path.cubicTo(pts[1], pts[2], pts[3]); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 159 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 160 | SkPaint paint; |
| 161 | paint.setStyle(SkPaint::kStroke_Style); |
| 162 | paint.setStrokeWidth(20); |
skia.committer@gmail.com | 391ca66 | 2013-04-11 07:01:45 +0000 | [diff] [blame] | 163 | |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 164 | SkPath dst; |
| 165 | // Before the fix, this would infinite-recurse, and run out of stack |
| 166 | // because we would keep trying to subdivide a degenerate cubic segment. |
| 167 | paint.getFillPath(path, &dst, NULL); |
| 168 | } |
| 169 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 170 | static void build_path_170666(SkPath& path) { |
| 171 | path.moveTo(17.9459f, 21.6344f); |
| 172 | path.lineTo(139.545f, -47.8105f); |
| 173 | path.lineTo(139.545f, -47.8105f); |
| 174 | path.lineTo(131.07f, -47.3888f); |
| 175 | path.lineTo(131.07f, -47.3888f); |
| 176 | path.lineTo(122.586f, -46.9532f); |
| 177 | path.lineTo(122.586f, -46.9532f); |
| 178 | path.lineTo(18076.6f, 31390.9f); |
| 179 | path.lineTo(18076.6f, 31390.9f); |
| 180 | path.lineTo(18085.1f, 31390.5f); |
| 181 | path.lineTo(18085.1f, 31390.5f); |
| 182 | path.lineTo(18076.6f, 31390.9f); |
| 183 | path.lineTo(18076.6f, 31390.9f); |
| 184 | path.lineTo(17955, 31460.3f); |
| 185 | path.lineTo(17955, 31460.3f); |
| 186 | path.lineTo(17963.5f, 31459.9f); |
| 187 | path.lineTo(17963.5f, 31459.9f); |
| 188 | path.lineTo(17971.9f, 31459.5f); |
| 189 | path.lineTo(17971.9f, 31459.5f); |
| 190 | path.lineTo(17.9551f, 21.6205f); |
| 191 | path.lineTo(17.9551f, 21.6205f); |
| 192 | path.lineTo(9.47091f, 22.0561f); |
| 193 | path.lineTo(9.47091f, 22.0561f); |
| 194 | path.lineTo(17.9459f, 21.6344f); |
| 195 | path.lineTo(17.9459f, 21.6344f); |
| 196 | path.close();path.moveTo(0.995934f, 22.4779f); |
| 197 | path.lineTo(0.986725f, 22.4918f); |
| 198 | path.lineTo(0.986725f, 22.4918f); |
| 199 | path.lineTo(17955, 31460.4f); |
| 200 | path.lineTo(17955, 31460.4f); |
| 201 | path.lineTo(17971.9f, 31459.5f); |
| 202 | path.lineTo(17971.9f, 31459.5f); |
| 203 | path.lineTo(18093.6f, 31390.1f); |
| 204 | path.lineTo(18093.6f, 31390.1f); |
| 205 | path.lineTo(18093.6f, 31390); |
| 206 | path.lineTo(18093.6f, 31390); |
| 207 | path.lineTo(139.555f, -47.8244f); |
| 208 | path.lineTo(139.555f, -47.8244f); |
| 209 | path.lineTo(122.595f, -46.9671f); |
| 210 | path.lineTo(122.595f, -46.9671f); |
| 211 | path.lineTo(0.995934f, 22.4779f); |
| 212 | path.lineTo(0.995934f, 22.4779f); |
| 213 | path.close(); |
| 214 | path.moveTo(5.43941f, 25.5223f); |
| 215 | path.lineTo(798267, -28871.1f); |
| 216 | path.lineTo(798267, -28871.1f); |
| 217 | path.lineTo(3.12512e+06f, -113102); |
| 218 | path.lineTo(3.12512e+06f, -113102); |
| 219 | path.cubicTo(5.16324e+06f, -186882, 8.15247e+06f, -295092, 1.1957e+07f, -432813); |
| 220 | path.cubicTo(1.95659e+07f, -708257, 3.04359e+07f, -1.10175e+06f, 4.34798e+07f, -1.57394e+06f); |
| 221 | path.cubicTo(6.95677e+07f, -2.51831e+06f, 1.04352e+08f, -3.77748e+06f, 1.39135e+08f, -5.03666e+06f); |
| 222 | path.cubicTo(1.73919e+08f, -6.29583e+06f, 2.08703e+08f, -7.555e+06f, 2.34791e+08f, -8.49938e+06f); |
| 223 | path.cubicTo(2.47835e+08f, -8.97157e+06f, 2.58705e+08f, -9.36506e+06f, 2.66314e+08f, -9.6405e+06f); |
| 224 | path.cubicTo(2.70118e+08f, -9.77823e+06f, 2.73108e+08f, -9.88644e+06f, 2.75146e+08f, -9.96022e+06f); |
| 225 | path.cubicTo(2.76165e+08f, -9.99711e+06f, 2.76946e+08f, -1.00254e+07f, 2.77473e+08f, -1.00444e+07f); |
| 226 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 227 | path.lineTo(2.78271e+08f, -1.00733e+07f); |
| 228 | path.cubicTo(2.78271e+08f, -1.00733e+07f, 2.08703e+08f, -7.555e+06f, 135.238f, 23.3517f); |
| 229 | path.cubicTo(131.191f, 23.4981f, 125.995f, 23.7976f, 123.631f, 24.0206f); |
| 230 | path.cubicTo(121.267f, 24.2436f, 122.631f, 24.3056f, 126.677f, 24.1591f); |
| 231 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 232 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 233 | path.lineTo(2.77473e+08f, -1.00444e+07f); |
| 234 | path.cubicTo(2.76946e+08f, -1.00254e+07f, 2.76165e+08f, -9.99711e+06f, 2.75146e+08f, -9.96022e+06f); |
| 235 | path.cubicTo(2.73108e+08f, -9.88644e+06f, 2.70118e+08f, -9.77823e+06f, 2.66314e+08f, -9.6405e+06f); |
| 236 | path.cubicTo(2.58705e+08f, -9.36506e+06f, 2.47835e+08f, -8.97157e+06f, 2.34791e+08f, -8.49938e+06f); |
| 237 | path.cubicTo(2.08703e+08f, -7.555e+06f, 1.73919e+08f, -6.29583e+06f, 1.39135e+08f, -5.03666e+06f); |
| 238 | path.cubicTo(1.04352e+08f, -3.77749e+06f, 6.95677e+07f, -2.51831e+06f, 4.34798e+07f, -1.57394e+06f); |
| 239 | path.cubicTo(3.04359e+07f, -1.10175e+06f, 1.95659e+07f, -708258, 1.1957e+07f, -432814); |
| 240 | path.cubicTo(8.15248e+06f, -295092, 5.16324e+06f, -186883, 3.12513e+06f, -113103); |
| 241 | path.lineTo(798284, -28872); |
| 242 | path.lineTo(798284, -28872); |
| 243 | path.lineTo(22.4044f, 24.6677f); |
| 244 | path.lineTo(22.4044f, 24.6677f); |
| 245 | path.cubicTo(22.5186f, 24.5432f, 18.8134f, 24.6337f, 14.1287f, 24.8697f); |
| 246 | path.cubicTo(9.4439f, 25.1057f, 5.55359f, 25.3978f, 5.43941f, 25.5223f); |
| 247 | path.close(); |
| 248 | } |
| 249 | |
| 250 | static void build_path_simple_170666(SkPath& path) { |
| 251 | path.moveTo(126.677f, 24.1591f); |
| 252 | path.cubicTo(2.08703e+08f, -7.555e+06f, 2.78271e+08f, -1.00733e+07f, 2.78271e+08f, -1.00733e+07f); |
| 253 | } |
| 254 | |
| 255 | // This used to assert in the SK_DEBUG build, as the clip step would fail with |
| 256 | // too-few interations in our cubic-line intersection code. That code now runs |
| 257 | // 24 interations (instead of 16). |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 258 | static void test_crbug_170666() { |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 259 | SkPath path; |
| 260 | SkPaint paint; |
| 261 | paint.setAntiAlias(true); |
| 262 | |
| 263 | SkAutoTUnref<SkSurface> surface(new_surface(1000, 1000)); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 264 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 265 | build_path_simple_170666(path); |
| 266 | surface->getCanvas()->drawPath(path, paint); |
skia.committer@gmail.com | 36df7ed | 2013-01-19 07:05:38 +0000 | [diff] [blame] | 267 | |
reed@google.com | 64d6295 | 2013-01-18 17:49:28 +0000 | [diff] [blame] | 268 | build_path_170666(path); |
| 269 | surface->getCanvas()->drawPath(path, paint); |
| 270 | } |
| 271 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 272 | // Make sure we stay non-finite once we get there (unless we reset or rewind). |
| 273 | static void test_addrect_isfinite(skiatest::Reporter* reporter) { |
| 274 | SkPath path; |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 275 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 276 | path.addRect(SkRect::MakeWH(50, 100)); |
| 277 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 278 | |
| 279 | path.moveTo(0, 0); |
| 280 | path.lineTo(SK_ScalarInfinity, 42); |
| 281 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 282 | |
| 283 | path.addRect(SkRect::MakeWH(50, 100)); |
| 284 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 285 | |
| 286 | path.reset(); |
| 287 | REPORTER_ASSERT(reporter, path.isFinite()); |
skia.committer@gmail.com | 8b0e234 | 2012-10-25 02:01:20 +0000 | [diff] [blame] | 288 | |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 289 | path.addRect(SkRect::MakeWH(50, 100)); |
| 290 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 291 | } |
| 292 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 293 | static void build_big_path(SkPath* path, bool reducedCase) { |
| 294 | if (reducedCase) { |
| 295 | path->moveTo(577330, 1971.72f); |
| 296 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 297 | } else { |
| 298 | path->moveTo(60.1631f, 7.70567f); |
| 299 | path->quadTo(60.1631f, 7.70567f, 0.99474f, 0.901199f); |
| 300 | path->lineTo(577379, 1977.77f); |
| 301 | path->quadTo(577364, 1979.57f, 577325, 1980.26f); |
| 302 | path->quadTo(577286, 1980.95f, 577245, 1980.13f); |
| 303 | path->quadTo(577205, 1979.3f, 577187, 1977.45f); |
| 304 | path->quadTo(577168, 1975.6f, 577183, 1973.8f); |
| 305 | path->quadTo(577198, 1972, 577238, 1971.31f); |
| 306 | path->quadTo(577277, 1970.62f, 577317, 1971.45f); |
| 307 | path->quadTo(577330, 1971.72f, 577341, 1972.11f); |
| 308 | path->cubicTo(10.7082f, -116.596f, 262.057f, 45.6468f, 294.694f, 1.96237f); |
| 309 | path->moveTo(306.718f, -32.912f); |
| 310 | path->cubicTo(30.531f, 10.0005f, 1502.47f, 13.2804f, 84.3088f, 9.99601f); |
| 311 | } |
| 312 | } |
| 313 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 314 | static void test_clipped_cubic() { |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 315 | SkAutoTUnref<SkSurface> surface(new_surface(640, 480)); |
| 316 | |
| 317 | // This path used to assert, because our cubic-chopping code incorrectly |
| 318 | // moved control points after the chop. This test should be run in SK_DEBUG |
| 319 | // mode to ensure that we no long assert. |
| 320 | SkPath path; |
| 321 | for (int doReducedCase = 0; doReducedCase <= 1; ++doReducedCase) { |
| 322 | build_big_path(&path, SkToBool(doReducedCase)); |
skia.committer@gmail.com | ff21c2e | 2013-01-16 07:05:56 +0000 | [diff] [blame] | 323 | |
reed@google.com | 848148e | 2013-01-15 15:51:59 +0000 | [diff] [blame] | 324 | SkPaint paint; |
| 325 | for (int doAA = 0; doAA <= 1; ++doAA) { |
| 326 | paint.setAntiAlias(SkToBool(doAA)); |
| 327 | surface->getCanvas()->drawPath(path, paint); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 332 | // Inspired by http://ie.microsoft.com/testdrive/Performance/Chalkboard/ |
| 333 | // which triggered an assert, from a tricky cubic. This test replicates that |
| 334 | // example, so we can ensure that we handle it (in SkEdge.cpp), and don't |
| 335 | // assert in the SK_DEBUG build. |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 336 | static void test_tricky_cubic() { |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 337 | const SkPoint pts[] = { |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 338 | { SkDoubleToScalar(18.8943768), SkDoubleToScalar(129.121277) }, |
| 339 | { SkDoubleToScalar(18.8937435), SkDoubleToScalar(129.121689) }, |
| 340 | { SkDoubleToScalar(18.8950119), SkDoubleToScalar(129.120422) }, |
| 341 | { SkDoubleToScalar(18.5030727), SkDoubleToScalar(129.13121) }, |
reed@google.com | 8cae835 | 2012-09-14 15:18:41 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | SkPath path; |
| 345 | path.moveTo(pts[0]); |
| 346 | path.cubicTo(pts[1], pts[2], pts[3]); |
| 347 | |
| 348 | SkPaint paint; |
| 349 | paint.setAntiAlias(true); |
| 350 | |
| 351 | SkSurface* surface = new_surface(19, 130); |
| 352 | surface->getCanvas()->drawPath(path, paint); |
| 353 | surface->unref(); |
| 354 | } |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 355 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 356 | // Inspired by http://code.google.com/p/chromium/issues/detail?id=141651 |
| 357 | // |
| 358 | static void test_isfinite_after_transform(skiatest::Reporter* reporter) { |
| 359 | SkPath path; |
| 360 | path.quadTo(157, 366, 286, 208); |
| 361 | path.arcTo(37, 442, 315, 163, 957494590897113.0f); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 362 | |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 363 | SkMatrix matrix; |
| 364 | matrix.setScale(1000*1000, 1000*1000); |
| 365 | |
| 366 | // Be sure that path::transform correctly updates isFinite and the bounds |
| 367 | // if the transformation overflows. The previous bug was that isFinite was |
| 368 | // set to true in this case, but the bounds were not set to empty (which |
| 369 | // they should be). |
| 370 | while (path.isFinite()) { |
| 371 | REPORTER_ASSERT(reporter, path.getBounds().isFinite()); |
| 372 | REPORTER_ASSERT(reporter, !path.getBounds().isEmpty()); |
| 373 | path.transform(matrix); |
| 374 | } |
| 375 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 376 | |
| 377 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 378 | path.transform(matrix); |
| 379 | // we need to still be non-finite |
| 380 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 381 | REPORTER_ASSERT(reporter, path.getBounds().isEmpty()); |
| 382 | } |
| 383 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 384 | static void add_corner_arc(SkPath* path, const SkRect& rect, |
| 385 | SkScalar xIn, SkScalar yIn, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 386 | int startAngle) |
| 387 | { |
| 388 | |
| 389 | SkScalar rx = SkMinScalar(rect.width(), xIn); |
| 390 | SkScalar ry = SkMinScalar(rect.height(), yIn); |
| 391 | |
| 392 | SkRect arcRect; |
| 393 | arcRect.set(-rx, -ry, rx, ry); |
| 394 | switch (startAngle) { |
| 395 | case 0: |
| 396 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fBottom - arcRect.fBottom); |
| 397 | break; |
| 398 | case 90: |
| 399 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fBottom - arcRect.fBottom); |
| 400 | break; |
| 401 | case 180: |
| 402 | arcRect.offset(rect.fLeft - arcRect.fLeft, rect.fTop - arcRect.fTop); |
| 403 | break; |
| 404 | case 270: |
| 405 | arcRect.offset(rect.fRight - arcRect.fRight, rect.fTop - arcRect.fTop); |
| 406 | break; |
| 407 | default: |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | path->arcTo(arcRect, SkIntToScalar(startAngle), SkIntToScalar(90), false); |
| 412 | } |
| 413 | |
skia.committer@gmail.com | 6a748ad | 2012-10-19 02:01:19 +0000 | [diff] [blame] | 414 | static void make_arb_round_rect(SkPath* path, const SkRect& r, |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 415 | SkScalar xCorner, SkScalar yCorner) { |
| 416 | // we are lazy here and use the same x & y for each corner |
| 417 | add_corner_arc(path, r, xCorner, yCorner, 270); |
| 418 | add_corner_arc(path, r, xCorner, yCorner, 0); |
| 419 | add_corner_arc(path, r, xCorner, yCorner, 90); |
| 420 | add_corner_arc(path, r, xCorner, yCorner, 180); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 421 | path->close(); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // Chrome creates its own round rects with each corner possibly being different. |
| 425 | // Performance will suffer if they are not convex. |
| 426 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 427 | // the same test (but with drawing) |
| 428 | static void test_arb_round_rect_is_convex(skiatest::Reporter* reporter) { |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 429 | SkMWCRandom rand; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 430 | SkRect r; |
| 431 | |
| 432 | for (int i = 0; i < 5000; ++i) { |
| 433 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 434 | SkScalar size = rand.nextUScalar1() * 30; |
| 435 | if (size < SK_Scalar1) { |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 436 | continue; |
| 437 | } |
| 438 | r.fLeft = rand.nextUScalar1() * 300; |
| 439 | r.fTop = rand.nextUScalar1() * 300; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 440 | r.fRight = r.fLeft + 2 * size; |
| 441 | r.fBottom = r.fTop + 2 * size; |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 442 | |
| 443 | SkPath temp; |
| 444 | |
| 445 | make_arb_round_rect(&temp, r, r.width() / 10, r.height() / 15); |
| 446 | |
| 447 | REPORTER_ASSERT(reporter, temp.isConvex()); |
| 448 | } |
| 449 | } |
| 450 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 451 | // Chrome will sometimes create a 0 radius round rect. The degenerate |
| 452 | // quads prevent the path from being converted to a rect |
| 453 | // Note: PathBench::ArbRoundRectBench performs almost exactly |
| 454 | // the same test (but with drawing) |
| 455 | static void test_arb_zero_rad_round_rect_is_rect(skiatest::Reporter* reporter) { |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 456 | SkMWCRandom rand; |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 457 | SkRect r; |
| 458 | |
| 459 | for (int i = 0; i < 5000; ++i) { |
| 460 | |
| 461 | SkScalar size = rand.nextUScalar1() * 30; |
| 462 | if (size < SK_Scalar1) { |
| 463 | continue; |
| 464 | } |
| 465 | r.fLeft = rand.nextUScalar1() * 300; |
| 466 | r.fTop = rand.nextUScalar1() * 300; |
| 467 | r.fRight = r.fLeft + 2 * size; |
| 468 | r.fBottom = r.fTop + 2 * size; |
| 469 | |
| 470 | SkPath temp; |
| 471 | |
| 472 | make_arb_round_rect(&temp, r, 0, 0); |
| 473 | |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 474 | SkRect result; |
| 475 | REPORTER_ASSERT(reporter, temp.isRect(&result)); |
| 476 | REPORTER_ASSERT(reporter, r == result); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 480 | static void test_rect_isfinite(skiatest::Reporter* reporter) { |
| 481 | const SkScalar inf = SK_ScalarInfinity; |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 482 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 483 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 484 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 485 | SkRect r; |
| 486 | r.setEmpty(); |
| 487 | REPORTER_ASSERT(reporter, r.isFinite()); |
caryclark@google.com | 7abfa49 | 2013-04-12 11:59:41 +0000 | [diff] [blame] | 488 | r.set(0, 0, inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 489 | REPORTER_ASSERT(reporter, !r.isFinite()); |
| 490 | r.set(0, 0, nan, 0); |
| 491 | REPORTER_ASSERT(reporter, !r.isFinite()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 492 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 493 | SkPoint pts[] = { |
| 494 | { 0, 0 }, |
| 495 | { SK_Scalar1, 0 }, |
| 496 | { 0, SK_Scalar1 }, |
| 497 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 498 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 499 | bool isFine = r.setBoundsCheck(pts, 3); |
| 500 | REPORTER_ASSERT(reporter, isFine); |
| 501 | REPORTER_ASSERT(reporter, !r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 502 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 503 | pts[1].set(inf, 0); |
| 504 | isFine = r.setBoundsCheck(pts, 3); |
| 505 | REPORTER_ASSERT(reporter, !isFine); |
| 506 | REPORTER_ASSERT(reporter, r.isEmpty()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 507 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 508 | pts[1].set(nan, 0); |
| 509 | isFine = r.setBoundsCheck(pts, 3); |
| 510 | REPORTER_ASSERT(reporter, !isFine); |
| 511 | REPORTER_ASSERT(reporter, r.isEmpty()); |
| 512 | } |
| 513 | |
| 514 | static void test_path_isfinite(skiatest::Reporter* reporter) { |
| 515 | const SkScalar inf = SK_ScalarInfinity; |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 516 | const SkScalar negInf = SK_ScalarNegativeInfinity; |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 517 | const SkScalar nan = SK_ScalarNaN; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 518 | |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 519 | SkPath path; |
| 520 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 521 | |
| 522 | path.reset(); |
| 523 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 524 | |
| 525 | path.reset(); |
| 526 | path.moveTo(SK_Scalar1, 0); |
| 527 | REPORTER_ASSERT(reporter, path.isFinite()); |
| 528 | |
| 529 | path.reset(); |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 530 | path.moveTo(inf, negInf); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 531 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 532 | |
| 533 | path.reset(); |
| 534 | path.moveTo(nan, 0); |
| 535 | REPORTER_ASSERT(reporter, !path.isFinite()); |
| 536 | } |
| 537 | |
| 538 | static void test_isfinite(skiatest::Reporter* reporter) { |
| 539 | test_rect_isfinite(reporter); |
| 540 | test_path_isfinite(reporter); |
| 541 | } |
| 542 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 543 | // assert that we always |
| 544 | // start with a moveTo |
| 545 | // only have 1 moveTo |
| 546 | // only have Lines after that |
| 547 | // end with a single close |
| 548 | // only have (at most) 1 close |
| 549 | // |
| 550 | static void test_poly(skiatest::Reporter* reporter, const SkPath& path, |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 551 | const SkPoint srcPts[], bool expectClose) { |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 552 | SkPath::RawIter iter(path); |
| 553 | SkPoint pts[4]; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 554 | |
| 555 | bool firstTime = true; |
| 556 | bool foundClose = false; |
| 557 | for (;;) { |
| 558 | switch (iter.next(pts)) { |
| 559 | case SkPath::kMove_Verb: |
| 560 | REPORTER_ASSERT(reporter, firstTime); |
| 561 | REPORTER_ASSERT(reporter, pts[0] == srcPts[0]); |
| 562 | srcPts++; |
| 563 | firstTime = false; |
| 564 | break; |
| 565 | case SkPath::kLine_Verb: |
| 566 | REPORTER_ASSERT(reporter, !firstTime); |
| 567 | REPORTER_ASSERT(reporter, pts[1] == srcPts[0]); |
| 568 | srcPts++; |
| 569 | break; |
| 570 | case SkPath::kQuad_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 571 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected quad verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 572 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 573 | case SkPath::kConic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 574 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected conic verb"); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 575 | break; |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 576 | case SkPath::kCubic_Verb: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 577 | REPORTER_ASSERT_MESSAGE(reporter, false, "unexpected cubic verb"); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 578 | break; |
| 579 | case SkPath::kClose_Verb: |
| 580 | REPORTER_ASSERT(reporter, !firstTime); |
| 581 | REPORTER_ASSERT(reporter, !foundClose); |
| 582 | REPORTER_ASSERT(reporter, expectClose); |
| 583 | foundClose = true; |
| 584 | break; |
| 585 | case SkPath::kDone_Verb: |
| 586 | goto DONE; |
| 587 | } |
| 588 | } |
| 589 | DONE: |
| 590 | REPORTER_ASSERT(reporter, foundClose == expectClose); |
| 591 | } |
| 592 | |
| 593 | static void test_addPoly(skiatest::Reporter* reporter) { |
| 594 | SkPoint pts[32]; |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 595 | SkMWCRandom rand; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 596 | |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 597 | for (size_t i = 0; i < SK_ARRAY_COUNT(pts); ++i) { |
| 598 | pts[i].fX = rand.nextSScalar1(); |
| 599 | pts[i].fY = rand.nextSScalar1(); |
| 600 | } |
| 601 | |
| 602 | for (int doClose = 0; doClose <= 1; ++doClose) { |
| 603 | for (size_t count = 1; count <= SK_ARRAY_COUNT(pts); ++count) { |
| 604 | SkPath path; |
| 605 | path.addPoly(pts, count, SkToBool(doClose)); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 606 | test_poly(reporter, path, pts, SkToBool(doClose)); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 611 | static void test_strokerec(skiatest::Reporter* reporter) { |
| 612 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 613 | REPORTER_ASSERT(reporter, rec.isFillStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 614 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 615 | rec.setHairlineStyle(); |
| 616 | REPORTER_ASSERT(reporter, rec.isHairlineStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 617 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 618 | rec.setStrokeStyle(SK_Scalar1, false); |
| 619 | REPORTER_ASSERT(reporter, SkStrokeRec::kStroke_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 620 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 621 | rec.setStrokeStyle(SK_Scalar1, true); |
| 622 | REPORTER_ASSERT(reporter, SkStrokeRec::kStrokeAndFill_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 623 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 624 | rec.setStrokeStyle(0, false); |
| 625 | REPORTER_ASSERT(reporter, SkStrokeRec::kHairline_Style == rec.getStyle()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 626 | |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 627 | rec.setStrokeStyle(0, true); |
| 628 | REPORTER_ASSERT(reporter, SkStrokeRec::kFill_Style == rec.getStyle()); |
| 629 | } |
| 630 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 631 | // Set this for paths that don't have a consistent direction such as a bowtie. |
| 632 | // (cheapComputeDirection is not expected to catch these.) |
| 633 | static const SkPath::Direction kDontCheckDir = static_cast<SkPath::Direction>(-1); |
| 634 | |
| 635 | static void check_direction(skiatest::Reporter* reporter, const SkPath& path, |
| 636 | SkPath::Direction expected) { |
| 637 | if (expected == kDontCheckDir) { |
| 638 | return; |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 639 | } |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 640 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 641 | |
| 642 | SkPath::Direction dir; |
| 643 | if (copy.cheapComputeDirection(&dir)) { |
| 644 | REPORTER_ASSERT(reporter, dir == expected); |
| 645 | } else { |
| 646 | REPORTER_ASSERT(reporter, SkPath::kUnknown_Direction == expected); |
| 647 | } |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 648 | } |
| 649 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 650 | static void test_direction(skiatest::Reporter* reporter) { |
| 651 | size_t i; |
| 652 | SkPath path; |
| 653 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 654 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCW_Direction)); |
| 655 | REPORTER_ASSERT(reporter, !path.cheapIsDirection(SkPath::kCCW_Direction)); |
reed@google.com | a8a3b3d | 2012-11-26 18:16:27 +0000 | [diff] [blame] | 656 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kUnknown_Direction)); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 657 | |
| 658 | static const char* gDegen[] = { |
| 659 | "M 10 10", |
| 660 | "M 10 10 M 20 20", |
| 661 | "M 10 10 L 20 20", |
| 662 | "M 10 10 L 10 10 L 10 10", |
| 663 | "M 10 10 Q 10 10 10 10", |
| 664 | "M 10 10 C 10 10 10 10 10 10", |
| 665 | }; |
| 666 | for (i = 0; i < SK_ARRAY_COUNT(gDegen); ++i) { |
| 667 | path.reset(); |
| 668 | bool valid = SkParsePath::FromSVGString(gDegen[i], &path); |
| 669 | REPORTER_ASSERT(reporter, valid); |
| 670 | REPORTER_ASSERT(reporter, !path.cheapComputeDirection(NULL)); |
| 671 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 672 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 673 | static const char* gCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 674 | "M 10 10 L 10 10 Q 20 10 20 20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 675 | "M 10 10 C 20 10 20 20 20 20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 676 | "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] | 677 | // rect with top two corners replaced by cubics with identical middle |
| 678 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 679 | "M 10 10 C 10 0 10 0 20 0 L 40 0 C 50 0 50 0 50 10", |
| 680 | "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] | 681 | }; |
| 682 | for (i = 0; i < SK_ARRAY_COUNT(gCW); ++i) { |
| 683 | path.reset(); |
| 684 | bool valid = SkParsePath::FromSVGString(gCW[i], &path); |
| 685 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 686 | check_direction(reporter, path, SkPath::kCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 687 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 688 | |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 689 | static const char* gCCW[] = { |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 690 | "M 10 10 L 10 10 Q 20 10 20 -20", |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 691 | "M 10 10 C 20 10 20 -20 20 -20", |
reed@google.com | d414666 | 2012-01-31 15:42:29 +0000 | [diff] [blame] | 692 | "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] | 693 | // rect with top two corners replaced by cubics with identical middle |
| 694 | // control points |
reed@google.com | 20fb0c7 | 2012-11-13 13:47:39 +0000 | [diff] [blame] | 695 | "M 50 10 C 50 0 50 0 40 0 L 20 0 C 10 0 10 0 10 10", |
| 696 | "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] | 697 | }; |
| 698 | for (i = 0; i < SK_ARRAY_COUNT(gCCW); ++i) { |
| 699 | path.reset(); |
| 700 | bool valid = SkParsePath::FromSVGString(gCCW[i], &path); |
| 701 | REPORTER_ASSERT(reporter, valid); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 702 | check_direction(reporter, path, SkPath::kCCW_Direction); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 703 | } |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 704 | |
| 705 | // Test two donuts, each wound a different direction. Only the outer contour |
| 706 | // determines the cheap direction |
| 707 | path.reset(); |
| 708 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCW_Direction); |
| 709 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 710 | check_direction(reporter, path, SkPath::kCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 711 | |
reed@google.com | ac8543f | 2012-01-30 20:51:25 +0000 | [diff] [blame] | 712 | path.reset(); |
| 713 | path.addCircle(0, 0, SkIntToScalar(1), SkPath::kCW_Direction); |
| 714 | path.addCircle(0, 0, SkIntToScalar(2), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 715 | check_direction(reporter, path, SkPath::kCCW_Direction); |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 716 | |
bsalomon@google.com | 6843ac4 | 2012-02-17 13:49:03 +0000 | [diff] [blame] | 717 | #ifdef SK_SCALAR_IS_FLOAT |
bsalomon@google.com | f0ed80a | 2012-02-17 13:38:26 +0000 | [diff] [blame] | 718 | // triangle with one point really far from the origin. |
| 719 | path.reset(); |
| 720 | // the first point is roughly 1.05e10, 1.05e10 |
bsalomon@google.com | 53aab78 | 2012-02-23 14:54:49 +0000 | [diff] [blame] | 721 | path.moveTo(SkFloatToScalar(SkBits2Float(0x501c7652)), SkFloatToScalar(SkBits2Float(0x501c7652))); |
| 722 | path.lineTo(110 * SK_Scalar1, -10 * SK_Scalar1); |
| 723 | path.lineTo(-10 * SK_Scalar1, 60 * SK_Scalar1); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 724 | check_direction(reporter, path, SkPath::kCCW_Direction); |
bsalomon@google.com | 53aab78 | 2012-02-23 14:54:49 +0000 | [diff] [blame] | 725 | #endif |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 726 | } |
| 727 | |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 728 | static void add_rect(SkPath* path, const SkRect& r) { |
| 729 | path->moveTo(r.fLeft, r.fTop); |
| 730 | path->lineTo(r.fRight, r.fTop); |
| 731 | path->lineTo(r.fRight, r.fBottom); |
| 732 | path->lineTo(r.fLeft, r.fBottom); |
| 733 | path->close(); |
| 734 | } |
| 735 | |
| 736 | static void test_bounds(skiatest::Reporter* reporter) { |
| 737 | static const SkRect rects[] = { |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 738 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(160) }, |
| 739 | { SkIntToScalar(610), SkIntToScalar(160), SkIntToScalar(610), SkIntToScalar(199) }, |
| 740 | { SkIntToScalar(10), SkIntToScalar(198), SkIntToScalar(610), SkIntToScalar(199) }, |
| 741 | { SkIntToScalar(10), SkIntToScalar(160), SkIntToScalar(10), SkIntToScalar(199) }, |
reed@google.com | ffdb018 | 2011-11-14 19:29:14 +0000 | [diff] [blame] | 742 | }; |
| 743 | |
| 744 | SkPath path0, path1; |
| 745 | for (size_t i = 0; i < SK_ARRAY_COUNT(rects); ++i) { |
| 746 | path0.addRect(rects[i]); |
| 747 | add_rect(&path1, rects[i]); |
| 748 | } |
| 749 | |
| 750 | REPORTER_ASSERT(reporter, path0.getBounds() == path1.getBounds()); |
| 751 | } |
| 752 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 753 | static void stroke_cubic(const SkPoint pts[4]) { |
| 754 | SkPath path; |
| 755 | path.moveTo(pts[0]); |
| 756 | path.cubicTo(pts[1], pts[2], pts[3]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 757 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 758 | SkPaint paint; |
| 759 | paint.setStyle(SkPaint::kStroke_Style); |
| 760 | paint.setStrokeWidth(SK_Scalar1 * 2); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 761 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 762 | SkPath fill; |
| 763 | paint.getFillPath(path, &fill); |
| 764 | } |
| 765 | |
| 766 | // just ensure this can run w/o any SkASSERTS firing in the debug build |
| 767 | // we used to assert due to differences in how we determine a degenerate vector |
| 768 | // but that was fixed with the introduction of SkPoint::CanNormalize |
| 769 | static void stroke_tiny_cubic() { |
| 770 | SkPoint p0[] = { |
| 771 | { 372.0f, 92.0f }, |
| 772 | { 372.0f, 92.0f }, |
| 773 | { 372.0f, 92.0f }, |
| 774 | { 372.0f, 92.0f }, |
| 775 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 776 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 777 | stroke_cubic(p0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 778 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 779 | SkPoint p1[] = { |
| 780 | { 372.0f, 92.0f }, |
| 781 | { 372.0007f, 92.000755f }, |
| 782 | { 371.99927f, 92.003922f }, |
| 783 | { 371.99826f, 92.003899f }, |
| 784 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 785 | |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 786 | stroke_cubic(p1); |
| 787 | } |
| 788 | |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 789 | static void check_close(skiatest::Reporter* reporter, const SkPath& path) { |
| 790 | for (int i = 0; i < 2; ++i) { |
robertphillips@google.com | 09042b8 | 2012-04-06 20:01:46 +0000 | [diff] [blame] | 791 | SkPath::Iter iter(path, SkToBool(i)); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 792 | SkPoint mv; |
| 793 | SkPoint pts[4]; |
| 794 | SkPath::Verb v; |
| 795 | int nMT = 0; |
| 796 | int nCL = 0; |
tomhudson@google.com | 221db3c | 2011-07-28 21:10:29 +0000 | [diff] [blame] | 797 | mv.set(0, 0); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 798 | while (SkPath::kDone_Verb != (v = iter.next(pts))) { |
| 799 | switch (v) { |
| 800 | case SkPath::kMove_Verb: |
| 801 | mv = pts[0]; |
| 802 | ++nMT; |
| 803 | break; |
| 804 | case SkPath::kClose_Verb: |
| 805 | REPORTER_ASSERT(reporter, mv == pts[0]); |
| 806 | ++nCL; |
| 807 | break; |
| 808 | default: |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | // if we force a close on the interator we should have a close |
| 813 | // for every moveTo |
| 814 | REPORTER_ASSERT(reporter, !i || nMT == nCL); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | static void test_close(skiatest::Reporter* reporter) { |
| 819 | SkPath closePt; |
| 820 | closePt.moveTo(0, 0); |
| 821 | closePt.close(); |
| 822 | check_close(reporter, closePt); |
| 823 | |
| 824 | SkPath openPt; |
| 825 | openPt.moveTo(0, 0); |
| 826 | check_close(reporter, openPt); |
| 827 | |
| 828 | SkPath empty; |
| 829 | check_close(reporter, empty); |
| 830 | empty.close(); |
| 831 | check_close(reporter, empty); |
| 832 | |
| 833 | SkPath rect; |
| 834 | rect.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 835 | check_close(reporter, rect); |
| 836 | rect.close(); |
| 837 | check_close(reporter, rect); |
| 838 | |
| 839 | SkPath quad; |
| 840 | quad.quadTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 841 | check_close(reporter, quad); |
| 842 | quad.close(); |
| 843 | check_close(reporter, quad); |
| 844 | |
| 845 | SkPath cubic; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 846 | quad.cubicTo(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 847 | 10*SK_Scalar1, 20 * SK_Scalar1, 20*SK_Scalar1); |
| 848 | check_close(reporter, cubic); |
| 849 | cubic.close(); |
| 850 | check_close(reporter, cubic); |
| 851 | |
| 852 | SkPath line; |
| 853 | line.moveTo(SK_Scalar1, SK_Scalar1); |
| 854 | line.lineTo(10 * SK_Scalar1, 10*SK_Scalar1); |
| 855 | check_close(reporter, line); |
| 856 | line.close(); |
| 857 | check_close(reporter, line); |
| 858 | |
| 859 | SkPath rect2; |
| 860 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 861 | rect2.close(); |
| 862 | rect2.addRect(SK_Scalar1, SK_Scalar1, 10 * SK_Scalar1, 10*SK_Scalar1); |
| 863 | check_close(reporter, rect2); |
| 864 | rect2.close(); |
| 865 | check_close(reporter, rect2); |
| 866 | |
| 867 | SkPath oval3; |
| 868 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*100,SK_Scalar1*100)); |
| 869 | oval3.close(); |
| 870 | oval3.addOval(SkRect::MakeWH(SK_Scalar1*200,SK_Scalar1*200)); |
| 871 | check_close(reporter, oval3); |
| 872 | oval3.close(); |
| 873 | check_close(reporter, oval3); |
| 874 | |
| 875 | SkPath moves; |
| 876 | moves.moveTo(SK_Scalar1, SK_Scalar1); |
| 877 | moves.moveTo(5 * SK_Scalar1, SK_Scalar1); |
| 878 | moves.moveTo(SK_Scalar1, 10 * SK_Scalar1); |
| 879 | moves.moveTo(10 *SK_Scalar1, SK_Scalar1); |
| 880 | check_close(reporter, moves); |
reed@google.com | 55b5f4b | 2011-09-07 12:23:41 +0000 | [diff] [blame] | 881 | |
| 882 | stroke_tiny_cubic(); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 883 | } |
| 884 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 885 | static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, |
| 886 | SkPath::Convexity expected) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 887 | SkPath copy(path); // we make a copy so that we don't cache the result on the passed in path. |
| 888 | SkPath::Convexity c = copy.getConvexity(); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 889 | REPORTER_ASSERT(reporter, c == expected); |
| 890 | } |
| 891 | |
| 892 | static void test_convexity2(skiatest::Reporter* reporter) { |
| 893 | SkPath pt; |
| 894 | pt.moveTo(0, 0); |
| 895 | pt.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 896 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 897 | check_direction(reporter, pt, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 898 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 899 | SkPath line; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 900 | line.moveTo(12*SK_Scalar1, 20*SK_Scalar1); |
| 901 | line.lineTo(-12*SK_Scalar1, -20*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 902 | line.close(); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 903 | check_convexity(reporter, line, SkPath::kConvex_Convexity); |
| 904 | check_direction(reporter, line, SkPath::kUnknown_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 905 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 906 | SkPath triLeft; |
| 907 | triLeft.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 908 | triLeft.lineTo(SK_Scalar1, 0); |
| 909 | triLeft.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 910 | triLeft.close(); |
| 911 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 912 | check_direction(reporter, triLeft, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 913 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 914 | SkPath triRight; |
| 915 | triRight.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 916 | triRight.lineTo(-SK_Scalar1, 0); |
| 917 | triRight.lineTo(SK_Scalar1, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 918 | triRight.close(); |
| 919 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 920 | check_direction(reporter, triRight, SkPath::kCCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 921 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 922 | SkPath square; |
| 923 | square.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 924 | square.lineTo(SK_Scalar1, 0); |
| 925 | square.lineTo(SK_Scalar1, SK_Scalar1); |
| 926 | square.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 927 | square.close(); |
| 928 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 929 | check_direction(reporter, square, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 930 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 931 | SkPath redundantSquare; |
| 932 | redundantSquare.moveTo(0, 0); |
| 933 | redundantSquare.lineTo(0, 0); |
| 934 | redundantSquare.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 935 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 936 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 937 | redundantSquare.lineTo(SK_Scalar1, 0); |
| 938 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 939 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 940 | redundantSquare.lineTo(SK_Scalar1, SK_Scalar1); |
| 941 | redundantSquare.lineTo(0, SK_Scalar1); |
| 942 | redundantSquare.lineTo(0, SK_Scalar1); |
| 943 | redundantSquare.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 944 | redundantSquare.close(); |
| 945 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 946 | check_direction(reporter, redundantSquare, SkPath::kCW_Direction); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 947 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 948 | SkPath bowTie; |
| 949 | bowTie.moveTo(0, 0); |
| 950 | bowTie.lineTo(0, 0); |
| 951 | bowTie.lineTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 952 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 953 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 954 | bowTie.lineTo(SK_Scalar1, SK_Scalar1); |
| 955 | bowTie.lineTo(SK_Scalar1, 0); |
| 956 | bowTie.lineTo(SK_Scalar1, 0); |
| 957 | bowTie.lineTo(SK_Scalar1, 0); |
| 958 | bowTie.lineTo(0, SK_Scalar1); |
| 959 | bowTie.lineTo(0, SK_Scalar1); |
| 960 | bowTie.lineTo(0, SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 961 | bowTie.close(); |
| 962 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 963 | check_direction(reporter, bowTie, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 964 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 965 | SkPath spiral; |
| 966 | spiral.moveTo(0, 0); |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 967 | spiral.lineTo(100*SK_Scalar1, 0); |
| 968 | spiral.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 969 | spiral.lineTo(0, 100*SK_Scalar1); |
| 970 | spiral.lineTo(0, 50*SK_Scalar1); |
| 971 | spiral.lineTo(50*SK_Scalar1, 50*SK_Scalar1); |
| 972 | spiral.lineTo(50*SK_Scalar1, 75*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 973 | spiral.close(); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 974 | check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 975 | check_direction(reporter, spiral, kDontCheckDir); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 976 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 977 | SkPath dent; |
schenney@chromium.org | 6c31d9d | 2011-12-20 16:33:30 +0000 | [diff] [blame] | 978 | dent.moveTo(0, 0); |
| 979 | dent.lineTo(100*SK_Scalar1, 100*SK_Scalar1); |
| 980 | dent.lineTo(0, 100*SK_Scalar1); |
| 981 | dent.lineTo(-50*SK_Scalar1, 200*SK_Scalar1); |
| 982 | dent.lineTo(-200*SK_Scalar1, 100*SK_Scalar1); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 983 | dent.close(); |
| 984 | check_convexity(reporter, dent, SkPath::kConcave_Convexity); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 985 | check_direction(reporter, dent, SkPath::kCW_Direction); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 986 | } |
| 987 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 988 | static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, |
| 989 | const SkRect& bounds) { |
| 990 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 991 | REPORTER_ASSERT(reporter, p.getBounds() == bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 992 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 993 | SkPath p2(p); |
| 994 | REPORTER_ASSERT(reporter, p2.isConvex()); |
| 995 | REPORTER_ASSERT(reporter, p2.getBounds() == bounds); |
| 996 | |
| 997 | SkPath other; |
| 998 | other.swap(p2); |
| 999 | REPORTER_ASSERT(reporter, other.isConvex()); |
| 1000 | REPORTER_ASSERT(reporter, other.getBounds() == bounds); |
| 1001 | } |
| 1002 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1003 | static void setFromString(SkPath* path, const char str[]) { |
| 1004 | bool first = true; |
| 1005 | while (str) { |
| 1006 | SkScalar x, y; |
| 1007 | str = SkParse::FindScalar(str, &x); |
| 1008 | if (NULL == str) { |
| 1009 | break; |
| 1010 | } |
| 1011 | str = SkParse::FindScalar(str, &y); |
| 1012 | SkASSERT(str); |
| 1013 | if (first) { |
| 1014 | path->moveTo(x, y); |
| 1015 | first = false; |
| 1016 | } else { |
| 1017 | path->lineTo(x, y); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | static void test_convexity(skiatest::Reporter* reporter) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1023 | SkPath path; |
| 1024 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1025 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1026 | path.addCircle(0, 0, SkIntToScalar(10)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1027 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1028 | path.addCircle(0, 0, SkIntToScalar(10)); // 2nd circle |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1029 | check_convexity(reporter, path, SkPath::kConcave_Convexity); |
| 1030 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1031 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1032 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1033 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1034 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCCW_Direction)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1035 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1036 | path.reset(); |
reed@google.com | e354397 | 2012-01-10 18:59:22 +0000 | [diff] [blame] | 1037 | path.addRect(0, 0, SkIntToScalar(10), SkIntToScalar(10), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1038 | check_convexity(reporter, path, SkPath::kConvex_Convexity); |
reed@google.com | 3e71a88 | 2012-01-10 18:44:37 +0000 | [diff] [blame] | 1039 | REPORTER_ASSERT(reporter, path.cheapIsDirection(SkPath::kCW_Direction)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1040 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1041 | static const struct { |
| 1042 | const char* fPathStr; |
| 1043 | SkPath::Convexity fExpectedConvexity; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1044 | SkPath::Direction fExpectedDirection; |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1045 | } gRec[] = { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1046 | { "", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1047 | { "0 0", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1048 | { "0 0 10 10", SkPath::kConvex_Convexity, SkPath::kUnknown_Direction }, |
| 1049 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity, SkPath::kUnknown_Direction }, |
| 1050 | { "0 0 10 10 10 20", SkPath::kConvex_Convexity, SkPath::kCW_Direction }, |
| 1051 | { "0 0 10 10 10 0", SkPath::kConvex_Convexity, SkPath::kCCW_Direction }, |
| 1052 | { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity, kDontCheckDir }, |
| 1053 | { "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] | 1054 | }; |
| 1055 | |
| 1056 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 1057 | SkPath path; |
| 1058 | setFromString(&path, gRec[i].fPathStr); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 1059 | check_convexity(reporter, path, gRec[i].fExpectedConvexity); |
| 1060 | check_direction(reporter, path, gRec[i].fExpectedDirection); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1064 | static void test_isLine(skiatest::Reporter* reporter) { |
| 1065 | SkPath path; |
| 1066 | SkPoint pts[2]; |
| 1067 | const SkScalar value = SkIntToScalar(5); |
| 1068 | |
| 1069 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1070 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 1071 | // set some non-zero values |
| 1072 | pts[0].set(value, value); |
| 1073 | pts[1].set(value, value); |
| 1074 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1075 | // check that pts was untouched |
| 1076 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1077 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1078 | |
| 1079 | const SkScalar moveX = SkIntToScalar(1); |
| 1080 | const SkScalar moveY = SkIntToScalar(2); |
| 1081 | SkASSERT(value != moveX && value != moveY); |
| 1082 | |
| 1083 | path.moveTo(moveX, moveY); |
| 1084 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1085 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1086 | // check that pts was untouched |
| 1087 | REPORTER_ASSERT(reporter, pts[0].equals(value, value)); |
| 1088 | REPORTER_ASSERT(reporter, pts[1].equals(value, value)); |
| 1089 | |
| 1090 | const SkScalar lineX = SkIntToScalar(2); |
| 1091 | const SkScalar lineY = SkIntToScalar(2); |
| 1092 | SkASSERT(value != lineX && value != lineY); |
| 1093 | |
| 1094 | path.lineTo(lineX, lineY); |
| 1095 | REPORTER_ASSERT(reporter, path.isLine(NULL)); |
| 1096 | |
| 1097 | REPORTER_ASSERT(reporter, !pts[0].equals(moveX, moveY)); |
| 1098 | REPORTER_ASSERT(reporter, !pts[1].equals(lineX, lineY)); |
| 1099 | REPORTER_ASSERT(reporter, path.isLine(pts)); |
| 1100 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1101 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 1102 | |
| 1103 | path.lineTo(0, 0); // too many points/verbs |
| 1104 | REPORTER_ASSERT(reporter, !path.isLine(NULL)); |
| 1105 | REPORTER_ASSERT(reporter, !path.isLine(pts)); |
| 1106 | REPORTER_ASSERT(reporter, pts[0].equals(moveX, moveY)); |
| 1107 | REPORTER_ASSERT(reporter, pts[1].equals(lineX, lineY)); |
| 1108 | } |
| 1109 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1110 | static void test_conservativelyContains(skiatest::Reporter* reporter) { |
| 1111 | SkPath path; |
| 1112 | |
| 1113 | // kBaseRect is used to construct most our test paths: a rect, a circle, and a round-rect. |
| 1114 | static const SkRect kBaseRect = SkRect::MakeWH(SkIntToScalar(100), SkIntToScalar(100)); |
| 1115 | |
| 1116 | // A circle that bounds kBaseRect (with a significant amount of slop) |
| 1117 | SkScalar circleR = SkMaxScalar(kBaseRect.width(), kBaseRect.height()); |
| 1118 | circleR = SkScalarMul(circleR, SkFloatToScalar(1.75f)) / 2; |
| 1119 | static const SkPoint kCircleC = {kBaseRect.centerX(), kBaseRect.centerY()}; |
| 1120 | |
| 1121 | // round-rect radii |
| 1122 | static const SkScalar kRRRadii[] = {SkIntToScalar(5), SkIntToScalar(3)}; |
skia.committer@gmail.com | cec8de6 | 2012-11-14 02:01:22 +0000 | [diff] [blame] | 1123 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1124 | static const struct SUPPRESS_VISIBILITY_WARNING { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1125 | SkRect fQueryRect; |
| 1126 | bool fInRect; |
| 1127 | bool fInCircle; |
| 1128 | bool fInRR; |
| 1129 | } kQueries[] = { |
| 1130 | {kBaseRect, true, true, false}, |
| 1131 | |
| 1132 | // rect well inside of kBaseRect |
| 1133 | {SkRect::MakeLTRB(kBaseRect.fLeft + SkFloatToScalar(0.25f)*kBaseRect.width(), |
| 1134 | kBaseRect.fTop + SkFloatToScalar(0.25f)*kBaseRect.height(), |
| 1135 | kBaseRect.fRight - SkFloatToScalar(0.25f)*kBaseRect.width(), |
| 1136 | kBaseRect.fBottom - SkFloatToScalar(0.25f)*kBaseRect.height()), |
| 1137 | true, true, true}, |
| 1138 | |
| 1139 | // rects with edges off by one from kBaseRect's edges |
| 1140 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1141 | kBaseRect.width(), kBaseRect.height() + 1), |
| 1142 | false, true, false}, |
| 1143 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1144 | kBaseRect.width() + 1, kBaseRect.height()), |
| 1145 | false, true, false}, |
| 1146 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, |
| 1147 | kBaseRect.width() + 1, kBaseRect.height() + 1), |
| 1148 | false, true, false}, |
| 1149 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1150 | kBaseRect.width(), kBaseRect.height()), |
| 1151 | false, true, false}, |
| 1152 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1153 | kBaseRect.width(), kBaseRect.height()), |
| 1154 | false, true, false}, |
| 1155 | {SkRect::MakeXYWH(kBaseRect.fLeft - 1, kBaseRect.fTop, |
| 1156 | kBaseRect.width() + 2, kBaseRect.height()), |
| 1157 | false, true, false}, |
| 1158 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop - 1, |
| 1159 | kBaseRect.width() + 2, kBaseRect.height()), |
| 1160 | false, true, false}, |
| 1161 | |
| 1162 | // zero-w/h rects at each corner of kBaseRect |
| 1163 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fTop, 0, 0), true, true, false}, |
| 1164 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fTop, 0, 0), true, true, false}, |
| 1165 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.fBottom, 0, 0), true, true, false}, |
| 1166 | {SkRect::MakeXYWH(kBaseRect.fRight, kBaseRect.fBottom, 0, 0), true, true, false}, |
| 1167 | |
| 1168 | // far away rect |
| 1169 | {SkRect::MakeXYWH(10 * kBaseRect.fRight, 10 * kBaseRect.fBottom, |
| 1170 | SkIntToScalar(10), SkIntToScalar(10)), |
| 1171 | false, false, false}, |
| 1172 | |
| 1173 | // very large rect containing kBaseRect |
| 1174 | {SkRect::MakeXYWH(kBaseRect.fLeft - 5 * kBaseRect.width(), |
| 1175 | kBaseRect.fTop - 5 * kBaseRect.height(), |
| 1176 | 11 * kBaseRect.width(), 11 * kBaseRect.height()), |
| 1177 | false, false, false}, |
| 1178 | |
| 1179 | // skinny rect that spans same y-range as kBaseRect |
| 1180 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1181 | SkIntToScalar(1), kBaseRect.height()), |
| 1182 | true, true, true}, |
| 1183 | |
| 1184 | // short rect that spans same x-range as kBaseRect |
| 1185 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), kBaseRect.width(), SkScalar(1)), |
| 1186 | true, true, true}, |
| 1187 | |
| 1188 | // skinny rect that spans slightly larger y-range than kBaseRect |
| 1189 | {SkRect::MakeXYWH(kBaseRect.centerX(), kBaseRect.fTop, |
| 1190 | SkIntToScalar(1), kBaseRect.height() + 1), |
| 1191 | false, true, false}, |
| 1192 | |
| 1193 | // short rect that spans slightly larger x-range than kBaseRect |
| 1194 | {SkRect::MakeXYWH(kBaseRect.fLeft, kBaseRect.centerY(), |
| 1195 | kBaseRect.width() + 1, SkScalar(1)), |
| 1196 | false, true, false}, |
| 1197 | }; |
| 1198 | |
| 1199 | for (int inv = 0; inv < 4; ++inv) { |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1200 | for (size_t q = 0; q < SK_ARRAY_COUNT(kQueries); ++q) { |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1201 | SkRect qRect = kQueries[q].fQueryRect; |
| 1202 | if (inv & 0x1) { |
| 1203 | SkTSwap(qRect.fLeft, qRect.fRight); |
| 1204 | } |
| 1205 | if (inv & 0x2) { |
| 1206 | SkTSwap(qRect.fTop, qRect.fBottom); |
| 1207 | } |
| 1208 | for (int d = 0; d < 2; ++d) { |
| 1209 | SkPath::Direction dir = d ? SkPath::kCCW_Direction : SkPath::kCW_Direction; |
| 1210 | path.reset(); |
| 1211 | path.addRect(kBaseRect, dir); |
| 1212 | REPORTER_ASSERT(reporter, kQueries[q].fInRect == |
| 1213 | path.conservativelyContainsRect(qRect)); |
| 1214 | |
| 1215 | path.reset(); |
| 1216 | path.addCircle(kCircleC.fX, kCircleC.fY, circleR, dir); |
| 1217 | REPORTER_ASSERT(reporter, kQueries[q].fInCircle == |
| 1218 | path.conservativelyContainsRect(qRect)); |
| 1219 | |
| 1220 | path.reset(); |
| 1221 | path.addRoundRect(kBaseRect, kRRRadii[0], kRRRadii[1], dir); |
| 1222 | REPORTER_ASSERT(reporter, kQueries[q].fInRR == |
| 1223 | path.conservativelyContainsRect(qRect)); |
| 1224 | } |
| 1225 | // Slightly non-convex shape, shouldn't contain any rects. |
| 1226 | path.reset(); |
| 1227 | path.moveTo(0, 0); |
| 1228 | path.lineTo(SkIntToScalar(50), SkFloatToScalar(0.05f)); |
| 1229 | path.lineTo(SkIntToScalar(100), 0); |
| 1230 | path.lineTo(SkIntToScalar(100), SkIntToScalar(100)); |
| 1231 | path.lineTo(0, SkIntToScalar(100)); |
| 1232 | path.close(); |
| 1233 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(qRect)); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | // make sure a minimal convex shape works, a right tri with edges along pos x and y axes. |
| 1238 | path.reset(); |
| 1239 | path.moveTo(0, 0); |
| 1240 | path.lineTo(SkIntToScalar(100), 0); |
| 1241 | path.lineTo(0, SkIntToScalar(100)); |
| 1242 | |
| 1243 | // inside, on along top edge |
| 1244 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1245 | SkIntToScalar(10), |
| 1246 | SkIntToScalar(10)))); |
| 1247 | // above |
| 1248 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect( |
| 1249 | SkRect::MakeXYWH(SkIntToScalar(50), |
| 1250 | SkIntToScalar(-10), |
| 1251 | SkIntToScalar(10), |
| 1252 | SkIntToScalar(10)))); |
| 1253 | // to the left |
| 1254 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(-10), |
| 1255 | SkIntToScalar(5), |
| 1256 | SkIntToScalar(5), |
| 1257 | SkIntToScalar(5)))); |
| 1258 | |
| 1259 | // outside the diagonal edge |
| 1260 | REPORTER_ASSERT(reporter, !path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(10), |
| 1261 | SkIntToScalar(200), |
| 1262 | SkIntToScalar(20), |
| 1263 | SkIntToScalar(5)))); |
commit-bot@chromium.org | 62df526 | 2013-08-01 15:35:06 +0000 | [diff] [blame] | 1264 | |
| 1265 | // same as above path and first test but with an extra moveTo. |
| 1266 | path.reset(); |
| 1267 | path.moveTo(100, 100); |
| 1268 | path.moveTo(0, 0); |
| 1269 | path.lineTo(SkIntToScalar(100), 0); |
| 1270 | path.lineTo(0, SkIntToScalar(100)); |
| 1271 | |
| 1272 | REPORTER_ASSERT(reporter, path.conservativelyContainsRect(SkRect::MakeXYWH(SkIntToScalar(50), 0, |
| 1273 | SkIntToScalar(10), |
| 1274 | SkIntToScalar(10)))); |
| 1275 | |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1278 | // Simple isRect test is inline TestPath, below. |
| 1279 | // test_isRect provides more extensive testing. |
| 1280 | static void test_isRect(skiatest::Reporter* reporter) { |
| 1281 | // passing tests (all moveTo / lineTo... |
| 1282 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; |
| 1283 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1284 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1285 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
| 1286 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; |
| 1287 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1288 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1289 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1290 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1291 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, |
| 1292 | {1, 0}, {.5f, 0}}; |
| 1293 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, |
| 1294 | {0, 1}, {0, .5f}}; |
| 1295 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1296 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1297 | 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] | 1298 | 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] | 1299 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1300 | // failing tests |
| 1301 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1302 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1303 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1304 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1305 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1306 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1307 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1308 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1309 | SkPoint f9[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, 0}, {2, 0}}; // overlaps |
| 1310 | SkPoint fa[] = {{1, 0}, {8, 0}, {8, 8}, {0, 8}, {0, -1}, {1, -1}}; // non colinear gap |
| 1311 | 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] | 1312 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1313 | // failing, no close |
| 1314 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 1315 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 1316 | |
| 1317 | size_t testLen[] = { |
| 1318 | sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6), |
| 1319 | sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc), |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1320 | sizeof(rd), sizeof(re), sizeof(rf), |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1321 | sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6), |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1322 | sizeof(f7), sizeof(f8), sizeof(f9), sizeof(fa), sizeof(fb), |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1323 | sizeof(c1), sizeof(c2) |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1324 | }; |
| 1325 | SkPoint* tests[] = { |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1326 | r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re, rf, |
| 1327 | f1, f2, f3, f4, f5, f6, f7, f8, f9, fa, fb, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1328 | c1, c2 |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1329 | }; |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1330 | SkPoint* lastPass = rf; |
| 1331 | SkPoint* lastClose = fb; |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1332 | bool fail = false; |
| 1333 | bool close = true; |
| 1334 | const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 1335 | size_t index; |
| 1336 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1337 | SkPath path; |
| 1338 | path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY); |
| 1339 | for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) { |
| 1340 | path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY); |
| 1341 | } |
| 1342 | if (close) { |
| 1343 | path.close(); |
| 1344 | } |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1345 | REPORTER_ASSERT(reporter, fail ^ path.isRect(0)); |
| 1346 | REPORTER_ASSERT(reporter, fail ^ path.isRect(NULL, NULL)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1347 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1348 | if (!fail) { |
| 1349 | SkRect computed, expected; |
| 1350 | expected.set(tests[testIndex], testLen[testIndex] / sizeof(SkPoint)); |
| 1351 | REPORTER_ASSERT(reporter, path.isRect(&computed)); |
| 1352 | REPORTER_ASSERT(reporter, expected == computed); |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 1353 | |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1354 | bool isClosed; |
| 1355 | SkPath::Direction direction, cheapDirection; |
| 1356 | REPORTER_ASSERT(reporter, path.cheapComputeDirection(&cheapDirection)); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1357 | REPORTER_ASSERT(reporter, path.isRect(&isClosed, &direction)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1358 | REPORTER_ASSERT(reporter, isClosed == close); |
| 1359 | REPORTER_ASSERT(reporter, direction == cheapDirection); |
| 1360 | } else { |
| 1361 | SkRect computed; |
| 1362 | computed.set(123, 456, 789, 1011); |
| 1363 | REPORTER_ASSERT(reporter, !path.isRect(&computed)); |
| 1364 | REPORTER_ASSERT(reporter, computed.fLeft == 123 && computed.fTop == 456); |
| 1365 | REPORTER_ASSERT(reporter, computed.fRight == 789 && computed.fBottom == 1011); |
| 1366 | |
| 1367 | bool isClosed = (bool) -1; |
| 1368 | SkPath::Direction direction = (SkPath::Direction) -1; |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1369 | REPORTER_ASSERT(reporter, !path.isRect(&isClosed, &direction)); |
caryclark@google.com | f68154a | 2012-11-21 15:18:06 +0000 | [diff] [blame] | 1370 | REPORTER_ASSERT(reporter, isClosed == (bool) -1); |
| 1371 | REPORTER_ASSERT(reporter, direction == (SkPath::Direction) -1); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1372 | } |
skia.committer@gmail.com | 1c9c0d3 | 2012-11-22 02:02:41 +0000 | [diff] [blame] | 1373 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1374 | if (tests[testIndex] == lastPass) { |
| 1375 | fail = true; |
| 1376 | } |
| 1377 | if (tests[testIndex] == lastClose) { |
| 1378 | close = false; |
| 1379 | } |
| 1380 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1381 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1382 | // fail, close then line |
| 1383 | SkPath path1; |
| 1384 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1385 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1386 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1387 | } |
| 1388 | path1.close(); |
| 1389 | path1.lineTo(1, 0); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1390 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1391 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1392 | // fail, move in the middle |
| 1393 | path1.reset(); |
| 1394 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1395 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1396 | if (index == 2) { |
| 1397 | path1.moveTo(1, .5f); |
| 1398 | } |
| 1399 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1400 | } |
| 1401 | path1.close(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1402 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1403 | |
| 1404 | // fail, move on the edge |
| 1405 | path1.reset(); |
| 1406 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1407 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1408 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1409 | } |
| 1410 | path1.close(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1411 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1412 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1413 | // fail, quad |
| 1414 | path1.reset(); |
| 1415 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1416 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1417 | if (index == 2) { |
| 1418 | path1.quadTo(1, .5f, 1, .5f); |
| 1419 | } |
| 1420 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1421 | } |
| 1422 | path1.close(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1423 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1424 | |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1425 | // fail, cubic |
| 1426 | path1.reset(); |
| 1427 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1428 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1429 | if (index == 2) { |
| 1430 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 1431 | } |
| 1432 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1433 | } |
| 1434 | path1.close(); |
robertphillips@google.com | 8fd1603 | 2013-06-25 15:39:58 +0000 | [diff] [blame] | 1435 | REPORTER_ASSERT(reporter, fail ^ path1.isRect(0)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1438 | static void test_isNestedRects(skiatest::Reporter* reporter) { |
| 1439 | // passing tests (all moveTo / lineTo... |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1440 | SkPoint r1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1441 | SkPoint r2[] = {{1, 0}, {1, 1}, {0, 1}, {0, 0}}; |
| 1442 | SkPoint r3[] = {{1, 1}, {0, 1}, {0, 0}, {1, 0}}; |
| 1443 | SkPoint r4[] = {{0, 1}, {0, 0}, {1, 0}, {1, 1}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1444 | SkPoint r5[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; // CCW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1445 | SkPoint r6[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
| 1446 | SkPoint r7[] = {{1, 1}, {1, 0}, {0, 0}, {0, 1}}; |
| 1447 | SkPoint r8[] = {{1, 0}, {0, 0}, {0, 1}, {1, 1}}; |
| 1448 | SkPoint r9[] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1449 | SkPoint ra[] = {{0, 0}, {0, .5f}, {0, 1}, {.5f, 1}, {1, 1}, {1, .5f}, // CCW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1450 | {1, 0}, {.5f, 0}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1451 | SkPoint rb[] = {{0, 0}, {.5f, 0}, {1, 0}, {1, .5f}, {1, 1}, {.5f, 1}, // CW |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1452 | {0, 1}, {0, .5f}}; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1453 | SkPoint rc[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // CW |
| 1454 | SkPoint rd[] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; // CCW |
| 1455 | 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] | 1456 | |
| 1457 | // failing tests |
| 1458 | SkPoint f1[] = {{0, 0}, {1, 0}, {1, 1}}; // too few points |
| 1459 | SkPoint f2[] = {{0, 0}, {1, 1}, {0, 1}, {1, 0}}; // diagonal |
| 1460 | SkPoint f3[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}, {1, 0}}; // wraps |
| 1461 | SkPoint f4[] = {{0, 0}, {1, 0}, {0, 0}, {1, 0}, {1, 1}, {0, 1}}; // backs up |
| 1462 | SkPoint f5[] = {{0, 0}, {1, 0}, {1, 1}, {2, 0}}; // end overshoots |
| 1463 | SkPoint f6[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 2}}; // end overshoots |
| 1464 | SkPoint f7[] = {{0, 0}, {1, 0}, {1, 1}, {0, 2}}; // end overshoots |
| 1465 | SkPoint f8[] = {{0, 0}, {1, 0}, {1, 1}, {1, 0}}; // 'L' |
| 1466 | |
| 1467 | // failing, no close |
| 1468 | SkPoint c1[] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}; // close doesn't match |
| 1469 | SkPoint c2[] = {{0, 0}, {1, 0}, {1, 2}, {0, 2}, {0, 1}}; // ditto |
| 1470 | |
| 1471 | size_t testLen[] = { |
| 1472 | sizeof(r1), sizeof(r2), sizeof(r3), sizeof(r4), sizeof(r5), sizeof(r6), |
| 1473 | sizeof(r7), sizeof(r8), sizeof(r9), sizeof(ra), sizeof(rb), sizeof(rc), |
| 1474 | sizeof(rd), sizeof(re), |
| 1475 | sizeof(f1), sizeof(f2), sizeof(f3), sizeof(f4), sizeof(f5), sizeof(f6), |
| 1476 | sizeof(f7), sizeof(f8), |
| 1477 | sizeof(c1), sizeof(c2) |
| 1478 | }; |
| 1479 | SkPoint* tests[] = { |
| 1480 | r1, r2, r3, r4, r5, r6, r7, r8, r9, ra, rb, rc, rd, re, |
| 1481 | f1, f2, f3, f4, f5, f6, f7, f8, |
| 1482 | c1, c2 |
| 1483 | }; |
skia.committer@gmail.com | 845220b | 2013-05-20 11:51:35 +0000 | [diff] [blame] | 1484 | SkPath::Direction dirs[] = { |
| 1485 | SkPath::kCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1486 | SkPath::kCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction, |
skia.committer@gmail.com | 845220b | 2013-05-20 11:51:35 +0000 | [diff] [blame] | 1487 | SkPath::kCCW_Direction, SkPath::kCCW_Direction, SkPath::kCCW_Direction, |
| 1488 | SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kCW_Direction, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1489 | SkPath::kCCW_Direction, SkPath::kCW_Direction, SkPath::kUnknown_Direction, |
| 1490 | SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, |
| 1491 | SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, |
| 1492 | SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, SkPath::kUnknown_Direction, |
| 1493 | }; |
| 1494 | SkASSERT(SK_ARRAY_COUNT(tests) == SK_ARRAY_COUNT(dirs)); |
| 1495 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1496 | const SkPoint* lastPass = re; |
| 1497 | const SkPoint* lastClose = f8; |
| 1498 | const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 1499 | size_t index; |
| 1500 | for (int rectFirst = 0; rectFirst <= 1; ++rectFirst) { |
| 1501 | bool fail = false; |
| 1502 | bool close = true; |
| 1503 | for (size_t testIndex = 0; testIndex < testCount; ++testIndex) { |
| 1504 | SkPath path; |
| 1505 | if (rectFirst) { |
| 1506 | path.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1507 | } |
| 1508 | path.moveTo(tests[testIndex][0].fX, tests[testIndex][0].fY); |
| 1509 | for (index = 1; index < testLen[testIndex] / sizeof(SkPoint); ++index) { |
| 1510 | path.lineTo(tests[testIndex][index].fX, tests[testIndex][index].fY); |
| 1511 | } |
| 1512 | if (close) { |
| 1513 | path.close(); |
| 1514 | } |
| 1515 | if (!rectFirst) { |
| 1516 | path.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1517 | } |
| 1518 | REPORTER_ASSERT(reporter, fail ^ path.isNestedRects(0)); |
| 1519 | if (!fail) { |
| 1520 | SkRect expected[2], computed[2]; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1521 | SkPath::Direction expectedDirs[2], computedDirs[2]; |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1522 | SkRect testBounds; |
| 1523 | testBounds.set(tests[testIndex], testLen[testIndex] / sizeof(SkPoint)); |
| 1524 | expected[0] = SkRect::MakeLTRB(-1, -1, 2, 2); |
| 1525 | expected[1] = testBounds; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1526 | if (rectFirst) { |
| 1527 | expectedDirs[0] = SkPath::kCW_Direction; |
| 1528 | } else { |
| 1529 | expectedDirs[0] = SkPath::kCCW_Direction; |
| 1530 | } |
| 1531 | expectedDirs[1] = dirs[testIndex]; |
| 1532 | REPORTER_ASSERT(reporter, path.isNestedRects(computed, computedDirs)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1533 | REPORTER_ASSERT(reporter, expected[0] == computed[0]); |
| 1534 | REPORTER_ASSERT(reporter, expected[1] == computed[1]); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 1535 | REPORTER_ASSERT(reporter, expectedDirs[0] == computedDirs[0]); |
| 1536 | REPORTER_ASSERT(reporter, expectedDirs[1] == computedDirs[1]); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1537 | } |
| 1538 | if (tests[testIndex] == lastPass) { |
| 1539 | fail = true; |
| 1540 | } |
| 1541 | if (tests[testIndex] == lastClose) { |
| 1542 | close = false; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // fail, close then line |
| 1547 | SkPath path1; |
| 1548 | if (rectFirst) { |
| 1549 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1550 | } |
| 1551 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1552 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1553 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1554 | } |
| 1555 | path1.close(); |
| 1556 | path1.lineTo(1, 0); |
| 1557 | if (!rectFirst) { |
| 1558 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1559 | } |
| 1560 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
| 1561 | |
| 1562 | // fail, move in the middle |
| 1563 | path1.reset(); |
| 1564 | if (rectFirst) { |
| 1565 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1566 | } |
| 1567 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1568 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1569 | if (index == 2) { |
| 1570 | path1.moveTo(1, .5f); |
| 1571 | } |
| 1572 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1573 | } |
| 1574 | path1.close(); |
| 1575 | if (!rectFirst) { |
| 1576 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1577 | } |
| 1578 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
| 1579 | |
| 1580 | // fail, move on the edge |
| 1581 | path1.reset(); |
| 1582 | if (rectFirst) { |
| 1583 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1584 | } |
| 1585 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1586 | path1.moveTo(r1[index - 1].fX, r1[index - 1].fY); |
| 1587 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1588 | } |
| 1589 | path1.close(); |
| 1590 | if (!rectFirst) { |
| 1591 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1592 | } |
| 1593 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
| 1594 | |
| 1595 | // fail, quad |
| 1596 | path1.reset(); |
| 1597 | if (rectFirst) { |
| 1598 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1599 | } |
| 1600 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1601 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1602 | if (index == 2) { |
| 1603 | path1.quadTo(1, .5f, 1, .5f); |
| 1604 | } |
| 1605 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1606 | } |
| 1607 | path1.close(); |
| 1608 | if (!rectFirst) { |
| 1609 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1610 | } |
| 1611 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
| 1612 | |
| 1613 | // fail, cubic |
| 1614 | path1.reset(); |
| 1615 | if (rectFirst) { |
| 1616 | path1.addRect(-1, -1, 2, 2, SkPath::kCW_Direction); |
| 1617 | } |
| 1618 | path1.moveTo(r1[0].fX, r1[0].fY); |
| 1619 | for (index = 1; index < testLen[0] / sizeof(SkPoint); ++index) { |
| 1620 | if (index == 2) { |
| 1621 | path1.cubicTo(1, .5f, 1, .5f, 1, .5f); |
| 1622 | } |
| 1623 | path1.lineTo(r1[index].fX, r1[index].fY); |
| 1624 | } |
| 1625 | path1.close(); |
| 1626 | if (!rectFirst) { |
| 1627 | path1.addRect(-1, -1, 2, 2, SkPath::kCCW_Direction); |
| 1628 | } |
| 1629 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
skia.committer@gmail.com | 3458716 | 2012-11-20 02:01:23 +0000 | [diff] [blame] | 1630 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1631 | // fail, not nested |
| 1632 | path1.reset(); |
| 1633 | path1.addRect(1, 1, 3, 3, SkPath::kCW_Direction); |
| 1634 | path1.addRect(2, 2, 4, 4, SkPath::kCW_Direction); |
| 1635 | REPORTER_ASSERT(reporter, fail ^ path1.isNestedRects(0)); |
| 1636 | } |
caryclark@google.com | bfe9037 | 2012-11-21 13:56:20 +0000 | [diff] [blame] | 1637 | |
| 1638 | // pass, stroke rect |
| 1639 | SkPath src, dst; |
| 1640 | src.addRect(1, 1, 7, 7, SkPath::kCW_Direction); |
| 1641 | SkPaint strokePaint; |
| 1642 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 1643 | strokePaint.setStrokeWidth(2); |
| 1644 | strokePaint.getFillPath(src, &dst); |
| 1645 | REPORTER_ASSERT(reporter, dst.isNestedRects(0)); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1648 | static void write_and_read_back(skiatest::Reporter* reporter, |
| 1649 | const SkPath& p) { |
| 1650 | SkWriter32 writer(100); |
| 1651 | writer.writePath(p); |
| 1652 | size_t size = writer.size(); |
| 1653 | SkAutoMalloc storage(size); |
| 1654 | writer.flatten(storage.get()); |
| 1655 | SkReader32 reader(storage.get(), size); |
| 1656 | |
| 1657 | SkPath readBack; |
| 1658 | REPORTER_ASSERT(reporter, readBack != p); |
| 1659 | reader.readPath(&readBack); |
| 1660 | REPORTER_ASSERT(reporter, readBack == p); |
| 1661 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1662 | REPORTER_ASSERT(reporter, readBack.getConvexityOrUnknown() == |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1663 | p.getConvexityOrUnknown()); |
| 1664 | |
| 1665 | REPORTER_ASSERT(reporter, readBack.isOval(NULL) == p.isOval(NULL)); |
| 1666 | |
| 1667 | const SkRect& origBounds = p.getBounds(); |
| 1668 | const SkRect& readBackBounds = readBack.getBounds(); |
| 1669 | |
| 1670 | REPORTER_ASSERT(reporter, origBounds == readBackBounds); |
| 1671 | } |
| 1672 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1673 | static void test_flattening(skiatest::Reporter* reporter) { |
| 1674 | SkPath p; |
| 1675 | |
| 1676 | static const SkPoint pts[] = { |
| 1677 | { 0, 0 }, |
| 1678 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 1679 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 1680 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 1681 | }; |
| 1682 | p.moveTo(pts[0]); |
| 1683 | p.lineTo(pts[1]); |
| 1684 | p.quadTo(pts[2], pts[3]); |
| 1685 | p.cubicTo(pts[4], pts[5], pts[6]); |
| 1686 | |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1687 | write_and_read_back(reporter, p); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 1688 | |
| 1689 | // create a buffer that should be much larger than the path so we don't |
| 1690 | // kill our stack if writer goes too far. |
| 1691 | char buffer[1024]; |
| 1692 | uint32_t size1 = p.writeToMemory(NULL); |
| 1693 | uint32_t size2 = p.writeToMemory(buffer); |
| 1694 | REPORTER_ASSERT(reporter, size1 == size2); |
| 1695 | |
| 1696 | SkPath p2; |
| 1697 | uint32_t size3 = p2.readFromMemory(buffer); |
| 1698 | REPORTER_ASSERT(reporter, size1 == size3); |
| 1699 | REPORTER_ASSERT(reporter, p == p2); |
| 1700 | |
| 1701 | char buffer2[1024]; |
| 1702 | size3 = p2.writeToMemory(buffer2); |
| 1703 | REPORTER_ASSERT(reporter, size1 == size3); |
| 1704 | REPORTER_ASSERT(reporter, memcmp(buffer, buffer2, size1) == 0); |
robertphillips@google.com | 2972bb5 | 2012-08-07 17:32:51 +0000 | [diff] [blame] | 1705 | |
| 1706 | // test persistence of the oval flag & convexity |
| 1707 | { |
| 1708 | SkPath oval; |
| 1709 | SkRect rect = SkRect::MakeWH(10, 10); |
| 1710 | oval.addOval(rect); |
| 1711 | |
| 1712 | write_and_read_back(reporter, oval); |
| 1713 | } |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static void test_transform(skiatest::Reporter* reporter) { |
| 1717 | SkPath p, p1; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1718 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1719 | static const SkPoint pts[] = { |
| 1720 | { 0, 0 }, |
| 1721 | { SkIntToScalar(10), SkIntToScalar(10) }, |
| 1722 | { SkIntToScalar(20), SkIntToScalar(10) }, { SkIntToScalar(20), 0 }, |
| 1723 | { 0, 0 }, { 0, SkIntToScalar(10) }, { SkIntToScalar(1), SkIntToScalar(10) } |
| 1724 | }; |
| 1725 | p.moveTo(pts[0]); |
| 1726 | p.lineTo(pts[1]); |
| 1727 | p.quadTo(pts[2], pts[3]); |
| 1728 | p.cubicTo(pts[4], pts[5], pts[6]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1729 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1730 | SkMatrix matrix; |
| 1731 | matrix.reset(); |
| 1732 | p.transform(matrix, &p1); |
| 1733 | REPORTER_ASSERT(reporter, p == p1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1734 | |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 1735 | matrix.setScale(SK_Scalar1 * 2, SK_Scalar1 * 3); |
| 1736 | p.transform(matrix, &p1); |
| 1737 | SkPoint pts1[7]; |
| 1738 | int count = p1.getPoints(pts1, 7); |
| 1739 | REPORTER_ASSERT(reporter, 7 == count); |
| 1740 | for (int i = 0; i < count; ++i) { |
| 1741 | SkPoint newPt = SkPoint::Make(pts[i].fX * 2, pts[i].fY * 3); |
| 1742 | REPORTER_ASSERT(reporter, newPt == pts1[i]); |
| 1743 | } |
| 1744 | } |
| 1745 | |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1746 | static void test_zero_length_paths(skiatest::Reporter* reporter) { |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1747 | SkPath p; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1748 | uint8_t verbs[32]; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1749 | |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 1750 | struct SUPPRESS_VISIBILITY_WARNING zeroPathTestData { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1751 | const char* testPath; |
| 1752 | const size_t numResultPts; |
| 1753 | const SkRect resultBound; |
| 1754 | const SkPath::Verb* resultVerbs; |
| 1755 | const size_t numResultVerbs; |
| 1756 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1757 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1758 | static const SkPath::Verb resultVerbs1[] = { SkPath::kMove_Verb }; |
| 1759 | static const SkPath::Verb resultVerbs2[] = { SkPath::kMove_Verb, SkPath::kMove_Verb }; |
| 1760 | static const SkPath::Verb resultVerbs3[] = { SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 1761 | static const SkPath::Verb resultVerbs4[] = { SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb }; |
| 1762 | static const SkPath::Verb resultVerbs5[] = { SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 1763 | static const SkPath::Verb resultVerbs6[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb }; |
| 1764 | static const SkPath::Verb resultVerbs7[] = { SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb }; |
| 1765 | static const SkPath::Verb resultVerbs8[] = { |
| 1766 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb |
| 1767 | }; |
| 1768 | static const SkPath::Verb resultVerbs9[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 1769 | static const SkPath::Verb resultVerbs10[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb }; |
| 1770 | static const SkPath::Verb resultVerbs11[] = { SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb }; |
| 1771 | static const SkPath::Verb resultVerbs12[] = { |
| 1772 | SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kQuad_Verb, SkPath::kClose_Verb |
| 1773 | }; |
| 1774 | static const SkPath::Verb resultVerbs13[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 1775 | static const SkPath::Verb resultVerbs14[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb }; |
| 1776 | static const SkPath::Verb resultVerbs15[] = { SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb }; |
| 1777 | static const SkPath::Verb resultVerbs16[] = { |
| 1778 | SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kCubic_Verb, SkPath::kClose_Verb |
| 1779 | }; |
| 1780 | static const struct zeroPathTestData gZeroLengthTests[] = { |
| 1781 | { "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] | 1782 | { "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] | 1783 | { "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] | 1784 | { "M 1 1 z M 2 1 z", 2, {SK_Scalar1, SK_Scalar1, 2*SK_Scalar1, SK_Scalar1}, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 1785 | { "M 1 1 L 1 1", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs5, SK_ARRAY_COUNT(resultVerbs5) }, |
| 1786 | { "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) }, |
| 1787 | { "M 1 1 L 1 1 z", 2, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs7, SK_ARRAY_COUNT(resultVerbs7) }, |
| 1788 | { "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) }, |
| 1789 | { "M 1 1 Q 1 1 1 1", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs9, SK_ARRAY_COUNT(resultVerbs9) }, |
| 1790 | { "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) }, |
| 1791 | { "M 1 1 Q 1 1 1 1 z", 3, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs11, SK_ARRAY_COUNT(resultVerbs11) }, |
| 1792 | { "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) }, |
| 1793 | { "M 1 1 C 1 1 1 1 1 1", 4, {SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1}, resultVerbs13, SK_ARRAY_COUNT(resultVerbs13) }, |
| 1794 | { "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] | 1795 | SK_ARRAY_COUNT(resultVerbs14) |
| 1796 | }, |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1797 | { "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) }, |
| 1798 | { "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] | 1799 | SK_ARRAY_COUNT(resultVerbs16) |
| 1800 | } |
| 1801 | }; |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1802 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1803 | for (size_t i = 0; i < SK_ARRAY_COUNT(gZeroLengthTests); ++i) { |
| 1804 | p.reset(); |
| 1805 | bool valid = SkParsePath::FromSVGString(gZeroLengthTests[i].testPath, &p); |
| 1806 | REPORTER_ASSERT(reporter, valid); |
| 1807 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 1808 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultPts == (size_t)p.countPoints()); |
| 1809 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultBound == p.getBounds()); |
| 1810 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].numResultVerbs == (size_t)p.getVerbs(verbs, SK_ARRAY_COUNT(verbs))); |
| 1811 | for (size_t j = 0; j < gZeroLengthTests[i].numResultVerbs; ++j) { |
| 1812 | REPORTER_ASSERT(reporter, gZeroLengthTests[i].resultVerbs[j] == verbs[j]); |
| 1813 | } |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 1814 | } |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | struct SegmentInfo { |
| 1818 | SkPath fPath; |
| 1819 | int fPointCount; |
| 1820 | }; |
| 1821 | |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 1822 | #define kCurveSegmentMask (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask) |
| 1823 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1824 | static void test_segment_masks(skiatest::Reporter* reporter) { |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1825 | SkPath p, p2; |
| 1826 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1827 | p.moveTo(0, 0); |
| 1828 | p.quadTo(100, 100, 200, 200); |
| 1829 | REPORTER_ASSERT(reporter, SkPath::kQuad_SegmentMask == p.getSegmentMasks()); |
| 1830 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1831 | p2 = p; |
| 1832 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1833 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 1834 | REPORTER_ASSERT(reporter, kCurveSegmentMask == p.getSegmentMasks()); |
| 1835 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1836 | p2 = p; |
| 1837 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
| 1838 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1839 | p.reset(); |
| 1840 | p.moveTo(0, 0); |
| 1841 | p.cubicTo(100, 100, 200, 200, 300, 300); |
| 1842 | REPORTER_ASSERT(reporter, SkPath::kCubic_SegmentMask == p.getSegmentMasks()); |
reed@google.com | eef938c | 2012-08-01 20:01:49 +0000 | [diff] [blame] | 1843 | p2 = p; |
| 1844 | REPORTER_ASSERT(reporter, p2.getSegmentMasks() == p.getSegmentMasks()); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1845 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1846 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
| 1847 | } |
| 1848 | |
| 1849 | static void test_iter(skiatest::Reporter* reporter) { |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1850 | SkPath p; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1851 | SkPoint pts[4]; |
| 1852 | |
| 1853 | // Test an iterator with no path |
| 1854 | SkPath::Iter noPathIter; |
| 1855 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1856 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1857 | // Test that setting an empty path works |
| 1858 | noPathIter.setPath(p, false); |
| 1859 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1860 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1861 | // Test that close path makes no difference for an empty path |
| 1862 | noPathIter.setPath(p, true); |
| 1863 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1864 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1865 | // Test an iterator with an initial empty path |
| 1866 | SkPath::Iter iter(p, false); |
| 1867 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1868 | |
| 1869 | // Test that close path makes no difference |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1870 | iter.setPath(p, true); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1871 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1872 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1873 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1874 | struct iterTestData { |
| 1875 | const char* testPath; |
| 1876 | const bool forceClose; |
| 1877 | const bool consumeDegenerates; |
| 1878 | const size_t* numResultPtsPerVerb; |
| 1879 | const SkPoint* resultPts; |
| 1880 | const SkPath::Verb* resultVerbs; |
| 1881 | const size_t numResultVerbs; |
| 1882 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1883 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1884 | static const SkPath::Verb resultVerbs1[] = { SkPath::kDone_Verb }; |
| 1885 | static const SkPath::Verb resultVerbs2[] = { |
| 1886 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kDone_Verb |
| 1887 | }; |
| 1888 | static const SkPath::Verb resultVerbs3[] = { |
| 1889 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1890 | }; |
| 1891 | static const SkPath::Verb resultVerbs4[] = { |
| 1892 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1893 | }; |
| 1894 | static const SkPath::Verb resultVerbs5[] = { |
| 1895 | SkPath::kMove_Verb, SkPath::kLine_Verb, SkPath::kClose_Verb, SkPath::kMove_Verb, SkPath::kClose_Verb, SkPath::kDone_Verb |
| 1896 | }; |
| 1897 | static const size_t resultPtsSizes1[] = { 0 }; |
schenney@chromium.org | fedd09b | 2012-06-13 18:29:20 +0000 | [diff] [blame] | 1898 | static const size_t resultPtsSizes2[] = { 1, 2, 2, 0 }; |
| 1899 | static const size_t resultPtsSizes3[] = { 1, 2, 2, 2, 1, 0 }; |
| 1900 | static const size_t resultPtsSizes4[] = { 1, 2, 1, 1, 0 }; |
| 1901 | static const size_t resultPtsSizes5[] = { 1, 2, 1, 1, 1, 0 }; |
schenney@chromium.org | aaf1688 | 2012-06-13 17:41:00 +0000 | [diff] [blame] | 1902 | static const SkPoint* resultPts1 = 0; |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1903 | static const SkPoint resultPts2[] = { |
| 1904 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 } |
| 1905 | }; |
| 1906 | static const SkPoint resultPts3[] = { |
| 1907 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, SK_Scalar1 }, { SK_Scalar1, SK_Scalar1 }, { 0, SK_Scalar1 }, |
| 1908 | { 0, SK_Scalar1 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 } |
| 1909 | }; |
| 1910 | static const SkPoint resultPts4[] = { |
| 1911 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 1912 | }; |
| 1913 | static const SkPoint resultPts5[] = { |
| 1914 | { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { SK_Scalar1, 0 }, { 0, 0 }, { 0, 0 } |
| 1915 | }; |
| 1916 | static const struct iterTestData gIterTests[] = { |
| 1917 | { "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] | 1918 | { "M 1 0 M 2 0 M 3 0 M 4 0 M 5 0", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1919 | { "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] | 1920 | { "z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1921 | { "z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1922 | { "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) }, |
| 1923 | { "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] | 1924 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", false, true, resultPtsSizes2, resultPts2, resultVerbs2, SK_ARRAY_COUNT(resultVerbs2) }, |
| 1925 | { "M 1 0 L 1 1 L 0 1 M 0 0 z", true, true, resultPtsSizes3, resultPts3, resultVerbs3, SK_ARRAY_COUNT(resultVerbs3) }, |
| 1926 | { "M 1 0 L 1 0 M 0 0 z", false, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1927 | { "M 1 0 L 1 0 M 0 0 z", true, true, resultPtsSizes1, resultPts1, resultVerbs1, SK_ARRAY_COUNT(resultVerbs1) }, |
| 1928 | { "M 1 0 L 1 0 M 0 0 z", false, false, resultPtsSizes4, resultPts4, resultVerbs4, SK_ARRAY_COUNT(resultVerbs4) }, |
| 1929 | { "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] | 1930 | }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1931 | |
schenney@chromium.org | 7e96360 | 2012-06-13 17:05:43 +0000 | [diff] [blame] | 1932 | for (size_t i = 0; i < SK_ARRAY_COUNT(gIterTests); ++i) { |
| 1933 | p.reset(); |
| 1934 | bool valid = SkParsePath::FromSVGString(gIterTests[i].testPath, &p); |
| 1935 | REPORTER_ASSERT(reporter, valid); |
| 1936 | iter.setPath(p, gIterTests[i].forceClose); |
| 1937 | int j = 0, l = 0; |
| 1938 | do { |
| 1939 | REPORTER_ASSERT(reporter, iter.next(pts, gIterTests[i].consumeDegenerates) == gIterTests[i].resultVerbs[j]); |
| 1940 | for (int k = 0; k < (int)gIterTests[i].numResultPtsPerVerb[j]; ++k) { |
| 1941 | REPORTER_ASSERT(reporter, pts[k] == gIterTests[i].resultPts[l++]); |
| 1942 | } |
| 1943 | } while (gIterTests[i].resultVerbs[j++] != SkPath::kDone_Verb); |
| 1944 | REPORTER_ASSERT(reporter, j == (int)gIterTests[i].numResultVerbs); |
| 1945 | } |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1946 | |
| 1947 | // The GM degeneratesegments.cpp test is more extensive |
| 1948 | } |
| 1949 | |
| 1950 | static void test_raw_iter(skiatest::Reporter* reporter) { |
| 1951 | SkPath p; |
| 1952 | SkPoint pts[4]; |
| 1953 | |
| 1954 | // Test an iterator with no path |
| 1955 | SkPath::RawIter noPathIter; |
| 1956 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
| 1957 | // Test that setting an empty path works |
| 1958 | noPathIter.setPath(p); |
| 1959 | REPORTER_ASSERT(reporter, noPathIter.next(pts) == SkPath::kDone_Verb); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 1960 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 1961 | // Test an iterator with an initial empty path |
| 1962 | SkPath::RawIter iter(p); |
| 1963 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1964 | |
| 1965 | // Test that a move-only path returns the move. |
| 1966 | p.moveTo(SK_Scalar1, 0); |
| 1967 | iter.setPath(p); |
| 1968 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1969 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1970 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1971 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1972 | |
| 1973 | // No matter how many moves we add, we should get them all back |
| 1974 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 1975 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 1976 | iter.setPath(p); |
| 1977 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1978 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 1979 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 1980 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1981 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 1982 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 1983 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 1984 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 1985 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 1986 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1987 | |
| 1988 | // Initial close is never ever stored |
| 1989 | p.reset(); |
| 1990 | p.close(); |
| 1991 | iter.setPath(p); |
| 1992 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 1993 | |
| 1994 | // Move/close sequences |
| 1995 | p.reset(); |
| 1996 | p.close(); // Not stored, no purpose |
| 1997 | p.moveTo(SK_Scalar1, 0); |
| 1998 | p.close(); |
| 1999 | p.close(); // Not stored, no purpose |
| 2000 | p.moveTo(SK_Scalar1*2, SK_Scalar1); |
| 2001 | p.close(); |
| 2002 | p.moveTo(SK_Scalar1*3, SK_Scalar1*2); |
| 2003 | p.moveTo(SK_Scalar1*4, SK_Scalar1*3); |
| 2004 | p.close(); |
| 2005 | iter.setPath(p); |
| 2006 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2007 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2008 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2009 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2010 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1); |
| 2011 | REPORTER_ASSERT(reporter, pts[0].fY == 0); |
| 2012 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2013 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2014 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2015 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2016 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*2); |
| 2017 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1); |
| 2018 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2019 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*3); |
| 2020 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*2); |
| 2021 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kMove_Verb); |
| 2022 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2023 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2024 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kClose_Verb); |
| 2025 | REPORTER_ASSERT(reporter, pts[0].fX == SK_Scalar1*4); |
| 2026 | REPORTER_ASSERT(reporter, pts[0].fY == SK_Scalar1*3); |
| 2027 | REPORTER_ASSERT(reporter, iter.next(pts) == SkPath::kDone_Verb); |
| 2028 | |
| 2029 | // Generate random paths and verify |
| 2030 | SkPoint randomPts[25]; |
| 2031 | for (int i = 0; i < 5; ++i) { |
| 2032 | for (int j = 0; j < 5; ++j) { |
| 2033 | randomPts[i*5+j].set(SK_Scalar1*i, SK_Scalar1*j); |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | // Max of 10 segments, max 3 points per segment |
jvanverth@google.com | c490f80 | 2013-03-04 13:56:38 +0000 | [diff] [blame] | 2038 | SkMWCRandom rand(9876543); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2039 | SkPoint expectedPts[31]; // May have leading moveTo |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2040 | SkPath::Verb expectedVerbs[22]; // May have leading moveTo |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2041 | SkPath::Verb nextVerb; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2042 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2043 | for (int i = 0; i < 500; ++i) { |
| 2044 | p.reset(); |
| 2045 | bool lastWasClose = true; |
| 2046 | bool haveMoveTo = false; |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2047 | SkPoint lastMoveToPt = { 0, 0 }; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2048 | int numPoints = 0; |
| 2049 | int numVerbs = (rand.nextU() >> 16) % 10; |
| 2050 | int numIterVerbs = 0; |
| 2051 | for (int j = 0; j < numVerbs; ++j) { |
| 2052 | do { |
| 2053 | nextVerb = static_cast<SkPath::Verb>((rand.nextU() >> 16) % SkPath::kDone_Verb); |
| 2054 | } while (lastWasClose && nextVerb == SkPath::kClose_Verb); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2055 | switch (nextVerb) { |
| 2056 | case SkPath::kMove_Verb: |
| 2057 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2058 | p.moveTo(expectedPts[numPoints]); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2059 | lastMoveToPt = expectedPts[numPoints]; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2060 | numPoints += 1; |
| 2061 | lastWasClose = false; |
| 2062 | haveMoveTo = true; |
| 2063 | break; |
| 2064 | case SkPath::kLine_Verb: |
| 2065 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2066 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2067 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2068 | haveMoveTo = true; |
| 2069 | } |
| 2070 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2071 | p.lineTo(expectedPts[numPoints]); |
| 2072 | numPoints += 1; |
| 2073 | lastWasClose = false; |
| 2074 | break; |
| 2075 | case SkPath::kQuad_Verb: |
| 2076 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2077 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2078 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2079 | haveMoveTo = true; |
| 2080 | } |
| 2081 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2082 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2083 | p.quadTo(expectedPts[numPoints], expectedPts[numPoints + 1]); |
| 2084 | numPoints += 2; |
| 2085 | lastWasClose = false; |
| 2086 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2087 | case SkPath::kConic_Verb: |
| 2088 | if (!haveMoveTo) { |
| 2089 | expectedPts[numPoints++] = lastMoveToPt; |
| 2090 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2091 | haveMoveTo = true; |
| 2092 | } |
| 2093 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2094 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2095 | p.conicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2096 | rand.nextUScalar1() * 4); |
| 2097 | numPoints += 2; |
| 2098 | lastWasClose = false; |
| 2099 | break; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2100 | case SkPath::kCubic_Verb: |
| 2101 | if (!haveMoveTo) { |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2102 | expectedPts[numPoints++] = lastMoveToPt; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2103 | expectedVerbs[numIterVerbs++] = SkPath::kMove_Verb; |
| 2104 | haveMoveTo = true; |
| 2105 | } |
| 2106 | expectedPts[numPoints] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2107 | expectedPts[numPoints + 1] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2108 | expectedPts[numPoints + 2] = randomPts[(rand.nextU() >> 16) % 25]; |
| 2109 | p.cubicTo(expectedPts[numPoints], expectedPts[numPoints + 1], |
| 2110 | expectedPts[numPoints + 2]); |
| 2111 | numPoints += 3; |
| 2112 | lastWasClose = false; |
| 2113 | break; |
| 2114 | case SkPath::kClose_Verb: |
| 2115 | p.close(); |
reed@google.com | d335d1d | 2012-01-12 18:17:11 +0000 | [diff] [blame] | 2116 | haveMoveTo = false; |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2117 | lastWasClose = true; |
| 2118 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2119 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2120 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2121 | } |
| 2122 | expectedVerbs[numIterVerbs++] = nextVerb; |
| 2123 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 2124 | |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2125 | iter.setPath(p); |
| 2126 | numVerbs = numIterVerbs; |
| 2127 | numIterVerbs = 0; |
| 2128 | int numIterPts = 0; |
| 2129 | SkPoint lastMoveTo; |
| 2130 | SkPoint lastPt; |
| 2131 | lastMoveTo.set(0, 0); |
| 2132 | lastPt.set(0, 0); |
| 2133 | while ((nextVerb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 2134 | REPORTER_ASSERT(reporter, nextVerb == expectedVerbs[numIterVerbs]); |
| 2135 | numIterVerbs++; |
| 2136 | switch (nextVerb) { |
| 2137 | case SkPath::kMove_Verb: |
| 2138 | REPORTER_ASSERT(reporter, numIterPts < numPoints); |
| 2139 | REPORTER_ASSERT(reporter, pts[0] == expectedPts[numIterPts]); |
| 2140 | lastPt = lastMoveTo = pts[0]; |
| 2141 | numIterPts += 1; |
| 2142 | break; |
| 2143 | case SkPath::kLine_Verb: |
| 2144 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 1); |
| 2145 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2146 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2147 | lastPt = pts[1]; |
| 2148 | numIterPts += 1; |
| 2149 | break; |
| 2150 | case SkPath::kQuad_Verb: |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2151 | case SkPath::kConic_Verb: |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2152 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 2); |
| 2153 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2154 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2155 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2156 | lastPt = pts[2]; |
| 2157 | numIterPts += 2; |
| 2158 | break; |
| 2159 | case SkPath::kCubic_Verb: |
| 2160 | REPORTER_ASSERT(reporter, numIterPts < numPoints + 3); |
| 2161 | REPORTER_ASSERT(reporter, pts[0] == lastPt); |
| 2162 | REPORTER_ASSERT(reporter, pts[1] == expectedPts[numIterPts]); |
| 2163 | REPORTER_ASSERT(reporter, pts[2] == expectedPts[numIterPts + 1]); |
| 2164 | REPORTER_ASSERT(reporter, pts[3] == expectedPts[numIterPts + 2]); |
| 2165 | lastPt = pts[3]; |
| 2166 | numIterPts += 3; |
| 2167 | break; |
| 2168 | case SkPath::kClose_Verb: |
| 2169 | REPORTER_ASSERT(reporter, pts[0] == lastMoveTo); |
| 2170 | lastPt = lastMoveTo; |
| 2171 | break; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2172 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 2173 | SkDEBUGFAIL("unexpected verb"); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | REPORTER_ASSERT(reporter, numIterPts == numPoints); |
| 2177 | REPORTER_ASSERT(reporter, numIterVerbs == numVerbs); |
| 2178 | } |
| 2179 | } |
| 2180 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2181 | static void check_for_circle(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2182 | const SkPath& path, |
| 2183 | bool expectedCircle, |
| 2184 | SkPath::Direction expectedDir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2185 | SkRect rect; |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2186 | REPORTER_ASSERT(reporter, path.isOval(&rect) == expectedCircle); |
| 2187 | REPORTER_ASSERT(reporter, path.cheapIsDirection(expectedDir)); |
skia.committer@gmail.com | fbb0ed9 | 2012-11-13 21:46:06 +0000 | [diff] [blame] | 2188 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2189 | if (expectedCircle) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2190 | REPORTER_ASSERT(reporter, rect.height() == rect.width()); |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | static void test_circle_skew(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2195 | const SkPath& path, |
| 2196 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2197 | SkPath tmp; |
| 2198 | |
| 2199 | SkMatrix m; |
| 2200 | m.setSkew(SkIntToScalar(3), SkIntToScalar(5)); |
| 2201 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2202 | // this matrix reverses the direction. |
| 2203 | if (SkPath::kCCW_Direction == dir) { |
| 2204 | dir = SkPath::kCW_Direction; |
| 2205 | } else { |
| 2206 | SkASSERT(SkPath::kCW_Direction == dir); |
| 2207 | dir = SkPath::kCCW_Direction; |
| 2208 | } |
| 2209 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | static void test_circle_translate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2213 | const SkPath& path, |
| 2214 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2215 | SkPath tmp; |
| 2216 | |
| 2217 | // translate at small offset |
| 2218 | SkMatrix m; |
| 2219 | m.setTranslate(SkIntToScalar(15), SkIntToScalar(15)); |
| 2220 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2221 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2222 | |
| 2223 | tmp.reset(); |
| 2224 | m.reset(); |
| 2225 | |
| 2226 | // translate at a relatively big offset |
| 2227 | m.setTranslate(SkIntToScalar(1000), SkIntToScalar(1000)); |
| 2228 | path.transform(m, &tmp); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2229 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | static void test_circle_rotate(skiatest::Reporter* reporter, |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2233 | const SkPath& path, |
| 2234 | SkPath::Direction dir) { |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2235 | for (int angle = 0; angle < 360; ++angle) { |
| 2236 | SkPath tmp; |
| 2237 | SkMatrix m; |
| 2238 | m.setRotate(SkIntToScalar(angle)); |
| 2239 | path.transform(m, &tmp); |
| 2240 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2241 | // 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] | 2242 | // degrees is not an oval anymore, this can be improved. we made this |
| 2243 | // for the simplicity of our implementation. |
| 2244 | if (angle % 90 == 0) { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2245 | check_for_circle(reporter, tmp, true, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2246 | } else { |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2247 | check_for_circle(reporter, tmp, false, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2252 | static void test_circle_mirror_x(skiatest::Reporter* reporter, |
| 2253 | const SkPath& path, |
| 2254 | SkPath::Direction dir) { |
| 2255 | SkPath tmp; |
| 2256 | SkMatrix m; |
| 2257 | m.reset(); |
| 2258 | m.setScaleX(-SK_Scalar1); |
| 2259 | path.transform(m, &tmp); |
| 2260 | |
| 2261 | if (SkPath::kCW_Direction == dir) { |
| 2262 | dir = SkPath::kCCW_Direction; |
| 2263 | } else { |
| 2264 | SkASSERT(SkPath::kCCW_Direction == dir); |
| 2265 | dir = SkPath::kCW_Direction; |
| 2266 | } |
| 2267 | |
| 2268 | check_for_circle(reporter, tmp, true, dir); |
| 2269 | } |
| 2270 | |
| 2271 | static void test_circle_mirror_y(skiatest::Reporter* reporter, |
| 2272 | const SkPath& path, |
| 2273 | SkPath::Direction dir) { |
| 2274 | SkPath tmp; |
| 2275 | SkMatrix m; |
| 2276 | m.reset(); |
| 2277 | m.setScaleY(-SK_Scalar1); |
| 2278 | path.transform(m, &tmp); |
| 2279 | |
| 2280 | if (SkPath::kCW_Direction == dir) { |
| 2281 | dir = SkPath::kCCW_Direction; |
| 2282 | } else { |
| 2283 | SkASSERT(SkPath::kCCW_Direction == dir); |
| 2284 | dir = SkPath::kCW_Direction; |
| 2285 | } |
| 2286 | |
| 2287 | check_for_circle(reporter, tmp, true, dir); |
| 2288 | } |
| 2289 | |
| 2290 | static void test_circle_mirror_xy(skiatest::Reporter* reporter, |
| 2291 | const SkPath& path, |
| 2292 | SkPath::Direction dir) { |
| 2293 | SkPath tmp; |
| 2294 | SkMatrix m; |
| 2295 | m.reset(); |
| 2296 | m.setScaleX(-SK_Scalar1); |
| 2297 | m.setScaleY(-SK_Scalar1); |
| 2298 | path.transform(m, &tmp); |
| 2299 | |
| 2300 | check_for_circle(reporter, tmp, true, dir); |
| 2301 | } |
| 2302 | |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2303 | static void test_circle_with_direction(skiatest::Reporter* reporter, |
| 2304 | SkPath::Direction dir) { |
| 2305 | SkPath path; |
| 2306 | |
| 2307 | // circle at origin |
| 2308 | path.addCircle(0, 0, SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2309 | check_for_circle(reporter, path, true, dir); |
| 2310 | test_circle_rotate(reporter, path, dir); |
| 2311 | test_circle_translate(reporter, path, dir); |
| 2312 | test_circle_skew(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2313 | |
| 2314 | // circle at an offset at (10, 10) |
| 2315 | path.reset(); |
| 2316 | path.addCircle(SkIntToScalar(10), SkIntToScalar(10), |
| 2317 | SkIntToScalar(20), dir); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2318 | check_for_circle(reporter, path, true, dir); |
| 2319 | test_circle_rotate(reporter, path, dir); |
| 2320 | test_circle_translate(reporter, path, dir); |
| 2321 | test_circle_skew(reporter, path, dir); |
| 2322 | test_circle_mirror_x(reporter, path, dir); |
| 2323 | test_circle_mirror_y(reporter, path, dir); |
| 2324 | test_circle_mirror_xy(reporter, path, dir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | static void test_circle_with_add_paths(skiatest::Reporter* reporter) { |
| 2328 | SkPath path; |
| 2329 | SkPath circle; |
| 2330 | SkPath rect; |
| 2331 | SkPath empty; |
| 2332 | |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2333 | static const SkPath::Direction kCircleDir = SkPath::kCW_Direction; |
| 2334 | static const SkPath::Direction kCircleDirOpposite = SkPath::kCCW_Direction; |
| 2335 | |
| 2336 | circle.addCircle(0, 0, SkIntToScalar(10), kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2337 | rect.addRect(SkIntToScalar(5), SkIntToScalar(5), |
| 2338 | SkIntToScalar(20), SkIntToScalar(20), SkPath::kCW_Direction); |
| 2339 | |
| 2340 | SkMatrix translate; |
| 2341 | translate.setTranslate(SkIntToScalar(12), SkIntToScalar(12)); |
| 2342 | |
| 2343 | // For simplicity, all the path concatenation related operations |
| 2344 | // would mark it non-circle, though in theory it's still a circle. |
| 2345 | |
| 2346 | // empty + circle (translate) |
| 2347 | path = empty; |
| 2348 | path.addPath(circle, translate); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2349 | check_for_circle(reporter, path, false, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2350 | |
| 2351 | // circle + empty (translate) |
| 2352 | path = circle; |
| 2353 | path.addPath(empty, translate); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2354 | check_for_circle(reporter, path, false, kCircleDir); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2355 | |
| 2356 | // test reverseAddPath |
| 2357 | path = circle; |
| 2358 | path.reverseAddPath(rect); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2359 | check_for_circle(reporter, path, false, kCircleDirOpposite); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | static void test_circle(skiatest::Reporter* reporter) { |
| 2363 | test_circle_with_direction(reporter, SkPath::kCW_Direction); |
| 2364 | test_circle_with_direction(reporter, SkPath::kCCW_Direction); |
| 2365 | |
| 2366 | // multiple addCircle() |
| 2367 | SkPath path; |
| 2368 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2369 | path.addCircle(0, 0, SkIntToScalar(20), SkPath::kCW_Direction); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2370 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2371 | |
| 2372 | // some extra lineTo() would make isOval() fail |
| 2373 | path.reset(); |
| 2374 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2375 | path.lineTo(0, 0); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2376 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2377 | |
| 2378 | // not back to the original point |
| 2379 | path.reset(); |
| 2380 | path.addCircle(0, 0, SkIntToScalar(10), SkPath::kCW_Direction); |
| 2381 | path.setLastPt(SkIntToScalar(5), SkIntToScalar(5)); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 2382 | check_for_circle(reporter, path, false, SkPath::kCW_Direction); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2383 | |
| 2384 | test_circle_with_add_paths(reporter); |
| 2385 | } |
| 2386 | |
| 2387 | static void test_oval(skiatest::Reporter* reporter) { |
| 2388 | SkRect rect; |
| 2389 | SkMatrix m; |
| 2390 | SkPath path; |
| 2391 | |
| 2392 | rect = SkRect::MakeWH(SkIntToScalar(30), SkIntToScalar(50)); |
| 2393 | path.addOval(rect); |
| 2394 | |
| 2395 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2396 | |
| 2397 | m.setRotate(SkIntToScalar(90)); |
| 2398 | SkPath tmp; |
| 2399 | path.transform(m, &tmp); |
| 2400 | // an oval rotated 90 degrees is still an oval. |
| 2401 | REPORTER_ASSERT(reporter, tmp.isOval(NULL)); |
| 2402 | |
| 2403 | m.reset(); |
| 2404 | m.setRotate(SkIntToScalar(30)); |
| 2405 | tmp.reset(); |
| 2406 | path.transform(m, &tmp); |
| 2407 | // an oval rotated 30 degrees is not an oval anymore. |
| 2408 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2409 | |
| 2410 | // since empty path being transformed. |
| 2411 | path.reset(); |
| 2412 | tmp.reset(); |
| 2413 | m.reset(); |
| 2414 | path.transform(m, &tmp); |
| 2415 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2416 | |
| 2417 | // empty path is not an oval |
| 2418 | tmp.reset(); |
| 2419 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2420 | |
| 2421 | // only has moveTo()s |
| 2422 | tmp.reset(); |
| 2423 | tmp.moveTo(0, 0); |
| 2424 | tmp.moveTo(SkIntToScalar(10), SkIntToScalar(10)); |
| 2425 | REPORTER_ASSERT(reporter, !tmp.isOval(NULL)); |
| 2426 | |
| 2427 | // mimic WebKit's calling convention, |
| 2428 | // call moveTo() first and then call addOval() |
| 2429 | path.reset(); |
| 2430 | path.moveTo(0, 0); |
| 2431 | path.addOval(rect); |
| 2432 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2433 | |
| 2434 | // copy path |
| 2435 | path.reset(); |
| 2436 | tmp.reset(); |
| 2437 | tmp.addOval(rect); |
| 2438 | path = tmp; |
| 2439 | REPORTER_ASSERT(reporter, path.isOval(NULL)); |
| 2440 | } |
| 2441 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2442 | static void test_empty(skiatest::Reporter* reporter, const SkPath& p) { |
| 2443 | SkPath empty; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2444 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2445 | REPORTER_ASSERT(reporter, p.isEmpty()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2446 | REPORTER_ASSERT(reporter, 0 == p.countPoints()); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2447 | REPORTER_ASSERT(reporter, 0 == p.countVerbs()); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2448 | REPORTER_ASSERT(reporter, 0 == p.getSegmentMasks()); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 2449 | REPORTER_ASSERT(reporter, p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2450 | REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); |
| 2451 | REPORTER_ASSERT(reporter, !p.isInverseFillType()); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2452 | REPORTER_ASSERT(reporter, p == empty); |
| 2453 | REPORTER_ASSERT(reporter, !(p != empty)); |
| 2454 | } |
| 2455 | |
| 2456 | static void TestPath(skiatest::Reporter* reporter) { |
| 2457 | SkTSize<SkScalar>::Make(3,4); |
| 2458 | |
| 2459 | SkPath p, empty; |
| 2460 | SkRect bounds, bounds2; |
| 2461 | test_empty(reporter, p); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2462 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 2463 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2464 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2465 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 2466 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 2467 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 2468 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2469 | // we have quads or cubics |
| 2470 | REPORTER_ASSERT(reporter, p.getSegmentMasks() & kCurveSegmentMask); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2471 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 2472 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 2473 | p.reset(); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2474 | test_empty(reporter, p); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2475 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 2476 | p.addOval(bounds); |
| 2477 | check_convex_bounds(reporter, p, bounds); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2478 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 2479 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2480 | p.rewind(); |
| 2481 | test_empty(reporter, p); |
| 2482 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2483 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 2484 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 10296cc | 2011-09-21 12:29:05 +0000 | [diff] [blame] | 2485 | // we have only lines |
| 2486 | REPORTER_ASSERT(reporter, SkPath::kLine_SegmentMask == p.getSegmentMasks()); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2487 | REPORTER_ASSERT(reporter, !p.isEmpty()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2488 | |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 2489 | REPORTER_ASSERT(reporter, p != empty); |
| 2490 | REPORTER_ASSERT(reporter, !(p == empty)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2491 | |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2492 | // do getPoints and getVerbs return the right result |
| 2493 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 0) == 4); |
| 2494 | REPORTER_ASSERT(reporter, p.getVerbs(NULL, 0) == 5); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2495 | SkPoint pts[4]; |
| 2496 | int count = p.getPoints(pts, 4); |
| 2497 | REPORTER_ASSERT(reporter, count == 4); |
bsalomon@google.com | df9d656 | 2012-06-07 21:43:15 +0000 | [diff] [blame] | 2498 | uint8_t verbs[6]; |
| 2499 | verbs[5] = 0xff; |
| 2500 | p.getVerbs(verbs, 5); |
| 2501 | REPORTER_ASSERT(reporter, SkPath::kMove_Verb == verbs[0]); |
| 2502 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[1]); |
| 2503 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[2]); |
| 2504 | REPORTER_ASSERT(reporter, SkPath::kLine_Verb == verbs[3]); |
| 2505 | REPORTER_ASSERT(reporter, SkPath::kClose_Verb == verbs[4]); |
| 2506 | REPORTER_ASSERT(reporter, 0xff == verbs[5]); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2507 | bounds2.set(pts, 4); |
| 2508 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2509 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2510 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 2511 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 2512 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2513 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2514 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
caryclark@google.com | f131694 | 2011-07-26 19:54:45 +0000 | [diff] [blame] | 2515 | bounds2.setEmpty(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2516 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 2517 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 2518 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2519 | // now force p to not be a rect |
| 2520 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 2521 | p.addRect(bounds); |
| 2522 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2523 | |
reed@google.com | 7e6c4d1 | 2012-05-10 14:05:43 +0000 | [diff] [blame] | 2524 | test_isLine(reporter); |
| 2525 | test_isRect(reporter); |
caryclark@google.com | 56f233a | 2012-11-19 13:06:06 +0000 | [diff] [blame] | 2526 | test_isNestedRects(reporter); |
schenney@chromium.org | 4da06ab | 2011-12-20 15:14:18 +0000 | [diff] [blame] | 2527 | test_zero_length_paths(reporter); |
reed@google.com | cabaf1d | 2012-01-11 21:03:05 +0000 | [diff] [blame] | 2528 | test_direction(reporter); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 2529 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 2530 | test_convexity2(reporter); |
bsalomon@google.com | 9bee33a | 2012-11-13 21:51:38 +0000 | [diff] [blame] | 2531 | test_conservativelyContains(reporter); |
bsalomon@google.com | b3b8dfa | 2011-07-13 17:44:36 +0000 | [diff] [blame] | 2532 | test_close(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2533 | test_segment_masks(reporter); |
reed@google.com | 53effc5 | 2011-09-21 19:05:12 +0000 | [diff] [blame] | 2534 | test_flattening(reporter); |
| 2535 | test_transform(reporter); |
reed@google.com | 3563c9e | 2011-11-14 19:34:57 +0000 | [diff] [blame] | 2536 | test_bounds(reporter); |
schenney@chromium.org | 6630d8d | 2012-01-04 21:05:51 +0000 | [diff] [blame] | 2537 | test_iter(reporter); |
| 2538 | test_raw_iter(reporter); |
bsalomon@google.com | 6aa2965 | 2012-04-18 13:29:52 +0000 | [diff] [blame] | 2539 | test_circle(reporter); |
| 2540 | test_oval(reporter); |
reed@google.com | 8b06f1a | 2012-05-29 12:03:46 +0000 | [diff] [blame] | 2541 | test_strokerec(reporter); |
reed@google.com | 744faba | 2012-05-29 19:54:52 +0000 | [diff] [blame] | 2542 | test_addPoly(reporter); |
reed@google.com | 0bb18bb | 2012-07-26 15:20:36 +0000 | [diff] [blame] | 2543 | test_isfinite(reporter); |
tomhudson@google.com | ed02c4d | 2012-08-10 14:10:45 +0000 | [diff] [blame] | 2544 | test_isfinite_after_transform(reporter); |
robertphillips@google.com | b95eaa8 | 2012-10-18 15:26:12 +0000 | [diff] [blame] | 2545 | test_arb_round_rect_is_convex(reporter); |
robertphillips@google.com | 158618e | 2012-10-23 16:56:56 +0000 | [diff] [blame] | 2546 | test_arb_zero_rad_round_rect_is_rect(reporter); |
reed@google.com | a8790de | 2012-10-24 21:04:04 +0000 | [diff] [blame] | 2547 | test_addrect_isfinite(reporter); |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 2548 | test_tricky_cubic(); |
| 2549 | test_clipped_cubic(); |
| 2550 | test_crbug_170666(); |
reed@google.com | 7a90daf | 2013-04-10 18:44:00 +0000 | [diff] [blame] | 2551 | test_bad_cubic_crbug229478(); |
reed@google.com | 3eff359 | 2013-05-08 21:08:21 +0000 | [diff] [blame] | 2552 | test_bad_cubic_crbug234190(); |
mtklein@google.com | 9c9d4a7 | 2013-08-07 19:17:53 +0000 | [diff] [blame] | 2553 | test_android_specific_behavior(reporter); |
commit-bot@chromium.org | 9d54aeb | 2013-08-09 19:48:26 +0000 | [diff] [blame] | 2554 | test_path_close_issue1474(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | #include "TestClassDef.h" |
| 2558 | DEFINE_TESTCLASS("Path", PathTestClass, TestPath) |