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" |
| 8 | #include "SkOpEdgeBuilder.h" |
| 9 | #include "SkPathOpsCommon.h" |
| 10 | #include "SkPathWriter.h" |
| 11 | |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 12 | static SkOpSegment* findChaseOp(SkTDArray<SkOpSpan*>& chase, int* tIndex, int* endIndex) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 13 | while (chase.count()) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 14 | SkOpSpan* span; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 15 | chase.pop(&span); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 16 | const SkOpSpan& backPtr = span->fOther->span(span->fOtherIndex); |
| 17 | SkOpSegment* segment = backPtr.fOther; |
| 18 | *tIndex = backPtr.fOtherIndex; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 19 | bool sortable = true; |
| 20 | bool done = true; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 21 | *endIndex = -1; |
| 22 | if (const SkOpAngle* last = segment->activeAngle(*tIndex, tIndex, endIndex, &done, |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 23 | &sortable)) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 24 | if (last->unorderable()) { |
| 25 | continue; |
| 26 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 27 | *tIndex = last->start(); |
| 28 | *endIndex = last->end(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 29 | #if TRY_ROTATE |
| 30 | *chase.insert(0) = span; |
| 31 | #else |
| 32 | *chase.append() = span; |
| 33 | #endif |
| 34 | return last->segment(); |
| 35 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 36 | if (done) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 37 | continue; |
| 38 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 39 | if (!sortable) { |
| 40 | continue; |
| 41 | } |
| 42 | // find first angle, initialize winding to computed fWindSum |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 43 | const SkOpAngle* angle = segment->spanToAngle(*tIndex, *endIndex); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 44 | if (!angle) { |
| 45 | continue; |
| 46 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 47 | const SkOpAngle* firstAngle = angle; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 48 | bool loop = false; |
| 49 | int winding = SK_MinS32; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 50 | do { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 51 | angle = angle->next(); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 52 | if (angle == firstAngle && loop) { |
| 53 | break; // if we get here, there's no winding, loop is unorderable |
| 54 | } |
| 55 | loop |= angle == firstAngle; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 56 | segment = angle->segment(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 57 | winding = segment->windSum(angle); |
| 58 | } while (winding == SK_MinS32); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 59 | if (winding == SK_MinS32) { |
| 60 | continue; |
| 61 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 62 | int sumMiWinding = segment->updateWindingReverse(angle); |
| 63 | int sumSuWinding = segment->updateOppWindingReverse(angle); |
| 64 | if (segment->operand()) { |
| 65 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 66 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 67 | SkOpSegment* first = NULL; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 68 | bool badData = false; |
| 69 | while ((angle = angle->next()) != firstAngle && !badData) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 70 | segment = angle->segment(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 71 | int start = angle->start(); |
| 72 | int end = angle->end(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 73 | int maxWinding, sumWinding, oppMaxWinding, oppSumWinding; |
| 74 | segment->setUpWindings(start, end, &sumMiWinding, &sumSuWinding, |
| 75 | &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
| 76 | if (!segment->done(angle)) { |
| 77 | if (!first) { |
| 78 | first = segment; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 79 | *tIndex = start; |
| 80 | *endIndex = end; |
| 81 | } |
| 82 | if (segment->inconsistentAngle(maxWinding, sumWinding, oppMaxWinding, |
| 83 | oppSumWinding, angle)) { |
| 84 | badData = true; |
| 85 | break; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 86 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 87 | // OPTIMIZATION: should this also add to the chase? |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 88 | (void) segment->markAngle(maxWinding, sumWinding, oppMaxWinding, |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 89 | oppSumWinding, angle); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 91 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 92 | if (badData) { |
| 93 | continue; |
| 94 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 95 | if (first) { |
| 96 | #if TRY_ROTATE |
| 97 | *chase.insert(0) = span; |
| 98 | #else |
| 99 | *chase.append() = span; |
| 100 | #endif |
| 101 | return first; |
| 102 | } |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 107 | /* |
| 108 | static bool windingIsActive(int winding, int oppWinding, int spanWinding, int oppSpanWinding, |
| 109 | bool windingIsOp, PathOp op) { |
| 110 | bool active = windingIsActive(winding, spanWinding); |
| 111 | if (!active) { |
| 112 | return false; |
| 113 | } |
| 114 | if (oppSpanWinding && windingIsActive(oppWinding, oppSpanWinding)) { |
| 115 | switch (op) { |
| 116 | case kIntersect_Op: |
| 117 | case kUnion_Op: |
| 118 | return true; |
| 119 | case kDifference_Op: { |
| 120 | int absSpan = abs(spanWinding); |
| 121 | int absOpp = abs(oppSpanWinding); |
| 122 | return windingIsOp ? absSpan < absOpp : absSpan > absOpp; |
| 123 | } |
| 124 | case kXor_Op: |
| 125 | return spanWinding != oppSpanWinding; |
| 126 | default: |
| 127 | SkASSERT(0); |
| 128 | } |
| 129 | } |
| 130 | bool opActive = oppWinding != 0; |
| 131 | return gOpLookup[op][opActive][windingIsOp]; |
| 132 | } |
| 133 | */ |
| 134 | |
| 135 | static bool bridgeOp(SkTArray<SkOpContour*, true>& contourList, const SkPathOp op, |
| 136 | const int xorMask, const int xorOpMask, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | bool firstContour = true; |
| 138 | bool unsortable = false; |
| 139 | bool topUnsortable = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 140 | bool firstPass = true; |
| 141 | SkPoint lastTopLeft; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; |
| 143 | do { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 144 | int index, endIndex; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 145 | bool topDone; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 146 | bool onlyVertical = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 147 | lastTopLeft = topLeft; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 148 | SkOpSegment* current = FindSortableTop(contourList, SkOpAngle::kBinarySingle, &firstContour, |
| 149 | &index, &endIndex, &topLeft, &topUnsortable, &topDone, &onlyVertical, firstPass); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 150 | if (!current) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 151 | if ((!topUnsortable || firstPass) && !topDone) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 152 | SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 153 | if (lastTopLeft.fX == SK_ScalarMin && lastTopLeft.fY == SK_ScalarMin) { |
| 154 | if (firstPass) { |
| 155 | firstPass = false; |
| 156 | } else { |
| 157 | break; |
| 158 | } |
| 159 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 160 | topLeft.fX = topLeft.fY = SK_ScalarMin; |
| 161 | continue; |
| 162 | } |
| 163 | break; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 164 | } else if (onlyVertical) { |
| 165 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 166 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 167 | firstPass = !topUnsortable || lastTopLeft != topLeft; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 168 | SkTDArray<SkOpSpan*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 169 | do { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 170 | if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 171 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 172 | if (!unsortable && current->done()) { |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 173 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 174 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 175 | SkASSERT(unsortable || !current->done()); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 176 | int nextStart = index; |
| 177 | int nextEnd = endIndex; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 178 | SkOpSegment* next = current->findNextOp(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | &unsortable, op, xorMask, xorOpMask); |
| 180 | if (!next) { |
| 181 | if (!unsortable && simple->hasMove() |
| 182 | && current->verb() != SkPath::kLine_Verb |
| 183 | && !simple->isClosed()) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 184 | current->addCurveTo(index, endIndex, simple, true); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 185 | #if DEBUG_ACTIVE_SPANS |
| 186 | if (!simple->isClosed()) { |
| 187 | DebugShowActiveSpans(contourList); |
| 188 | } |
| 189 | #endif |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 190 | // SkASSERT(simple->isClosed()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | } |
| 192 | break; |
| 193 | } |
| 194 | #if DEBUG_FLOW |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 195 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 196 | current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY, |
| 197 | current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 198 | #endif |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 199 | current->addCurveTo(index, endIndex, simple, true); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 200 | current = next; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 201 | index = nextStart; |
| 202 | endIndex = nextEnd; |
| 203 | } while (!simple->isClosed() && (!unsortable |
| 204 | || !current->done(SkMin32(index, endIndex)))); |
| 205 | if (current->activeWinding(index, endIndex) && !simple->isClosed()) { |
| 206 | // FIXME : add to simplify, xor cpaths |
| 207 | int min = SkMin32(index, endIndex); |
| 208 | if (!unsortable && !simple->isEmpty()) { |
| 209 | unsortable = current->checkSmall(min); |
| 210 | } |
| 211 | if (!current->done(min)) { |
| 212 | current->addCurveTo(index, endIndex, simple, true); |
| 213 | current->markDoneBinary(min); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | simple->close(); |
| 217 | } else { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 218 | SkOpSpan* last = current->markAndChaseDoneBinary(index, endIndex); |
| 219 | if (last && !last->fChased && !last->fLoop) { |
| 220 | last->fChased = true; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 221 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
| 222 | *chase.append() = last; |
| 223 | #if DEBUG_WINDING |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 224 | SkDebugf("%s chase.append id=%d windSum=%d small=%d\n", __FUNCTION__, |
| 225 | last->fOther->span(last->fOtherIndex).fOther->debugID(), last->fWindSum, |
| 226 | last->fSmall); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 227 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 230 | current = findChaseOp(chase, &index, &endIndex); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 231 | #if DEBUG_ACTIVE_SPANS |
| 232 | DebugShowActiveSpans(contourList); |
| 233 | #endif |
| 234 | if (!current) { |
| 235 | break; |
| 236 | } |
| 237 | } while (true); |
| 238 | } while (true); |
| 239 | return simple->someAssemblyRequired(); |
| 240 | } |
| 241 | |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 242 | // pretty picture: |
| 243 | // https://docs.google.com/a/google.com/drawings/d/1sPV8rPfpEFXymBp3iSbDRWAycp1b-7vD9JP2V-kn9Ss/edit?usp=sharing |
| 244 | static const SkPathOp gOpInverse[kReverseDifference_PathOp + 1][2][2] = { |
| 245 | // inside minuend outside minuend |
| 246 | // inside subtrahend outside subtrahend inside subtrahend outside subtrahend |
| 247 | {{ kDifference_PathOp, kIntersect_PathOp }, { kUnion_PathOp, kReverseDifference_PathOp }}, |
| 248 | {{ kIntersect_PathOp, kDifference_PathOp }, { kReverseDifference_PathOp, kUnion_PathOp }}, |
| 249 | {{ kUnion_PathOp, kReverseDifference_PathOp }, { kDifference_PathOp, kIntersect_PathOp }}, |
| 250 | {{ kXOR_PathOp, kXOR_PathOp }, { kXOR_PathOp, kXOR_PathOp }}, |
| 251 | {{ kReverseDifference_PathOp, kUnion_PathOp }, { kIntersect_PathOp, kDifference_PathOp }}, |
| 252 | }; |
| 253 | |
| 254 | static const bool gOutInverse[kReverseDifference_PathOp + 1][2][2] = { |
| 255 | {{ false, false }, { true, false }}, // diff |
| 256 | {{ false, false }, { false, true }}, // sect |
| 257 | {{ false, true }, { true, true }}, // union |
| 258 | {{ false, true }, { true, false }}, // xor |
| 259 | {{ false, true }, { false, false }}, // rev diff |
| 260 | }; |
| 261 | |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 262 | #define DEBUGGING_PATHOPS_FROM_HOST 0 // enable to debug svg in chrome -- note path hardcoded below |
| 263 | #if DEBUGGING_PATHOPS_FROM_HOST |
| 264 | #include "SkData.h" |
| 265 | #include "SkStream.h" |
| 266 | |
| 267 | static void dump_path(FILE* file, const SkPath& path, bool force, bool dumpAsHex) { |
| 268 | SkDynamicMemoryWStream wStream; |
| 269 | path.dump(&wStream, force, dumpAsHex); |
| 270 | SkAutoDataUnref data(wStream.copyToData()); |
| 271 | fprintf(file, "%.*s\n", (int) data->size(), data->data()); |
| 272 | } |
| 273 | |
| 274 | static int dumpID = 0; |
| 275 | |
| 276 | static void dump_op(const SkPath& one, const SkPath& two, SkPathOp op) { |
| 277 | #if SK_BUILD_FOR_MAC |
| 278 | FILE* file = fopen("/Users/caryclark/Documents/svgop.txt", "w"); |
| 279 | #else |
| 280 | FILE* file = fopen("/usr/local/google/home/caryclark/Documents/svgop.txt", "w"); |
| 281 | #endif |
| 282 | fprintf(file, |
| 283 | "\nstatic void fuzz763_%d(skiatest::Reporter* reporter, const char* filename) {\n", |
| 284 | ++dumpID); |
| 285 | fprintf(file, " SkPath path;\n"); |
| 286 | fprintf(file, " path.setFillType((SkPath::FillType) %d);\n", one.getFillType()); |
| 287 | dump_path(file, one, false, true); |
| 288 | fprintf(file, " SkPath path1(path);\n"); |
| 289 | fprintf(file, " path.reset();\n"); |
| 290 | fprintf(file, " path.setFillType((SkPath::FillType) %d);\n", two.getFillType()); |
| 291 | dump_path(file, two, false, true); |
| 292 | fprintf(file, " SkPath path2(path);\n"); |
| 293 | fprintf(file, " testPathOp(reporter, path1, path2, (SkPathOp) %d, filename);\n", op); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 294 | fprintf(file, "}\n"); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 295 | fclose(file); |
| 296 | } |
| 297 | #endif |
| 298 | |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 299 | bool Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result) { |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 300 | #if DEBUGGING_PATHOPS_FROM_HOST |
| 301 | dump_op(one, two, op); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 302 | #endif |
| 303 | #if DEBUG_SHOW_TEST_NAME |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 304 | char* debugName = DEBUG_FILENAME_STRING; |
| 305 | if (debugName && debugName[0]) { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 306 | SkPathOpsDebug::BumpTestName(debugName); |
| 307 | SkPathOpsDebug::ShowPath(one, two, op, debugName); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 308 | } |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 309 | #endif |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 310 | op = gOpInverse[op][one.isInverseFillType()][two.isInverseFillType()]; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 311 | SkPath::FillType fillType = gOutInverse[op][one.isInverseFillType()][two.isInverseFillType()] |
| 312 | ? SkPath::kInverseEvenOdd_FillType : SkPath::kEvenOdd_FillType; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 313 | const SkPath* minuend = &one; |
| 314 | const SkPath* subtrahend = &two; |
| 315 | if (op == kReverseDifference_PathOp) { |
| 316 | minuend = &two; |
| 317 | subtrahend = &one; |
| 318 | op = kDifference_PathOp; |
| 319 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 320 | #if DEBUG_SORT || DEBUG_SWAP_TOP |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 321 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 322 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 323 | // turn path into list of segments |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 324 | SkTArray<SkOpContour> contours; |
| 325 | // FIXME: add self-intersecting cubics' T values to segment |
| 326 | SkOpEdgeBuilder builder(*minuend, contours); |
caryclark | d751ac0 | 2014-10-03 05:36:27 -0700 | [diff] [blame] | 327 | if (builder.unparseable()) { |
| 328 | return false; |
| 329 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 330 | const int xorMask = builder.xorMask(); |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 331 | builder.addOperand(*subtrahend); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 332 | if (!builder.finish()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 333 | return false; |
| 334 | } |
caryclark@google.com | 6dc7df6 | 2013-04-25 11:51:54 +0000 | [diff] [blame] | 335 | result->reset(); |
| 336 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 337 | const int xorOpMask = builder.xorMask(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 338 | SkTArray<SkOpContour*, true> contourList; |
| 339 | MakeContourList(contours, contourList, xorMask == kEvenOdd_PathOpsMask, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 340 | xorOpMask == kEvenOdd_PathOpsMask); |
| 341 | SkOpContour** currentPtr = contourList.begin(); |
| 342 | if (!currentPtr) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 343 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 344 | } |
| 345 | SkOpContour** listEnd = contourList.end(); |
| 346 | // find all intersections between segments |
| 347 | do { |
| 348 | SkOpContour** nextPtr = currentPtr; |
| 349 | SkOpContour* current = *currentPtr++; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 350 | if (current->containsCubics()) { |
| 351 | AddSelfIntersectTs(current); |
| 352 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 353 | SkOpContour* next; |
| 354 | do { |
| 355 | next = *nextPtr++; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 356 | } while (AddIntersectTs(current, next) && nextPtr != listEnd); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 357 | } while (currentPtr != listEnd); |
| 358 | // eat through coincident edges |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 359 | |
| 360 | int total = 0; |
| 361 | int index; |
| 362 | for (index = 0; index < contourList.count(); ++index) { |
| 363 | total += contourList[index]->segments().count(); |
| 364 | } |
| 365 | if (!HandleCoincidence(&contourList, total)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 368 | // construct closed contours |
| 369 | SkPathWriter wrapper(*result); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame^] | 370 | bridgeOp(contourList, op, xorMask, xorOpMask, &wrapper); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 371 | { // if some edges could not be resolved, assemble remaining fragments |
| 372 | SkPath temp; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 373 | temp.setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 374 | SkPathWriter assembled(temp); |
| 375 | Assemble(wrapper, &assembled); |
| 376 | *result = *assembled.nativePath(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 377 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 378 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 379 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 380 | } |