blob: 043f63465ab18ec4410f2a5b56e478d3987d2807 [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) {
John Stiles7bf79992021-06-25 11:05:20 -0400103 SkDebugf("----- [%zu] %g %g -> %g %g\n",
104 i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
reed@android.coma3d90102009-11-30 12:48:33 +0000105 }
106 REPORTER_ASSERT(reporter, !valid);
107 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
reed@android.coma3d90102009-11-30 12:48:33 +0000109 static const SkPoint gFull[] = {
110 // diagonals, chords
111 { L, T }, { R, B },
112 { L, B }, { R, T },
113 { CX, T }, { CX, B },
114 { L, CY }, { R, CY },
115 { CX, T }, { R, CY },
116 { CX, T }, { L, CY },
117 { L, CY }, { CX, B },
118 { R, CY }, { CX, B },
119 // edges
120 { L, T }, { L, B },
121 { R, T }, { R, B },
122 { L, T }, { R, T },
123 { L, B }, { R, B },
124 };
125 for (i = 0; i < SK_ARRAY_COUNT(gFull); i += 2) {
126 bool valid = SkLineClipper::IntersectLine(&gFull[i], gR, dst);
John Stilesc1c3c6d2020-08-15 23:22:53 -0400127 if (!valid || 0 != memcmp(&gFull[i], dst, sizeof(dst))) {
John Stiles7bf79992021-06-25 11:05:20 -0400128 SkDebugf("++++ [%zu] %g %g -> %g %g\n",
129 i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
reed@android.coma3d90102009-11-30 12:48:33 +0000130 }
131 REPORTER_ASSERT(reporter, valid && !memcmp(&gFull[i], dst, sizeof(dst)));
132 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000133
reed@android.coma3d90102009-11-30 12:48:33 +0000134 static const SkPoint gPartial[] = {
135 { L - 10, CY }, { CX, CY }, { L, CY }, { CX, CY },
136 { CX, T - 10 }, { CX, CY }, { CX, T }, { CX, CY },
137 { R + 10, CY }, { CX, CY }, { R, CY }, { CX, CY },
138 { CX, B + 10 }, { CX, CY }, { CX, B }, { CX, CY },
139 // extended edges
140 { L, T - 10 }, { L, B + 10 }, { L, T }, { L, B },
141 { R, T - 10 }, { R, B + 10 }, { R, T }, { R, B },
142 { L - 10, T }, { R + 10, T }, { L, T }, { R, T },
143 { L - 10, B }, { R + 10, B }, { L, B }, { R, B },
144 };
145 for (i = 0; i < SK_ARRAY_COUNT(gPartial); i += 4) {
146 bool valid = SkLineClipper::IntersectLine(&gPartial[i], gR, dst);
John Stilesc1c3c6d2020-08-15 23:22:53 -0400147 if (!valid || 0 != memcmp(&gPartial[i+2], dst, sizeof(dst))) {
John Stiles7bf79992021-06-25 11:05:20 -0400148 SkDebugf("++++ [%zu] %g %g -> %g %g\n",
149 i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
reed@android.coma3d90102009-11-30 12:48:33 +0000150 }
151 REPORTER_ASSERT(reporter, valid &&
152 !memcmp(&gPartial[i+2], dst, sizeof(dst)));
153 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
reed@android.coma3d90102009-11-30 12:48:33 +0000155}
156
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000157DEF_TEST(Clipper, reporter) {
reed@android.coma3d90102009-11-30 12:48:33 +0000158 test_intersectline(reporter);
sugoi@google.com54f0d1b2013-02-27 19:17:41 +0000159 test_edgeclipper();
reed@google.com0a072652012-03-12 21:11:18 +0000160 test_hairclipping(reporter);
reed@android.coma3d90102009-11-30 12:48:33 +0000161}
Mike Reed30549892018-08-24 17:11:28 -0400162
Mike Reed30549892018-08-24 17:11:28 -0400163DEF_TEST(LineClipper_skbug_7981, r) {
164 SkPoint src[] = {{ -5.77698802E+17f, -1.81758057E+23f}, {38127, 2}};
165 SkPoint dst[2];
166 SkRect clip = { -32767, -32767, 32767, 32767 };
167
168 SkLineClipper::IntersectLine(src, clip, dst);
169}
170