ClipStackTest.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
8bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com#include "Test.h"
9bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com#include "SkClipStack.h"
101e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org#include "SkPath.h"
111e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org#include "SkRect.h"
121e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
131e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.orgstatic void test_assign_and_comparison(skiatest::Reporter* reporter) {
141e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    SkClipStack s;
151e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
161e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Build up a clip stack with a path, an empty clip, and a rect.
171e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
181e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    SkPath p;
191e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    p.moveTo(5, 6);
201e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    p.lineTo(7, 8);
211e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    p.lineTo(5, 9);
221e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    p.close();
231e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevPath(p);
241e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
251e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
261e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
271e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r);
281e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    r = SkRect::MakeLTRB(10, 11, 12, 13);
291e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r);
301e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
311e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
321e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    r = SkRect::MakeLTRB(14, 15, 16, 17);
331e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r, SkRegion::kUnion_Op);
341e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
351e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that assignment works.
361e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    SkClipStack copy = s;
371e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s == copy);
381e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
391e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that different save levels triggers not equal.
401e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
411e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s != copy);
421e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
431e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that an equal, but not copied version is equal.
441e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
451e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    r = SkRect::MakeLTRB(14, 15, 16, 17);
461e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r, SkRegion::kUnion_Op);
471e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s == copy);
481e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
491e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that a different op on one level triggers not equal.
501e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
511e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
521e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    r = SkRect::MakeLTRB(14, 15, 16, 17);
531e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r);
541e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s != copy);
551e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
561e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that different state (clip type) triggers not equal.
571e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
581e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
591e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    SkPath rp;
601e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    rp.addRect(r);
611e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevPath(rp, SkRegion::kUnion_Op);
621e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s != copy);
631e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
641e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that different rects triggers not equal.
651e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
661e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
671e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    r = SkRect::MakeLTRB(24, 25, 26, 27);
681e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevRect(r, SkRegion::kUnion_Op);
691e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s != copy);
701e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
711e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Sanity check
721e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
731e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    copy.restore();
741e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s == copy);
751e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
761e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    copy.restore();
771e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s == copy);
781e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
791e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    // Test that different paths triggers not equal.
801e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.restore();
811e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.save();
821e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    p.addRect(r);
831e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    s.clipDevPath(p);
841e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    REPORTER_ASSERT(reporter, s != copy);
851e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org}
86bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
87bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.comstatic void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
88bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com                         int count) {
89bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, count == stack.getSaveCount());
90bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
91bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    SkClipStack::B2FIter iter(stack);
92bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    int counter = 0;
93bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    while (iter.next()) {
94bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        counter += 1;
95bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    }
96bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, count == counter);
97bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com}
98bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
99bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.comstatic void TestClipStack(skiatest::Reporter* reporter) {
100bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    SkClipStack stack;
101bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
102bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    assert_count(reporter, stack, 0);
103bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
104bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    static const SkIRect gRects[] = {
105bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        { 0, 0, 100, 100 },
106bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        { 25, 25, 125, 125 },
107bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        { 0, 0, 1000, 1000 },
108bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        { 0, 0, 75, 75 }
109bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    };
110bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
111bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com        stack.clipDevRect(gRects[i]);
112bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    }
113bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
114bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    // all of the above rects should have been intersected, leaving only 1 rect
115bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    SkClipStack::B2FIter iter(stack);
116bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    const SkClipStack::B2FIter::Clip* clip = iter.next();
1172047f00e4698f83499ab91911999a65c21a951c9epoger@google.com    SkRect answer;
1182047f00e4698f83499ab91911999a65c21a951c9epoger@google.com    answer.iset(25, 25, 75, 75);
119bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
120bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, clip);
121bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, clip->fRect);
122bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, !clip->fPath);
123bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
124bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, *clip->fRect == answer);
125bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    // now check that we only had one in our iterator
126bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    REPORTER_ASSERT(reporter, !iter.next());
127bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
128bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    stack.reset();
129bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com    assert_count(reporter, stack, 0);
1301e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org
1311e1c36f4f89ad39e1d248edb745919e493242c68vandebo@chromium.org    test_assign_and_comparison(reporter);
132bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com}
133bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com
134bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.com#include "TestClassDef.h"
135bdee9fc778d4387d805d717f2cd7fc7074991fdbreed@google.comDEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)
136