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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkMatrix.h" |
| 9 | #include "include/pathops/SkPathOps.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 10 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkPathPriv.h" |
| 12 | #include "src/pathops/SkOpEdgeBuilder.h" |
| 13 | #include "src/pathops/SkPathOpsCommon.h" |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 14 | |
| 15 | static bool one_contour(const SkPath& path) { |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 16 | SkSTArenaAlloc<256> allocator; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 17 | int verbCount = path.countVerbs(); |
Herb Derby | c3cc5fa | 2017-03-07 11:11:47 -0500 | [diff] [blame] | 18 | uint8_t* verbs = (uint8_t*) allocator.makeArrayDefault<uint8_t>(verbCount); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 19 | (void) path.getVerbs(verbs, verbCount); |
| 20 | for (int index = 1; index < verbCount; ++index) { |
| 21 | if (verbs[index] == SkPath::kMove_Verb) { |
| 22 | return false; |
| 23 | } |
| 24 | } |
| 25 | return true; |
| 26 | } |
| 27 | |
caryclark | 51c5678 | 2016-11-07 05:09:28 -0800 | [diff] [blame] | 28 | void SkOpBuilder::ReversePath(SkPath* path) { |
| 29 | SkPath temp; |
| 30 | SkPoint lastPt; |
| 31 | SkAssertResult(path->getLastPt(&lastPt)); |
| 32 | temp.moveTo(lastPt); |
| 33 | temp.reversePathTo(*path); |
| 34 | temp.close(); |
| 35 | *path = temp; |
| 36 | } |
| 37 | |
| 38 | bool SkOpBuilder::FixWinding(SkPath* path) { |
Mike Reed | cf0e3c6 | 2019-12-03 16:26:15 -0500 | [diff] [blame] | 39 | SkPathFillType fillType = path->getFillType(); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 40 | if (fillType == SkPathFillType::kInverseEvenOdd) { |
| 41 | fillType = SkPathFillType::kInverseWinding; |
| 42 | } else if (fillType == SkPathFillType::kEvenOdd) { |
| 43 | fillType = SkPathFillType::kWinding; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 44 | } |
Mike Reed | 85f51b2 | 2020-08-30 10:32:06 -0400 | [diff] [blame^] | 45 | if (one_contour(*path)) { |
| 46 | SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*path); |
| 47 | if (dir != SkPathFirstDirection::kUnknown) { |
| 48 | if (dir == SkPathFirstDirection::kCW) { |
| 49 | ReversePath(path); |
| 50 | } |
| 51 | path->setFillType(fillType); |
| 52 | return true; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 53 | } |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 54 | } |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 55 | SkSTArenaAlloc<4096> allocator; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 56 | SkOpContourHead contourHead; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 57 | SkOpGlobalState globalState(&contourHead, &allocator SkDEBUGPARAMS(false) |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 58 | SkDEBUGPARAMS(nullptr)); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 59 | SkOpEdgeBuilder builder(*path, &contourHead, &globalState); |
caryclark | 7b33bf1 | 2016-07-21 08:53:32 -0700 | [diff] [blame] | 60 | if (builder.unparseable() || !builder.finish()) { |
| 61 | return false; |
| 62 | } |
| 63 | if (!contourHead.count()) { |
| 64 | return true; |
| 65 | } |
caryclark | e3a4e99 | 2016-09-28 09:22:17 -0700 | [diff] [blame] | 66 | if (!contourHead.next()) { |
| 67 | return false; |
| 68 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 69 | contourHead.joinAllSegments(); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 70 | contourHead.resetReverse(); |
| 71 | bool writePath = false; |
| 72 | SkOpSpan* topSpan; |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 73 | globalState.setPhase(SkOpPhase::kFixWinding); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 74 | while ((topSpan = FindSortableTop(&contourHead))) { |
| 75 | SkOpSegment* topSegment = topSpan->segment(); |
| 76 | SkOpContour* topContour = topSegment->contour(); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 77 | SkASSERT(topContour->isCcw() >= 0); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 78 | #if DEBUG_WINDING |
| 79 | SkDebugf("%s id=%d nested=%d ccw=%d\n", __FUNCTION__, |
| 80 | topSegment->debugID(), globalState.nested(), topContour->isCcw()); |
| 81 | #endif |
| 82 | if ((globalState.nested() & 1) != SkToBool(topContour->isCcw())) { |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 83 | topContour->setReverse(); |
| 84 | writePath = true; |
| 85 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 86 | topContour->markAllDone(); |
caryclark | 4e1a4c9 | 2015-05-18 12:56:57 -0700 | [diff] [blame] | 87 | globalState.clearNested(); |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 88 | } |
| 89 | if (!writePath) { |
| 90 | path->setFillType(fillType); |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 91 | return true; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 92 | } |
| 93 | SkPath empty; |
| 94 | SkPathWriter woundPath(empty); |
| 95 | SkOpContour* test = &contourHead; |
| 96 | do { |
Cary Clark | fa9193d | 2016-12-21 08:25:00 -0500 | [diff] [blame] | 97 | if (!test->count()) { |
| 98 | continue; |
| 99 | } |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 100 | if (test->reversed()) { |
| 101 | test->toReversePath(&woundPath); |
| 102 | } else { |
| 103 | test->toPath(&woundPath); |
| 104 | } |
| 105 | } while ((test = test->next())); |
| 106 | *path = *woundPath.nativePath(); |
| 107 | path->setFillType(fillType); |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 108 | return true; |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 109 | } |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 110 | |
| 111 | void SkOpBuilder::add(const SkPath& path, SkPathOp op) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 112 | if (0 == fOps.count() && op != kUnion_SkPathOp) { |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 113 | fPathRefs.push_back() = SkPath(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 114 | *fOps.append() = kUnion_SkPathOp; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 115 | } |
| 116 | fPathRefs.push_back() = path; |
| 117 | *fOps.append() = op; |
| 118 | } |
| 119 | |
| 120 | void SkOpBuilder::reset() { |
| 121 | fPathRefs.reset(); |
| 122 | fOps.reset(); |
| 123 | } |
| 124 | |
| 125 | /* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex |
| 126 | paths with union ops could be locally resolved and still improve over doing the |
| 127 | ops one at a time. */ |
| 128 | bool SkOpBuilder::resolve(SkPath* result) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 129 | SkPath original = *result; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 130 | int count = fOps.count(); |
| 131 | bool allUnion = true; |
Mike Reed | 3872c98 | 2020-08-29 17:46:51 -0400 | [diff] [blame] | 132 | SkPathFirstDirection firstDir = SkPathFirstDirection::kUnknown; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 133 | for (int index = 0; index < count; ++index) { |
| 134 | SkPath* test = &fPathRefs[index]; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 135 | if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) { |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 136 | allUnion = false; |
| 137 | break; |
| 138 | } |
| 139 | // If all paths are convex, track direction, reversing as needed. |
| 140 | if (test->isConvex()) { |
Mike Reed | 85f51b2 | 2020-08-30 10:32:06 -0400 | [diff] [blame^] | 141 | SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*test); |
| 142 | if (dir == SkPathFirstDirection::kUnknown) { |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 143 | allUnion = false; |
| 144 | break; |
| 145 | } |
Mike Reed | 3872c98 | 2020-08-29 17:46:51 -0400 | [diff] [blame] | 146 | if (firstDir == SkPathFirstDirection::kUnknown) { |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 147 | firstDir = dir; |
| 148 | } else if (firstDir != dir) { |
caryclark | 51c5678 | 2016-11-07 05:09:28 -0800 | [diff] [blame] | 149 | ReversePath(test); |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 150 | } |
| 151 | continue; |
| 152 | } |
| 153 | // If the path is not convex but its bounds do not intersect the others, simplify is enough. |
| 154 | const SkRect& testBounds = test->getBounds(); |
| 155 | for (int inner = 0; inner < index; ++inner) { |
| 156 | // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds? |
| 157 | if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) { |
| 158 | allUnion = false; |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (!allUnion) { |
| 164 | *result = fPathRefs[0]; |
| 165 | for (int index = 1; index < count; ++index) { |
| 166 | if (!Op(*result, fPathRefs[index], fOps[index], result)) { |
| 167 | reset(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 168 | *result = original; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | reset(); |
| 173 | return true; |
| 174 | } |
| 175 | SkPath sum; |
| 176 | for (int index = 0; index < count; ++index) { |
| 177 | if (!Simplify(fPathRefs[index], &fPathRefs[index])) { |
| 178 | reset(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 179 | *result = original; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 180 | return false; |
| 181 | } |
caryclark | 218f21a | 2015-06-29 11:41:52 -0700 | [diff] [blame] | 182 | if (!fPathRefs[index].isEmpty()) { |
| 183 | // convert the even odd result back to winding form before accumulating it |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 184 | if (!FixWinding(&fPathRefs[index])) { |
| 185 | *result = original; |
| 186 | return false; |
| 187 | } |
caryclark | 218f21a | 2015-06-29 11:41:52 -0700 | [diff] [blame] | 188 | sum.addPath(fPathRefs[index]); |
| 189 | } |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 190 | } |
| 191 | reset(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 192 | bool success = Simplify(sum, result); |
| 193 | if (!success) { |
| 194 | *result = original; |
| 195 | } |
| 196 | return success; |
caryclark | 45fa447 | 2015-01-16 07:04:10 -0800 | [diff] [blame] | 197 | } |