blob: adce7413083757aaa884d7c60adc60f1cf78a3dd [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.combdee9fc2011-02-22 20:17:43 +00008#include "Test.h"
bsalomon@google.coma4e13c82012-11-26 21:38:37 +00009#if SK_SUPPORT_GPU
10 #include "GrClipMaskManager.h"
11#endif
reed@google.combdee9fc2011-02-22 20:17:43 +000012#include "SkClipStack.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000013#include "SkPath.h"
bsalomon@google.com51a62862012-11-26 21:19:43 +000014#include "SkRandom.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000015#include "SkRect.h"
bsalomon@google.com51a62862012-11-26 21:19:43 +000016#include "SkRegion.h"
robertphillips@google.com80214e22012-07-20 15:33:18 +000017
18
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000019static void test_assign_and_comparison(skiatest::Reporter* reporter) {
20 SkClipStack s;
reed@google.comd9f2dea2011-10-12 14:43:27 +000021 bool doAA = false;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000022
robertphillips@google.com80214e22012-07-20 15:33:18 +000023 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
24
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000025 // Build up a clip stack with a path, an empty clip, and a rect.
26 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000027 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
28
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000029 SkPath p;
30 p.moveTo(5, 6);
31 p.lineTo(7, 8);
32 p.lineTo(5, 9);
33 p.close();
reed@google.comd9f2dea2011-10-12 14:43:27 +000034 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000035
36 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000037 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
38
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000039 SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
reed@google.comd9f2dea2011-10-12 14:43:27 +000040 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000041 r = SkRect::MakeLTRB(10, 11, 12, 13);
reed@google.comd9f2dea2011-10-12 14:43:27 +000042 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000043
44 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000045 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
46
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000047 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000048 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000049
50 // Test that assignment works.
51 SkClipStack copy = s;
52 REPORTER_ASSERT(reporter, s == copy);
53
54 // Test that different save levels triggers not equal.
55 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000056 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000057 REPORTER_ASSERT(reporter, s != copy);
58
59 // Test that an equal, but not copied version is equal.
60 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000061 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
62
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000063 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000064 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000065 REPORTER_ASSERT(reporter, s == copy);
66
67 // Test that a different op on one level triggers not equal.
68 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000069 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000070 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000071 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
72
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000073 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000074 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000075 REPORTER_ASSERT(reporter, s != copy);
76
77 // Test that different state (clip type) triggers not equal.
tomhudson@google.com4c433722012-03-09 16:48:20 +000078 // NO LONGER VALID: if a path contains only a rect, we turn
79 // it into a bare rect for performance reasons (working
80 // around Chromium/JavaScript bad pattern).
81/*
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000082 s.restore();
83 s.save();
84 SkPath rp;
85 rp.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +000086 s.clipDevPath(rp, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000087 REPORTER_ASSERT(reporter, s != copy);
tomhudson@google.com4c433722012-03-09 16:48:20 +000088*/
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000089
90 // Test that different rects triggers not equal.
91 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000092 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000093 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000094 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
95
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000096 r = SkRect::MakeLTRB(24, 25, 26, 27);
reed@google.comd9f2dea2011-10-12 14:43:27 +000097 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000098 REPORTER_ASSERT(reporter, s != copy);
99
100 // Sanity check
101 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000102 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
103
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000104 copy.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000105 REPORTER_ASSERT(reporter, 2 == copy.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000106 REPORTER_ASSERT(reporter, s == copy);
107 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000108 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000109 copy.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000110 REPORTER_ASSERT(reporter, 1 == copy.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000111 REPORTER_ASSERT(reporter, s == copy);
112
113 // Test that different paths triggers not equal.
114 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000115 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000116 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000117 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
118
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000119 p.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +0000120 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000121 REPORTER_ASSERT(reporter, s != copy);
122}
reed@google.combdee9fc2011-02-22 20:17:43 +0000123
124static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
125 int count) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000126 SkClipStack::B2TIter iter(stack);
reed@google.combdee9fc2011-02-22 20:17:43 +0000127 int counter = 0;
128 while (iter.next()) {
129 counter += 1;
130 }
131 REPORTER_ASSERT(reporter, count == counter);
132}
133
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000134// Exercise the SkClipStack's bottom to top and bidirectional iterators
135// (including the skipToTopmost functionality)
robertphillips@google.com80214e22012-07-20 15:33:18 +0000136static void test_iterators(skiatest::Reporter* reporter) {
137 SkClipStack stack;
138
139 static const SkRect gRects[] = {
140 { 0, 0, 40, 40 },
141 { 60, 0, 100, 40 },
142 { 0, 60, 40, 100 },
143 { 60, 60, 100, 100 }
144 };
145
146 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
147 // the union op will prevent these from being fused together
148 stack.clipDevRect(gRects[i], SkRegion::kUnion_Op, false);
149 }
150
151 assert_count(reporter, stack, 4);
152
153 // bottom to top iteration
154 {
155 const SkClipStack::B2TIter::Clip* clip = NULL;
156
157 SkClipStack::B2TIter iter(stack);
158 int i;
159
160 for (i = 0, clip = iter.next(); clip; ++i, clip = iter.next()) {
161 REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]);
162 }
163
164 SkASSERT(i == 4);
165 }
166
167 // top to bottom iteration
168 {
169 const SkClipStack::Iter::Clip* clip = NULL;
170
171 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
172 int i;
173
174 for (i = 3, clip = iter.prev(); clip; --i, clip = iter.prev()) {
175 REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]);
176 }
177
178 SkASSERT(i == -1);
179 }
180
181 // skipToTopmost
182 {
183 const SkClipStack::Iter::Clip*clip = NULL;
184
185 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
186
187 clip = iter.skipToTopmost(SkRegion::kUnion_Op);
188 REPORTER_ASSERT(reporter, *clip->fRect == gRects[3]);
189 }
190}
191
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000192// Exercise the SkClipStack's getConservativeBounds computation
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000193static void test_bounds(skiatest::Reporter* reporter, bool useRects) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000194
195 static const int gNumCases = 20;
196 static const SkRect gAnswerRectsBW[gNumCases] = {
197 // A op B
198 { 40, 40, 50, 50 },
199 { 10, 10, 50, 50 },
200 { 10, 10, 80, 80 },
201 { 10, 10, 80, 80 },
202 { 40, 40, 80, 80 },
203
204 // invA op B
205 { 40, 40, 80, 80 },
206 { 0, 0, 100, 100 },
207 { 0, 0, 100, 100 },
208 { 0, 0, 100, 100 },
209 { 40, 40, 50, 50 },
210
211 // A op invB
212 { 10, 10, 50, 50 },
213 { 40, 40, 50, 50 },
214 { 0, 0, 100, 100 },
215 { 0, 0, 100, 100 },
216 { 0, 0, 100, 100 },
217
218 // invA op invB
219 { 0, 0, 100, 100 },
220 { 40, 40, 80, 80 },
221 { 0, 0, 100, 100 },
222 { 10, 10, 80, 80 },
223 { 10, 10, 50, 50 },
224 };
225
226 static const SkRegion::Op gOps[] = {
227 SkRegion::kIntersect_Op,
228 SkRegion::kDifference_Op,
229 SkRegion::kUnion_Op,
230 SkRegion::kXOR_Op,
231 SkRegion::kReverseDifference_Op
232 };
233
234 SkRect rectA, rectB;
235
236 rectA.iset(10, 10, 50, 50);
237 rectB.iset(40, 40, 80, 80);
238
239 SkPath clipA, clipB;
240
241 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
242 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
243
244 SkClipStack stack;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000245 SkRect devClipBound;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000246 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000247
248 int testCase = 0;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000249 int numBitTests = useRects ? 1 : 4;
250 for (int invBits = 0; invBits < numBitTests; ++invBits) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000251 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
252
253 stack.save();
254 bool doInvA = SkToBool(invBits & 1);
255 bool doInvB = SkToBool(invBits & 2);
256
257 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
258 SkPath::kEvenOdd_FillType);
259 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
260 SkPath::kEvenOdd_FillType);
261
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000262 if (useRects) {
263 stack.clipDevRect(rectA, SkRegion::kIntersect_Op, false);
264 stack.clipDevRect(rectB, gOps[op], false);
265 } else {
266 stack.clipDevPath(clipA, SkRegion::kIntersect_Op, false);
267 stack.clipDevPath(clipB, gOps[op], false);
268 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000269
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000270 REPORTER_ASSERT(reporter, !stack.isWideOpen());
271
robertphillips@google.com7b112892012-07-31 15:18:21 +0000272 stack.getConservativeBounds(0, 0, 100, 100, &devClipBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000273 &isIntersectionOfRects);
274
275 if (useRects) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000276 REPORTER_ASSERT(reporter, isIntersectionOfRects ==
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000277 (gOps[op] == SkRegion::kIntersect_Op));
278 } else {
279 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
280 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000281
282 SkASSERT(testCase < gNumCases);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000283 REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000284 ++testCase;
285
286 stack.restore();
287 }
288 }
289}
290
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000291// Test out 'isWideOpen' entry point
292static void test_isWideOpen(skiatest::Reporter* reporter) {
293
294 SkRect rectA, rectB;
295
296 rectA.iset(10, 10, 40, 40);
297 rectB.iset(50, 50, 80, 80);
298
299 // Stack should initially be wide open
300 {
301 SkClipStack stack;
302
303 REPORTER_ASSERT(reporter, stack.isWideOpen());
304 }
305
306 // Test out case where the user specifies a union that includes everything
307 {
308 SkClipStack stack;
309
310 SkPath clipA, clipB;
311
312 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
313 clipA.setFillType(SkPath::kInverseEvenOdd_FillType);
314
315 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
316 clipB.setFillType(SkPath::kInverseEvenOdd_FillType);
317
318 stack.clipDevPath(clipA, SkRegion::kReplace_Op, false);
319 stack.clipDevPath(clipB, SkRegion::kUnion_Op, false);
320
321 REPORTER_ASSERT(reporter, stack.isWideOpen());
322 }
323
324 // Test out union w/ a wide open clip
325 {
326 SkClipStack stack;
327
328 stack.clipDevRect(rectA, SkRegion::kUnion_Op, false);
329
330 REPORTER_ASSERT(reporter, stack.isWideOpen());
331 }
332
333 // Test out empty difference from a wide open clip
334 {
335 SkClipStack stack;
336
337 SkRect emptyRect;
338 emptyRect.setEmpty();
339
340 stack.clipDevRect(emptyRect, SkRegion::kDifference_Op, false);
341
342 REPORTER_ASSERT(reporter, stack.isWideOpen());
343 }
344
345 // Test out return to wide open
346 {
347 SkClipStack stack;
348
349 stack.save();
350
351 stack.clipDevRect(rectA, SkRegion::kReplace_Op, false);
352
353 REPORTER_ASSERT(reporter, !stack.isWideOpen());
354
355 stack.restore();
356
357 REPORTER_ASSERT(reporter, stack.isWideOpen());
358 }
359}
360
bsalomon@google.com100abf42012-09-05 17:40:04 +0000361static int count(const SkClipStack& stack) {
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000362
363 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
364
365 const SkClipStack::Iter::Clip* clip = NULL;
366 int count = 0;
367
368 for (clip = iter.prev(); clip; clip = iter.prev(), ++count) {
369 ;
370 }
371
372 return count;
373}
374
375// Test out SkClipStack's merging of rect clips. In particular exercise
376// merging of aa vs. bw rects.
377static void test_rect_merging(skiatest::Reporter* reporter) {
378
379 SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
380 SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
381
382 SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
383 SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
384
385 SkRect bound;
386 SkClipStack::BoundsType type;
387 bool isIntersectionOfRects;
388
389 // all bw overlapping - should merge
390 {
391 SkClipStack stack;
392
393 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, false);
394
395 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
396
397 REPORTER_ASSERT(reporter, 1 == count(stack));
398
399 stack.getBounds(&bound, &type, &isIntersectionOfRects);
400
401 REPORTER_ASSERT(reporter, isIntersectionOfRects);
402 }
403
404 // all aa overlapping - should merge
405 {
406 SkClipStack stack;
407
408 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
409
410 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true);
411
412 REPORTER_ASSERT(reporter, 1 == count(stack));
413
414 stack.getBounds(&bound, &type, &isIntersectionOfRects);
415
416 REPORTER_ASSERT(reporter, isIntersectionOfRects);
417 }
418
419 // mixed overlapping - should _not_ merge
420 {
421 SkClipStack stack;
422
423 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
424
425 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
426
427 REPORTER_ASSERT(reporter, 2 == count(stack));
428
429 stack.getBounds(&bound, &type, &isIntersectionOfRects);
430
431 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
432 }
433
434 // mixed nested (bw inside aa) - should merge
435 {
436 SkClipStack stack;
437
438 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, true);
439
440 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false);
441
442 REPORTER_ASSERT(reporter, 1 == count(stack));
443
444 stack.getBounds(&bound, &type, &isIntersectionOfRects);
445
446 REPORTER_ASSERT(reporter, isIntersectionOfRects);
447 }
448
449 // mixed nested (aa inside bw) - should merge
450 {
451 SkClipStack stack;
452
453 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, false);
454
455 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true);
456
457 REPORTER_ASSERT(reporter, 1 == count(stack));
458
459 stack.getBounds(&bound, &type, &isIntersectionOfRects);
460
461 REPORTER_ASSERT(reporter, isIntersectionOfRects);
462 }
463
464 // reverse nested (aa inside bw) - should _not_ merge
465 {
466 SkClipStack stack;
467
468 stack.clipDevRect(nestedChild, SkRegion::kReplace_Op, false);
469
470 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true);
471
472 REPORTER_ASSERT(reporter, 2 == count(stack));
473
474 stack.getBounds(&bound, &type, &isIntersectionOfRects);
475
476 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
477 }
478}
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000479
bsalomon@google.com51a62862012-11-26 21:19:43 +0000480///////////////////////////////////////////////////////////////////////////////////////////////////
481
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000482#if SK_SUPPORT_GPU
bsalomon@google.com705e8402012-11-27 15:43:57 +0000483// Functions that add a shape to the clip stack. The shape is computed from a rectangle.
484// AA is always disabled since the clip stack reducer can cause changes in aa rasterization of the
485// stack. A fractional edge repeated in different elements may be rasterized fewer times using the
486// reduced stack.
487typedef void (*AddElementFunc) (const SkRect& rect,
488 bool invert,
489 SkRegion::Op op,
490 SkClipStack* stack);
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000491
bsalomon@google.com705e8402012-11-27 15:43:57 +0000492static void add_round_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000493 SkPath path;
494 SkScalar rx = rect.width() / 10;
bsalomon@google.com705e8402012-11-27 15:43:57 +0000495 SkScalar ry = rect.height() / 20;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000496 path.addRoundRect(rect, rx, ry);
bsalomon@google.com705e8402012-11-27 15:43:57 +0000497 if (invert) {
498 path.setFillType(SkPath::kInverseWinding_FillType);
499 }
500 stack->clipDevPath(path, op, false);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000501};
502
bsalomon@google.com705e8402012-11-27 15:43:57 +0000503static void add_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
504 if (invert) {
505 SkPath path;
506 path.addRect(rect);
507 path.setFillType(SkPath::kInverseWinding_FillType);
508 stack->clipDevPath(path, op, false);
509 } else {
510 stack->clipDevRect(rect, op, false);
511 }
bsalomon@google.com51a62862012-11-26 21:19:43 +0000512};
513
bsalomon@google.com705e8402012-11-27 15:43:57 +0000514static void add_oval(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000515 SkPath path;
516 path.addOval(rect);
bsalomon@google.com705e8402012-11-27 15:43:57 +0000517 if (invert) {
518 path.setFillType(SkPath::kInverseWinding_FillType);
519 }
520 stack->clipDevPath(path, op, false);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000521};
522
523static void add_elem_to_stack(const SkClipStack::Iter::Clip& clip, SkClipStack* stack) {
524 if (NULL != clip.fPath) {
525 stack->clipDevPath(*clip.fPath, clip.fOp, clip.fDoAA);
526 } else if (NULL != clip.fRect) {
527 stack->clipDevRect(*clip.fRect, clip.fOp, clip.fDoAA);
528 }
529}
530
531static void add_elem_to_region(const SkClipStack::Iter::Clip& clip,
532 const SkIRect& bounds,
533 SkRegion* region) {
534 SkRegion elemRegion;
535 SkRegion boundsRgn(bounds);
536
537 if (NULL != clip.fPath) {
538 elemRegion.setPath(*clip.fPath, boundsRgn);
539 } else if (NULL != clip.fRect) {
540 SkPath path;
541 path.addRect(*clip.fRect);
542 elemRegion.setPath(path, boundsRgn);
543 } else {
544 // TODO: Figure out why we sometimes get here in the reduced clip stack.
545 region->setEmpty();
546 return;
547 }
548 region->op(elemRegion, clip.fOp);
549}
550
551// This can assist with debugging the clip stack reduction code when the test below fails.
552static void print_clip(const SkClipStack::Iter::Clip& clip) {
553 static const char* kOpStrs[] = {
554 "DF",
555 "IS",
556 "UN",
557 "XR",
558 "RD",
559 "RP",
560 };
bsalomon@google.com705e8402012-11-27 15:43:57 +0000561 if (NULL != clip.fRect || NULL != clip.fPath) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000562 const SkRect& bounds = clip.getBounds();
bsalomon@google.com705e8402012-11-27 15:43:57 +0000563 SkDebugf("%s %s %s [%f %f] x [%f %f]\n",
bsalomon@google.com51a62862012-11-26 21:19:43 +0000564 kOpStrs[clip.fOp],
bsalomon@google.com705e8402012-11-27 15:43:57 +0000565 (NULL != clip.fRect ? "R" : "P"),
566 ((NULL != clip.fPath && clip.fPath->isInverseFillType() ? "I" : " ")),
bsalomon@google.com51a62862012-11-26 21:19:43 +0000567 bounds.fLeft, bounds.fRight, bounds.fTop, bounds.fBottom);
568 } else {
569 SkDebugf("EM\n");
570 }
571}
572
573static void test_reduced_clip_stack(skiatest::Reporter* reporter) {
574 // We construct random clip stacks, reduce them, and then rasterize both versions to verify that
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000575 // they are equal.
bsalomon@google.com51a62862012-11-26 21:19:43 +0000576
577 // All the clip elements will be contained within these bounds.
578 static const SkRect kBounds = SkRect::MakeWH(100, 100);
579
580 enum {
581 kNumTests = 200,
582 kMinElemsPerTest = 1,
583 kMaxElemsPerTest = 50,
584 };
585
586 // min/max size of a clip element as a fraction of kBounds.
587 static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5;
588 static const SkScalar kMaxElemSizeFrac = SK_Scalar1;
589
590 static const SkRegion::Op kOps[] = {
591 SkRegion::kDifference_Op,
592 SkRegion::kIntersect_Op,
593 SkRegion::kUnion_Op,
594 SkRegion::kXOR_Op,
595 SkRegion::kReverseDifference_Op,
596 SkRegion::kReplace_Op,
597 };
598
599 // Replace operations short-circuit the optimizer. We want to make sure that we test this code
600 // path a little bit but we don't want it to prevent us from testing many longer traversals in
601 // the optimizer.
602 static const int kReplaceDiv = 4 * kMaxElemsPerTest;
603
bsalomon@google.com705e8402012-11-27 15:43:57 +0000604 // We want to test inverse fills. However, they are quite rare in practice so don't over do it.
605 static const SkScalar kFractionInverted = SK_Scalar1 / kMaxElemsPerTest;
606
bsalomon@google.com51a62862012-11-26 21:19:43 +0000607 static const AddElementFunc kElementFuncs[] = {
608 add_rect,
609 add_round_rect,
610 add_oval,
611 };
612
613 SkRandom r;
614
615 for (int i = 0; i < kNumTests; ++i) {
616 // Randomly generate a clip stack.
617 SkClipStack stack;
618 int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest);
619 for (int e = 0; e < numElems; ++e) {
620 SkRegion::Op op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))];
621 if (op == SkRegion::kReplace_Op) {
622 if (r.nextU() % kReplaceDiv) {
623 --e;
624 continue;
625 }
626 }
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000627
bsalomon@google.com51a62862012-11-26 21:19:43 +0000628 // saves can change the clip stack behavior when an element is added.
629 bool doSave = r.nextBool();
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000630
bsalomon@google.com51a62862012-11-26 21:19:43 +0000631 SkSize size = SkSize::Make(
632 SkScalarFloorToScalar(SkScalarMul(kBounds.width(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))),
633 SkScalarFloorToScalar(SkScalarMul(kBounds.height(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))));
634
635 SkPoint xy = {SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth)),
636 SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight))};
637
638 SkRect rect = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
639
bsalomon@google.com705e8402012-11-27 15:43:57 +0000640 bool invert = r.nextBiasedBool(kFractionInverted);
641 kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, invert, op, &stack);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000642 if (doSave) {
643 stack.save();
644 }
645 }
646
647 // Get the reduced version of the stack.
648 SkTDArray<SkClipStack::Iter::Clip> reducedClips;
649 SkRect resultBounds;
650 bool bounded;
651 GrReducedClip::InitialState initial;
652 GrReducedClip::GrReduceClipStack(stack, &reducedClips, &resultBounds, &bounded, &initial);
653
654 // Build a new clip stack based on the reduced clip elements
655 SkClipStack reducedStack;
656 if (GrReducedClip::kAllOut_InitialState == initial) {
657 // whether the result is bounded or not, the whole plane should start outside the clip.
658 reducedStack.clipEmpty();
659 }
660 for (int c = 0; c < reducedClips.count(); ++c) {
661 add_elem_to_stack(reducedClips[c], &reducedStack);
662 }
663 if (bounded) {
664 // GrReduceClipStack() assumes that there is an implicit clip to the bounds
665 reducedStack.clipDevRect(resultBounds, SkRegion::kIntersect_Op, true);
666 }
667
668 // convert both the original stack and reduced stack to SkRegions and see if they're equal
669 SkRect inflatedBounds = kBounds;
670 inflatedBounds.outset(kBounds.width() / 2, kBounds.height() / 2);
671 SkIRect inflatedIBounds;
672 inflatedBounds.roundOut(&inflatedIBounds);
673
674 SkRegion region;
675 SkRegion reducedRegion;
676
677 region.setRect(inflatedIBounds);
678 const SkClipStack::Iter::Clip* clip;
679 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
680 while ((clip = iter.next())) {
681 add_elem_to_region(*clip, inflatedIBounds, &region);
682 }
683
684 reducedRegion.setRect(inflatedIBounds);
685 iter.reset(reducedStack, SkClipStack::Iter::kBottom_IterStart);
686 while ((clip = iter.next())) {
687 add_elem_to_region(*clip, inflatedIBounds, &reducedRegion);
688 }
689
690 REPORTER_ASSERT(reporter, region == reducedRegion);
691 }
692}
693
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000694#endif
bsalomon@google.com51a62862012-11-26 21:19:43 +0000695///////////////////////////////////////////////////////////////////////////////////////////////////
696
reed@google.combdee9fc2011-02-22 20:17:43 +0000697static void TestClipStack(skiatest::Reporter* reporter) {
698 SkClipStack stack;
699
robertphillips@google.com80214e22012-07-20 15:33:18 +0000700 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000701 assert_count(reporter, stack, 0);
702
703 static const SkIRect gRects[] = {
704 { 0, 0, 100, 100 },
705 { 25, 25, 125, 125 },
706 { 0, 0, 1000, 1000 },
707 { 0, 0, 75, 75 }
708 };
709 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
reed@google.comd9f2dea2011-10-12 14:43:27 +0000710 stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
reed@google.combdee9fc2011-02-22 20:17:43 +0000711 }
712
713 // all of the above rects should have been intersected, leaving only 1 rect
robertphillips@google.com80214e22012-07-20 15:33:18 +0000714 SkClipStack::B2TIter iter(stack);
715 const SkClipStack::B2TIter::Clip* clip = iter.next();
epoger@google.com2047f002011-05-17 17:36:59 +0000716 SkRect answer;
717 answer.iset(25, 25, 75, 75);
reed@google.combdee9fc2011-02-22 20:17:43 +0000718
719 REPORTER_ASSERT(reporter, clip);
720 REPORTER_ASSERT(reporter, clip->fRect);
721 REPORTER_ASSERT(reporter, !clip->fPath);
722 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
723 REPORTER_ASSERT(reporter, *clip->fRect == answer);
724 // now check that we only had one in our iterator
725 REPORTER_ASSERT(reporter, !iter.next());
726
727 stack.reset();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000728 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000729 assert_count(reporter, stack, 0);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000730
731 test_assign_and_comparison(reporter);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000732 test_iterators(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000733 test_bounds(reporter, true); // once with rects
734 test_bounds(reporter, false); // once with paths
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000735 test_isWideOpen(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000736 test_rect_merging(reporter);
bsalomon@google.come7b3d292012-11-26 21:42:32 +0000737#if SK_SUPPORT_GPU
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000738 test_reduced_clip_stack(reporter);
bsalomon@google.come7b3d292012-11-26 21:42:32 +0000739#endif
reed@google.combdee9fc2011-02-22 20:17:43 +0000740}
741
742#include "TestClassDef.h"
743DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)