blob: f2b25c04ecb52ab303519752d2db61ccd29168d3 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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
reed0dc4dd62015-03-24 13:55:33 -070012static SkOpSegment* findChaseOp(SkTDArray<SkOpSpan*>& chase, int* tIndex, int* endIndex) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000013 while (chase.count()) {
reed0dc4dd62015-03-24 13:55:33 -070014 SkOpSpan* span;
caryclark@google.com07393ca2013-04-08 11:47:37 +000015 chase.pop(&span);
reed0dc4dd62015-03-24 13:55:33 -070016 const SkOpSpan& backPtr = span->fOther->span(span->fOtherIndex);
17 SkOpSegment* segment = backPtr.fOther;
18 *tIndex = backPtr.fOtherIndex;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000019 bool sortable = true;
20 bool done = true;
reed0dc4dd62015-03-24 13:55:33 -070021 *endIndex = -1;
22 if (const SkOpAngle* last = segment->activeAngle(*tIndex, tIndex, endIndex, &done,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000023 &sortable)) {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000024 if (last->unorderable()) {
25 continue;
26 }
reed0dc4dd62015-03-24 13:55:33 -070027 *tIndex = last->start();
28 *endIndex = last->end();
caryclark@google.com07393ca2013-04-08 11:47:37 +000029 #if TRY_ROTATE
30 *chase.insert(0) = span;
31 #else
32 *chase.append() = span;
33 #endif
34 return last->segment();
35 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000036 if (done) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000037 continue;
38 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000039 if (!sortable) {
40 continue;
41 }
42 // find first angle, initialize winding to computed fWindSum
reed0dc4dd62015-03-24 13:55:33 -070043 const SkOpAngle* angle = segment->spanToAngle(*tIndex, *endIndex);
caryclarkdac1d172014-06-17 05:15:38 -070044 if (!angle) {
45 continue;
46 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000047 const SkOpAngle* firstAngle = angle;
caryclark65f55312014-11-13 06:58:52 -080048 bool loop = false;
49 int winding = SK_MinS32;
caryclark@google.com07393ca2013-04-08 11:47:37 +000050 do {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000051 angle = angle->next();
caryclark65f55312014-11-13 06:58:52 -080052 if (angle == firstAngle && loop) {
53 break; // if we get here, there's no winding, loop is unorderable
54 }
55 loop |= angle == firstAngle;
caryclark@google.com07393ca2013-04-08 11:47:37 +000056 segment = angle->segment();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000057 winding = segment->windSum(angle);
58 } while (winding == SK_MinS32);
caryclark65f55312014-11-13 06:58:52 -080059 if (winding == SK_MinS32) {
60 continue;
61 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000062 int sumMiWinding = segment->updateWindingReverse(angle);
63 int sumSuWinding = segment->updateOppWindingReverse(angle);
64 if (segment->operand()) {
65 SkTSwap<int>(sumMiWinding, sumSuWinding);
66 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000067 SkOpSegment* first = NULL;
reed0dc4dd62015-03-24 13:55:33 -070068 bool badData = false;
69 while ((angle = angle->next()) != firstAngle && !badData) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000070 segment = angle->segment();
reed0dc4dd62015-03-24 13:55:33 -070071 int start = angle->start();
72 int end = angle->end();
caryclark@google.com07393ca2013-04-08 11:47:37 +000073 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;
reed0dc4dd62015-03-24 13:55:33 -070079 *tIndex = start;
80 *endIndex = end;
81 }
82 if (segment->inconsistentAngle(maxWinding, sumWinding, oppMaxWinding,
83 oppSumWinding, angle)) {
84 badData = true;
85 break;
caryclark65f55312014-11-13 06:58:52 -080086 }
caryclarkdac1d172014-06-17 05:15:38 -070087 // OPTIMIZATION: should this also add to the chase?
caryclark@google.com07393ca2013-04-08 11:47:37 +000088 (void) segment->markAngle(maxWinding, sumWinding, oppMaxWinding,
commit-bot@chromium.org866f4e32013-11-21 17:04:29 +000089 oppSumWinding, angle);
caryclark@google.com07393ca2013-04-08 11:47:37 +000090 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000091 }
reed0dc4dd62015-03-24 13:55:33 -070092 if (badData) {
93 continue;
94 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000095 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
reed0dc4dd62015-03-24 13:55:33 -0700107/*
108static 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
135static bool bridgeOp(SkTArray<SkOpContour*, true>& contourList, const SkPathOp op,
136 const int xorMask, const int xorOpMask, SkPathWriter* simple) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000137 bool firstContour = true;
138 bool unsortable = false;
139 bool topUnsortable = false;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000140 bool firstPass = true;
141 SkPoint lastTopLeft;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000142 SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin};
143 do {
reed0dc4dd62015-03-24 13:55:33 -0700144 int index, endIndex;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000145 bool topDone;
caryclarkdac1d172014-06-17 05:15:38 -0700146 bool onlyVertical = false;
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000147 lastTopLeft = topLeft;
reed0dc4dd62015-03-24 13:55:33 -0700148 SkOpSegment* current = FindSortableTop(contourList, SkOpAngle::kBinarySingle, &firstContour,
149 &index, &endIndex, &topLeft, &topUnsortable, &topDone, &onlyVertical, firstPass);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000150 if (!current) {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000151 if ((!topUnsortable || firstPass) && !topDone) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000152 SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000153 if (lastTopLeft.fX == SK_ScalarMin && lastTopLeft.fY == SK_ScalarMin) {
154 if (firstPass) {
155 firstPass = false;
156 } else {
157 break;
158 }
159 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000160 topLeft.fX = topLeft.fY = SK_ScalarMin;
161 continue;
162 }
163 break;
caryclarkdac1d172014-06-17 05:15:38 -0700164 } else if (onlyVertical) {
165 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000166 }
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000167 firstPass = !topUnsortable || lastTopLeft != topLeft;
reed0dc4dd62015-03-24 13:55:33 -0700168 SkTDArray<SkOpSpan*> chase;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000169 do {
reed0dc4dd62015-03-24 13:55:33 -0700170 if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000171 do {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000172 if (!unsortable && current->done()) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000173 break;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000174 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000175 SkASSERT(unsortable || !current->done());
reed0dc4dd62015-03-24 13:55:33 -0700176 int nextStart = index;
177 int nextEnd = endIndex;
caryclarkdac1d172014-06-17 05:15:38 -0700178 SkOpSegment* next = current->findNextOp(&chase, &nextStart, &nextEnd,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000179 &unsortable, op, xorMask, xorOpMask);
180 if (!next) {
181 if (!unsortable && simple->hasMove()
182 && current->verb() != SkPath::kLine_Verb
183 && !simple->isClosed()) {
reed0dc4dd62015-03-24 13:55:33 -0700184 current->addCurveTo(index, endIndex, simple, true);
caryclarkdac1d172014-06-17 05:15:38 -0700185 #if DEBUG_ACTIVE_SPANS
186 if (!simple->isClosed()) {
187 DebugShowActiveSpans(contourList);
188 }
189 #endif
reed0dc4dd62015-03-24 13:55:33 -0700190// SkASSERT(simple->isClosed());
caryclark@google.com07393ca2013-04-08 11:47:37 +0000191 }
192 break;
193 }
194 #if DEBUG_FLOW
reed0dc4dd62015-03-24 13:55:33 -0700195 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.com07393ca2013-04-08 11:47:37 +0000198 #endif
reed0dc4dd62015-03-24 13:55:33 -0700199 current->addCurveTo(index, endIndex, simple, true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000200 current = next;
reed0dc4dd62015-03-24 13:55:33 -0700201 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.com07393ca2013-04-08 11:47:37 +0000214 }
215 }
216 simple->close();
217 } else {
reed0dc4dd62015-03-24 13:55:33 -0700218 SkOpSpan* last = current->markAndChaseDoneBinary(index, endIndex);
219 if (last && !last->fChased && !last->fLoop) {
220 last->fChased = true;
caryclarkdac1d172014-06-17 05:15:38 -0700221 SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last));
222 *chase.append() = last;
223#if DEBUG_WINDING
reed0dc4dd62015-03-24 13:55:33 -0700224 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);
caryclarkdac1d172014-06-17 05:15:38 -0700227#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000228 }
229 }
reed0dc4dd62015-03-24 13:55:33 -0700230 current = findChaseOp(chase, &index, &endIndex);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000231 #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.com7dfbb072013-04-22 14:37:05 +0000242// pretty picture:
243// https://docs.google.com/a/google.com/drawings/d/1sPV8rPfpEFXymBp3iSbDRWAycp1b-7vD9JP2V-kn9Ss/edit?usp=sharing
244static 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
254static 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
caryclark65f55312014-11-13 06:58:52 -0800262#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
267static 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
274static int dumpID = 0;
275
276static 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);
reed0dc4dd62015-03-24 13:55:33 -0700294 fprintf(file, "}\n");
caryclark65f55312014-11-13 06:58:52 -0800295 fclose(file);
296}
297#endif
298
caryclark@google.com66560ca2013-04-26 19:51:16 +0000299bool Op(const SkPath& one, const SkPath& two, SkPathOp op, SkPath* result) {
caryclark65f55312014-11-13 06:58:52 -0800300#if DEBUGGING_PATHOPS_FROM_HOST
301 dump_op(one, two, op);
reed0dc4dd62015-03-24 13:55:33 -0700302#endif
303#if DEBUG_SHOW_TEST_NAME
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000304 char* debugName = DEBUG_FILENAME_STRING;
305 if (debugName && debugName[0]) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000306 SkPathOpsDebug::BumpTestName(debugName);
307 SkPathOpsDebug::ShowPath(one, two, op, debugName);
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000308 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000309#endif
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000310 op = gOpInverse[op][one.isInverseFillType()][two.isInverseFillType()];
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000311 SkPath::FillType fillType = gOutInverse[op][one.isInverseFillType()][two.isInverseFillType()]
312 ? SkPath::kInverseEvenOdd_FillType : SkPath::kEvenOdd_FillType;
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000313 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.com07393ca2013-04-08 11:47:37 +0000320#if DEBUG_SORT || DEBUG_SWAP_TOP
caryclark@google.com570863f2013-09-16 15:55:01 +0000321 SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000322#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000323 // turn path into list of segments
reed0dc4dd62015-03-24 13:55:33 -0700324 SkTArray<SkOpContour> contours;
325 // FIXME: add self-intersecting cubics' T values to segment
326 SkOpEdgeBuilder builder(*minuend, contours);
caryclarkd751ac02014-10-03 05:36:27 -0700327 if (builder.unparseable()) {
328 return false;
329 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000330 const int xorMask = builder.xorMask();
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000331 builder.addOperand(*subtrahend);
reed0dc4dd62015-03-24 13:55:33 -0700332 if (!builder.finish()) {
caryclark@google.com66560ca2013-04-26 19:51:16 +0000333 return false;
334 }
caryclark@google.com6dc7df62013-04-25 11:51:54 +0000335 result->reset();
336 result->setFillType(fillType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000337 const int xorOpMask = builder.xorMask();
reed0dc4dd62015-03-24 13:55:33 -0700338 SkTArray<SkOpContour*, true> contourList;
339 MakeContourList(contours, contourList, xorMask == kEvenOdd_PathOpsMask,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000340 xorOpMask == kEvenOdd_PathOpsMask);
341 SkOpContour** currentPtr = contourList.begin();
342 if (!currentPtr) {
caryclark@google.com66560ca2013-04-26 19:51:16 +0000343 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000344 }
345 SkOpContour** listEnd = contourList.end();
346 // find all intersections between segments
347 do {
348 SkOpContour** nextPtr = currentPtr;
349 SkOpContour* current = *currentPtr++;
reed0dc4dd62015-03-24 13:55:33 -0700350 if (current->containsCubics()) {
351 AddSelfIntersectTs(current);
352 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000353 SkOpContour* next;
354 do {
355 next = *nextPtr++;
reed0dc4dd62015-03-24 13:55:33 -0700356 } while (AddIntersectTs(current, next) && nextPtr != listEnd);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000357 } while (currentPtr != listEnd);
358 // eat through coincident edges
reed0dc4dd62015-03-24 13:55:33 -0700359
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.org4431e772014-04-14 17:08:59 +0000366 return false;
367 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000368 // construct closed contours
369 SkPathWriter wrapper(*result);
reed0dc4dd62015-03-24 13:55:33 -0700370 bridgeOp(contourList, op, xorMask, xorOpMask, &wrapper);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000371 { // if some edges could not be resolved, assemble remaining fragments
372 SkPath temp;
caryclark@google.com7dfbb072013-04-22 14:37:05 +0000373 temp.setFillType(fillType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000374 SkPathWriter assembled(temp);
375 Assemble(wrapper, &assembled);
376 *result = *assembled.nativePath();
caryclark@google.com66560ca2013-04-26 19:51:16 +0000377 result->setFillType(fillType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000378 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000379 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000380}