caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | #include "SkAddIntersections.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 8 | #include "SkOpCoincidence.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkOpEdgeBuilder.h" |
| 10 | #include "SkPathOpsCommon.h" |
| 11 | #include "SkPathWriter.h" |
| 12 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 13 | static bool bridgeWinding(SkTDArray<SkOpContour* >& contourList, SkPathWriter* simple, |
| 14 | SkChunkAlloc* allocator) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 15 | bool firstContour = true; |
| 16 | bool unsortable = false; |
| 17 | bool topUnsortable = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 18 | bool firstPass = true; |
| 19 | SkPoint lastTopLeft; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 20 | SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; |
| 21 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 22 | SkOpSpanBase* start; |
| 23 | SkOpSpanBase* end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 24 | bool topDone; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 25 | bool onlyVertical = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 26 | lastTopLeft = topLeft; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 27 | SkOpSegment* current = FindSortableTop(contourList, firstPass, SkOpAngle::kUnaryWinding, |
| 28 | &firstContour, &start, &end, &topLeft, &topUnsortable, &topDone, &onlyVertical, |
| 29 | allocator); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 30 | if (!current) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 31 | if ((!topUnsortable || firstPass) && !topDone) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 32 | SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 33 | if (lastTopLeft.fX == SK_ScalarMin && lastTopLeft.fY == SK_ScalarMin) { |
| 34 | if (firstPass) { |
| 35 | firstPass = false; |
| 36 | } else { |
| 37 | break; |
| 38 | } |
| 39 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 40 | topLeft.fX = topLeft.fY = SK_ScalarMin; |
| 41 | continue; |
| 42 | } |
| 43 | break; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 44 | } else if (onlyVertical) { |
| 45 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 46 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 47 | firstPass = !topUnsortable || lastTopLeft != topLeft; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 48 | SkTDArray<SkOpSpanBase*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 49 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 50 | if (current->activeWinding(start, end)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 51 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 52 | if (!unsortable && current->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 53 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 54 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 56 | SkOpSpanBase* nextStart = start; |
| 57 | SkOpSpanBase* nextEnd = end; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 58 | SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 59 | &unsortable); |
| 60 | if (!next) { |
| 61 | if (!unsortable && simple->hasMove() |
| 62 | && current->verb() != SkPath::kLine_Verb |
| 63 | && !simple->isClosed()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 64 | current->addCurveTo(start, end, simple, true); |
| 65 | #if DEBUG_ACTIVE_SPANS |
| 66 | if (!simple->isClosed()) { |
| 67 | DebugShowActiveSpans(contourList); |
| 68 | } |
| 69 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 70 | } |
| 71 | break; |
| 72 | } |
| 73 | #if DEBUG_FLOW |
| 74 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 75 | current->debugID(), start->pt().fX, start->pt().fY, |
| 76 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 77 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 78 | current->addCurveTo(start, end, simple, true); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 79 | current = next; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 80 | start = nextStart; |
| 81 | end = nextEnd; |
| 82 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
| 83 | if (current->activeWinding(start, end) && !simple->isClosed()) { |
| 84 | SkOpSpan* spanStart = start->starter(end); |
| 85 | if (!spanStart->done()) { |
| 86 | current->addCurveTo(start, end, simple, true); |
| 87 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | simple->close(); |
| 91 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 92 | SkOpSpanBase* last = current->markAndChaseDone(start, end); |
| 93 | if (last && !last->chased()) { |
| 94 | last->setChased(true); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 95 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 96 | // assert that last isn't already in array |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 97 | *chase.append() = last; |
| 98 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 99 | SkDebugf("%s chase.append id=%d", __FUNCTION__, last->segment()->debugID()); |
| 100 | if (!last->final()) { |
| 101 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 102 | } |
| 103 | SkDebugf("\n"); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 104 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 107 | current = FindChase(&chase, &start, &end); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | #if DEBUG_ACTIVE_SPANS |
| 109 | DebugShowActiveSpans(contourList); |
| 110 | #endif |
| 111 | if (!current) { |
| 112 | break; |
| 113 | } |
| 114 | } while (true); |
| 115 | } while (true); |
| 116 | return simple->someAssemblyRequired(); |
| 117 | } |
| 118 | |
| 119 | // returns true if all edges were processed |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | static bool bridgeXor(SkTDArray<SkOpContour* >& contourList, SkPathWriter* simple, |
| 121 | SkChunkAlloc* allocator) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 122 | SkOpSegment* current; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 123 | SkOpSpanBase* start; |
| 124 | SkOpSpanBase* end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 125 | bool unsortable = false; |
| 126 | bool closable = true; |
| 127 | while ((current = FindUndone(contourList, &start, &end))) { |
| 128 | do { |
| 129 | #if DEBUG_ACTIVE_SPANS |
| 130 | if (!unsortable && current->done()) { |
| 131 | DebugShowActiveSpans(contourList); |
| 132 | } |
| 133 | #endif |
| 134 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 135 | SkOpSpanBase* nextStart = start; |
| 136 | SkOpSpanBase* nextEnd = end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable); |
| 138 | if (!next) { |
| 139 | if (!unsortable && simple->hasMove() |
| 140 | && current->verb() != SkPath::kLine_Verb |
| 141 | && !simple->isClosed()) { |
| 142 | current->addCurveTo(start, end, simple, true); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 143 | #if DEBUG_ACTIVE_SPANS |
| 144 | if (!simple->isClosed()) { |
| 145 | DebugShowActiveSpans(contourList); |
| 146 | } |
| 147 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 148 | } |
| 149 | break; |
| 150 | } |
| 151 | #if DEBUG_FLOW |
| 152 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 153 | current->debugID(), start->pt().fX, start->pt().fY, |
| 154 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 155 | #endif |
| 156 | current->addCurveTo(start, end, simple, true); |
| 157 | current = next; |
| 158 | start = nextStart; |
| 159 | end = nextEnd; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 160 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | if (!simple->isClosed()) { |
| 162 | SkASSERT(unsortable); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 163 | SkOpSpan* spanStart = start->starter(end); |
| 164 | if (!spanStart->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | current->addCurveTo(start, end, simple, true); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 166 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 167 | } |
| 168 | closable = false; |
| 169 | } |
| 170 | simple->close(); |
| 171 | #if DEBUG_ACTIVE_SPANS |
| 172 | DebugShowActiveSpans(contourList); |
| 173 | #endif |
| 174 | } |
| 175 | return closable; |
| 176 | } |
| 177 | |
| 178 | // FIXME : add this as a member of SkPath |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 179 | bool Simplify(const SkPath& path, SkPath* result) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 180 | SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 181 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
| 182 | SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 183 | : SkPath::kEvenOdd_FillType; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 184 | if (path.isConvex()) { |
| 185 | if (result != &path) { |
| 186 | *result = path; |
| 187 | } |
| 188 | result->setFillType(fillType); |
| 189 | return true; |
| 190 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 191 | // turn path into list of segments |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 192 | SkOpCoincidence coincidence; |
| 193 | SkOpContour contour; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame^] | 194 | SkOpGlobalState globalState(&coincidence SkDEBUGPARAMS(&contour)); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 195 | #if DEBUG_SORT || DEBUG_SWAP_TOP |
| 196 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
| 197 | #endif |
| 198 | SkOpEdgeBuilder builder(path, &contour, &allocator, &globalState); |
| 199 | if (!builder.finish(&allocator)) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 202 | #if !FORCE_RELEASE |
| 203 | contour.dumpSegments((SkPathOp) -1); |
| 204 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 205 | SkTDArray<SkOpContour* > contourList; |
| 206 | MakeContourList(&contour, contourList, false, false); |
| 207 | SkOpContour** currentPtr = contourList.begin(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 208 | if (!currentPtr) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame^] | 209 | result->reset(); |
| 210 | result->setFillType(fillType); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 211 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 212 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 213 | if ((*currentPtr)->count() == 0) { |
| 214 | SkASSERT((*currentPtr)->next() == NULL); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame^] | 215 | result->reset(); |
| 216 | result->setFillType(fillType); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | SkOpContour** listEnd2 = contourList.end(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 220 | // find all intersections between segments |
| 221 | do { |
| 222 | SkOpContour** nextPtr = currentPtr; |
| 223 | SkOpContour* current = *currentPtr++; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 224 | SkOpContour* next; |
| 225 | do { |
| 226 | next = *nextPtr++; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 227 | } while (AddIntersectTs(current, next, &coincidence, &allocator) && nextPtr != listEnd2); |
| 228 | } while (currentPtr != listEnd2); |
| 229 | #if DEBUG_VALIDATE |
| 230 | globalState.setPhase(SkOpGlobalState::kWalking); |
| 231 | #endif |
| 232 | if (!HandleCoincidence(&contourList, &coincidence, &allocator, &globalState)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 233 | return false; |
| 234 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 235 | // construct closed contours |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame^] | 236 | result->reset(); |
| 237 | result->setFillType(fillType); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 238 | SkPathWriter wrapper(*result); |
| 239 | if (builder.xorMask() == kWinding_PathOpsMask ? bridgeWinding(contourList, &wrapper, &allocator) |
| 240 | : !bridgeXor(contourList, &wrapper, &allocator)) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 241 | { // if some edges could not be resolved, assemble remaining fragments |
| 242 | SkPath temp; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 243 | temp.setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 244 | SkPathWriter assembled(temp); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 245 | Assemble(wrapper, &assembled); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 246 | *result = *assembled.nativePath(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 247 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 248 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 249 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 250 | } |