caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 7 | |
| 8 | #include "PathOpsExtendedTest.h" |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 9 | #include "PathOpsTestCommon.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 11 | #include "Test.h" |
| 12 | |
| 13 | DEF_TEST(PathOpsBuilder, reporter) { |
| 14 | SkOpBuilder builder; |
| 15 | SkPath result; |
| 16 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 17 | REPORTER_ASSERT(reporter, result.isEmpty()); |
| 18 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 19 | builder.add(result, kDifference_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 20 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 21 | REPORTER_ASSERT(reporter, result.isEmpty()); |
| 22 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 23 | builder.add(result, kUnion_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 24 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 25 | REPORTER_ASSERT(reporter, result.isEmpty()); |
| 26 | |
| 27 | SkPath rectPath; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 28 | rectPath.setFillType(SkPath::kEvenOdd_FillType); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 29 | rectPath.addRect(0, 1, 2, 3, SkPath::kCW_Direction); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 30 | builder.add(rectPath, kUnion_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 31 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 32 | bool closed; |
| 33 | SkPath::Direction dir; |
| 34 | REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir)); |
| 35 | REPORTER_ASSERT(reporter, closed); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 36 | REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 37 | int pixelDiff = comparePaths(reporter, __FUNCTION__, rectPath, result); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 38 | REPORTER_ASSERT(reporter, pixelDiff == 0); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 39 | |
| 40 | rectPath.reset(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 41 | rectPath.setFillType(SkPath::kEvenOdd_FillType); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 42 | rectPath.addRect(0, 1, 2, 3, SkPath::kCCW_Direction); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 43 | builder.add(rectPath, kUnion_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 45 | REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir)); |
| 46 | REPORTER_ASSERT(reporter, closed); |
| 47 | REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 48 | REPORTER_ASSERT(reporter, rectPath == result); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 49 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 50 | builder.add(rectPath, kDifference_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 51 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 52 | REPORTER_ASSERT(reporter, result.isEmpty()); |
| 53 | |
| 54 | SkPath rect2, rect3; |
| 55 | rect2.addRect(2, 1, 4, 3, SkPath::kCW_Direction); |
| 56 | rect3.addRect(4, 1, 5, 3, SkPath::kCCW_Direction); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 57 | builder.add(rectPath, kUnion_SkPathOp); |
| 58 | builder.add(rect2, kUnion_SkPathOp); |
| 59 | builder.add(rect3, kUnion_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
| 61 | REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir)); |
| 62 | REPORTER_ASSERT(reporter, closed); |
| 63 | SkRect expected; |
| 64 | expected.set(0, 1, 5, 3); |
| 65 | REPORTER_ASSERT(reporter, result.getBounds() == expected); |
| 66 | |
| 67 | SkPath circle1, circle2, circle3; |
| 68 | circle1.addCircle(5, 6, 4, SkPath::kCW_Direction); |
| 69 | circle2.addCircle(7, 4, 8, SkPath::kCCW_Direction); |
| 70 | circle3.addCircle(6, 5, 6, SkPath::kCW_Direction); |
| 71 | SkPath opCompare; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 72 | Op(circle1, circle2, kUnion_SkPathOp, &opCompare); |
| 73 | Op(opCompare, circle3, kDifference_SkPathOp, &opCompare); |
| 74 | builder.add(circle1, kUnion_SkPathOp); |
| 75 | builder.add(circle2, kUnion_SkPathOp); |
| 76 | builder.add(circle3, kDifference_SkPathOp); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, builder.resolve(&result)); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 78 | pixelDiff = comparePaths(reporter, __FUNCTION__, opCompare, result); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, pixelDiff == 0); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 80 | } |
caryclark | fba9da7 | 2015-05-14 14:18:13 -0700 | [diff] [blame] | 81 | |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 82 | DEF_TEST(BuilderIssue3838, reporter) { |
caryclark | fba9da7 | 2015-05-14 14:18:13 -0700 | [diff] [blame] | 83 | SkPath path; |
| 84 | path.moveTo(200, 170); |
| 85 | path.lineTo(220, 170); |
| 86 | path.lineTo(220, 230); |
| 87 | path.lineTo(240, 230); |
| 88 | path.lineTo(240, 210); |
| 89 | path.lineTo(180, 210); |
| 90 | path.lineTo(180, 190); |
| 91 | path.lineTo(260, 190); |
| 92 | path.lineTo(260, 250); |
| 93 | path.lineTo(200, 250); |
| 94 | path.lineTo(200, 170); |
| 95 | path.close(); |
caryclark | fba9da7 | 2015-05-14 14:18:13 -0700 | [diff] [blame] | 96 | SkPath path2; |
| 97 | SkOpBuilder builder; |
| 98 | builder.add(path, kUnion_SkPathOp); |
| 99 | builder.resolve(&path2); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 100 | int pixelDiff = comparePaths(reporter, __FUNCTION__, path, path2); |
caryclark | fba9da7 | 2015-05-14 14:18:13 -0700 | [diff] [blame] | 101 | REPORTER_ASSERT(reporter, pixelDiff == 0); |
| 102 | } |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 103 | |
| 104 | DEF_TEST(BuilderIssue3838_2, reporter) { |
| 105 | SkPath path; |
| 106 | path.addCircle(100, 100, 50); |
| 107 | |
| 108 | SkOpBuilder builder; |
| 109 | builder.add(path, kUnion_SkPathOp); |
| 110 | builder.add(path, kUnion_SkPathOp); |
| 111 | |
| 112 | SkPath result; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 113 | builder.resolve(&result); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 114 | int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result); |
| 115 | REPORTER_ASSERT(reporter, pixelDiff == 0); |
| 116 | } |
| 117 | |
| 118 | DEF_TEST(BuilderIssue3838_3, reporter) { |
| 119 | SkPath path; |
| 120 | path.moveTo(40, 10); |
| 121 | path.lineTo(60, 10); |
| 122 | path.lineTo(60, 30); |
| 123 | path.lineTo(40, 30); |
| 124 | path.lineTo(40, 10); |
| 125 | path.moveTo(41, 11); |
| 126 | path.lineTo(41, 29); |
| 127 | path.lineTo(59, 29); |
| 128 | path.lineTo(59, 11); |
| 129 | path.lineTo(41, 11); |
| 130 | |
| 131 | SkOpBuilder builder; |
| 132 | builder.add(path, kUnion_SkPathOp); |
| 133 | SkPath result; |
| 134 | builder.resolve(&result); |
| 135 | int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, pixelDiff == 0); |
| 137 | } |
caryclark | 218f21a | 2015-06-29 11:41:52 -0700 | [diff] [blame^] | 138 | |
| 139 | DEF_TEST(BuilderIssue502792_2, reporter) { |
| 140 | SkPath path, pathB; |
| 141 | path.setFillType(SkPath::kWinding_FillType); |
| 142 | path.addRect(0, 0, 1, 1, SkPath::kCW_Direction); |
| 143 | path.addRect(2, 2, 3, 3, SkPath::kCW_Direction); |
| 144 | pathB.setFillType(SkPath::kEvenOdd_FillType); |
| 145 | pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction); |
| 146 | pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction); |
| 147 | SkOpBuilder builder; |
| 148 | builder.add(path, kUnion_SkPathOp); |
| 149 | builder.add(pathB, kDifference_SkPathOp); |
| 150 | SkPath result; |
| 151 | builder.resolve(&result); |
| 152 | } |