Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: tests/RRectInPath.cpp

Issue 1461763004: add SkPath::isRRect (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remove erroneous qualification Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/core/SkPath.cpp ('K') | « src/core/SkPathRef.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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 "SkPathRef.h"
11 #include "SkPathOps.h"
12 #include "SkRRect.h"
13 #include "Test.h"
14
15 static void path_contains_rrect(skiatest::Reporter* reporter, const SkPath& path ) {
16 SkRRect out;
17 REPORTER_ASSERT(reporter, path.isRRect(&out));
18 SkPath path2, xorBoth;
19 path2.addRRect(out);
20 Op(path, path2, SkPathOp::kXOR_SkPathOp, &xorBoth);
21 REPORTER_ASSERT(reporter, xorBoth.isEmpty());
22 }
23
24 static void path_contains_rrect(skiatest::Reporter* reporter, const SkRRect& in) {
25 SkPath path;
26 path.addRRect(in);
27 path_contains_rrect(reporter, path);
28 }
29
30 static void path_contains_rrect(skiatest::Reporter* reporter, const SkRect& r,
31 SkVector v[4]) {
32 SkRRect rrect;
33 rrect.setRectRadii(r, v);
34 path_contains_rrect(reporter, rrect);
35 }
36
37 class ForceIsRRect_Private {
38 public:
39 ForceIsRRect_Private(SkPath* path) {
40 path->fPathRef->setIsRRect(true);
41 }
42 };
43
44 static void force_path_contains_rrect(skiatest::Reporter* reporter, SkPath& path ) {
45 ForceIsRRect_Private force_rrect(&path);
46 path_contains_rrect(reporter, path);
47 }
48
49 static void test_undetected_paths(skiatest::Reporter* reporter) {
50 SkPath path;
51 path.moveTo(0, 62.5f);
52 path.lineTo(0, 3.5f);
53 path.conicTo(0, 0, 3.5f, 0, 0.70710677f);
54 path.lineTo(196.5f, 0);
55 path.conicTo(200, 0, 200, 3.5f, 0.70710677f);
56 path.lineTo(200, 62.5f);
57 path.conicTo(200, 66, 196.5f, 66, 0.70710677f);
58 path.lineTo(3.5f, 66);
59 path.conicTo(0, 66, 0, 62.5, 0.70710677f);
60 path.close();
61 force_path_contains_rrect(reporter, path);
62
63 path.reset();
64 path.moveTo(0, 81.5f);
65 path.lineTo(0, 3.5f);
66 path.conicTo(0, 0, 3.5f, 0, 0.70710677f);
67 path.lineTo(149.5, 0);
68 path.conicTo(153, 0, 153, 3.5f, 0.70710677f);
69 path.lineTo(153, 81.5f);
70 path.conicTo(153, 85, 149.5f, 85, 0.70710677f);
71 path.lineTo(3.5f, 85);
72 path.conicTo(0, 85, 0, 81.5f, 0.70710677f);
73 path.close();
74 force_path_contains_rrect(reporter, path);
75
76 path.reset();
77 path.moveTo(14, 1189);
78 path.lineTo(14, 21);
79 path.conicTo(14, 14, 21, 14, 0.70710677f);
80 path.lineTo(1363, 14);
81 path.conicTo(1370, 14, 1370, 21, 0.70710677f);
82 path.lineTo(1370, 1189);
83 path.conicTo(1370, 1196, 1363, 1196, 0.70710677f);
84 path.lineTo(21, 1196);
85 path.conicTo(14, 1196, 14, 1189, 0.70710677f);
86 path.close();
87 force_path_contains_rrect(reporter, path);
88
89 path.reset();
90 path.moveTo(14, 1743);
91 path.lineTo(14, 21);
92 path.conicTo(14, 14, 21, 14, 0.70710677f);
93 path.lineTo(1363, 14);
94 path.conicTo(1370, 14, 1370, 21, 0.70710677f);
95 path.lineTo(1370, 1743);
96 path.conicTo(1370, 1750, 1363, 1750, 0.70710677f);
97 path.lineTo(21, 1750);
98 path.conicTo(14, 1750, 14, 1743, 0.70710677f);
99 path.close();
100 force_path_contains_rrect(reporter, path);
101 }
102
103 static const SkScalar kWidth = 100.0f;
104 static const SkScalar kHeight = 100.0f;
105
106 static void test_tricky_radii(skiatest::Reporter* reporter) {
107 {
108 // crbug.com/458522
109 SkRRect rr;
110 const SkRect bounds = { 3709, 3709, 3709 + 7402, 3709 + 29825 };
111 const SkScalar rad = 12814;
112 const SkVector vec[] = { { rad, rad }, { 0, rad }, { rad, rad }, { 0, ra d } };
113 rr.setRectRadii(bounds, vec);
114 path_contains_rrect(reporter, rr);
115 }
116
117 {
118 // crbug.com//463920
119 SkRect r = SkRect::MakeLTRB(0, 0, 1009, 33554432.0);
120 SkVector radii[4] = {
121 { 13.0f, 8.0f }, { 170.0f, 2.0 }, { 256.0f, 33554432.0 }, { 110.0f, 5.0f }
122 };
123 SkRRect rr;
124 rr.setRectRadii(r, radii);
125 path_contains_rrect(reporter, rr);
126 }
127 }
128
129 static void test_empty_crbug_458524(skiatest::Reporter* reporter) {
130 SkRRect rr;
131 const SkRect bounds = { 3709, 3709, 3709 + 7402, 3709 + 29825 };
132 const SkScalar rad = 40;
133 rr.setRectXY(bounds, rad, rad);
134 path_contains_rrect(reporter, rr);
135
136 SkRRect other;
137 SkMatrix matrix;
138 matrix.setScale(0, 1);
139 rr.transform(matrix, &other);
140 path_contains_rrect(reporter, rr);
141 }
142
143 static void test_inset(skiatest::Reporter* reporter) {
144 SkRRect rr, rr2;
145 SkRect r = { 0, 0, 100, 100 };
146
147 rr.setRect(r);
148 rr.inset(-20, -20, &rr2);
149 path_contains_rrect(reporter, rr);
150
151 rr.inset(20, 20, &rr2);
152 path_contains_rrect(reporter, rr);
153
154 rr.inset(r.width()/2, r.height()/2, &rr2);
155 path_contains_rrect(reporter, rr);
156
157 rr.setRectXY(r, 20, 20);
158 rr.inset(19, 19, &rr2);
159 path_contains_rrect(reporter, rr);
160 rr.inset(20, 20, &rr2);
161 path_contains_rrect(reporter, rr);
162 }
163
164
165 static void test_9patch_rrect(skiatest::Reporter* reporter,
166 const SkRect& rect,
167 SkScalar l, SkScalar t, SkScalar r, SkScalar b,
168 bool checkRadii) {
169 SkRRect rr;
170 rr.setNinePatch(rect, l, t, r, b);
171 path_contains_rrect(reporter, rr);
172
173 SkRRect rr2; // construct the same RR using the most general set function
174 SkVector radii[4] = { { l, t }, { r, t }, { r, b }, { l, b } };
175 rr2.setRectRadii(rect, radii);
176 path_contains_rrect(reporter, rr);
177 }
178
179 // Test out the basic API entry points
180 static void test_round_rect_basic(skiatest::Reporter* reporter) {
181
182 //----
183 SkRect rect = SkRect::MakeLTRB(0, 0, kWidth, kHeight);
184
185 SkRRect rr1;
186 rr1.setRect(rect);
187 path_contains_rrect(reporter, rr1);
188
189 SkRRect rr1_2; // construct the same RR using the most general set function
190 SkVector rr1_2_radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
191 rr1_2.setRectRadii(rect, rr1_2_radii);
192 path_contains_rrect(reporter, rr1_2);
193 SkRRect rr1_3; // construct the same RR using the nine patch set function
194 rr1_3.setNinePatch(rect, 0, 0, 0, 0);
195 path_contains_rrect(reporter, rr1_2);
196
197 //----
198 SkPoint halfPoint = { SkScalarHalf(kWidth), SkScalarHalf(kHeight) };
199 SkRRect rr2;
200 rr2.setOval(rect);
201 path_contains_rrect(reporter, rr2);
202
203 SkRRect rr2_2; // construct the same RR using the most general set function
204 SkVector rr2_2_radii[4] = { { halfPoint.fX, halfPoint.fY }, { halfPoint.fX, halfPoint.fY },
205 { halfPoint.fX, halfPoint.fY }, { halfPoint.fX, halfPoint.fY } };
206 rr2_2.setRectRadii(rect, rr2_2_radii);
207 path_contains_rrect(reporter, rr2_2);
208 SkRRect rr2_3; // construct the same RR using the nine patch set function
209 rr2_3.setNinePatch(rect, halfPoint.fX, halfPoint.fY, halfPoint.fX, halfPoint .fY);
210 path_contains_rrect(reporter, rr2_3);
211
212 //----
213 SkPoint p = { 5, 5 };
214 SkRRect rr3;
215 rr3.setRectXY(rect, p.fX, p.fY);
216 path_contains_rrect(reporter, rr3);
217
218 SkRRect rr3_2; // construct the same RR using the most general set function
219 SkVector rr3_2_radii[4] = { { 5, 5 }, { 5, 5 }, { 5, 5 }, { 5, 5 } };
220 rr3_2.setRectRadii(rect, rr3_2_radii);
221 path_contains_rrect(reporter, rr3_2);
222 SkRRect rr3_3; // construct the same RR using the nine patch set function
223 rr3_3.setNinePatch(rect, 5, 5, 5, 5);
224 path_contains_rrect(reporter, rr3_3);
225
226 //----
227 test_9patch_rrect(reporter, rect, 10, 9, 8, 7, true);
228
229 {
230 // Test out the rrect from skia:3466
231 SkRect rect2 = SkRect::MakeLTRB(0.358211994f, 0.755430222f, 0.872866154f , 0.806214333f);
232
233 test_9patch_rrect(reporter,
234 rect2,
235 0.926942348f, 0.642850280f, 0.529063463f, 0.587844372f ,
236 false);
237 }
238
239 //----
240 SkPoint radii2[4] = { { 0, 0 }, { 0, 0 }, { 50, 50 }, { 20, 50 } };
241
242 SkRRect rr5;
243 rr5.setRectRadii(rect, radii2);
244 path_contains_rrect(reporter, rr5);
245 }
246
247 // Test out the cases when the RR degenerates to a rect
248 static void test_round_rect_rects(skiatest::Reporter* reporter) {
249
250 //----
251 SkRect rect = SkRect::MakeLTRB(0, 0, kWidth, kHeight);
252 SkRRect rr1;
253 rr1.setRectXY(rect, 0, 0);
254
255 path_contains_rrect(reporter, rr1);
256
257 //----
258 SkPoint radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
259
260 SkRRect rr2;
261 rr2.setRectRadii(rect, radii);
262
263 path_contains_rrect(reporter, rr2);
264
265 //----
266 SkPoint radii2[4] = { { 0, 0 }, { 20, 20 }, { 50, 50 }, { 20, 50 } };
267
268 SkRRect rr3;
269 rr3.setRectRadii(rect, radii2);
270 path_contains_rrect(reporter, rr3);
271 }
272
273 // Test out the cases when the RR degenerates to an oval
274 static void test_round_rect_ovals(skiatest::Reporter* reporter) {
275 //----
276 SkRect rect = SkRect::MakeLTRB(0, 0, kWidth, kHeight);
277 SkRRect rr1;
278 rr1.setRectXY(rect, SkScalarHalf(kWidth), SkScalarHalf(kHeight));
279
280 path_contains_rrect(reporter, rr1);
281 }
282
283 // Test out the non-degenerate RR cases
284 static void test_round_rect_general(skiatest::Reporter* reporter) {
285 //----
286 SkRect rect = SkRect::MakeLTRB(0, 0, kWidth, kHeight);
287 SkRRect rr1;
288 rr1.setRectXY(rect, 20, 20);
289
290 path_contains_rrect(reporter, rr1);
291
292 //----
293 SkPoint radii[4] = { { 0, 0 }, { 20, 20 }, { 50, 50 }, { 20, 50 } };
294
295 SkRRect rr2;
296 rr2.setRectRadii(rect, radii);
297
298 path_contains_rrect(reporter, rr2);
299 }
300
301 static void test_round_rect_iffy_parameters(skiatest::Reporter* reporter) {
302 SkRect rect = SkRect::MakeLTRB(0, 0, kWidth, kHeight);
303 SkPoint radii[4] = { { 50, 100 }, { 100, 50 }, { 50, 100 }, { 100, 50 } };
304 SkRRect rr1;
305 rr1.setRectRadii(rect, radii);
306 path_contains_rrect(reporter, rr1);
307 }
308
309 static void set_radii(SkVector radii[4], int index, float rad) {
310 sk_bzero(radii, sizeof(SkVector) * 4);
311 radii[index].set(rad, rad);
312 }
313
314 static void test_add_rrect(skiatest::Reporter* reporter, const SkRect& bounds,
315 const SkVector radii[4]) {
316 SkRRect rrect;
317 rrect.setRectRadii(bounds, radii);
318 REPORTER_ASSERT(reporter, bounds == rrect.rect());
319
320 SkPath path;
321 // this line should not assert in the debug build (from validate)
322 path.addRRect(rrect);
323 REPORTER_ASSERT(reporter, bounds == path.getBounds());
324 }
325
326 static void test_skbug_3239(skiatest::Reporter* reporter) {
327 const float min = SkBits2Float(0xcb7f16c8); /* -16717512.000000 */
328 const float max = SkBits2Float(0x4b7f1c1d); /* 16718877.000000 */
329 const float big = SkBits2Float(0x4b7f1bd7); /* 16718807.000000 */
330
331 const float rad = 33436320;
332
333 const SkRect rectx = SkRect::MakeLTRB(min, min, max, big);
334 const SkRect recty = SkRect::MakeLTRB(min, min, big, max);
335
336 SkVector radii[4];
337 for (int i = 0; i < 4; ++i) {
338 set_radii(radii, i, rad);
339 path_contains_rrect(reporter, rectx, radii);
340 path_contains_rrect(reporter, recty, radii);
341 }
342 }
343
344 static void test_mix(skiatest::Reporter* reporter) {
345 // Test out mixed degenerate and non-degenerate geometry with Conics
346 const SkVector radii[4] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 100, 100 } };
347 SkRect r = SkRect::MakeWH(100, 100);
348 SkRRect rr;
349 rr.setRectRadii(r, radii);
350 path_contains_rrect(reporter, rr);
351 }
352
353 DEF_TEST(RoundRectInPath, reporter) {
354 test_tricky_radii(reporter);
355 test_empty_crbug_458524(reporter);
356 test_inset(reporter);
357 test_round_rect_basic(reporter);
358 test_round_rect_rects(reporter);
359 test_round_rect_ovals(reporter);
360 test_round_rect_general(reporter);
361 test_undetected_paths(reporter);
362 test_round_rect_iffy_parameters(reporter);
363 test_skbug_3239(reporter);
364 test_mix(reporter);
365 }
OLDNEW
« src/core/SkPath.cpp ('K') | « src/core/SkPathRef.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698