blob: 03da7f18fe69a13b6e4f044bc406fb8f02939ef6 [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
bsalomon@google.com170bd792012-12-05 22:26:11 +000010 #include "GrReducedClip.h"
bsalomon@google.coma4e13c82012-11-26 21:38:37 +000011#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 {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000155 const SkClipStack::Element* element = NULL;
robertphillips@google.com80214e22012-07-20 15:33:18 +0000156
157 SkClipStack::B2TIter iter(stack);
158 int i;
159
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000160 for (i = 0, element = iter.next(); element; ++i, element = iter.next()) {
161 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
162 REPORTER_ASSERT(reporter, element->getRect() == gRects[i]);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000163 }
164
165 SkASSERT(i == 4);
166 }
167
168 // top to bottom iteration
169 {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000170 const SkClipStack::Element* element = NULL;
robertphillips@google.com80214e22012-07-20 15:33:18 +0000171
172 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
173 int i;
174
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000175 for (i = 3, element = iter.prev(); element; --i, element = iter.prev()) {
176 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
177 REPORTER_ASSERT(reporter, element->getRect() == gRects[i]);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000178 }
179
180 SkASSERT(i == -1);
181 }
182
183 // skipToTopmost
184 {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000185 const SkClipStack::Element* element = NULL;
robertphillips@google.com80214e22012-07-20 15:33:18 +0000186
187 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
188
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000189 element = iter.skipToTopmost(SkRegion::kUnion_Op);
190 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
191 REPORTER_ASSERT(reporter, element->getRect() == gRects[3]);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000192 }
193}
194
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000195// Exercise the SkClipStack's getConservativeBounds computation
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000196static void test_bounds(skiatest::Reporter* reporter, bool useRects) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000197
198 static const int gNumCases = 20;
199 static const SkRect gAnswerRectsBW[gNumCases] = {
200 // A op B
201 { 40, 40, 50, 50 },
202 { 10, 10, 50, 50 },
203 { 10, 10, 80, 80 },
204 { 10, 10, 80, 80 },
205 { 40, 40, 80, 80 },
206
207 // invA op B
208 { 40, 40, 80, 80 },
209 { 0, 0, 100, 100 },
210 { 0, 0, 100, 100 },
211 { 0, 0, 100, 100 },
212 { 40, 40, 50, 50 },
213
214 // A op invB
215 { 10, 10, 50, 50 },
216 { 40, 40, 50, 50 },
217 { 0, 0, 100, 100 },
218 { 0, 0, 100, 100 },
219 { 0, 0, 100, 100 },
220
221 // invA op invB
222 { 0, 0, 100, 100 },
223 { 40, 40, 80, 80 },
224 { 0, 0, 100, 100 },
225 { 10, 10, 80, 80 },
226 { 10, 10, 50, 50 },
227 };
228
229 static const SkRegion::Op gOps[] = {
230 SkRegion::kIntersect_Op,
231 SkRegion::kDifference_Op,
232 SkRegion::kUnion_Op,
233 SkRegion::kXOR_Op,
234 SkRegion::kReverseDifference_Op
235 };
236
237 SkRect rectA, rectB;
238
239 rectA.iset(10, 10, 50, 50);
240 rectB.iset(40, 40, 80, 80);
241
242 SkPath clipA, clipB;
243
244 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
245 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
246
247 SkClipStack stack;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000248 SkRect devClipBound;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000249 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000250
251 int testCase = 0;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000252 int numBitTests = useRects ? 1 : 4;
253 for (int invBits = 0; invBits < numBitTests; ++invBits) {
robertphillips@google.com607fe072012-07-24 13:54:00 +0000254 for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
255
256 stack.save();
257 bool doInvA = SkToBool(invBits & 1);
258 bool doInvB = SkToBool(invBits & 2);
259
260 clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
261 SkPath::kEvenOdd_FillType);
262 clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
263 SkPath::kEvenOdd_FillType);
264
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000265 if (useRects) {
266 stack.clipDevRect(rectA, SkRegion::kIntersect_Op, false);
267 stack.clipDevRect(rectB, gOps[op], false);
268 } else {
269 stack.clipDevPath(clipA, SkRegion::kIntersect_Op, false);
270 stack.clipDevPath(clipB, gOps[op], false);
271 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000272
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000273 REPORTER_ASSERT(reporter, !stack.isWideOpen());
274
robertphillips@google.com7b112892012-07-31 15:18:21 +0000275 stack.getConservativeBounds(0, 0, 100, 100, &devClipBound,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000276 &isIntersectionOfRects);
277
278 if (useRects) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000279 REPORTER_ASSERT(reporter, isIntersectionOfRects ==
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000280 (gOps[op] == SkRegion::kIntersect_Op));
281 } else {
282 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
283 }
robertphillips@google.com607fe072012-07-24 13:54:00 +0000284
285 SkASSERT(testCase < gNumCases);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000286 REPORTER_ASSERT(reporter, devClipBound == gAnswerRectsBW[testCase]);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000287 ++testCase;
288
289 stack.restore();
290 }
291 }
292}
293
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000294// Test out 'isWideOpen' entry point
295static void test_isWideOpen(skiatest::Reporter* reporter) {
296
297 SkRect rectA, rectB;
298
299 rectA.iset(10, 10, 40, 40);
300 rectB.iset(50, 50, 80, 80);
301
302 // Stack should initially be wide open
303 {
304 SkClipStack stack;
305
306 REPORTER_ASSERT(reporter, stack.isWideOpen());
307 }
308
309 // Test out case where the user specifies a union that includes everything
310 {
311 SkClipStack stack;
312
313 SkPath clipA, clipB;
314
315 clipA.addRoundRect(rectA, SkIntToScalar(5), SkIntToScalar(5));
316 clipA.setFillType(SkPath::kInverseEvenOdd_FillType);
317
318 clipB.addRoundRect(rectB, SkIntToScalar(5), SkIntToScalar(5));
319 clipB.setFillType(SkPath::kInverseEvenOdd_FillType);
320
321 stack.clipDevPath(clipA, SkRegion::kReplace_Op, false);
322 stack.clipDevPath(clipB, SkRegion::kUnion_Op, false);
323
324 REPORTER_ASSERT(reporter, stack.isWideOpen());
325 }
326
327 // Test out union w/ a wide open clip
328 {
329 SkClipStack stack;
330
331 stack.clipDevRect(rectA, SkRegion::kUnion_Op, false);
332
333 REPORTER_ASSERT(reporter, stack.isWideOpen());
334 }
335
336 // Test out empty difference from a wide open clip
337 {
338 SkClipStack stack;
339
340 SkRect emptyRect;
341 emptyRect.setEmpty();
342
343 stack.clipDevRect(emptyRect, SkRegion::kDifference_Op, false);
344
345 REPORTER_ASSERT(reporter, stack.isWideOpen());
346 }
347
348 // Test out return to wide open
349 {
350 SkClipStack stack;
351
352 stack.save();
353
354 stack.clipDevRect(rectA, SkRegion::kReplace_Op, false);
355
356 REPORTER_ASSERT(reporter, !stack.isWideOpen());
357
358 stack.restore();
359
360 REPORTER_ASSERT(reporter, stack.isWideOpen());
361 }
362}
363
bsalomon@google.com100abf42012-09-05 17:40:04 +0000364static int count(const SkClipStack& stack) {
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000365
366 SkClipStack::Iter iter(stack, SkClipStack::Iter::kTop_IterStart);
367
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000368 const SkClipStack::Element* element = NULL;
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000369 int count = 0;
370
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000371 for (element = iter.prev(); element; element = iter.prev(), ++count) {
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000372 ;
373 }
374
375 return count;
376}
377
junov@chromium.orgedf32d52012-12-10 14:57:54 +0000378static void test_rect_inverse_fill(skiatest::Reporter* reporter) {
379 // non-intersecting rectangles
380 SkRect rect = SkRect::MakeLTRB(0, 0, 10, 10);
381
382 SkPath path;
383 path.addRect(rect);
384 path.toggleInverseFillType();
385 SkClipStack stack;
386 stack.clipDevPath(path, SkRegion::kIntersect_Op, false);
387
388 SkRect bounds;
389 SkClipStack::BoundsType boundsType;
390 stack.getBounds(&bounds, &boundsType);
391 REPORTER_ASSERT(reporter, SkClipStack::kInsideOut_BoundsType == boundsType);
392 REPORTER_ASSERT(reporter, bounds == rect);
393}
394
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000395// Test out SkClipStack's merging of rect clips. In particular exercise
396// merging of aa vs. bw rects.
397static void test_rect_merging(skiatest::Reporter* reporter) {
398
399 SkRect overlapLeft = SkRect::MakeLTRB(10, 10, 50, 50);
400 SkRect overlapRight = SkRect::MakeLTRB(40, 40, 80, 80);
401
402 SkRect nestedParent = SkRect::MakeLTRB(10, 10, 90, 90);
403 SkRect nestedChild = SkRect::MakeLTRB(40, 40, 60, 60);
404
405 SkRect bound;
406 SkClipStack::BoundsType type;
407 bool isIntersectionOfRects;
408
409 // all bw overlapping - should merge
410 {
411 SkClipStack stack;
412
413 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, false);
414
415 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
416
417 REPORTER_ASSERT(reporter, 1 == count(stack));
418
419 stack.getBounds(&bound, &type, &isIntersectionOfRects);
420
421 REPORTER_ASSERT(reporter, isIntersectionOfRects);
422 }
423
424 // all aa overlapping - should merge
425 {
426 SkClipStack stack;
427
428 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
429
430 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, true);
431
432 REPORTER_ASSERT(reporter, 1 == count(stack));
433
434 stack.getBounds(&bound, &type, &isIntersectionOfRects);
435
436 REPORTER_ASSERT(reporter, isIntersectionOfRects);
437 }
438
439 // mixed overlapping - should _not_ merge
440 {
441 SkClipStack stack;
442
443 stack.clipDevRect(overlapLeft, SkRegion::kReplace_Op, true);
444
445 stack.clipDevRect(overlapRight, SkRegion::kIntersect_Op, false);
446
447 REPORTER_ASSERT(reporter, 2 == count(stack));
448
449 stack.getBounds(&bound, &type, &isIntersectionOfRects);
450
451 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
452 }
453
454 // mixed nested (bw inside aa) - should merge
455 {
456 SkClipStack stack;
457
458 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, true);
459
460 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, false);
461
462 REPORTER_ASSERT(reporter, 1 == count(stack));
463
464 stack.getBounds(&bound, &type, &isIntersectionOfRects);
465
466 REPORTER_ASSERT(reporter, isIntersectionOfRects);
467 }
468
469 // mixed nested (aa inside bw) - should merge
470 {
471 SkClipStack stack;
472
473 stack.clipDevRect(nestedParent, SkRegion::kReplace_Op, false);
474
475 stack.clipDevRect(nestedChild, SkRegion::kIntersect_Op, true);
476
477 REPORTER_ASSERT(reporter, 1 == count(stack));
478
479 stack.getBounds(&bound, &type, &isIntersectionOfRects);
480
481 REPORTER_ASSERT(reporter, isIntersectionOfRects);
482 }
483
484 // reverse nested (aa inside bw) - should _not_ merge
485 {
486 SkClipStack stack;
487
488 stack.clipDevRect(nestedChild, SkRegion::kReplace_Op, false);
489
490 stack.clipDevRect(nestedParent, SkRegion::kIntersect_Op, true);
491
492 REPORTER_ASSERT(reporter, 2 == count(stack));
493
494 stack.getBounds(&bound, &type, &isIntersectionOfRects);
495
496 REPORTER_ASSERT(reporter, !isIntersectionOfRects);
497 }
498}
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000499
bsalomon@google.com51a62862012-11-26 21:19:43 +0000500///////////////////////////////////////////////////////////////////////////////////////////////////
501
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000502#if SK_SUPPORT_GPU
bsalomon@google.com705e8402012-11-27 15:43:57 +0000503// Functions that add a shape to the clip stack. The shape is computed from a rectangle.
504// AA is always disabled since the clip stack reducer can cause changes in aa rasterization of the
505// stack. A fractional edge repeated in different elements may be rasterized fewer times using the
506// reduced stack.
507typedef void (*AddElementFunc) (const SkRect& rect,
508 bool invert,
509 SkRegion::Op op,
510 SkClipStack* stack);
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000511
bsalomon@google.com705e8402012-11-27 15:43:57 +0000512static void add_round_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000513 SkPath path;
514 SkScalar rx = rect.width() / 10;
bsalomon@google.com705e8402012-11-27 15:43:57 +0000515 SkScalar ry = rect.height() / 20;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000516 path.addRoundRect(rect, rx, ry);
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
bsalomon@google.com705e8402012-11-27 15:43:57 +0000523static void add_rect(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
524 if (invert) {
525 SkPath path;
526 path.addRect(rect);
527 path.setFillType(SkPath::kInverseWinding_FillType);
528 stack->clipDevPath(path, op, false);
529 } else {
530 stack->clipDevRect(rect, op, false);
531 }
bsalomon@google.com51a62862012-11-26 21:19:43 +0000532};
533
bsalomon@google.com705e8402012-11-27 15:43:57 +0000534static void add_oval(const SkRect& rect, bool invert, SkRegion::Op op, SkClipStack* stack) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000535 SkPath path;
536 path.addOval(rect);
bsalomon@google.com705e8402012-11-27 15:43:57 +0000537 if (invert) {
538 path.setFillType(SkPath::kInverseWinding_FillType);
539 }
540 stack->clipDevPath(path, op, false);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000541};
542
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000543static void add_elem_to_stack(const SkClipStack::Element& element, SkClipStack* stack) {
544 switch (element.getType()) {
545 case SkClipStack::Element::kRect_Type:
546 stack->clipDevRect(element.getRect(), element.getOp(), element.isAA());
547 break;
548 case SkClipStack::Element::kPath_Type:
549 stack->clipDevPath(element.getPath(), element.getOp(), element.isAA());
550 break;
551 case SkClipStack::Element::kEmpty_Type:
552 SkDEBUGFAIL("Why did the reducer produce an explicit empty.");
553 stack->clipEmpty();
554 break;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000555 }
556}
557
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000558static void add_elem_to_region(const SkClipStack::Element& element,
bsalomon@google.com51a62862012-11-26 21:19:43 +0000559 const SkIRect& bounds,
560 SkRegion* region) {
561 SkRegion elemRegion;
562 SkRegion boundsRgn(bounds);
563
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000564 switch (element.getType()) {
565 case SkClipStack::Element::kRect_Type: {
566 SkPath path;
567 path.addRect(element.getRect());
568 elemRegion.setPath(path, boundsRgn);
569 break;
570 }
571 case SkClipStack::Element::kPath_Type:
572 elemRegion.setPath(element.getPath(), boundsRgn);
573 break;
574 case SkClipStack::Element::kEmpty_Type:
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000575 //
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000576 region->setEmpty();
577 return;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000578 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000579 region->op(elemRegion, element.getOp());
bsalomon@google.com51a62862012-11-26 21:19:43 +0000580}
581
582// This can assist with debugging the clip stack reduction code when the test below fails.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000583static void print_clip(const SkClipStack::Element& element) {
bsalomon@google.com51a62862012-11-26 21:19:43 +0000584 static const char* kOpStrs[] = {
585 "DF",
586 "IS",
587 "UN",
588 "XR",
589 "RD",
590 "RP",
591 };
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000592 if (SkClipStack::Element::kEmpty_Type != element.getType()) {
593 const SkRect& bounds = element.getBounds();
594 bool isRect = SkClipStack::Element::kRect_Type == element.getType();
bsalomon@google.com705e8402012-11-27 15:43:57 +0000595 SkDebugf("%s %s %s [%f %f] x [%f %f]\n",
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000596 kOpStrs[element.getOp()],
597 (isRect ? "R" : "P"),
598 (element.isInverseFilled() ? "I" : " "),
bsalomon@google.com51a62862012-11-26 21:19:43 +0000599 bounds.fLeft, bounds.fRight, bounds.fTop, bounds.fBottom);
600 } else {
601 SkDebugf("EM\n");
602 }
603}
604
605static void test_reduced_clip_stack(skiatest::Reporter* reporter) {
606 // We construct random clip stacks, reduce them, and then rasterize both versions to verify that
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000607 // they are equal.
bsalomon@google.com51a62862012-11-26 21:19:43 +0000608
609 // All the clip elements will be contained within these bounds.
610 static const SkRect kBounds = SkRect::MakeWH(100, 100);
611
612 enum {
613 kNumTests = 200,
614 kMinElemsPerTest = 1,
615 kMaxElemsPerTest = 50,
616 };
617
618 // min/max size of a clip element as a fraction of kBounds.
619 static const SkScalar kMinElemSizeFrac = SK_Scalar1 / 5;
620 static const SkScalar kMaxElemSizeFrac = SK_Scalar1;
621
622 static const SkRegion::Op kOps[] = {
623 SkRegion::kDifference_Op,
624 SkRegion::kIntersect_Op,
625 SkRegion::kUnion_Op,
626 SkRegion::kXOR_Op,
627 SkRegion::kReverseDifference_Op,
628 SkRegion::kReplace_Op,
629 };
630
631 // Replace operations short-circuit the optimizer. We want to make sure that we test this code
632 // path a little bit but we don't want it to prevent us from testing many longer traversals in
633 // the optimizer.
634 static const int kReplaceDiv = 4 * kMaxElemsPerTest;
635
bsalomon@google.com705e8402012-11-27 15:43:57 +0000636 // We want to test inverse fills. However, they are quite rare in practice so don't over do it.
637 static const SkScalar kFractionInverted = SK_Scalar1 / kMaxElemsPerTest;
638
bsalomon@google.com51a62862012-11-26 21:19:43 +0000639 static const AddElementFunc kElementFuncs[] = {
640 add_rect,
641 add_round_rect,
642 add_oval,
643 };
644
645 SkRandom r;
646
647 for (int i = 0; i < kNumTests; ++i) {
648 // Randomly generate a clip stack.
649 SkClipStack stack;
650 int numElems = r.nextRangeU(kMinElemsPerTest, kMaxElemsPerTest);
651 for (int e = 0; e < numElems; ++e) {
652 SkRegion::Op op = kOps[r.nextULessThan(SK_ARRAY_COUNT(kOps))];
653 if (op == SkRegion::kReplace_Op) {
654 if (r.nextU() % kReplaceDiv) {
655 --e;
656 continue;
657 }
658 }
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000659
bsalomon@google.com51a62862012-11-26 21:19:43 +0000660 // saves can change the clip stack behavior when an element is added.
661 bool doSave = r.nextBool();
skia.committer@gmail.com8ccf5902012-11-27 02:01:19 +0000662
bsalomon@google.com51a62862012-11-26 21:19:43 +0000663 SkSize size = SkSize::Make(
664 SkScalarFloorToScalar(SkScalarMul(kBounds.width(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))),
665 SkScalarFloorToScalar(SkScalarMul(kBounds.height(), r.nextRangeScalar(kMinElemSizeFrac, kMaxElemSizeFrac))));
666
667 SkPoint xy = {SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fLeft, kBounds.fRight - size.fWidth)),
668 SkScalarFloorToScalar(r.nextRangeScalar(kBounds.fTop, kBounds.fBottom - size.fHeight))};
669
670 SkRect rect = SkRect::MakeXYWH(xy.fX, xy.fY, size.fWidth, size.fHeight);
671
bsalomon@google.com705e8402012-11-27 15:43:57 +0000672 bool invert = r.nextBiasedBool(kFractionInverted);
673 kElementFuncs[r.nextULessThan(SK_ARRAY_COUNT(kElementFuncs))](rect, invert, op, &stack);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000674 if (doSave) {
675 stack.save();
676 }
677 }
678
bsalomon@google.coma4444302012-12-04 15:22:12 +0000679 SkRect inflatedBounds = kBounds;
680 inflatedBounds.outset(kBounds.width() / 2, kBounds.height() / 2);
681 SkIRect inflatedIBounds;
682 inflatedBounds.roundOut(&inflatedIBounds);
683
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000684 typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000685 // Get the reduced version of the stack.
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000686 ElementList reducedClips;
bsalomon@google.coma4444302012-12-04 15:22:12 +0000687
bsalomon@google.com51a62862012-11-26 21:19:43 +0000688 GrReducedClip::InitialState initial;
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000689 SkIRect tBounds;
690 SkIRect* tightBounds = r.nextBool() ? &tBounds : NULL;
691 GrReducedClip::ReduceClipStack(stack,
692 inflatedIBounds,
693 &reducedClips,
694 &initial,
695 tightBounds);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000696
697 // Build a new clip stack based on the reduced clip elements
698 SkClipStack reducedStack;
699 if (GrReducedClip::kAllOut_InitialState == initial) {
700 // whether the result is bounded or not, the whole plane should start outside the clip.
701 reducedStack.clipEmpty();
702 }
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000703 for (ElementList::Iter iter = reducedClips.headIter(); NULL != iter.get(); iter.next()) {
704 add_elem_to_stack(*iter.get(), &reducedStack);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000705 }
bsalomon@google.com51a62862012-11-26 21:19:43 +0000706
bsalomon@google.com34cd70a2012-12-06 14:23:20 +0000707 // GrReducedClipStack assumes that the final result is clipped to the returned bounds
708 if (NULL != tightBounds) {
709 reducedStack.clipDevRect(*tightBounds, SkRegion::kIntersect_Op);
710 }
711
bsalomon@google.com51a62862012-11-26 21:19:43 +0000712 // convert both the original stack and reduced stack to SkRegions and see if they're equal
bsalomon@google.com51a62862012-11-26 21:19:43 +0000713 SkRegion region;
714 SkRegion reducedRegion;
715
716 region.setRect(inflatedIBounds);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000717 const SkClipStack::Element* element;
bsalomon@google.com51a62862012-11-26 21:19:43 +0000718 SkClipStack::Iter iter(stack, SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000719 while ((element = iter.next())) {
720 add_elem_to_region(*element, inflatedIBounds, &region);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000721 }
722
723 reducedRegion.setRect(inflatedIBounds);
724 iter.reset(reducedStack, SkClipStack::Iter::kBottom_IterStart);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000725 while ((element = iter.next())) {
726 add_elem_to_region(*element, inflatedIBounds, &reducedRegion);
bsalomon@google.com51a62862012-11-26 21:19:43 +0000727 }
728
729 REPORTER_ASSERT(reporter, region == reducedRegion);
730 }
731}
732
bsalomon@google.coma4e13c82012-11-26 21:38:37 +0000733#endif
bsalomon@google.com51a62862012-11-26 21:19:43 +0000734///////////////////////////////////////////////////////////////////////////////////////////////////
735
reed@google.combdee9fc2011-02-22 20:17:43 +0000736static void TestClipStack(skiatest::Reporter* reporter) {
737 SkClipStack stack;
738
robertphillips@google.com80214e22012-07-20 15:33:18 +0000739 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000740 assert_count(reporter, stack, 0);
741
742 static const SkIRect gRects[] = {
743 { 0, 0, 100, 100 },
744 { 25, 25, 125, 125 },
745 { 0, 0, 1000, 1000 },
746 { 0, 0, 75, 75 }
747 };
748 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
reed@google.comd9f2dea2011-10-12 14:43:27 +0000749 stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
reed@google.combdee9fc2011-02-22 20:17:43 +0000750 }
751
752 // all of the above rects should have been intersected, leaving only 1 rect
robertphillips@google.com80214e22012-07-20 15:33:18 +0000753 SkClipStack::B2TIter iter(stack);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000754 const SkClipStack::Element* element = iter.next();
epoger@google.com2047f002011-05-17 17:36:59 +0000755 SkRect answer;
756 answer.iset(25, 25, 75, 75);
reed@google.combdee9fc2011-02-22 20:17:43 +0000757
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000758 REPORTER_ASSERT(reporter, NULL != element);
759 REPORTER_ASSERT(reporter, SkClipStack::Element::kRect_Type == element->getType());
760 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == element->getOp());
761 REPORTER_ASSERT(reporter, element->getRect() == answer);
reed@google.combdee9fc2011-02-22 20:17:43 +0000762 // now check that we only had one in our iterator
763 REPORTER_ASSERT(reporter, !iter.next());
764
765 stack.reset();
robertphillips@google.com80214e22012-07-20 15:33:18 +0000766 REPORTER_ASSERT(reporter, 0 == stack.getSaveCount());
reed@google.combdee9fc2011-02-22 20:17:43 +0000767 assert_count(reporter, stack, 0);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000768
769 test_assign_and_comparison(reporter);
robertphillips@google.com80214e22012-07-20 15:33:18 +0000770 test_iterators(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000771 test_bounds(reporter, true); // once with rects
772 test_bounds(reporter, false); // once with paths
robertphillips@google.comcc6493b2012-07-26 18:39:13 +0000773 test_isWideOpen(reporter);
robertphillips@google.com08eacc12012-08-02 12:49:00 +0000774 test_rect_merging(reporter);
junov@chromium.orgedf32d52012-12-10 14:57:54 +0000775 test_rect_inverse_fill(reporter);
bsalomon@google.come7b3d292012-11-26 21:42:32 +0000776#if SK_SUPPORT_GPU
bsalomon@google.comedb26fd2012-11-28 14:42:41 +0000777 test_reduced_clip_stack(reporter);
bsalomon@google.come7b3d292012-11-26 21:42:32 +0000778#endif
reed@google.combdee9fc2011-02-22 20:17:43 +0000779}
780
781#include "TestClassDef.h"
782DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)