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