blob: 1c21a5984c464ba849e92cc23f7221ae2376adb2 [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"
9#include "SkPath.h"
10#include "SkPathOps.h"
11
12void SkOpBuilder::add(const SkPath& path, SkPathOp op) {
caryclark54359292015-03-26 07:52:43 -070013 if (0 == fOps.count() && op != kUnion_SkPathOp) {
caryclark45fa4472015-01-16 07:04:10 -080014 fPathRefs.push_back() = SkPath();
caryclark54359292015-03-26 07:52:43 -070015 *fOps.append() = kUnion_SkPathOp;
caryclark45fa4472015-01-16 07:04:10 -080016 }
17 fPathRefs.push_back() = path;
18 *fOps.append() = op;
19}
20
21void SkOpBuilder::reset() {
22 fPathRefs.reset();
23 fOps.reset();
24}
25
26/* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex
27 paths with union ops could be locally resolved and still improve over doing the
28 ops one at a time. */
29bool SkOpBuilder::resolve(SkPath* result) {
30 int count = fOps.count();
31 bool allUnion = true;
32 SkPath::Direction firstDir;
33 for (int index = 0; index < count; ++index) {
34 SkPath* test = &fPathRefs[index];
caryclark54359292015-03-26 07:52:43 -070035 if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) {
caryclark45fa4472015-01-16 07:04:10 -080036 allUnion = false;
37 break;
38 }
39 // If all paths are convex, track direction, reversing as needed.
40 if (test->isConvex()) {
41 SkPath::Direction dir;
42 if (!test->cheapComputeDirection(&dir)) {
43 allUnion = false;
44 break;
45 }
46 if (index == 0) {
47 firstDir = dir;
48 } else if (firstDir != dir) {
49 SkPath temp;
50 temp.reverseAddPath(*test);
51 *test = temp;
52 }
53 continue;
54 }
55 // If the path is not convex but its bounds do not intersect the others, simplify is enough.
56 const SkRect& testBounds = test->getBounds();
57 for (int inner = 0; inner < index; ++inner) {
58 // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds?
59 if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) {
60 allUnion = false;
61 break;
62 }
63 }
64 }
65 if (!allUnion) {
66 *result = fPathRefs[0];
67 for (int index = 1; index < count; ++index) {
68 if (!Op(*result, fPathRefs[index], fOps[index], result)) {
69 reset();
70 return false;
71 }
72 }
73 reset();
74 return true;
75 }
76 SkPath sum;
77 for (int index = 0; index < count; ++index) {
78 if (!Simplify(fPathRefs[index], &fPathRefs[index])) {
79 reset();
80 return false;
81 }
82 sum.addPath(fPathRefs[index]);
83 }
84 reset();
85 return Simplify(sum, result);
86}