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