blob: a1e8323116cad57f3161f18e3968ccfc707f4560 [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"
9#include "SkClipStack.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000010#include "SkPath.h"
11#include "SkRect.h"
12
13static void test_assign_and_comparison(skiatest::Reporter* reporter) {
14 SkClipStack s;
reed@google.comd9f2dea2011-10-12 14:43:27 +000015 bool doAA = false;
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000016
17 // Build up a clip stack with a path, an empty clip, and a rect.
18 s.save();
19 SkPath p;
20 p.moveTo(5, 6);
21 p.lineTo(7, 8);
22 p.lineTo(5, 9);
23 p.close();
reed@google.comd9f2dea2011-10-12 14:43:27 +000024 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000025
26 s.save();
27 SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
reed@google.comd9f2dea2011-10-12 14:43:27 +000028 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000029 r = SkRect::MakeLTRB(10, 11, 12, 13);
reed@google.comd9f2dea2011-10-12 14:43:27 +000030 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000031
32 s.save();
33 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000034 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000035
36 // Test that assignment works.
37 SkClipStack copy = s;
38 REPORTER_ASSERT(reporter, s == copy);
39
40 // Test that different save levels triggers not equal.
41 s.restore();
42 REPORTER_ASSERT(reporter, s != copy);
43
44 // Test that an equal, but not copied version is equal.
45 s.save();
46 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000047 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000048 REPORTER_ASSERT(reporter, s == copy);
49
50 // Test that a different op on one level triggers not equal.
51 s.restore();
52 s.save();
53 r = SkRect::MakeLTRB(14, 15, 16, 17);
reed@google.comd9f2dea2011-10-12 14:43:27 +000054 s.clipDevRect(r, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000055 REPORTER_ASSERT(reporter, s != copy);
56
57 // Test that different state (clip type) triggers not equal.
tomhudson@google.com4c433722012-03-09 16:48:20 +000058 // NO LONGER VALID: if a path contains only a rect, we turn
59 // it into a bare rect for performance reasons (working
60 // around Chromium/JavaScript bad pattern).
61/*
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000062 s.restore();
63 s.save();
64 SkPath rp;
65 rp.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +000066 s.clipDevPath(rp, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000067 REPORTER_ASSERT(reporter, s != copy);
tomhudson@google.com4c433722012-03-09 16:48:20 +000068*/
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000069
70 // Test that different rects triggers not equal.
71 s.restore();
72 s.save();
73 r = SkRect::MakeLTRB(24, 25, 26, 27);
reed@google.comd9f2dea2011-10-12 14:43:27 +000074 s.clipDevRect(r, SkRegion::kUnion_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000075 REPORTER_ASSERT(reporter, s != copy);
76
77 // Sanity check
78 s.restore();
79 copy.restore();
80 REPORTER_ASSERT(reporter, s == copy);
81 s.restore();
82 copy.restore();
83 REPORTER_ASSERT(reporter, s == copy);
84
85 // Test that different paths triggers not equal.
86 s.restore();
87 s.save();
88 p.addRect(r);
reed@google.comd9f2dea2011-10-12 14:43:27 +000089 s.clipDevPath(p, SkRegion::kIntersect_Op, doAA);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000090 REPORTER_ASSERT(reporter, s != copy);
91}
reed@google.combdee9fc2011-02-22 20:17:43 +000092
93static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
94 int count) {
95 REPORTER_ASSERT(reporter, count == stack.getSaveCount());
96
97 SkClipStack::B2FIter iter(stack);
98 int counter = 0;
99 while (iter.next()) {
100 counter += 1;
101 }
102 REPORTER_ASSERT(reporter, count == counter);
103}
104
105static void TestClipStack(skiatest::Reporter* reporter) {
106 SkClipStack stack;
107
108 assert_count(reporter, stack, 0);
109
110 static const SkIRect gRects[] = {
111 { 0, 0, 100, 100 },
112 { 25, 25, 125, 125 },
113 { 0, 0, 1000, 1000 },
114 { 0, 0, 75, 75 }
115 };
116 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
reed@google.comd9f2dea2011-10-12 14:43:27 +0000117 stack.clipDevRect(gRects[i], SkRegion::kIntersect_Op);
reed@google.combdee9fc2011-02-22 20:17:43 +0000118 }
119
120 // all of the above rects should have been intersected, leaving only 1 rect
121 SkClipStack::B2FIter iter(stack);
122 const SkClipStack::B2FIter::Clip* clip = iter.next();
epoger@google.com2047f002011-05-17 17:36:59 +0000123 SkRect answer;
124 answer.iset(25, 25, 75, 75);
reed@google.combdee9fc2011-02-22 20:17:43 +0000125
126 REPORTER_ASSERT(reporter, clip);
127 REPORTER_ASSERT(reporter, clip->fRect);
128 REPORTER_ASSERT(reporter, !clip->fPath);
129 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
130 REPORTER_ASSERT(reporter, *clip->fRect == answer);
131 // now check that we only had one in our iterator
132 REPORTER_ASSERT(reporter, !iter.next());
133
134 stack.reset();
135 assert_count(reporter, stack, 0);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000136
137 test_assign_and_comparison(reporter);
reed@google.combdee9fc2011-02-22 20:17:43 +0000138}
139
140#include "TestClassDef.h"
141DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)