blob: a52bf5b04aba572fff4746364537310c0e47c97c [file] [log] [blame]
caryclark45fa4472015-01-16 07:04:10 -08001/*
2 * Copyright 2014 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkMatrix.h"
9#include "include/pathops/SkPathOps.h"
Ben Wagner729a23f2019-05-17 16:29:34 -040010#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkPathPriv.h"
12#include "src/pathops/SkOpEdgeBuilder.h"
13#include "src/pathops/SkPathOpsCommon.h"
caryclark5b5ddd72015-05-18 05:12:56 -070014
15static bool one_contour(const SkPath& path) {
Florin Malita14a64302017-05-24 14:53:44 -040016 SkSTArenaAlloc<256> allocator;
caryclark5b5ddd72015-05-18 05:12:56 -070017 int verbCount = path.countVerbs();
Herb Derbyc3cc5fa2017-03-07 11:11:47 -050018 uint8_t* verbs = (uint8_t*) allocator.makeArrayDefault<uint8_t>(verbCount);
caryclark5b5ddd72015-05-18 05:12:56 -070019 (void) path.getVerbs(verbs, verbCount);
20 for (int index = 1; index < verbCount; ++index) {
21 if (verbs[index] == SkPath::kMove_Verb) {
22 return false;
23 }
24 }
25 return true;
26}
27
caryclark51c56782016-11-07 05:09:28 -080028void SkOpBuilder::ReversePath(SkPath* path) {
29 SkPath temp;
30 SkPoint lastPt;
31 SkAssertResult(path->getLastPt(&lastPt));
32 temp.moveTo(lastPt);
33 temp.reversePathTo(*path);
34 temp.close();
35 *path = temp;
36}
37
38bool SkOpBuilder::FixWinding(SkPath* path) {
Mike Reedcf0e3c62019-12-03 16:26:15 -050039 SkPathFillType fillType = path->getFillType();
Mike Reed7d34dc72019-11-26 12:17:17 -050040 if (fillType == SkPathFillType::kInverseEvenOdd) {
41 fillType = SkPathFillType::kInverseWinding;
42 } else if (fillType == SkPathFillType::kEvenOdd) {
43 fillType = SkPathFillType::kWinding;
caryclark5b5ddd72015-05-18 05:12:56 -070044 }
Mike Reed85f51b22020-08-30 10:32:06 -040045 if (one_contour(*path)) {
46 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*path);
47 if (dir != SkPathFirstDirection::kUnknown) {
48 if (dir == SkPathFirstDirection::kCW) {
49 ReversePath(path);
50 }
51 path->setFillType(fillType);
52 return true;
caryclark5b5ddd72015-05-18 05:12:56 -070053 }
caryclark5b5ddd72015-05-18 05:12:56 -070054 }
Florin Malita14a64302017-05-24 14:53:44 -040055 SkSTArenaAlloc<4096> allocator;
caryclark5b5ddd72015-05-18 05:12:56 -070056 SkOpContourHead contourHead;
caryclark55888e42016-07-18 10:01:36 -070057 SkOpGlobalState globalState(&contourHead, &allocator SkDEBUGPARAMS(false)
caryclarkdae6b972016-06-08 04:28:19 -070058 SkDEBUGPARAMS(nullptr));
caryclark55888e42016-07-18 10:01:36 -070059 SkOpEdgeBuilder builder(*path, &contourHead, &globalState);
caryclark7b33bf12016-07-21 08:53:32 -070060 if (builder.unparseable() || !builder.finish()) {
61 return false;
62 }
63 if (!contourHead.count()) {
64 return true;
65 }
caryclarke3a4e992016-09-28 09:22:17 -070066 if (!contourHead.next()) {
67 return false;
68 }
caryclarkeed356d2016-09-14 07:18:20 -070069 contourHead.joinAllSegments();
caryclark5b5ddd72015-05-18 05:12:56 -070070 contourHead.resetReverse();
71 bool writePath = false;
72 SkOpSpan* topSpan;
Cary Clarkab87d7a2016-10-04 10:01:04 -040073 globalState.setPhase(SkOpPhase::kFixWinding);
caryclark5b5ddd72015-05-18 05:12:56 -070074 while ((topSpan = FindSortableTop(&contourHead))) {
75 SkOpSegment* topSegment = topSpan->segment();
76 SkOpContour* topContour = topSegment->contour();
caryclark5b5ddd72015-05-18 05:12:56 -070077 SkASSERT(topContour->isCcw() >= 0);
caryclark4e1a4c92015-05-18 12:56:57 -070078#if DEBUG_WINDING
79 SkDebugf("%s id=%d nested=%d ccw=%d\n", __FUNCTION__,
80 topSegment->debugID(), globalState.nested(), topContour->isCcw());
81#endif
82 if ((globalState.nested() & 1) != SkToBool(topContour->isCcw())) {
caryclark5b5ddd72015-05-18 05:12:56 -070083 topContour->setReverse();
84 writePath = true;
85 }
caryclark55888e42016-07-18 10:01:36 -070086 topContour->markAllDone();
caryclark4e1a4c92015-05-18 12:56:57 -070087 globalState.clearNested();
caryclark5b5ddd72015-05-18 05:12:56 -070088 }
89 if (!writePath) {
90 path->setFillType(fillType);
caryclarkdae6b972016-06-08 04:28:19 -070091 return true;
caryclark5b5ddd72015-05-18 05:12:56 -070092 }
93 SkPath empty;
94 SkPathWriter woundPath(empty);
95 SkOpContour* test = &contourHead;
96 do {
Cary Clarkfa9193d2016-12-21 08:25:00 -050097 if (!test->count()) {
98 continue;
99 }
caryclark5b5ddd72015-05-18 05:12:56 -0700100 if (test->reversed()) {
101 test->toReversePath(&woundPath);
102 } else {
103 test->toPath(&woundPath);
104 }
105 } while ((test = test->next()));
106 *path = *woundPath.nativePath();
107 path->setFillType(fillType);
caryclarkdae6b972016-06-08 04:28:19 -0700108 return true;
caryclark5b5ddd72015-05-18 05:12:56 -0700109}
caryclark45fa4472015-01-16 07:04:10 -0800110
111void SkOpBuilder::add(const SkPath& path, SkPathOp op) {
caryclark54359292015-03-26 07:52:43 -0700112 if (0 == fOps.count() && op != kUnion_SkPathOp) {
caryclark45fa4472015-01-16 07:04:10 -0800113 fPathRefs.push_back() = SkPath();
caryclark54359292015-03-26 07:52:43 -0700114 *fOps.append() = kUnion_SkPathOp;
caryclark45fa4472015-01-16 07:04:10 -0800115 }
116 fPathRefs.push_back() = path;
117 *fOps.append() = op;
118}
119
120void SkOpBuilder::reset() {
121 fPathRefs.reset();
122 fOps.reset();
123}
124
125/* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex
126 paths with union ops could be locally resolved and still improve over doing the
127 ops one at a time. */
128bool SkOpBuilder::resolve(SkPath* result) {
caryclark1049f122015-04-20 08:31:59 -0700129 SkPath original = *result;
caryclark45fa4472015-01-16 07:04:10 -0800130 int count = fOps.count();
131 bool allUnion = true;
Mike Reed3872c982020-08-29 17:46:51 -0400132 SkPathFirstDirection firstDir = SkPathFirstDirection::kUnknown;
caryclark45fa4472015-01-16 07:04:10 -0800133 for (int index = 0; index < count; ++index) {
134 SkPath* test = &fPathRefs[index];
caryclark54359292015-03-26 07:52:43 -0700135 if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) {
caryclark45fa4472015-01-16 07:04:10 -0800136 allUnion = false;
137 break;
138 }
139 // If all paths are convex, track direction, reversing as needed.
140 if (test->isConvex()) {
Mike Reed85f51b22020-08-30 10:32:06 -0400141 SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*test);
142 if (dir == SkPathFirstDirection::kUnknown) {
caryclark45fa4472015-01-16 07:04:10 -0800143 allUnion = false;
144 break;
145 }
Mike Reed3872c982020-08-29 17:46:51 -0400146 if (firstDir == SkPathFirstDirection::kUnknown) {
caryclark45fa4472015-01-16 07:04:10 -0800147 firstDir = dir;
148 } else if (firstDir != dir) {
caryclark51c56782016-11-07 05:09:28 -0800149 ReversePath(test);
caryclark45fa4472015-01-16 07:04:10 -0800150 }
151 continue;
152 }
153 // If the path is not convex but its bounds do not intersect the others, simplify is enough.
154 const SkRect& testBounds = test->getBounds();
155 for (int inner = 0; inner < index; ++inner) {
156 // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds?
157 if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) {
158 allUnion = false;
159 break;
160 }
161 }
162 }
163 if (!allUnion) {
164 *result = fPathRefs[0];
165 for (int index = 1; index < count; ++index) {
166 if (!Op(*result, fPathRefs[index], fOps[index], result)) {
167 reset();
caryclark1049f122015-04-20 08:31:59 -0700168 *result = original;
caryclark45fa4472015-01-16 07:04:10 -0800169 return false;
170 }
171 }
172 reset();
173 return true;
174 }
175 SkPath sum;
176 for (int index = 0; index < count; ++index) {
177 if (!Simplify(fPathRefs[index], &fPathRefs[index])) {
178 reset();
caryclark1049f122015-04-20 08:31:59 -0700179 *result = original;
caryclark45fa4472015-01-16 07:04:10 -0800180 return false;
181 }
caryclark218f21a2015-06-29 11:41:52 -0700182 if (!fPathRefs[index].isEmpty()) {
183 // convert the even odd result back to winding form before accumulating it
caryclarkdae6b972016-06-08 04:28:19 -0700184 if (!FixWinding(&fPathRefs[index])) {
185 *result = original;
186 return false;
187 }
caryclark218f21a2015-06-29 11:41:52 -0700188 sum.addPath(fPathRefs[index]);
189 }
caryclark45fa4472015-01-16 07:04:10 -0800190 }
191 reset();
caryclark1049f122015-04-20 08:31:59 -0700192 bool success = Simplify(sum, result);
193 if (!success) {
194 *result = original;
195 }
196 return success;
caryclark45fa4472015-01-16 07:04:10 -0800197}