blob: f56645e006177a67b7ef031001c06720ce38478a [file] [log] [blame]
reed@google.com209c4152011-10-26 15:03:48 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Test.h"
9#include "SkAAClip.h"
10#include "SkPath.h"
reed@google.comd8676d22011-10-26 18:01:25 +000011#include "SkRandom.h"
12
13static const SkRegion::Op gRgnOps[] = {
reed@google.comc9041912011-10-27 16:58:46 +000014 SkRegion::kDifference_Op,
reed@google.comd8676d22011-10-26 18:01:25 +000015 SkRegion::kIntersect_Op,
16 SkRegion::kUnion_Op,
17 SkRegion::kXOR_Op,
reed@google.comc9041912011-10-27 16:58:46 +000018 SkRegion::kReverseDifference_Op,
reed@google.comd8676d22011-10-26 18:01:25 +000019 SkRegion::kReplace_Op
20};
21
22static const char* gRgnOpNames[] = {
reed@google.comc9041912011-10-27 16:58:46 +000023 "DIFF", "INTERSECT", "UNION", "XOR", "REVERSE_DIFF", "REPLACE"
reed@google.comd8676d22011-10-26 18:01:25 +000024};
reed@google.com209c4152011-10-26 15:03:48 +000025
reed@google.com12e15252011-10-26 15:19:36 +000026static void imoveTo(SkPath& path, int x, int y) {
27 path.moveTo(SkIntToScalar(x), SkIntToScalar(y));
28}
29
30static void icubicTo(SkPath& path, int x0, int y0, int x1, int y1, int x2, int y2) {
31 path.cubicTo(SkIntToScalar(x0), SkIntToScalar(y0),
32 SkIntToScalar(x1), SkIntToScalar(y1),
33 SkIntToScalar(x2), SkIntToScalar(y2));
34}
35
reed@google.comd8676d22011-10-26 18:01:25 +000036static void test_path_bounds(skiatest::Reporter* reporter) {
reed@google.com209c4152011-10-26 15:03:48 +000037 SkPath path;
38 SkAAClip clip;
39 const int height = 40;
40 const SkScalar sheight = SkIntToScalar(height);
41
42 path.addOval(SkRect::MakeWH(sheight, sheight));
43 REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
44 clip.setPath(path, NULL, true);
45 REPORTER_ASSERT(reporter, height == clip.getBounds().height());
46
47 // this is the trimmed height of this cubic (with aa). The critical thing
48 // for this test is that it is less than height, which represents just
49 // the bounds of the path's control-points.
50 //
51 // This used to fail until we tracked the MinY in the BuilderBlitter.
52 //
53 const int teardrop_height = 12;
54 path.reset();
reed@google.com12e15252011-10-26 15:19:36 +000055 imoveTo(path, 0, 20);
56 icubicTo(path, 40, 40, 40, 0, 0, 20);
reed@google.com209c4152011-10-26 15:03:48 +000057 REPORTER_ASSERT(reporter, sheight == path.getBounds().height());
58 clip.setPath(path, NULL, true);
59 REPORTER_ASSERT(reporter, teardrop_height == clip.getBounds().height());
60}
61
reed@google.comd8676d22011-10-26 18:01:25 +000062static void test_empty(skiatest::Reporter* reporter) {
63 SkAAClip clip0, clip1;
64
65 REPORTER_ASSERT(reporter, clip0.isEmpty());
66 REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
67 REPORTER_ASSERT(reporter, clip1 == clip0);
68
69 clip0.translate(10, 10); // should have no effect on empty
70 REPORTER_ASSERT(reporter, clip0.isEmpty());
71 REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
72 REPORTER_ASSERT(reporter, clip1 == clip0);
73
74 SkIRect r = { 10, 10, 40, 50 };
75 clip0.setRect(r);
76 REPORTER_ASSERT(reporter, !clip0.isEmpty());
77 REPORTER_ASSERT(reporter, !clip0.getBounds().isEmpty());
78 REPORTER_ASSERT(reporter, clip0 != clip1);
79 REPORTER_ASSERT(reporter, clip0.getBounds() == r);
80
81 clip0.setEmpty();
82 REPORTER_ASSERT(reporter, clip0.isEmpty());
83 REPORTER_ASSERT(reporter, clip0.getBounds().isEmpty());
84 REPORTER_ASSERT(reporter, clip1 == clip0);
85
86 SkMask mask;
87 mask.fImage = NULL;
88 clip0.copyToMask(&mask);
89 REPORTER_ASSERT(reporter, NULL == mask.fImage);
90 REPORTER_ASSERT(reporter, mask.fBounds.isEmpty());
91}
92
93static void rand_irect(SkIRect* r, int N, SkRandom& rand) {
94 r->setXYWH(0, 0, rand.nextU() % N, rand.nextU() % N);
95 int dx = rand.nextU() % (2*N);
96 int dy = rand.nextU() % (2*N);
97 // use int dx,dy to make the subtract be signed
98 r->offset(N - dx, N - dy);
99}
100
101static void test_irect(skiatest::Reporter* reporter) {
102 SkRandom rand;
103
reed@google.comc9041912011-10-27 16:58:46 +0000104 for (int i = 0; i < 10000; i++) {
reed@google.comd8676d22011-10-26 18:01:25 +0000105 SkAAClip clip0, clip1;
106 SkRegion rgn0, rgn1;
107 SkIRect r0, r1;
108
109 rand_irect(&r0, 10, rand);
110 rand_irect(&r1, 10, rand);
111 clip0.setRect(r0);
112 clip1.setRect(r1);
113 rgn0.setRect(r0);
114 rgn1.setRect(r1);
115 for (size_t j = 0; j < SK_ARRAY_COUNT(gRgnOps); ++j) {
116 SkRegion::Op op = gRgnOps[j];
117 SkAAClip clip2;
118 SkRegion rgn2;
119 bool nonEmptyAA = clip2.op(clip0, clip1, op);
120 bool nonEmptyBW = rgn2.op(rgn0, rgn1, op);
121 if (nonEmptyAA != nonEmptyBW || clip2.getBounds() != rgn2.getBounds()) {
122 SkDebugf("[%d %d %d %d] %s [%d %d %d %d] = BW:[%d %d %d %d] AA:[%d %d %d %d]\n",
123 r0.fLeft, r0.fTop, r0.right(), r0.bottom(),
124 gRgnOpNames[j],
125 r1.fLeft, r1.fTop, r1.right(), r1.bottom(),
126 rgn2.getBounds().fLeft, rgn2.getBounds().fTop,
127 rgn2.getBounds().right(), rgn2.getBounds().bottom(),
128 clip2.getBounds().fLeft, clip2.getBounds().fTop,
129 clip2.getBounds().right(), clip2.getBounds().bottom());
130 }
131 REPORTER_ASSERT(reporter, nonEmptyAA == nonEmptyBW);
132 REPORTER_ASSERT(reporter, clip2.getBounds() == rgn2.getBounds());
133 }
134 }
135}
136
reed@google.com209c4152011-10-26 15:03:48 +0000137static void TestAAClip(skiatest::Reporter* reporter) {
reed@google.comd8676d22011-10-26 18:01:25 +0000138 test_empty(reporter);
139 test_path_bounds(reporter);
140 test_irect(reporter);
reed@google.com209c4152011-10-26 15:03:48 +0000141}
142
143#include "TestClassDef.h"
144DEFINE_TESTCLASS("AAClip", AAClipTestClass, TestAAClip)