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