blob: d9d08799866b11cb4fb9efba412dcf785fd0d59a [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"
caryclark54359292015-03-26 07:52:43 -07008#include "SkOpCoincidence.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00009#include "SkOpEdgeBuilder.h"
10#include "SkPathOpsCommon.h"
11#include "SkPathWriter.h"
12
caryclarkeed356d2016-09-14 07:18:20 -070013static bool bridgeWinding(SkOpContourHead* contourList, SkPathWriter* simple) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000014 bool unsortable = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +000015 do {
caryclark624637c2015-05-11 07:21:27 -070016 SkOpSpan* span = FindSortableTop(contourList);
17 if (!span) {
caryclarkdac1d172014-06-17 05:15:38 -070018 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +000019 }
caryclark624637c2015-05-11 07:21:27 -070020 SkOpSegment* current = span->segment();
21 SkOpSpanBase* start = span->next();
22 SkOpSpanBase* end = span;
caryclark54359292015-03-26 07:52:43 -070023 SkTDArray<SkOpSpanBase*> chase;
caryclark@google.com07393ca2013-04-08 11:47:37 +000024 do {
caryclark54359292015-03-26 07:52:43 -070025 if (current->activeWinding(start, end)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000026 do {
caryclark@google.com07393ca2013-04-08 11:47:37 +000027 if (!unsortable && current->done()) {
caryclarkdac1d172014-06-17 05:15:38 -070028 break;
caryclark@google.coma5e55922013-05-07 18:51:31 +000029 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000030 SkASSERT(unsortable || !current->done());
caryclark54359292015-03-26 07:52:43 -070031 SkOpSpanBase* nextStart = start;
32 SkOpSpanBase* nextEnd = end;
caryclarkdac1d172014-06-17 05:15:38 -070033 SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd,
caryclark@google.com07393ca2013-04-08 11:47:37 +000034 &unsortable);
35 if (!next) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000036 break;
37 }
38 #if DEBUG_FLOW
39 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
caryclark54359292015-03-26 07:52:43 -070040 current->debugID(), start->pt().fX, start->pt().fY,
41 end->pt().fX, end->pt().fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +000042 #endif
caryclarkef784fb2015-10-30 12:03:06 -070043 if (!current->addCurveTo(start, end, simple)) {
44 return false;
45 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000046 current = next;
caryclark54359292015-03-26 07:52:43 -070047 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()) {
caryclarkef784fb2015-10-30 12:03:06 -070053 if (!current->addCurveTo(start, end, simple)) {
54 return false;
55 }
caryclark54359292015-03-26 07:52:43 -070056 current->markDone(spanStart);
caryclark@google.com07393ca2013-04-08 11:47:37 +000057 }
58 }
caryclarkeed356d2016-09-14 07:18:20 -070059 simple->finishContour();
caryclark@google.com07393ca2013-04-08 11:47:37 +000060 } else {
caryclark54359292015-03-26 07:52:43 -070061 SkOpSpanBase* last = current->markAndChaseDone(start, end);
62 if (last && !last->chased()) {
63 last->setChased(true);
caryclarkdac1d172014-06-17 05:15:38 -070064 SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last));
caryclarkdac1d172014-06-17 05:15:38 -070065 *chase.append() = last;
66#if DEBUG_WINDING
caryclark54359292015-03-26 07:52:43 -070067 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");
caryclarkdac1d172014-06-17 05:15:38 -070072#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +000073 }
74 }
caryclark54359292015-03-26 07:52:43 -070075 current = FindChase(&chase, &start, &end);
caryclark55888e42016-07-18 10:01:36 -070076 SkPathOpsDebug::ShowActiveSpans(contourList);
caryclark@google.com07393ca2013-04-08 11:47:37 +000077 if (!current) {
78 break;
79 }
80 } while (true);
81 } while (true);
caryclarkef784fb2015-10-30 12:03:06 -070082 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000083}
84
85// returns true if all edges were processed
caryclarkeed356d2016-09-14 07:18:20 -070086static bool bridgeXor(SkOpContourHead* contourList, SkPathWriter* simple) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000087 bool unsortable = false;
Cary Clarkab2d73b2016-12-16 17:17:25 -050088 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.com07393ca2013-04-08 11:47:37 +000096 do {
caryclark@google.com07393ca2013-04-08 11:47:37 +000097 if (!unsortable && current->done()) {
Cary Clarkab2d73b2016-12-16 17:17:25 -050098 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +000099 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000100 SkASSERT(unsortable || !current->done());
caryclark54359292015-03-26 07:52:43 -0700101 SkOpSpanBase* nextStart = start;
102 SkOpSpanBase* nextEnd = end;
Cary Clarkab2d73b2016-12-16 17:17:25 -0500103 SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd,
104 &unsortable);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000105 if (!next) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000106 break;
107 }
108 #if DEBUG_FLOW
109 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
caryclark54359292015-03-26 07:52:43 -0700110 current->debugID(), start->pt().fX, start->pt().fY,
111 end->pt().fX, end->pt().fY);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000112 #endif
caryclarkef784fb2015-10-30 12:03:06 -0700113 if (!current->addCurveTo(start, end, simple)) {
114 return false;
115 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000116 current = next;
117 start = nextStart;
118 end = nextEnd;
caryclark54359292015-03-26 07:52:43 -0700119 } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done()));
caryclark@google.com07393ca2013-04-08 11:47:37 +0000120 if (!simple->isClosed()) {
caryclark54359292015-03-26 07:52:43 -0700121 SkOpSpan* spanStart = start->starter(end);
122 if (!spanStart->done()) {
caryclarkef784fb2015-10-30 12:03:06 -0700123 if (!current->addCurveTo(start, end, simple)) {
124 return false;
125 }
caryclark54359292015-03-26 07:52:43 -0700126 current->markDone(spanStart);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000127 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000128 }
caryclarkeed356d2016-09-14 07:18:20 -0700129 simple->finishContour();
caryclark55888e42016-07-18 10:01:36 -0700130 SkPathOpsDebug::ShowActiveSpans(contourList);
Cary Clarkab2d73b2016-12-16 17:17:25 -0500131 } while (true);
caryclarkef784fb2015-10-30 12:03:06 -0700132 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133}
134
135// FIXME : add this as a member of SkPath
caryclark55888e42016-07-18 10:01:36 -0700136bool SimplifyDebug(const SkPath& path, SkPath* result
137 SkDEBUGPARAMS(bool skipAssert) SkDEBUGPARAMS(const char* testName)) {
reed0dc4dd62015-03-24 13:55:33 -0700138 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
139 SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType
140 : SkPath::kEvenOdd_FillType;
caryclark54359292015-03-26 07:52:43 -0700141 if (path.isConvex()) {
142 if (result != &path) {
143 *result = path;
144 }
145 result->setFillType(fillType);
146 return true;
147 }
reed0dc4dd62015-03-24 13:55:33 -0700148 // turn path into list of segments
Florin Malita14a64302017-05-24 14:53:44 -0400149 SkSTArenaAlloc<4096> allocator; // FIXME: constant-ize, tune
caryclark54359292015-03-26 07:52:43 -0700150 SkOpContour contour;
caryclark624637c2015-05-11 07:21:27 -0700151 SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour);
caryclark55888e42016-07-18 10:01:36 -0700152 SkOpGlobalState globalState(contourList, &allocator
153 SkDEBUGPARAMS(skipAssert) SkDEBUGPARAMS(testName));
154 SkOpCoincidence coincidence(&globalState);
Cary Clark918fb1f2016-11-15 13:22:25 -0500155#if DEBUG_DUMP_VERIFY
156#ifndef SK_DEBUG
157 const char* testName = "release";
158#endif
caryclark13260682016-10-24 05:10:14 -0700159 if (SkPathOpsDebug::gDumpOp) {
160 SkPathOpsDebug::DumpSimplify(path, testName);
161 }
162#endif
caryclark55888e42016-07-18 10:01:36 -0700163 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 }
caryclark624637c2015-05-11 07:21:27 -0700172#if DEBUG_SORT
caryclark54359292015-03-26 07:52:43 -0700173 SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault;
174#endif
caryclarkeed356d2016-09-14 07:18:20 -0700175 SkOpEdgeBuilder builder(*workingPath, contourList, &globalState);
caryclark55888e42016-07-18 10:01:36 -0700176 if (!builder.finish()) {
caryclark@google.com66560ca2013-04-26 19:51:16 +0000177 return false;
178 }
caryclark03b03ca2015-04-23 09:13:37 -0700179#if DEBUG_DUMP_SEGMENTS
caryclark26ad22a2015-10-16 09:03:38 -0700180 contour.dumpSegments();
caryclark54359292015-03-26 07:52:43 -0700181#endif
caryclark624637c2015-05-11 07:21:27 -0700182 if (!SortContourList(&contourList, false, false)) {
caryclark1049f122015-04-20 08:31:59 -0700183 result->reset();
184 result->setFillType(fillType);
caryclark@google.com66560ca2013-04-26 19:51:16 +0000185 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000186 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000187 // find all intersections between segments
caryclark624637c2015-05-11 07:21:27 -0700188 SkOpContour* current = contourList;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000189 do {
caryclark624637c2015-05-11 07:21:27 -0700190 SkOpContour* next = current;
caryclark55888e42016-07-18 10:01:36 -0700191 while (AddIntersectTs(current, next, &coincidence)
caryclark624637c2015-05-11 07:21:27 -0700192 && (next = next->next()));
193 } while ((current = current->next()));
caryclark54359292015-03-26 07:52:43 -0700194#if DEBUG_VALIDATE
Cary Clarkab87d7a2016-10-04 10:01:04 -0400195 globalState.setPhase(SkOpPhase::kWalking);
caryclark54359292015-03-26 07:52:43 -0700196#endif
Cary Clarkab87d7a2016-10-04 10:01:04 -0400197 bool success = HandleCoincidence(contourList, &coincidence);
198#if DEBUG_COIN
199 globalState.debugAddToGlobalCoinDicts();
200#endif
201 if (!success) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000202 return false;
203 }
caryclark26ad22a2015-10-16 09:03:38 -0700204#if DEBUG_DUMP_ALIGNMENT
205 contour.dumpSegments("aligned");
206#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000207 // construct closed contours
caryclark1049f122015-04-20 08:31:59 -0700208 result->reset();
209 result->setFillType(fillType);
caryclark54359292015-03-26 07:52:43 -0700210 SkPathWriter wrapper(*result);
caryclarkeed356d2016-09-14 07:18:20 -0700211 if (builder.xorMask() == kWinding_PathOpsMask ? !bridgeWinding(contourList, &wrapper)
212 : !bridgeXor(contourList, &wrapper)) {
caryclarkef784fb2015-10-30 12:03:06 -0700213 return false;
214 }
caryclarkeed356d2016-09-14 07:18:20 -0700215 wrapper.assemble(); // if some edges could not be resolved, assemble remaining
caryclark55888e42016-07-18 10:01:36 -0700216 if (scaleFactor > 1) {
217 ScalePath(*result, scaleFactor, result);
218 }
caryclark@google.com66560ca2013-04-26 19:51:16 +0000219 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000220}
caryclark5b5ddd72015-05-18 05:12:56 -0700221
caryclark55888e42016-07-18 10:01:36 -0700222bool Simplify(const SkPath& path, SkPath* result) {
Cary Clark918fb1f2016-11-15 13:22:25 -0500223#if DEBUG_DUMP_VERIFY
caryclark13260682016-10-24 05:10:14 -0700224 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
caryclark55888e42016-07-18 10:01:36 -0700233 return SimplifyDebug(path, result SkDEBUGPARAMS(true) SkDEBUGPARAMS(nullptr));
234}