blob: 6f2e0af4f2113b7c0bddae10ad5b35506838b4b4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +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 */
reed@android.coma3d90102009-11-30 12:48:33 +00007
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
reed@google.com0a072652012-03-12 21:11:18 +000010#include "SkCanvas.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000011#include "SkEdgeClipper.h"
12#include "SkLineClipper.h"
13#include "SkPath.h"
14
reed@google.com0a072652012-03-12 21:11:18 +000015static void test_hairclipping(skiatest::Reporter* reporter) {
16 SkBitmap bm;
17 bm.setConfig(SkBitmap::kARGB_8888_Config, 4, 4);
18 bm.allocPixels();
19 bm.eraseColor(SK_ColorWHITE);
rmistry@google.comd6176b02012-08-23 18:14:13 +000020
reed@google.com0a072652012-03-12 21:11:18 +000021 SkPaint paint;
22 paint.setAntiAlias(true);
23
24 SkCanvas canvas(bm);
reed@google.com470f07f2012-03-12 21:31:00 +000025 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(4), SkIntToScalar(2)));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000026 canvas.drawLine(1.5f, 1.5f,
27 3.5f, 3.5f, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000028
reed@google.com0a072652012-03-12 21:11:18 +000029 /**
30 * We had a bug where we misinterpreted the bottom of the clip, and
31 * would draw another pixel (to the right in this case) on the same
32 * last scanline. i.e. we would draw to [2,1], even though this hairline
33 * should just draw to [1,1], [2,2], [3,3] modulo the clip.
34 *
35 * The result of this entire draw should be that we only draw to [1,1]
36 *
37 * Fixed in rev. 3366
38 */
39 for (int y = 0; y < 4; ++y) {
40 for (int x = 0; x < 4; ++x) {
41 bool nonWhite = (1 == y) && (1 == x);
42 SkPMColor c = *bm.getAddr32(x, y);
43 if (nonWhite) {
44 REPORTER_ASSERT(reporter, 0xFFFFFFFF != c);
45 } else {
46 REPORTER_ASSERT(reporter, 0xFFFFFFFF == c);
47 }
48 }
49 }
50}
51
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000052static void test_edgeclipper() {
reed@google.com6da3d172012-01-11 16:41:26 +000053 SkEdgeClipper clipper;
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@google.com6da3d172012-01-11 16:41:26 +000055 const SkPoint pts[] = {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000056 { 3.0995476e+010f, 42.929779f },
57 { -3.0995163e+010f, 51.050385f },
58 { -3.0995157e+010f, 51.050392f },
59 { -3.0995134e+010f, 51.050400f },
reed@google.com6da3d172012-01-11 16:41:26 +000060 };
61
epoger@google.comdc7a5062012-01-11 20:43:29 +000062 const SkRect clip = { 0, 0, SkIntToScalar(300), SkIntToScalar(200) };
reed@google.com6da3d172012-01-11 16:41:26 +000063
64 // this should not assert, even though our choppers do a poor numerical
65 // job when computing their t values.
66 // http://code.google.com/p/skia/issues/detail?id=444
67 clipper.clipCubic(pts, clip);
68}
69
reed@android.coma3d90102009-11-30 12:48:33 +000070static void test_intersectline(skiatest::Reporter* reporter) {
71 static const SkScalar L = 0;
72 static const SkScalar T = 0;
73 static const SkScalar R = SkIntToScalar(100);
74 static const SkScalar B = SkIntToScalar(100);
75 static const SkScalar CX = SkScalarHalf(L + R);
76 static const SkScalar CY = SkScalarHalf(T + B);
77 static const SkRect gR = { L, T, R, B };
78
79 size_t i;
80 SkPoint dst[2];
81
82 static const SkPoint gEmpty[] = {
83 // sides
84 { L, CY }, { L - 10, CY },
85 { R, CY }, { R + 10, CY },
86 { CX, T }, { CX, T - 10 },
87 { CX, B }, { CX, B + 10 },
88 // corners
89 { L, T }, { L - 10, T - 10 },
90 { L, B }, { L - 10, B + 10 },
91 { R, T }, { R + 10, T - 10 },
92 { R, B }, { R + 10, B + 10 },
93 };
94 for (i = 0; i < SK_ARRAY_COUNT(gEmpty); i += 2) {
95 bool valid = SkLineClipper::IntersectLine(&gEmpty[i], gR, dst);
96 if (valid) {
97 SkDebugf("----- [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
98 }
99 REPORTER_ASSERT(reporter, !valid);
100 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000101
reed@android.coma3d90102009-11-30 12:48:33 +0000102 static const SkPoint gFull[] = {
103 // diagonals, chords
104 { L, T }, { R, B },
105 { L, B }, { R, T },
106 { CX, T }, { CX, B },
107 { L, CY }, { R, CY },
108 { CX, T }, { R, CY },
109 { CX, T }, { L, CY },
110 { L, CY }, { CX, B },
111 { R, CY }, { CX, B },
112 // edges
113 { L, T }, { L, B },
114 { R, T }, { R, B },
115 { L, T }, { R, T },
116 { L, B }, { R, B },
117 };
118 for (i = 0; i < SK_ARRAY_COUNT(gFull); i += 2) {
119 bool valid = SkLineClipper::IntersectLine(&gFull[i], gR, dst);
120 if (!valid || memcmp(&gFull[i], dst, sizeof(dst))) {
121 SkDebugf("++++ [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
122 }
123 REPORTER_ASSERT(reporter, valid && !memcmp(&gFull[i], dst, sizeof(dst)));
124 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
reed@android.coma3d90102009-11-30 12:48:33 +0000126 static const SkPoint gPartial[] = {
127 { L - 10, CY }, { CX, CY }, { L, CY }, { CX, CY },
128 { CX, T - 10 }, { CX, CY }, { CX, T }, { CX, CY },
129 { R + 10, CY }, { CX, CY }, { R, CY }, { CX, CY },
130 { CX, B + 10 }, { CX, CY }, { CX, B }, { CX, CY },
131 // extended edges
132 { L, T - 10 }, { L, B + 10 }, { L, T }, { L, B },
133 { R, T - 10 }, { R, B + 10 }, { R, T }, { R, B },
134 { L - 10, T }, { R + 10, T }, { L, T }, { R, T },
135 { L - 10, B }, { R + 10, B }, { L, B }, { R, B },
136 };
137 for (i = 0; i < SK_ARRAY_COUNT(gPartial); i += 4) {
138 bool valid = SkLineClipper::IntersectLine(&gPartial[i], gR, dst);
139 if (!valid || memcmp(&gPartial[i+2], dst, sizeof(dst))) {
140 SkDebugf("++++ [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
141 }
142 REPORTER_ASSERT(reporter, valid &&
143 !memcmp(&gPartial[i+2], dst, sizeof(dst)));
144 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145
reed@android.coma3d90102009-11-30 12:48:33 +0000146}
147
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000148DEF_TEST(Clipper, reporter) {
reed@android.coma3d90102009-11-30 12:48:33 +0000149 test_intersectline(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000150 test_edgeclipper();
reed@google.com0a072652012-03-12 21:11:18 +0000151 test_hairclipping(reporter);
reed@android.coma3d90102009-11-30 12:48:33 +0000152}