blob: 4f53dee056645bc9658d6e020bdab4597bf1999a [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
8#include "SkMatrix.h"
caryclark5b5ddd72015-05-18 05:12:56 -07009#include "SkOpEdgeBuilder.h"
reed026beb52015-06-10 14:23:15 -070010#include "SkPathPriv.h"
caryclark45fa4472015-01-16 07:04:10 -080011#include "SkPathOps.h"
caryclark5b5ddd72015-05-18 05:12:56 -070012#include "SkPathOpsCommon.h"
13
14static bool one_contour(const SkPath& path) {
15 SkChunkAlloc allocator(256);
16 int verbCount = path.countVerbs();
17 uint8_t* verbs = (uint8_t*) allocator.alloc(sizeof(uint8_t) * verbCount,
18 SkChunkAlloc::kThrow_AllocFailType);
19 (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
28void FixWinding(SkPath* path) {
29 SkPath::FillType fillType = path->getFillType();
30 if (fillType == SkPath::kInverseEvenOdd_FillType) {
31 fillType = SkPath::kInverseWinding_FillType;
32 } else if (fillType == SkPath::kEvenOdd_FillType) {
33 fillType = SkPath::kWinding_FillType;
34 }
reed026beb52015-06-10 14:23:15 -070035 SkPathPriv::FirstDirection dir;
36 if (one_contour(*path) && SkPathPriv::CheapComputeFirstDirection(*path, &dir)) {
37 if (dir != SkPathPriv::kCCW_FirstDirection) {
caryclark5b5ddd72015-05-18 05:12:56 -070038 SkPath temp;
39 temp.reverseAddPath(*path);
40 *path = temp;
41 }
42 path->setFillType(fillType);
43 return;
44 }
45 SkChunkAlloc allocator(4096);
46 SkOpContourHead contourHead;
halcanary96fcdcc2015-08-27 07:41:13 -070047 SkOpGlobalState globalState(nullptr, &contourHead SkDEBUGPARAMS(nullptr));
caryclark5b5ddd72015-05-18 05:12:56 -070048 SkOpEdgeBuilder builder(*path, &contourHead, &allocator, &globalState);
49 builder.finish(&allocator);
50 SkASSERT(contourHead.next());
51 contourHead.resetReverse();
52 bool writePath = false;
53 SkOpSpan* topSpan;
54 globalState.setPhase(SkOpGlobalState::kFixWinding);
55 while ((topSpan = FindSortableTop(&contourHead))) {
56 SkOpSegment* topSegment = topSpan->segment();
57 SkOpContour* topContour = topSegment->contour();
caryclark5b5ddd72015-05-18 05:12:56 -070058 SkASSERT(topContour->isCcw() >= 0);
caryclark4e1a4c92015-05-18 12:56:57 -070059#if DEBUG_WINDING
60 SkDebugf("%s id=%d nested=%d ccw=%d\n", __FUNCTION__,
61 topSegment->debugID(), globalState.nested(), topContour->isCcw());
62#endif
63 if ((globalState.nested() & 1) != SkToBool(topContour->isCcw())) {
caryclark5b5ddd72015-05-18 05:12:56 -070064 topContour->setReverse();
65 writePath = true;
66 }
67 topContour->markDone();
caryclark4e1a4c92015-05-18 12:56:57 -070068 globalState.clearNested();
caryclark5b5ddd72015-05-18 05:12:56 -070069 }
70 if (!writePath) {
71 path->setFillType(fillType);
72 return;
73 }
74 SkPath empty;
75 SkPathWriter woundPath(empty);
76 SkOpContour* test = &contourHead;
77 do {
78 if (test->reversed()) {
79 test->toReversePath(&woundPath);
80 } else {
81 test->toPath(&woundPath);
82 }
83 } while ((test = test->next()));
84 *path = *woundPath.nativePath();
85 path->setFillType(fillType);
86}
caryclark45fa4472015-01-16 07:04:10 -080087
88void SkOpBuilder::add(const SkPath& path, SkPathOp op) {
caryclark54359292015-03-26 07:52:43 -070089 if (0 == fOps.count() && op != kUnion_SkPathOp) {
caryclark45fa4472015-01-16 07:04:10 -080090 fPathRefs.push_back() = SkPath();
caryclark54359292015-03-26 07:52:43 -070091 *fOps.append() = kUnion_SkPathOp;
caryclark45fa4472015-01-16 07:04:10 -080092 }
93 fPathRefs.push_back() = path;
94 *fOps.append() = op;
95}
96
97void SkOpBuilder::reset() {
98 fPathRefs.reset();
99 fOps.reset();
100}
101
102/* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex
103 paths with union ops could be locally resolved and still improve over doing the
104 ops one at a time. */
105bool SkOpBuilder::resolve(SkPath* result) {
caryclark1049f122015-04-20 08:31:59 -0700106 SkPath original = *result;
caryclark45fa4472015-01-16 07:04:10 -0800107 int count = fOps.count();
108 bool allUnion = true;
caryclarkd83f4952015-06-17 08:46:12 -0700109 SkPathPriv::FirstDirection firstDir = SkPathPriv::kUnknown_FirstDirection;
caryclark45fa4472015-01-16 07:04:10 -0800110 for (int index = 0; index < count; ++index) {
111 SkPath* test = &fPathRefs[index];
caryclark54359292015-03-26 07:52:43 -0700112 if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) {
caryclark45fa4472015-01-16 07:04:10 -0800113 allUnion = false;
114 break;
115 }
116 // If all paths are convex, track direction, reversing as needed.
117 if (test->isConvex()) {
reed026beb52015-06-10 14:23:15 -0700118 SkPathPriv::FirstDirection dir;
119 if (!SkPathPriv::CheapComputeFirstDirection(*test, &dir)) {
caryclark45fa4472015-01-16 07:04:10 -0800120 allUnion = false;
121 break;
122 }
caryclarkd83f4952015-06-17 08:46:12 -0700123 if (firstDir == SkPathPriv::kUnknown_FirstDirection) {
caryclark45fa4472015-01-16 07:04:10 -0800124 firstDir = dir;
125 } else if (firstDir != dir) {
126 SkPath temp;
127 temp.reverseAddPath(*test);
128 *test = temp;
129 }
130 continue;
131 }
132 // If the path is not convex but its bounds do not intersect the others, simplify is enough.
133 const SkRect& testBounds = test->getBounds();
134 for (int inner = 0; inner < index; ++inner) {
135 // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds?
136 if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) {
137 allUnion = false;
138 break;
139 }
140 }
141 }
142 if (!allUnion) {
143 *result = fPathRefs[0];
144 for (int index = 1; index < count; ++index) {
145 if (!Op(*result, fPathRefs[index], fOps[index], result)) {
146 reset();
caryclark1049f122015-04-20 08:31:59 -0700147 *result = original;
caryclark45fa4472015-01-16 07:04:10 -0800148 return false;
149 }
150 }
151 reset();
152 return true;
153 }
154 SkPath sum;
155 for (int index = 0; index < count; ++index) {
156 if (!Simplify(fPathRefs[index], &fPathRefs[index])) {
157 reset();
caryclark1049f122015-04-20 08:31:59 -0700158 *result = original;
caryclark45fa4472015-01-16 07:04:10 -0800159 return false;
160 }
caryclark218f21a2015-06-29 11:41:52 -0700161 if (!fPathRefs[index].isEmpty()) {
162 // convert the even odd result back to winding form before accumulating it
163 FixWinding(&fPathRefs[index]);
164 sum.addPath(fPathRefs[index]);
165 }
caryclark45fa4472015-01-16 07:04:10 -0800166 }
167 reset();
caryclark1049f122015-04-20 08:31:59 -0700168 bool success = Simplify(sum, result);
169 if (!success) {
170 *result = original;
171 }
172 return success;
caryclark45fa4472015-01-16 07:04:10 -0800173}