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 | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 13 | static bool bridgeWinding(SkOpContourHead* contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 14 | bool unsortable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 15 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 16 | SkOpSpan* span = FindSortableTop(contourList); |
| 17 | if (!span) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 18 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 19 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 20 | SkOpSegment* current = span->segment(); |
| 21 | SkOpSpanBase* start = span->next(); |
| 22 | SkOpSpanBase* end = span; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 23 | SkTDArray<SkOpSpanBase*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 24 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 25 | if (current->activeWinding(start, end)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | if (!unsortable && current->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 28 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 29 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 30 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 31 | SkOpSpanBase* nextStart = start; |
| 32 | SkOpSpanBase* nextEnd = end; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 33 | SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 34 | &unsortable); |
| 35 | if (!next) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 36 | break; |
| 37 | } |
| 38 | #if DEBUG_FLOW |
| 39 | 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] | 40 | current->debugID(), start->pt().fX, start->pt().fY, |
| 41 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 42 | #endif |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 43 | if (!current->addCurveTo(start, end, simple)) { |
| 44 | return false; |
| 45 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 46 | current = next; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 47 | start = nextStart; |
| 48 | end = nextEnd; |
| 49 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
| 50 | if (current->activeWinding(start, end) && !simple->isClosed()) { |
| 51 | SkOpSpan* spanStart = start->starter(end); |
| 52 | if (!spanStart->done()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 53 | if (!current->addCurveTo(start, end, simple)) { |
| 54 | return false; |
| 55 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 56 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 59 | simple->finishContour(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 60 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 61 | SkOpSpanBase* last = current->markAndChaseDone(start, end); |
| 62 | if (last && !last->chased()) { |
| 63 | last->setChased(true); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 64 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 65 | *chase.append() = last; |
| 66 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 67 | SkDebugf("%s chase.append id=%d", __FUNCTION__, last->segment()->debugID()); |
| 68 | if (!last->final()) { |
| 69 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 70 | } |
| 71 | SkDebugf("\n"); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 72 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 75 | current = FindChase(&chase, &start, &end); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 76 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 77 | if (!current) { |
| 78 | break; |
| 79 | } |
| 80 | } while (true); |
| 81 | } while (true); |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 82 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // returns true if all edges were processed |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 86 | static bool bridgeXor(SkOpContourHead* contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 87 | SkOpSegment* current; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 88 | SkOpSpanBase* start; |
| 89 | SkOpSpanBase* end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | bool unsortable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 91 | while ((current = FindUndone(contourList, &start, &end))) { |
| 92 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 93 | if (!unsortable && current->done()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 94 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 95 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 96 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 97 | SkOpSpanBase* nextStart = start; |
| 98 | SkOpSpanBase* nextEnd = end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 99 | SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable); |
| 100 | if (!next) { |
| 101 | if (!unsortable && simple->hasMove() |
| 102 | && current->verb() != SkPath::kLine_Verb |
| 103 | && !simple->isClosed()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 104 | if (!current->addCurveTo(start, end, simple)) { |
| 105 | return false; |
| 106 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 107 | if (!simple->isClosed()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 108 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 109 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 110 | } |
| 111 | break; |
| 112 | } |
| 113 | #if DEBUG_FLOW |
| 114 | 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] | 115 | current->debugID(), start->pt().fX, start->pt().fY, |
| 116 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 117 | #endif |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 118 | if (!current->addCurveTo(start, end, simple)) { |
| 119 | return false; |
| 120 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 121 | current = next; |
| 122 | start = nextStart; |
| 123 | end = nextEnd; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 124 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 125 | if (!simple->isClosed()) { |
| 126 | SkASSERT(unsortable); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 127 | SkOpSpan* spanStart = start->starter(end); |
| 128 | if (!spanStart->done()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 129 | if (!current->addCurveTo(start, end, simple)) { |
| 130 | return false; |
| 131 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 132 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 133 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 134 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 135 | simple->finishContour(); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 136 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | } |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 138 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // FIXME : add this as a member of SkPath |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 142 | bool SimplifyDebug(const SkPath& path, SkPath* result |
| 143 | SkDEBUGPARAMS(bool skipAssert) SkDEBUGPARAMS(const char* testName)) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 144 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
| 145 | SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 146 | : SkPath::kEvenOdd_FillType; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 147 | if (path.isConvex()) { |
| 148 | if (result != &path) { |
| 149 | *result = path; |
| 150 | } |
| 151 | result->setFillType(fillType); |
| 152 | return true; |
| 153 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 154 | // turn path into list of segments |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 155 | SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 156 | SkOpContour contour; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 157 | SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 158 | SkOpGlobalState globalState(contourList, &allocator |
| 159 | SkDEBUGPARAMS(skipAssert) SkDEBUGPARAMS(testName)); |
| 160 | SkOpCoincidence coincidence(&globalState); |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 161 | #if DEBUG_DUMP_VERIFY |
| 162 | #ifndef SK_DEBUG |
| 163 | const char* testName = "release"; |
| 164 | #endif |
caryclark | 1326068 | 2016-10-24 05:10:14 -0700 | [diff] [blame] | 165 | if (SkPathOpsDebug::gDumpOp) { |
| 166 | SkPathOpsDebug::DumpSimplify(path, testName); |
| 167 | } |
| 168 | #endif |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 169 | SkScalar scaleFactor = ScaleFactor(path); |
| 170 | SkPath scaledPath; |
| 171 | const SkPath* workingPath; |
| 172 | if (scaleFactor > SK_Scalar1) { |
| 173 | ScalePath(path, 1.f / scaleFactor, &scaledPath); |
| 174 | workingPath = &scaledPath; |
| 175 | } else { |
| 176 | workingPath = &path; |
| 177 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 178 | #if DEBUG_SORT |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 179 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
| 180 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 181 | SkOpEdgeBuilder builder(*workingPath, contourList, &globalState); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 182 | if (!builder.finish()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 183 | return false; |
| 184 | } |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 185 | #if DEBUG_DUMP_SEGMENTS |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 186 | contour.dumpSegments(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 187 | #endif |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 188 | if (!SortContourList(&contourList, false, false)) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 189 | result->reset(); |
| 190 | result->setFillType(fillType); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 191 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 192 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 193 | // find all intersections between segments |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 194 | SkOpContour* current = contourList; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 196 | SkOpContour* next = current; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 197 | while (AddIntersectTs(current, next, &coincidence) |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 198 | && (next = next->next())); |
| 199 | } while ((current = current->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 200 | #if DEBUG_VALIDATE |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 201 | globalState.setPhase(SkOpPhase::kWalking); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 202 | #endif |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 203 | bool success = HandleCoincidence(contourList, &coincidence); |
| 204 | #if DEBUG_COIN |
| 205 | globalState.debugAddToGlobalCoinDicts(); |
| 206 | #endif |
| 207 | if (!success) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 210 | #if DEBUG_DUMP_ALIGNMENT |
| 211 | contour.dumpSegments("aligned"); |
| 212 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 213 | // construct closed contours |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 214 | result->reset(); |
| 215 | result->setFillType(fillType); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 216 | SkPathWriter wrapper(*result); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 217 | if (builder.xorMask() == kWinding_PathOpsMask ? !bridgeWinding(contourList, &wrapper) |
| 218 | : !bridgeXor(contourList, &wrapper)) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 221 | wrapper.assemble(); // if some edges could not be resolved, assemble remaining |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 222 | if (scaleFactor > 1) { |
| 223 | ScalePath(*result, scaleFactor, result); |
| 224 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 225 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 226 | } |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 227 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 228 | bool Simplify(const SkPath& path, SkPath* result) { |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 229 | #if DEBUG_DUMP_VERIFY |
caryclark | 1326068 | 2016-10-24 05:10:14 -0700 | [diff] [blame] | 230 | if (SkPathOpsDebug::gVerifyOp) { |
| 231 | if (!SimplifyDebug(path, result SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr))) { |
| 232 | SkPathOpsDebug::ReportSimplifyFail(path); |
| 233 | return false; |
| 234 | } |
| 235 | SkPathOpsDebug::VerifySimplify(path, *result); |
| 236 | return true; |
| 237 | } |
| 238 | #endif |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 239 | return SimplifyDebug(path, result SkDEBUGPARAMS(true) SkDEBUGPARAMS(nullptr)); |
| 240 | } |