| 1 |
/* |
| 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 "include/core/SkPath.h" |
| 9 |
#include "include/core/SkPathTypes.h" |
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/pathops/SkPathOps.h" |
| 14 |
#include "include/private/base/SkTArray.h" |
| 15 |
#include "include/private/base/SkTDArray.h" |
| 16 |
#include "include/private/base/SkTo.h" |
| 17 |
#include "src/base/SkArenaAlloc.h" |
| 18 |
#include "src/core/SkPathEnums.h" |
| 19 |
#include "src/core/SkPathPriv.h" |
| 20 |
#include "src/pathops/SkOpContour.h" |
| 21 |
#include "src/pathops/SkOpEdgeBuilder.h" |
| 22 |
#include "src/pathops/SkOpSegment.h" |
| 23 |
#include "src/pathops/SkOpSpan.h" |
| 24 |
#include "src/pathops/SkPathOpsCommon.h" |
| 25 |
#include "src/pathops/SkPathOpsTypes.h" |
| 26 |
#include "src/pathops/SkPathWriter.h" |
| 27 |
|
| 28 |
#include <cstdint> |
| 29 |
|
| 30 |
static bool one_contour(const SkPath& path) { |
| 31 |
const auto raw = SkPathPriv::Raw(path, SkResolveConvexity::kNo); |
| 32 |
if (!raw) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
const auto verbs = raw->verbs(); |
| 37 |
for (size_t i = 1; i < verbs.size(); ++i) { |
| 38 |
if (verbs[i] == SkPathVerb::kMove) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
void SkOpBuilder::ReversePath(SkPath* path) { |
| 47 |
auto lastPt = path->getLastPt(); |
| 48 |
SkASSERT(lastPt.has_value()); |
| 49 |
SkPathBuilder temp; |
| 50 |
temp.moveTo(*lastPt); |
| 51 |
SkPathPriv::ReversePathTo(&temp, *path); |
| 52 |
temp.close(); |
| 53 |
*path = temp.detach(); |
| 54 |
} |
| 55 |
|
| 56 |
bool SkOpBuilder::FixWinding(SkPath* path) { |
| 57 |
SkPathFillType fillType = path->getFillType(); |
| 58 |
if (fillType == SkPathFillType::kInverseEvenOdd) { |
| 59 |
fillType = SkPathFillType::kInverseWinding; |
| 60 |
} else if (fillType == SkPathFillType::kEvenOdd) { |
| 61 |
fillType = SkPathFillType::kWinding; |
| 62 |
} |
| 63 |
if (one_contour(*path)) { |
| 64 |
SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*path); |
| 65 |
if (dir != SkPathFirstDirection::kUnknown) { |
| 66 |
if (dir == SkPathFirstDirection::kCW) { |
| 67 |
ReversePath(path); |
| 68 |
} |
| 69 |
path->setFillType(fillType); |
| 70 |
return true; |
| 71 |
} |
| 72 |
} |
| 73 |
SkSTArenaAlloc<4096> allocator; |
| 74 |
SkOpContourHead contourHead; |
| 75 |
SkOpGlobalState globalState(&contourHead, &allocator SkDEBUGPARAMS(false) |
| 76 |
SkDEBUGPARAMS(nullptr)); |
| 77 |
SkOpEdgeBuilder builder(*path, &contourHead, &globalState); |
| 78 |
if (builder.unparseable() || !builder.finish()) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
if (!contourHead.count()) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
if (!contourHead.next()) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
contourHead.joinAllSegments(); |
| 88 |
contourHead.resetReverse(); |
| 89 |
bool writePath = false; |
| 90 |
SkOpSpan* topSpan; |
| 91 |
globalState.setPhase(SkOpPhase::kFixWinding); |
| 92 |
while ((topSpan = FindSortableTop(&contourHead))) { |
| 93 |
SkOpSegment* topSegment = topSpan->segment(); |
| 94 |
SkOpContour* topContour = topSegment->contour(); |
| 95 |
SkASSERT(topContour->isCcw() >= 0); |
| 96 |
#if DEBUG_WINDING |
| 97 |
SkDebugf("%s id=%d nested=%d ccw=%d\n", __FUNCTION__, |
| 98 |
topSegment->debugID(), globalState.nested(), topContour->isCcw()); |
| 99 |
#endif |
| 100 |
if ((globalState.nested() & 1) != SkToBool(topContour->isCcw())) { |
| 101 |
topContour->setReverse(); |
| 102 |
writePath = true; |
| 103 |
} |
| 104 |
topContour->markAllDone(); |
| 105 |
globalState.clearNested(); |
| 106 |
} |
| 107 |
if (!writePath) { |
| 108 |
path->setFillType(fillType); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
SkPathWriter woundPath(fillType); |
| 113 |
SkOpContour* test = &contourHead; |
| 114 |
do { |
| 115 |
if (!test->count()) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
if (test->reversed()) { |
| 119 |
test->toReversePath(&woundPath); |
| 120 |
} else { |
| 121 |
test->toPath(&woundPath); |
| 122 |
} |
| 123 |
} while ((test = test->next())); |
| 124 |
*path = woundPath.nativePath(); |
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
void SkOpBuilder::add(const SkPath& path, SkPathOp op) { |
| 129 |
if (fOps.empty() && op != kUnion_SkPathOp) { |
| 130 |
fPathRefs.push_back() = SkPath(); |
| 131 |
*fOps.append() = kUnion_SkPathOp; |
| 132 |
} |
| 133 |
fPathRefs.push_back() = path; |
| 134 |
*fOps.append() = op; |
| 135 |
} |
| 136 |
|
| 137 |
void SkOpBuilder::reset() { |
| 138 |
fPathRefs.clear(); |
| 139 |
fOps.reset(); |
| 140 |
} |
| 141 |
|
| 142 |
/* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex |
| 143 |
paths with union ops could be locally resolved and still improve over doing the |
| 144 |
ops one at a time. */ |
| 145 |
std::optional<SkPath> SkOpBuilder::resolve() { |
| 146 |
int count = fOps.size(); |
| 147 |
bool allUnion = true; |
| 148 |
SkPathFirstDirection firstDir = SkPathFirstDirection::kUnknown; |
| 149 |
for (int index = 0; index < count; ++index) { |
| 150 |
SkPath* test = &fPathRefs[index]; |
| 151 |
if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) { |
| 152 |
allUnion = false; |
| 153 |
break; |
| 154 |
} |
| 155 |
// If all paths are convex, track direction, reversing as needed. |
| 156 |
if (test->isConvex()) { |
| 157 |
SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*test); |
| 158 |
if (dir == SkPathFirstDirection::kUnknown) { |
| 159 |
allUnion = false; |
| 160 |
break; |
| 161 |
} |
| 162 |
if (firstDir == SkPathFirstDirection::kUnknown) { |
| 163 |
firstDir = dir; |
| 164 |
} else if (firstDir != dir) { |
| 165 |
ReversePath(test); |
| 166 |
} |
| 167 |
continue; |
| 168 |
} |
| 169 |
// If the path is not convex but its bounds do not intersect the others, simplify is enough. |
| 170 |
const SkRect& testBounds = test->getBounds(); |
| 171 |
for (int inner = 0; inner < index; ++inner) { |
| 172 |
// OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds? |
| 173 |
if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) { |
| 174 |
allUnion = false; |
| 175 |
break; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
if (!allUnion) { |
| 180 |
SkPath result = fPathRefs[0]; |
| 181 |
for (int index = 1; index < count; ++index) { |
| 182 |
if (auto res = Op(result, fPathRefs[index], fOps[index])) { |
| 183 |
result = *res; |
| 184 |
} else { |
| 185 |
reset(); |
| 186 |
return {}; |
| 187 |
} |
| 188 |
} |
| 189 |
reset(); |
| 190 |
return result; |
| 191 |
} |
| 192 |
SkPathBuilder sum; |
| 193 |
for (int index = 0; index < count; ++index) { |
| 194 |
auto result = Simplify(fPathRefs[index]); |
| 195 |
if (!result.has_value()) { |
| 196 |
reset(); |
| 197 |
return {}; |
| 198 |
} |
| 199 |
fPathRefs[index] = *result; |
| 200 |
if (!fPathRefs[index].isEmpty()) { |
| 201 |
// convert the even odd result back to winding form before accumulating it |
| 202 |
if (!FixWinding(&fPathRefs[index])) { |
| 203 |
return {}; |
| 204 |
} |
| 205 |
sum.addPath(fPathRefs[index]); |
| 206 |
} |
| 207 |
} |
| 208 |
reset(); |
| 209 |
|
| 210 |
return Simplify(sum.detach()); |
| 211 |
} |