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 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 15 | static SkOpSegment* findChaseOp(SkTDArray<SkOpSpanBase*>& chase, SkOpSpanBase** startPtr, |
| 16 | SkOpSpanBase** endPtr) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 17 | while (chase.count()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 18 | SkOpSpanBase* span; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 19 | chase.pop(&span); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 20 | // OPTIMIZE: prev makes this compatible with old code -- but is it necessary? |
| 21 | *startPtr = span->ptT()->prev()->span(); |
| 22 | SkOpSegment* segment = (*startPtr)->segment(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 23 | bool done = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 24 | *endPtr = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 25 | if (SkOpAngle* last = segment->activeAngle(*startPtr, startPtr, endPtr, &done)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 26 | *startPtr = last->start(); |
| 27 | *endPtr = last->end(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 28 | #if TRY_ROTATE |
| 29 | *chase.insert(0) = span; |
| 30 | #else |
| 31 | *chase.append() = span; |
| 32 | #endif |
| 33 | return last->segment(); |
| 34 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 35 | if (done) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 36 | continue; |
| 37 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 38 | int winding; |
| 39 | bool sortable; |
| 40 | const SkOpAngle* angle = AngleWinding(*startPtr, *endPtr, &winding, &sortable); |
caryclark | aa7ceb6 | 2016-06-29 10:46:08 -0700 | [diff] [blame] | 41 | if (!angle) { |
| 42 | return nullptr; |
| 43 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 44 | if (winding == SK_MinS32) { |
| 45 | continue; |
| 46 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 47 | int sumMiWinding, sumSuWinding; |
| 48 | if (sortable) { |
| 49 | segment = angle->segment(); |
| 50 | sumMiWinding = segment->updateWindingReverse(angle); |
caryclark | 7941809 | 2016-08-26 14:24:24 -0700 | [diff] [blame] | 51 | if (sumMiWinding == SK_MinS32) { |
| 52 | SkASSERT(segment->globalState()->debugSkipAssert()); |
| 53 | return nullptr; |
| 54 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 55 | sumSuWinding = segment->updateOppWindingReverse(angle); |
caryclark | 7941809 | 2016-08-26 14:24:24 -0700 | [diff] [blame] | 56 | if (sumSuWinding == SK_MinS32) { |
| 57 | SkASSERT(segment->globalState()->debugSkipAssert()); |
| 58 | return nullptr; |
| 59 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 60 | if (segment->operand()) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 61 | using std::swap; |
| 62 | swap(sumMiWinding, sumSuWinding); |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 63 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 64 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | SkOpSegment* first = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 66 | const SkOpAngle* firstAngle = angle; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 67 | while ((angle = angle->next()) != firstAngle) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 68 | segment = angle->segment(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 69 | SkOpSpanBase* start = angle->start(); |
| 70 | SkOpSpanBase* end = angle->end(); |
Kevin Lubick | 4284613 | 2018-01-05 10:11:11 -0500 | [diff] [blame] | 71 | int maxWinding = 0, sumWinding = 0, oppMaxWinding = 0, oppSumWinding = 0; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 72 | if (sortable) { |
| 73 | segment->setUpWindings(start, end, &sumMiWinding, &sumSuWinding, |
| 74 | &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
| 75 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 76 | if (!segment->done(angle)) { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 77 | if (!first && (sortable || start->starter(end)->windSum() != SK_MinS32)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 78 | first = segment; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 79 | *startPtr = start; |
| 80 | *endPtr = end; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 81 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 82 | // OPTIMIZATION: should this also add to the chase? |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 83 | if (sortable) { |
| 84 | (void) segment->markAngle(maxWinding, sumWinding, oppMaxWinding, |
| 85 | oppSumWinding, angle); |
| 86 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 87 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 88 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 89 | if (first) { |
| 90 | #if TRY_ROTATE |
| 91 | *chase.insert(0) = span; |
| 92 | #else |
| 93 | *chase.append() = span; |
| 94 | #endif |
| 95 | return first; |
| 96 | } |
| 97 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 101 | static bool bridgeOp(SkOpContourHead* contourList, const SkPathOp op, |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 102 | const int xorMask, const int xorOpMask, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 103 | bool unsortable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 104 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 105 | SkOpSpan* span = FindSortableTop(contourList); |
| 106 | if (!span) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 107 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 109 | SkOpSegment* current = span->segment(); |
| 110 | SkOpSpanBase* start = span->next(); |
| 111 | SkOpSpanBase* end = span; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 112 | SkTDArray<SkOpSpanBase*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 113 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 114 | if (current->activeOp(start, end, xorMask, xorOpMask, op)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 115 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 116 | if (!unsortable && current->done()) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 117 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 118 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 119 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | SkOpSpanBase* nextStart = start; |
| 121 | SkOpSpanBase* nextEnd = end; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 122 | SkOpSegment* next = current->findNextOp(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | &unsortable, op, xorMask, xorOpMask); |
| 124 | if (!next) { |
| 125 | if (!unsortable && simple->hasMove() |
| 126 | && current->verb() != SkPath::kLine_Verb |
| 127 | && !simple->isClosed()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 128 | if (!current->addCurveTo(start, end, simple)) { |
| 129 | return false; |
| 130 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 131 | if (!simple->isClosed()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 132 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 133 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 134 | } |
| 135 | break; |
| 136 | } |
| 137 | #if DEBUG_FLOW |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 138 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 139 | current->debugID(), start->pt().fX, start->pt().fY, |
| 140 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 141 | #endif |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 142 | if (!current->addCurveTo(start, end, simple)) { |
| 143 | return false; |
| 144 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 145 | current = next; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 146 | start = nextStart; |
| 147 | end = nextEnd; |
| 148 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
| 149 | if (current->activeWinding(start, end) && !simple->isClosed()) { |
| 150 | SkOpSpan* spanStart = start->starter(end); |
| 151 | if (!spanStart->done()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 152 | if (!current->addCurveTo(start, end, simple)) { |
| 153 | return false; |
| 154 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 155 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 158 | simple->finishContour(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 159 | } else { |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame^] | 160 | SkOpSpanBase* last; |
| 161 | if (!current->markAndChaseDone(start, end, &last)) { |
| 162 | return false; |
| 163 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 164 | if (last && !last->chased()) { |
| 165 | last->setChased(true); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 166 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
| 167 | *chase.append() = last; |
| 168 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 169 | SkDebugf("%s chase.append id=%d", __FUNCTION__, last->segment()->debugID()); |
| 170 | if (!last->final()) { |
| 171 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 172 | } |
| 173 | SkDebugf("\n"); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 174 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 177 | current = findChaseOp(chase, &start, &end); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 178 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | if (!current) { |
| 180 | break; |
| 181 | } |
| 182 | } while (true); |
| 183 | } while (true); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 184 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Cary Clark | d8a39a0 | 2018-01-09 16:56:46 -0500 | [diff] [blame] | 187 | // diagram of why this simplifcation is possible is here: |
| 188 | // https://skia.org/dev/present/pathops link at bottom of the page |
| 189 | // https://drive.google.com/file/d/0BwoLUwz9PYkHLWpsaXd0UDdaN00/view?usp=sharing |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 190 | static const SkPathOp gOpInverse[kReverseDifference_SkPathOp + 1][2][2] = { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 191 | // inside minuend outside minuend |
| 192 | // inside subtrahend outside subtrahend inside subtrahend outside subtrahend |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 193 | {{ kDifference_SkPathOp, kIntersect_SkPathOp }, { kUnion_SkPathOp, kReverseDifference_SkPathOp }}, |
| 194 | {{ kIntersect_SkPathOp, kDifference_SkPathOp }, { kReverseDifference_SkPathOp, kUnion_SkPathOp }}, |
| 195 | {{ kUnion_SkPathOp, kReverseDifference_SkPathOp }, { kDifference_SkPathOp, kIntersect_SkPathOp }}, |
| 196 | {{ kXOR_SkPathOp, kXOR_SkPathOp }, { kXOR_SkPathOp, kXOR_SkPathOp }}, |
| 197 | {{ kReverseDifference_SkPathOp, kUnion_SkPathOp }, { kIntersect_SkPathOp, kDifference_SkPathOp }}, |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 200 | static const bool gOutInverse[kReverseDifference_SkPathOp + 1][2][2] = { |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 201 | {{ false, false }, { true, false }}, // diff |
| 202 | {{ false, false }, { false, true }}, // sect |
| 203 | {{ false, true }, { true, true }}, // union |
| 204 | {{ false, true }, { true, false }}, // xor |
| 205 | {{ false, true }, { false, false }}, // rev diff |
| 206 | }; |
| 207 | |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 208 | #if DEBUG_T_SECT_LOOP_COUNT |
| 209 | |
| 210 | #include "SkMutex.h" |
| 211 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 212 | SK_DECLARE_STATIC_MUTEX(debugWorstLoop); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 213 | |
Cary Clark | 74b4290 | 2018-03-09 07:38:47 -0500 | [diff] [blame] | 214 | SkOpGlobalState debugWorstState(nullptr, nullptr SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr)); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 215 | |
| 216 | void ReportPathOpsDebugging() { |
| 217 | debugWorstState.debugLoopReport(); |
| 218 | } |
| 219 | |
| 220 | extern void (*gVerboseFinalize)(); |
| 221 | |
| 222 | #endif |
| 223 | |
caryclark | 3f0753d | 2016-06-28 09:23:57 -0700 | [diff] [blame] | 224 | bool OpDebug(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result |
| 225 | SkDEBUGPARAMS(bool skipAssert) SkDEBUGPARAMS(const char* testName)) { |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 226 | #if DEBUG_DUMP_VERIFY |
| 227 | #ifndef SK_DEBUG |
| 228 | const char* testName = "release"; |
| 229 | #endif |
caryclark | 1326068 | 2016-10-24 05:10:14 -0700 | [diff] [blame] | 230 | if (SkPathOpsDebug::gDumpOp) { |
| 231 | SkPathOpsDebug::DumpOp(one, two, op, testName); |
| 232 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 233 | #endif |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 234 | op = gOpInverse[op][one.isInverseFillType()][two.isInverseFillType()]; |
Cary Clark | 1bb47df | 2018-06-18 08:53:00 -0400 | [diff] [blame] | 235 | bool inverseFill = gOutInverse[op][one.isInverseFillType()][two.isInverseFillType()]; |
| 236 | SkPath::FillType fillType = inverseFill ? SkPath::kInverseEvenOdd_FillType : |
| 237 | SkPath::kEvenOdd_FillType; |
| 238 | SkRect rect1, rect2; |
| 239 | if (kIntersect_SkPathOp == op && one.isRect(&rect1) && two.isRect(&rect2)) { |
| 240 | result->reset(); |
| 241 | result->setFillType(fillType); |
| 242 | if (rect1.intersect(rect2)) { |
| 243 | result->addRect(rect1); |
| 244 | } |
| 245 | return true; |
| 246 | } |
| 247 | if (one.isEmpty() || two.isEmpty()) { |
| 248 | SkPath work; |
| 249 | switch (op) { |
| 250 | case kIntersect_SkPathOp: |
| 251 | break; |
| 252 | case kUnion_SkPathOp: |
| 253 | case kXOR_SkPathOp: |
| 254 | work = one.isEmpty() ? two : one; |
| 255 | break; |
| 256 | case kDifference_SkPathOp: |
| 257 | if (!one.isEmpty()) { |
| 258 | work = one; |
| 259 | } |
| 260 | break; |
| 261 | case kReverseDifference_SkPathOp: |
| 262 | if (!two.isEmpty()) { |
| 263 | work = two; |
| 264 | } |
| 265 | break; |
| 266 | default: |
| 267 | SkASSERT(0); // unhandled case |
| 268 | } |
| 269 | if (inverseFill != work.isInverseFillType()) { |
| 270 | work.toggleInverseFillType(); |
| 271 | } |
| 272 | return Simplify(work, result); |
| 273 | } |
| 274 | SkSTArenaAlloc<4096> allocator; // FIXME: add a constant expression here, tune |
| 275 | SkOpContour contour; |
| 276 | SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); |
| 277 | SkOpGlobalState globalState(contourList, &allocator |
| 278 | SkDEBUGPARAMS(skipAssert) SkDEBUGPARAMS(testName)); |
| 279 | SkOpCoincidence coincidence(&globalState); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 280 | SkScalar scaleFactor = SkTMax(ScaleFactor(one), ScaleFactor(two)); |
| 281 | SkPath scaledOne, scaledTwo; |
| 282 | const SkPath* minuend, * subtrahend; |
| 283 | if (scaleFactor > SK_Scalar1) { |
| 284 | ScalePath(one, 1.f / scaleFactor, &scaledOne); |
| 285 | minuend = &scaledOne; |
| 286 | ScalePath(two, 1.f / scaleFactor, &scaledTwo); |
| 287 | subtrahend = &scaledTwo; |
| 288 | } else { |
| 289 | minuend = &one; |
| 290 | subtrahend = &two; |
| 291 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 292 | if (op == kReverseDifference_SkPathOp) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 293 | using std::swap; |
| 294 | swap(minuend, subtrahend); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 295 | op = kDifference_SkPathOp; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 296 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 297 | #if DEBUG_SORT |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 298 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 299 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 300 | // turn path into list of segments |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 301 | SkOpEdgeBuilder builder(*minuend, contourList, &globalState); |
caryclark | d751ac0 | 2014-10-03 05:36:27 -0700 | [diff] [blame] | 302 | if (builder.unparseable()) { |
| 303 | return false; |
| 304 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 305 | const int xorMask = builder.xorMask(); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 306 | builder.addOperand(*subtrahend); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 307 | if (!builder.finish()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 308 | return false; |
| 309 | } |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 310 | #if DEBUG_DUMP_SEGMENTS |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 311 | contourList->dumpSegments("seg", op); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 312 | #endif |
| 313 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 314 | const int xorOpMask = builder.xorMask(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 315 | if (!SortContourList(&contourList, xorMask == kEvenOdd_PathOpsMask, |
| 316 | xorOpMask == kEvenOdd_PathOpsMask)) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 317 | result->reset(); |
| 318 | result->setFillType(fillType); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 319 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 320 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 321 | // find all intersections between segments |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 322 | SkOpContour* current = contourList; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 323 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 324 | SkOpContour* next = current; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 325 | while (AddIntersectTs(current, next, &coincidence) |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 326 | && (next = next->next())) |
| 327 | ; |
| 328 | } while ((current = current->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 329 | #if DEBUG_VALIDATE |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 330 | globalState.setPhase(SkOpPhase::kWalking); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 331 | #endif |
Cary Clark | ab87d7a | 2016-10-04 10:01:04 -0400 | [diff] [blame] | 332 | bool success = HandleCoincidence(contourList, &coincidence); |
| 333 | #if DEBUG_COIN |
| 334 | globalState.debugAddToGlobalCoinDicts(); |
| 335 | #endif |
| 336 | if (!success) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 337 | return false; |
| 338 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 339 | #if DEBUG_ALIGNMENT |
| 340 | contourList->dumpSegments("aligned"); |
| 341 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 342 | // construct closed contours |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 343 | result->reset(); |
| 344 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 345 | SkPathWriter wrapper(*result); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 346 | if (!bridgeOp(contourList, op, xorMask, xorOpMask, &wrapper)) { |
| 347 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 348 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 349 | wrapper.assemble(); // if some edges could not be resolved, assemble remaining |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 350 | #if DEBUG_T_SECT_LOOP_COUNT |
| 351 | { |
| 352 | SkAutoMutexAcquire autoM(debugWorstLoop); |
| 353 | if (!gVerboseFinalize) { |
| 354 | gVerboseFinalize = &ReportPathOpsDebugging; |
| 355 | } |
| 356 | debugWorstState.debugDoYourWorst(&globalState); |
| 357 | } |
| 358 | #endif |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 359 | if (scaleFactor > 1) { |
| 360 | ScalePath(*result, scaleFactor, result); |
| 361 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 362 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 363 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 364 | |
| 365 | bool Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result) { |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 366 | #if DEBUG_DUMP_VERIFY |
caryclark | 1326068 | 2016-10-24 05:10:14 -0700 | [diff] [blame] | 367 | if (SkPathOpsDebug::gVerifyOp) { |
| 368 | if (!OpDebug(one, two, op, result SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr))) { |
| 369 | SkPathOpsDebug::ReportOpFail(one, two, op); |
| 370 | return false; |
| 371 | } |
| 372 | SkPathOpsDebug::VerifyOp(one, two, op, *result); |
| 373 | return true; |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 374 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 375 | #endif |
caryclark | 1326068 | 2016-10-24 05:10:14 -0700 | [diff] [blame] | 376 | return OpDebug(one, two, op, result SkDEBUGPARAMS(true) SkDEBUGPARAMS(nullptr)); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 377 | } |