1 /*
2 * Copyright 2013 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 #include "include/core/SkBitmap.h"
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkRect.h"
13 #include "include/pathops/SkPathOps.h"
14 #include "include/private/base/SkTDArray.h"
15 #include "src/base/SkRandom.h"
16 #include "src/pathops/SkPathOpsCommon.h"
17 #include "tests/PathOpsExtendedTest.h"
18 #include "tests/PathOpsThreadedCommon.h"
19 #include "tests/Test.h"
20
21 #include <algorithm>
22 #include <cstdint>
23
testTightBoundsLines(PathOpsThreadState * data)24 static void testTightBoundsLines(PathOpsThreadState* data) {
25 SkRandom ran;
26 for (int index = 0; index < 1000; ++index) {
27 SkPath path;
28 int contourCount = ran.nextRangeU(1, 10);
29 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
30 int lineCount = ran.nextRangeU(1, 10);
31 path.moveTo(ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000));
32 for (int lIndex = 0; lIndex < lineCount; ++lIndex) {
33 path.lineTo(ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000));
34 }
35 if (ran.nextBool()) {
36 path.close();
37 }
38 }
39 SkRect classicBounds = path.getBounds();
40 SkRect tightBounds;
41 REPORTER_ASSERT(data->fReporter, ComputeTightBounds(path, &tightBounds));
42 REPORTER_ASSERT(data->fReporter, classicBounds == tightBounds);
43 }
44 }
45
DEF_TEST(PathOpsTightBoundsLines,reporter)46 DEF_TEST(PathOpsTightBoundsLines, reporter) {
47 initializeTests(reporter, "tightBoundsLines");
48 PathOpsThreadedTestRunner testRunner(reporter);
49 int outerCount = reporter->allowExtendedTest() ? 100 : 1;
50 for (int index = 0; index < outerCount; ++index) {
51 for (int idx2 = 0; idx2 < 10; ++idx2) {
52 *testRunner.fRunnables.append() =
53 new PathOpsThreadedRunnable(&testTightBoundsLines, 0, 0, 0, 0, &testRunner);
54 }
55 }
56 testRunner.render();
57 }
58
testTightBoundsQuads(PathOpsThreadState * data)59 static void testTightBoundsQuads(PathOpsThreadState* data) {
60 SkRandom ran;
61 const int bitWidth = 32;
62 const int bitHeight = 32;
63 const float pathMin = 1;
64 const float pathMax = (float) (bitHeight - 2);
65 SkBitmap& bits = *data->fBitmap;
66 if (bits.width() == 0) {
67 bits.allocN32Pixels(bitWidth, bitHeight);
68 }
69 SkCanvas canvas(bits);
70 SkPaint paint;
71 for (int index = 0; index < 100; ++index) {
72 SkPath path;
73 int contourCount = ran.nextRangeU(1, 10);
74 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
75 int lineCount = ran.nextRangeU(1, 10);
76 path.moveTo(ran.nextRangeF(1, pathMax), ran.nextRangeF(pathMin, pathMax));
77 for (int lIndex = 0; lIndex < lineCount; ++lIndex) {
78 if (ran.nextBool()) {
79 path.lineTo(ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax));
80 } else {
81 path.quadTo(ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax),
82 ran.nextRangeF(pathMin, pathMax), ran.nextRangeF(pathMin, pathMax));
83 }
84 }
85 if (ran.nextBool()) {
86 path.close();
87 }
88 }
89 SkRect classicBounds = path.getBounds();
90 SkRect tightBounds;
91 REPORTER_ASSERT(data->fReporter, ComputeTightBounds(path, &tightBounds));
92 REPORTER_ASSERT(data->fReporter, classicBounds.contains(tightBounds));
93 canvas.drawColor(SK_ColorWHITE);
94 canvas.drawPath(path, paint);
95 SkIRect bitsWritten = {31, 31, 0, 0};
96 for (int y = 0; y < bitHeight; ++y) {
97 uint32_t* addr1 = data->fBitmap->getAddr32(0, y);
98 bool lineWritten = false;
99 for (int x = 0; x < bitWidth; ++x) {
100 if (addr1[x] == (uint32_t) -1) {
101 continue;
102 }
103 lineWritten = true;
104 bitsWritten.fLeft = std::min(bitsWritten.fLeft, x);
105 bitsWritten.fRight = std::max(bitsWritten.fRight, x);
106 }
107 if (!lineWritten) {
108 continue;
109 }
110 bitsWritten.fTop = std::min(bitsWritten.fTop, y);
111 bitsWritten.fBottom = std::max(bitsWritten.fBottom, y);
112 }
113 if (!bitsWritten.isEmpty()) {
114 SkIRect tightOut;
115 tightBounds.roundOut(&tightOut);
116 REPORTER_ASSERT(data->fReporter, tightOut.contains(bitsWritten));
117 }
118 }
119 }
120
DEF_TEST(PathOpsTightBoundsQuads,reporter)121 DEF_TEST(PathOpsTightBoundsQuads, reporter) {
122 initializeTests(reporter, "tightBoundsQuads");
123 PathOpsThreadedTestRunner testRunner(reporter);
124 int outerCount = reporter->allowExtendedTest() ? 100 : 1;
125 for (int index = 0; index < outerCount; ++index) {
126 for (int idx2 = 0; idx2 < 10; ++idx2) {
127 *testRunner.fRunnables.append() =
128 new PathOpsThreadedRunnable(&testTightBoundsQuads, 0, 0, 0, 0, &testRunner);
129 }
130 }
131 testRunner.render();
132 }
133
DEF_TEST(PathOpsTightBoundsMove,reporter)134 DEF_TEST(PathOpsTightBoundsMove, reporter) {
135 SkPath path;
136 path.moveTo(10, 10);
137 path.close();
138 path.moveTo(20, 20);
139 path.lineTo(20, 20);
140 path.close();
141 path.moveTo(15, 15);
142 path.lineTo(15, 15);
143 path.close();
144 const SkRect& bounds = path.getBounds();
145 SkRect tight;
146 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
147 REPORTER_ASSERT(reporter, bounds == tight);
148 }
149
DEF_TEST(PathOpsTightBoundsMoveOne,reporter)150 DEF_TEST(PathOpsTightBoundsMoveOne, reporter) {
151 SkPath path;
152 path.moveTo(20, 20);
153 const SkRect& bounds = path.getBounds();
154 SkRect tight;
155 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
156 REPORTER_ASSERT(reporter, bounds == tight);
157 }
158
DEF_TEST(PathOpsTightBoundsMoveTwo,reporter)159 DEF_TEST(PathOpsTightBoundsMoveTwo, reporter) {
160 SkPath path;
161 path.moveTo(20, 20);
162 path.moveTo(40, 40);
163 const SkRect& bounds = path.getBounds();
164 SkRect tight;
165 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
166 REPORTER_ASSERT(reporter, bounds == tight);
167 }
168
DEF_TEST(PathOpsTightBoundsTiny,reporter)169 DEF_TEST(PathOpsTightBoundsTiny, reporter) {
170 SkPath path;
171 path.moveTo(1, 1);
172 path.quadTo(1.000001f, 1, 1, 1);
173 const SkRect& bounds = path.getBounds();
174 SkRect tight;
175 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
176 SkRect moveBounds = {1, 1, 1, 1};
177 REPORTER_ASSERT(reporter, bounds != tight);
178 REPORTER_ASSERT(reporter, moveBounds == tight);
179 }
180
DEF_TEST(PathOpsTightBoundsWellBehaved,reporter)181 DEF_TEST(PathOpsTightBoundsWellBehaved, reporter) {
182 SkPath path;
183 path.moveTo(1, 1);
184 path.quadTo(2, 3, 4, 5);
185 const SkRect& bounds = path.getBounds();
186 SkRect tight;
187 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
188 REPORTER_ASSERT(reporter, bounds == tight);
189 }
190
DEF_TEST(PathOpsTightBoundsIllBehaved,reporter)191 DEF_TEST(PathOpsTightBoundsIllBehaved, reporter) {
192 SkPath path;
193 path.moveTo(1, 1);
194 path.quadTo(4, 3, 2, 2);
195 const SkRect& bounds = path.getBounds();
196 SkRect tight;
197 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
198 REPORTER_ASSERT(reporter, bounds != tight);
199 }
200
DEF_TEST(PathOpsTightBoundsIllBehavedScaled,reporter)201 DEF_TEST(PathOpsTightBoundsIllBehavedScaled, reporter) {
202 SkPath path;
203 path.moveTo(0, 0);
204 path.quadTo(1048578, 1048577, 1048576, 1048576);
205 const SkRect& bounds = path.getBounds();
206 SkRect tight;
207 REPORTER_ASSERT(reporter, ComputeTightBounds(path, &tight));
208 REPORTER_ASSERT(reporter, bounds != tight);
209 REPORTER_ASSERT(reporter, tight.right() == 1048576);
210 REPORTER_ASSERT(reporter, tight.bottom() == 1048576);
211 }
212