blob: cdb4de0a119b4c26d03619ff0b52848263bcc58d [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.com51a62862012-11-26 21:19:43 +00009#include "GrClipMaskManager.h"
reed@google.combdee9fc2011-02-22 20:17:43 +000010#include "SkClipStack.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000011#include "SkPath.h"
bsalomon@google.com51a62862012-11-26 21:19:43 +000012#include "SkRandom.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000013#include "SkRect.h"
bsalomon@google.com51a62862012-11-26 21:19:43 +000014#include "SkRegion.h"
robertphillips@google.com80214e22012-07-20 15:33:18 +000015
16
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000017static void test_assign_and_comparison(skiatest::Reporter* reporter) {
18 SkClipStack s;
reed@google.comd9f2dea2011-10-12 14:43:27 +000019 bool doAA = false;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000020
robertphillips@google.com80214e22012-07-20 15:33:18 +000021 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
22
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000023 // Build up a clip stack with a path, an empty clip, and a rect.
24 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000025 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
26
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000027 SkPath p;
28 p.moveTo(5, 6);
29 p.lineTo(7, 8);
30 p.lineTo(5, 9);
31 p.close();
reed@google.comd9f2dea2011-10-12 14:43:27 +000032 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000033
34 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000035 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
36
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000037 SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
reed@google.comd9f2dea2011-10-12 14:43:27 +000038 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000039 r = SkRect::MakeLTRB(10, 11, 12, 13);
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
42 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000043 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
44
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000045 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000046 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000047
48 // Test that assignment works.
49 SkClipStack copy = s;
50 REPORTER_ASSERT(reporter, s == copy);
51
52 // Test that different save levels triggers not equal.
53 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000054 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000055 REPORTER_ASSERT(reporter, s != copy);
56
57 // Test that an equal, but not copied version is equal.
58 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000059 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
60
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000061 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000062 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000063 REPORTER_ASSERT(reporter, s == copy);
64
65 // Test that a different op on one level triggers not equal.
66 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000067 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000068 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000069 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
70
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000071 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000072 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000073 REPORTER_ASSERT(reporter, s != copy);
74
75 // Test that different state (clip type) triggers not equal.
tomhudson@google.com4c433722012-03-09 16:48:20 +000076 // NO LONGER VALID: if a path contains only a rect, we turn
77 // it into a bare rect for performance reasons (working
78 // around Chromium/JavaScript bad pattern).
79/*
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000080 s.restore();
81 s.save();
82 SkPath rp;
83 rp.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +000084 s.clipDevPath(rp, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000085 REPORTER_ASSERT(reporter, s != copy);
tomhudson@google.com4c433722012-03-09 16:48:20 +000086*/
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000087
88 // Test that different rects triggers not equal.
89 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +000090 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000091 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +000092 REPORTER_ASSERT(reporter, 3 == s.getSaveCount());
93
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000094 r = SkRect::MakeLTRB(24, 25, 26, 27);
reed@google.comd9f2dea2011-10-12 14:43:27 +000095 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000096 REPORTER_ASSERT(reporter, s != copy);
97
98 // Sanity check
99 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000100 REPORTER_ASSERT(reporter, 2 == s.getSaveCount());
101
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000102 copy.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000103 REPORTER_ASSERT(reporter, 2 == copy.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000104 REPORTER_ASSERT(reporter, s == copy);
105 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000106 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000107 copy.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000108 REPORTER_ASSERT(reporter, 1 == copy.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000109 REPORTER_ASSERT(reporter, s == copy);
110
111 // Test that different paths triggers not equal.
112 s.restore();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000113 REPORTER_ASSERT(reporter, 0 == s.getSaveCount());
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000114 s.save();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000115 REPORTER_ASSERT(reporter, 1 == s.getSaveCount());
116
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000117 p.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +0000118 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000119 REPORTER_ASSERT(reporter, s != copy);
120}
reed@google.combdee9fc2011-02-22 20:17:43 +0000121
122static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
123 int count) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000124 SkClipStack::B2TIter iter(stack);
reed@google.combdee9fc2011-02-22 20:17:43 +0000125 int counter = 0;
126 while (iter.next()) {
127 counter += 1;
128 }
129 REPORTER_ASSERT(reporter, count == counter);
130}
131
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000132// Exercise the SkClipStack's bottom to top and bidirectional iterators
133// (including the skipToTopmost functionality)
robertphillips@google.com80214e22012-07-20 15:33:18 +0000134static void test_iterators(skiatest::Reporter* reporter) {
135 SkClipStack stack;
136
137 static const SkRect gRects[] = {
138 { 0, 0, 40, 40 },
139 { 60, 0, 100, 40 },
140 { 0, 60, 40, 100 },
141 { 60, 60, 100, 100 }
142 };
143
144 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
145 // the union op will prevent these from being fused together
146 stack.clipDevRect(gRects[i], SkRegion::kUnion_Op, false);
147 }
148
149 assert_count(reporter, stack, 4);
150
151 // bottom to top iteration
152 {
153 const SkClipStack::B2TIter::Clip* clip = NULL;
154
155 SkClipStack::B2TIter iter(stack);
156 int i;
157
158 for (i = 0, clip = iter.next(); clip; ++i, clip = iter.next()) {
159 REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]);
160 }
161
162 SkASSERT(i == 4);
163 }
164
165 // top to bottom iteration
166 {
167 const SkClipStack::Iter::Clip* clip = NULL;
168
169 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
170 int i;
171
172 for (i = 3, clip = iter.prev(); clip; --i, clip = iter.prev()) {
173 REPORTER_ASSERT(reporter, *clip->fRect == gRects[i]);
174 }
175
176 SkASSERT(i == -1);
177 }
178
179 // skipToTopmost
180 {
181 const SkClipStack::Iter::Clip*clip = NULL;
182
183 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
184
185 clip = iter.skipToTopmost(SkRegion::kUnion_Op);
186 REPORTER_ASSERT(reporter, *clip->fRect == gRects[3]);
187 }
188}
189
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000190// Exercise the SkClipStack's getConservativeBounds computation
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000191static void test_bounds(skiatest::Reporter* reporter, bool useRects) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000192
193 static const int gNumCases = 20;
194 static const SkRect gAnswerRectsBW[gNumCases] = {
195 // A op B
196 { 40, 40, 50, 50 },
197 { 10, 10, 50, 50 },
198 { 10, 10, 80, 80 },
199 { 10, 10, 80, 80 },
200 { 40, 40, 80, 80 },
201
202 // invA op B
203 { 40, 40, 80, 80 },
204 { 0, 0, 100, 100 },
205 { 0, 0, 100, 100 },
206 { 0, 0, 100, 100 },
207 { 40, 40, 50, 50 },
208
209 // A op invB
210 { 10, 10, 50, 50 },
211 { 40, 40, 50, 50 },
212 { 0, 0, 100, 100 },
213 { 0, 0, 100, 100 },
214 { 0, 0, 100, 100 },
215
216 // invA op invB
217 { 0, 0, 100, 100 },
218 { 40, 40, 80, 80 },
219 { 0, 0, 100, 100 },
220 { 10, 10, 80, 80 },
221 { 10, 10, 50, 50 },
222 };
223
224 static const SkRegion::Op gOps[] = {
225 SkRegion::kIntersect_Op,
226 SkRegion::kDifference_Op,
227 SkRegion::kUnion_Op,
228 SkRegion::kXOR_Op,
229 SkRegion::kReverseDifference_Op
230 };
231
232 SkRect rectA, rectB;
233
234 rectA.iset(10, 10, 50, 50);
235 rectB.iset(40, 40, 80, 80);
236
237 SkPath clipA, clipB;
238
239 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
240 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
241
242 SkClipStack stack;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000243 SkRect devClipBound;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000244 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000245
246 int testCase = 0;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000247 int numBitTests = useRects ? 1 : 4;
248 for (int invBits = 0; invBits < numBitTests; ++invBits) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000249 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
250
251 stack.save();
252 bool doInvA = SkToBool(invBits & 1);
253 bool doInvB = SkToBool(invBits & 2);
254
255 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
256 SkPath::kEvenOdd_FillType);
257 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
258 SkPath::kEvenOdd_FillType);
259
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000260 if (useRects) {
261 stack.clipDevRect(rectA, SkRegion::kIntersect_Op, false);
262 stack.clipDevRect(rectB, gOps[op], false);
263 } else {
264 stack.clipDevPath(clipA, SkRegion::kIntersect_Op, false);
265 stack.clipDevPath(clipB, gOps[op], false);
266 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000267
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000268 REPORTER_ASSERT(reporter, !stack.isWideOpen());
269
robertphillips@google.com7b112892012-07-31 15:18:21 +0000270 stack.getConservativeBounds(0, 0, 100, 100, &devClipBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000271 &isIntersectionOfRects);
272
273 if (useRects) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000274 REPORTER_ASSERT(reporter, isIntersectionOfRects ==
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000275 (gOps[op] == SkRegion::kIntersect_Op));
276 } else {
277 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
278 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000279
280 SkASSERT(testCase < gNumCases);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000281 REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000282 ++testCase;
283
284 stack.restore();
285 }
286 }
287}
288
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000289// Test out 'isWideOpen' entry point
290static void test_isWideOpen(skiatest::Reporter* reporter) {
291
292 SkRect rectA, rectB;
293
294 rectA.iset(10, 10, 40, 40);
295 rectB.iset(50, 50, 80, 80);
296
297 // Stack should initially be wide open
298 {
299 SkClipStack stack;
300
301 REPORTER_ASSERT(reporter, stack.isWideOpen());
302 }
303
304 // Test out case where the user specifies a union that includes everything
305 {
306 SkClipStack stack;
307
308 SkPath clipA, clipB;
309
310 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
311 clipA.setFillType(SkPath::kInverseEvenOdd_FillType);
312
313 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
314 clipB.setFillType(SkPath::kInverseEvenOdd_FillType);
315
316 stack.clipDevPath(clipA, SkRegion::kReplace_Op, false);
317 stack.clipDevPath(clipB, SkRegion::kUnion_Op, false);
318
319 REPORTER_ASSERT(reporter, stack.isWideOpen());
320 }
321
322 // Test out union w/ a wide open clip
323 {
324 SkClipStack stack;
325
326 stack.clipDevRect(rectA, SkRegion::kUnion_Op, false);
327
328 REPORTER_ASSERT(reporter, stack.isWideOpen());
329 }
330
331 // Test out empty difference from a wide open clip
332 {
333 SkClipStack stack;
334
335 SkRect emptyRect;
336 emptyRect.setEmpty();
337
338 stack.clipDevRect(emptyRect, SkRegion::kDifference_Op, false);
339
340 REPORTER_ASSERT(reporter, stack.isWideOpen());
341 }
342
343 // Test out return to wide open
344 {
345 SkClipStack stack;
346
347 stack.save();
348
349 stack.clipDevRect(rectA, SkRegion::kReplace_Op, false);
350
351 REPORTER_ASSERT(reporter, !stack.isWideOpen());
352
353 stack.restore();
354
355 REPORTER_ASSERT(reporter, stack.isWideOpen());
356 }
357}
358
bsalomon@google.com100abf42012-09-05 17:40:04 +0000359static int count(const SkClipStack& stack) {
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000360
361 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
362
363 const SkClipStack::Iter::Clip* clip = NULL;
364 int count = 0;
365
366 for (clip = iter.prev(); clip; clip = iter.prev(), ++count) {
367 ;
368 }
369
370 return count;
371}
372
373// Test out SkClipStack's merging of rect clips. In particular exercise
374// merging of aa vs. bw rects.
375static void test_rect_merging(skiatest::Reporter* reporter) {
376
377 SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
378 SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
379
380 SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
381 SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
382
383 SkRect bound;
384 SkClipStack::BoundsType type;
385 bool isIntersectionOfRects;
386
387 // all bw overlapping - should merge
388 {
389 SkClipStack stack;
390
391 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, false);
392
393 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
394
395 REPORTER_ASSERT(reporter, 1 == count(stack));
396
397 stack.getBounds(&bound, &type, &isIntersectionOfRects);
398
399 REPORTER_ASSERT(reporter, isIntersectionOfRects);
400 }
401
402 // all aa overlapping - should merge
403 {
404 SkClipStack stack;
405
406 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
407
408 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true);
409
410 REPORTER_ASSERT(reporter, 1 == count(stack));
411
412 stack.getBounds(&bound, &type, &isIntersectionOfRects);
413
414 REPORTER_ASSERT(reporter, isIntersectionOfRects);
415 }
416
417 // mixed overlapping - should _not_ merge
418 {
419 SkClipStack stack;
420
421 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
422
423 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
424
425 REPORTER_ASSERT(reporter, 2 == count(stack));
426
427 stack.getBounds(&bound, &type, &isIntersectionOfRects);
428
429 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
430 }
431
432 // mixed nested (bw inside aa) - should merge
433 {
434 SkClipStack stack;
435
436 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, true);
437
438 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false);
439
440 REPORTER_ASSERT(reporter, 1 == count(stack));
441
442 stack.getBounds(&bound, &type, &isIntersectionOfRects);
443
444 REPORTER_ASSERT(reporter, isIntersectionOfRects);
445 }
446
447 // mixed nested (aa inside bw) - should merge
448 {
449 SkClipStack stack;
450
451 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, false);
452
453 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true);
454
455 REPORTER_ASSERT(reporter, 1 == count(stack));
456
457 stack.getBounds(&bound, &type, &isIntersectionOfRects);
458
459 REPORTER_ASSERT(reporter, isIntersectionOfRects);
460 }
461
462 // reverse nested (aa inside bw) - should _not_ merge
463 {
464 SkClipStack stack;
465
466 stack.clipDevRect(nestedChild, SkRegion::kReplace_Op, false);
467
468 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true);
469
470 REPORTER_ASSERT(reporter, 2 == count(stack));
471
472 stack.getBounds(&bound, &type, &isIntersectionOfRects);
473
474 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
475 }
476}
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000477
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000478
479// This is similar to the above test but tests the iterator's ability to merge rects in the
480// middle of a clip stack's sequence using nextCombined(). There is a save after every clip
481// element to prevent the clip stack from merging the rectangles as they are added.
482static void test_iter_rect_merging(skiatest::Reporter* reporter) {
483
484 SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
485 SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
486
487 SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
488 SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
489
490 SkRect farAway = SkRect::MakeLTRB(1000, 1000, 1010, 1010);
491
492 SkRect overlapIntersect;
493 overlapIntersect.intersect(overlapLeft, overlapRight);
494
495 SkPath path1, path2;
496 path1.addCircle(SkIntToScalar(30), SkIntToScalar(30), SkIntToScalar(1000));
497 path2.addOval(SkRect::MakeWH(500, 600));
498
499 const SkClipStack::Iter::Clip* clip;
500
501 // call nextCombined with an empty clip stack
502 {
503 SkClipStack stack;
504 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
505 REPORTER_ASSERT(reporter, NULL == iter.nextCombined());
506 }
507
508 // two bw overlapping - should merge, bracketed by paths
509 {
510 SkClipStack stack;
511 stack.clipDevPath(path1, SkRegion::kIntersect_Op, false); stack.save();
512
513 stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, false); stack.save();
514
515 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save();
516
517 stack.clipDevPath(path2, SkRegion::kIntersect_Op, false); stack.save();
518
519 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
520
521 clip = iter.nextCombined();
522 REPORTER_ASSERT(reporter, *clip->fPath == path1 && !clip->fDoAA);
523
524 clip = iter.nextCombined();
525 REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == overlapIntersect);
526
527 clip = iter.nextCombined();
528 REPORTER_ASSERT(reporter, *clip->fPath == path2 && !clip->fDoAA);
529
530 clip = iter.nextCombined();
531 REPORTER_ASSERT(reporter, NULL == clip);
532 }
533
534 // same as above but rects are aa and no final path.
535 {
536 SkClipStack stack;
537 stack.clipDevPath(path1, SkRegion::kIntersect_Op, false); stack.save();
538
539 stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save();
540
541 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true); stack.save();
542
543 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
544
545 clip = iter.nextCombined();
546 REPORTER_ASSERT(reporter, *clip->fPath == path1 && !clip->fDoAA);
547
548 clip = iter.nextCombined();
549 REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapIntersect);
550
551 clip = iter.nextCombined();
552 REPORTER_ASSERT(reporter, NULL == clip);
553 }
554
555 // mixed overlapping - no paths - should _not_ merge
556 {
557 SkClipStack stack;
558
559 stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save();
560 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save();
561
562 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
563
564 clip = iter.nextCombined();
565 REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapLeft);
566
567 clip = iter.nextCombined();
568 REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == overlapRight);
569
570 clip = iter.nextCombined();
571 REPORTER_ASSERT(reporter, NULL == clip);
572 }
573
574 // three rects in a row where the third rect uses a non-intersect op.
575 {
576 SkClipStack stack;
577
578 stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, true); stack.save();
579 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true); stack.save();
580 stack.clipDevRect(nestedParent, SkRegion::kXOR_Op, true); stack.save();
581
582 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
583
584 clip = iter.nextCombined();
585 REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == overlapIntersect);
586 clip = iter.nextCombined();
587 REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == nestedParent);
588 clip = iter.nextCombined();
589 REPORTER_ASSERT(reporter, NULL == clip);
590 }
591
592 // mixed nested (bw inside aa) - should merge
593 {
594 SkClipStack stack;
595 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, false); stack.save();
596
597 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true); stack.save();
598
599 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000600
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000601 clip = iter.nextCombined();
602 REPORTER_ASSERT(reporter, clip->fDoAA && *clip->fRect == nestedChild);
603
604 clip = iter.nextCombined();
605 REPORTER_ASSERT(reporter, NULL == clip);
606 }
607
608 // mixed nested (aa inside bw) - should merge
609 {
610 SkClipStack stack;
611 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false); stack.save();
612
613 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true); stack.save();
614
615 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +0000616
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000617 clip = iter.nextCombined();
618 REPORTER_ASSERT(reporter, !clip->fDoAA && *clip->fRect == nestedChild);
619
620 clip = iter.nextCombined();
621 REPORTER_ASSERT(reporter, NULL == clip);
622 }
623
624 // three rect intersects in a row where result is empty after the second.
625 {
626 SkClipStack stack;
627
628 stack.clipDevRect(overlapLeft, SkRegion::kIntersect_Op, false); stack.save();
629 stack.clipDevRect(farAway, SkRegion::kIntersect_Op, false); stack.save();
630 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false); stack.save();
631
632 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
633
634 clip = iter.nextCombined();
635 REPORTER_ASSERT(reporter, clip->fRect->isEmpty());
636
637 clip = iter.nextCombined();
638 REPORTER_ASSERT(reporter, *clip->fRect == overlapRight);
639
640 clip = iter.nextCombined();
641 REPORTER_ASSERT(reporter, NULL == clip);
642 }
643}
644
bsalomon@google.com51a62862012-11-26 21:19:43 +0000645///////////////////////////////////////////////////////////////////////////////////////////////////
646
647typedef void (*AddElementFunc) (const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack);
648
649static void add_round_rect(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) {
650 SkPath path;
651 SkScalar rx = rect.width() / 10;
652 SkScalar ry = rect.width() / 20;
653 path.addRoundRect(rect, rx, ry);
654 stack->clipDevPath(path, op, aa);
655};
656
657static void add_rect(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) {
658 stack->clipDevRect(rect, op, aa);
659};
660
661static void add_oval(const SkRect& rect, bool aa, SkRegion::Op op, SkClipStack* stack) {
662 SkPath path;
663 path.addOval(rect);
664 stack->clipDevPath(path, op, aa);
665};
666
667static void add_elem_to_stack(const SkClipStack::Iter::Clip& clip, SkClipStack* stack) {
668 if (NULL != clip.fPath) {
669 stack->clipDevPath(*clip.fPath, clip.fOp, clip.fDoAA);
670 } else if (NULL != clip.fRect) {
671 stack->clipDevRect(*clip.fRect, clip.fOp, clip.fDoAA);
672 }
673}
674
675static void add_elem_to_region(const SkClipStack::Iter::Clip& clip,
676 const SkIRect& bounds,
677 SkRegion* region) {
678 SkRegion elemRegion;
679 SkRegion boundsRgn(bounds);
680
681 if (NULL != clip.fPath) {
682 elemRegion.setPath(*clip.fPath, boundsRgn);
683 } else if (NULL != clip.fRect) {
684 SkPath path;
685 path.addRect(*clip.fRect);
686 elemRegion.setPath(path, boundsRgn);
687 } else {
688 // TODO: Figure out why we sometimes get here in the reduced clip stack.
689 region->setEmpty();
690 return;
691 }
692 region->op(elemRegion, clip.fOp);
693}
694
695// This can assist with debugging the clip stack reduction code when the test below fails.
696static void print_clip(const SkClipStack::Iter::Clip& clip) {
697 static const char* kOpStrs[] = {
698 "DF",
699 "IS",
700 "UN",
701 "XR",
702 "RD",
703 "RP",
704 };
705 if (clip.fRect || clip.fPath) {
706 const SkRect& bounds = clip.getBounds();
707 SkDebugf("%s %s [%f %f] x [%f %f]\n",
708 kOpStrs[clip.fOp],
709 (clip.fRect ? "R" : "P"),
710 bounds.fLeft, bounds.fRight, bounds.fTop, bounds.fBottom);
711 } else {
712 SkDebugf("EM\n");
713 }
714}
715
716static void test_reduced_clip_stack(skiatest::Reporter* reporter) {
717 // We construct random clip stacks, reduce them, and then rasterize both versions to verify that
718 // they are equal.
719
720 // All the clip elements will be contained within these bounds.
721 static const SkRect kBounds = SkRect::MakeWH(100, 100);
722
723 enum {
724 kNumTests = 200,
725 kMinElemsPerTest = 1,
726 kMaxElemsPerTest = 50,
727 };
728
729 // min/max size of a clip element as a fraction of kBounds.
730 static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5;
731 static const SkScalar kMaxElemSizeFrac = SK_Scalar1;
732
733 static const SkRegion::Op kOps[] = {
734 SkRegion::kDifference_Op,
735 SkRegion::kIntersect_Op,
736 SkRegion::kUnion_Op,
737 SkRegion::kXOR_Op,
738 SkRegion::kReverseDifference_Op,
739 SkRegion::kReplace_Op,
740 };
741
742 // Replace operations short-circuit the optimizer. We want to make sure that we test this code
743 // path a little bit but we don't want it to prevent us from testing many longer traversals in
744 // the optimizer.
745 static const int kReplaceDiv = 4 * kMaxElemsPerTest;
746
747 static const AddElementFunc kElementFuncs[] = {
748 add_rect,
749 add_round_rect,
750 add_oval,
751 };
752
753 SkRandom r;
754
755 for (int i = 0; i < kNumTests; ++i) {
756 // Randomly generate a clip stack.
757 SkClipStack stack;
758 int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest);
759 for (int e = 0; e < numElems; ++e) {
760 SkRegion::Op op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))];
761 if (op == SkRegion::kReplace_Op) {
762 if (r.nextU() % kReplaceDiv) {
763 --e;
764 continue;
765 }
766 }
767
768 // saves can change the clip stack behavior when an element is added.
769 bool doSave = r.nextBool();
770
771 SkSize size = SkSize::Make(
772 SkScalarFloorToScalar(SkScalarMul(kBounds.width(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))),
773 SkScalarFloorToScalar(SkScalarMul(kBounds.height(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))));
774
775 SkPoint xy = {SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth)),
776 SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight))};
777
778 SkRect rect = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
779
780 // AA is always disabled. The optimizer can cause changes in aa rasterization of the
781 // clip stack. A fractional edge repeated in different elements may be rasterized fewer
782 // times using the reduced stack.
783 kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, false, op, &stack);
784 if (doSave) {
785 stack.save();
786 }
787 }
788
789 // Get the reduced version of the stack.
790 SkTDArray<SkClipStack::Iter::Clip> reducedClips;
791 SkRect resultBounds;
792 bool bounded;
793 GrReducedClip::InitialState initial;
794 GrReducedClip::GrReduceClipStack(stack, &reducedClips, &resultBounds, &bounded, &initial);
795
796 // Build a new clip stack based on the reduced clip elements
797 SkClipStack reducedStack;
798 if (GrReducedClip::kAllOut_InitialState == initial) {
799 // whether the result is bounded or not, the whole plane should start outside the clip.
800 reducedStack.clipEmpty();
801 }
802 for (int c = 0; c < reducedClips.count(); ++c) {
803 add_elem_to_stack(reducedClips[c], &reducedStack);
804 }
805 if (bounded) {
806 // GrReduceClipStack() assumes that there is an implicit clip to the bounds
807 reducedStack.clipDevRect(resultBounds, SkRegion::kIntersect_Op, true);
808 }
809
810 // convert both the original stack and reduced stack to SkRegions and see if they're equal
811 SkRect inflatedBounds = kBounds;
812 inflatedBounds.outset(kBounds.width() / 2, kBounds.height() / 2);
813 SkIRect inflatedIBounds;
814 inflatedBounds.roundOut(&inflatedIBounds);
815
816 SkRegion region;
817 SkRegion reducedRegion;
818
819 region.setRect(inflatedIBounds);
820 const SkClipStack::Iter::Clip* clip;
821 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
822 while ((clip = iter.next())) {
823 add_elem_to_region(*clip, inflatedIBounds, &region);
824 }
825
826 reducedRegion.setRect(inflatedIBounds);
827 iter.reset(reducedStack, SkClipStack::Iter::kBottom_IterStart);
828 while ((clip = iter.next())) {
829 add_elem_to_region(*clip, inflatedIBounds, &reducedRegion);
830 }
831
832 REPORTER_ASSERT(reporter, region == reducedRegion);
833 }
834}
835
836///////////////////////////////////////////////////////////////////////////////////////////////////
837
reed@google.combdee9fc2011-02-22 20:17:43 +0000838static void TestClipStack(skiatest::Reporter* reporter) {
839 SkClipStack stack;
840
robertphillips@google.com80214e22012-07-20 15:33:18 +0000841 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000842 assert_count(reporter, stack, 0);
843
844 static const SkIRect gRects[] = {
845 { 0, 0, 100, 100 },
846 { 25, 25, 125, 125 },
847 { 0, 0, 1000, 1000 },
848 { 0, 0, 75, 75 }
849 };
850 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
reed@google.comd9f2dea2011-10-12 14:43:27 +0000851 stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
reed@google.combdee9fc2011-02-22 20:17:43 +0000852 }
853
854 // all of the above rects should have been intersected, leaving only 1 rect
robertphillips@google.com80214e22012-07-20 15:33:18 +0000855 SkClipStack::B2TIter iter(stack);
856 const SkClipStack::B2TIter::Clip* clip = iter.next();
epoger@google.com2047f002011-05-17 17:36:59 +0000857 SkRect answer;
858 answer.iset(25, 25, 75, 75);
reed@google.combdee9fc2011-02-22 20:17:43 +0000859
860 REPORTER_ASSERT(reporter, clip);
861 REPORTER_ASSERT(reporter, clip->fRect);
862 REPORTER_ASSERT(reporter, !clip->fPath);
863 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
864 REPORTER_ASSERT(reporter, *clip->fRect == answer);
865 // now check that we only had one in our iterator
866 REPORTER_ASSERT(reporter, !iter.next());
867
868 stack.reset();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000869 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000870 assert_count(reporter, stack, 0);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000871
872 test_assign_and_comparison(reporter);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000873 test_iterators(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000874 test_bounds(reporter, true); // once with rects
875 test_bounds(reporter, false); // once with paths
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000876 test_isWideOpen(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000877 test_rect_merging(reporter);
bsalomon@google.come8ca6c62012-11-07 21:19:10 +0000878 test_iter_rect_merging(reporter);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000879 test_reduced_clip_stack(reporter);
reed@google.combdee9fc2011-02-22 20:17:43 +0000880}
881
882#include "TestClassDef.h"
883DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)