blob: d518c8d88764db58e602510246a8b790f63cbf45 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPoint.h"
13#include "include/core/SkRect.h"
14#include "include/core/SkScalar.h"
15#include "include/core/SkTypes.h"
16#include "src/core/SkEdgeClipper.h"
17#include "src/core/SkLineClipper.h"
18#include "tests/Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000019
Ben Wagnerb607a8f2018-03-12 13:46:21 -040020#include <cstring>
21
reed@google.com0a072652012-03-12 21:11:18 +000022static void test_hairclipping(skiatest::Reporter* reporter) {
23 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000024 bm.allocN32Pixels(4, 4);
reed@google.com0a072652012-03-12 21:11:18 +000025 bm.eraseColor(SK_ColorWHITE);
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
reed@google.com0a072652012-03-12 21:11:18 +000027 SkPaint paint;
28 paint.setAntiAlias(true);
29
30 SkCanvas canvas(bm);
reed@google.com470f07f2012-03-12 21:31:00 +000031 canvas.clipRect(SkRect::MakeWH(SkIntToScalar(4), SkIntToScalar(2)));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000032 canvas.drawLine(1.5f, 1.5f,
33 3.5f, 3.5f, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000034
reed@google.com0a072652012-03-12 21:11:18 +000035 /**
36 * We had a bug where we misinterpreted the bottom of the clip, and
37 * would draw another pixel (to the right in this case) on the same
38 * last scanline. i.e. we would draw to [2,1], even though this hairline
39 * should just draw to [1,1], [2,2], [3,3] modulo the clip.
40 *
41 * The result of this entire draw should be that we only draw to [1,1]
42 *
43 * Fixed in rev. 3366
44 */
45 for (int y = 0; y < 4; ++y) {
46 for (int x = 0; x < 4; ++x) {
47 bool nonWhite = (1 == y) && (1 == x);
48 SkPMColor c = *bm.getAddr32(x, y);
49 if (nonWhite) {
50 REPORTER_ASSERT(reporter, 0xFFFFFFFF != c);
51 } else {
52 REPORTER_ASSERT(reporter, 0xFFFFFFFF == c);
53 }
54 }
55 }
56}
57
sugoi@google.com54f0d1b2013-02-27 19:17:41 +000058static void test_edgeclipper() {
reed31223e02015-02-09 08:33:07 -080059 SkEdgeClipper clipper(false);
rmistry@google.comd6176b02012-08-23 18:14:13 +000060
reed@google.com6da3d172012-01-11 16:41:26 +000061 const SkPoint pts[] = {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000062 { 3.0995476e+010f, 42.929779f },
63 { -3.0995163e+010f, 51.050385f },
64 { -3.0995157e+010f, 51.050392f },
65 { -3.0995134e+010f, 51.050400f },
reed@google.com6da3d172012-01-11 16:41:26 +000066 };
67
epoger@google.comdc7a5062012-01-11 20:43:29 +000068 const SkRect clip = { 0, 0, SkIntToScalar(300), SkIntToScalar(200) };
reed@google.com6da3d172012-01-11 16:41:26 +000069
70 // this should not assert, even though our choppers do a poor numerical
71 // job when computing their t values.
72 // http://code.google.com/p/skia/issues/detail?id=444
73 clipper.clipCubic(pts, clip);
74}
75
reed@android.coma3d90102009-11-30 12:48:33 +000076static void test_intersectline(skiatest::Reporter* reporter) {
77 static const SkScalar L = 0;
78 static const SkScalar T = 0;
79 static const SkScalar R = SkIntToScalar(100);
80 static const SkScalar B = SkIntToScalar(100);
81 static const SkScalar CX = SkScalarHalf(L + R);
82 static const SkScalar CY = SkScalarHalf(T + B);
83 static const SkRect gR = { L, T, R, B };
84
85 size_t i;
86 SkPoint dst[2];
87
88 static const SkPoint gEmpty[] = {
89 // sides
90 { L, CY }, { L - 10, CY },
91 { R, CY }, { R + 10, CY },
92 { CX, T }, { CX, T - 10 },
93 { CX, B }, { CX, B + 10 },
94 // corners
95 { L, T }, { L - 10, T - 10 },
96 { L, B }, { L - 10, B + 10 },
97 { R, T }, { R + 10, T - 10 },
98 { R, B }, { R + 10, B + 10 },
99 };
100 for (i = 0; i < SK_ARRAY_COUNT(gEmpty); i += 2) {
101 bool valid = SkLineClipper::IntersectLine(&gEmpty[i], gR, dst);
102 if (valid) {
103 SkDebugf("----- [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
104 }
105 REPORTER_ASSERT(reporter, !valid);
106 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
reed@android.coma3d90102009-11-30 12:48:33 +0000108 static const SkPoint gFull[] = {
109 // diagonals, chords
110 { L, T }, { R, B },
111 { L, B }, { R, T },
112 { CX, T }, { CX, B },
113 { L, CY }, { R, CY },
114 { CX, T }, { R, CY },
115 { CX, T }, { L, CY },
116 { L, CY }, { CX, B },
117 { R, CY }, { CX, B },
118 // edges
119 { L, T }, { L, B },
120 { R, T }, { R, B },
121 { L, T }, { R, T },
122 { L, B }, { R, B },
123 };
124 for (i = 0; i < SK_ARRAY_COUNT(gFull); i += 2) {
125 bool valid = SkLineClipper::IntersectLine(&gFull[i], gR, dst);
126 if (!valid || memcmp(&gFull[i], dst, sizeof(dst))) {
127 SkDebugf("++++ [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
128 }
129 REPORTER_ASSERT(reporter, valid && !memcmp(&gFull[i], dst, sizeof(dst)));
130 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131
reed@android.coma3d90102009-11-30 12:48:33 +0000132 static const SkPoint gPartial[] = {
133 { L - 10, CY }, { CX, CY }, { L, CY }, { CX, CY },
134 { CX, T - 10 }, { CX, CY }, { CX, T }, { CX, CY },
135 { R + 10, CY }, { CX, CY }, { R, CY }, { CX, CY },
136 { CX, B + 10 }, { CX, CY }, { CX, B }, { CX, CY },
137 // extended edges
138 { L, T - 10 }, { L, B + 10 }, { L, T }, { L, B },
139 { R, T - 10 }, { R, B + 10 }, { R, T }, { R, B },
140 { L - 10, T }, { R + 10, T }, { L, T }, { R, T },
141 { L - 10, B }, { R + 10, B }, { L, B }, { R, B },
142 };
143 for (i = 0; i < SK_ARRAY_COUNT(gPartial); i += 4) {
144 bool valid = SkLineClipper::IntersectLine(&gPartial[i], gR, dst);
145 if (!valid || memcmp(&gPartial[i+2], dst, sizeof(dst))) {
146 SkDebugf("++++ [%d] %g %g -> %g %g\n", i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
147 }
148 REPORTER_ASSERT(reporter, valid &&
149 !memcmp(&gPartial[i+2], dst, sizeof(dst)));
150 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000151
reed@android.coma3d90102009-11-30 12:48:33 +0000152}
153
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000154DEF_TEST(Clipper, reporter) {
reed@android.coma3d90102009-11-30 12:48:33 +0000155 test_intersectline(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000156 test_edgeclipper();
reed@google.com0a072652012-03-12 21:11:18 +0000157 test_hairclipping(reporter);
reed@android.coma3d90102009-11-30 12:48:33 +0000158}
Mike Reed30549892018-08-24 17:11:28 -0400159
Mike Reed30549892018-08-24 17:11:28 -0400160DEF_TEST(LineClipper_skbug_7981, r) {
161 SkPoint src[] = {{ -5.77698802E+17f, -1.81758057E+23f}, {38127, 2}};
162 SkPoint dst[2];
163 SkRect clip = { -32767, -32767, 32767, 32767 };
164
165 SkLineClipper::IntersectLine(src, clip, dst);
166}
167