blob: 2f19b332bccb98ed095ca08a348604da0fe1f339 [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) {
caryclark1049f122015-04-20 08:31:59 -070030 SkPath original = *result;
caryclark45fa4472015-01-16 07:04:10 -080031 int count = fOps.count();
32 bool allUnion = true;
33 SkPath::Direction firstDir;
34 for (int index = 0; index < count; ++index) {
35 SkPath* test = &fPathRefs[index];
caryclark54359292015-03-26 07:52:43 -070036 if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) {
caryclark45fa4472015-01-16 07:04:10 -080037 allUnion = false;
38 break;
39 }
40 // If all paths are convex, track direction, reversing as needed.
41 if (test->isConvex()) {
42 SkPath::Direction dir;
43 if (!test->cheapComputeDirection(&dir)) {
44 allUnion = false;
45 break;
46 }
47 if (index == 0) {
48 firstDir = dir;
49 } else if (firstDir != dir) {
50 SkPath temp;
51 temp.reverseAddPath(*test);
52 *test = temp;
53 }
54 continue;
55 }
56 // If the path is not convex but its bounds do not intersect the others, simplify is enough.
57 const SkRect& testBounds = test->getBounds();
58 for (int inner = 0; inner < index; ++inner) {
59 // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds?
60 if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) {
61 allUnion = false;
62 break;
63 }
64 }
65 }
66 if (!allUnion) {
67 *result = fPathRefs[0];
68 for (int index = 1; index < count; ++index) {
69 if (!Op(*result, fPathRefs[index], fOps[index], result)) {
70 reset();
caryclark1049f122015-04-20 08:31:59 -070071 *result = original;
caryclark45fa4472015-01-16 07:04:10 -080072 return false;
73 }
74 }
75 reset();
76 return true;
77 }
78 SkPath sum;
79 for (int index = 0; index < count; ++index) {
80 if (!Simplify(fPathRefs[index], &fPathRefs[index])) {
81 reset();
caryclark1049f122015-04-20 08:31:59 -070082 *result = original;
caryclark45fa4472015-01-16 07:04:10 -080083 return false;
84 }
85 sum.addPath(fPathRefs[index]);
86 }
87 reset();
caryclark1049f122015-04-20 08:31:59 -070088 bool success = Simplify(sum, result);
89 if (!success) {
90 *result = original;
91 }
92 return success;
caryclark45fa4472015-01-16 07:04:10 -080093}