• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "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"
19 
20 #include <cstring>
21 
test_hairclipping(skiatest::Reporter * reporter)22 static void test_hairclipping(skiatest::Reporter* reporter) {
23     SkBitmap bm;
24     bm.allocN32Pixels(4, 4);
25     bm.eraseColor(SK_ColorWHITE);
26 
27     SkPaint paint;
28     paint.setAntiAlias(true);
29 
30     SkCanvas canvas(bm);
31     canvas.clipRect(SkRect::MakeWH(SkIntToScalar(4), SkIntToScalar(2)));
32     canvas.drawLine(1.5f, 1.5f,
33                     3.5f, 3.5f, paint);
34 
35     /**
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 
test_edgeclipper()58 static void test_edgeclipper() {
59     SkEdgeClipper clipper(false);
60 
61     const SkPoint pts[] = {
62         { 3.0995476e+010f,  42.929779f },
63         { -3.0995163e+010f, 51.050385f },
64         { -3.0995157e+010f, 51.050392f },
65         { -3.0995134e+010f, 51.050400f },
66     };
67 
68     const SkRect clip = { 0, 0, SkIntToScalar(300), SkIntToScalar(200) };
69 
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 
test_intersectline(skiatest::Reporter * reporter)76 static 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("----- [%zu] %g %g -> %g %g\n",
104                      i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
105         }
106         REPORTER_ASSERT(reporter, !valid);
107     }
108 
109     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);
127         if (!valid || 0 != memcmp(&gFull[i], dst, sizeof(dst))) {
128             SkDebugf("++++ [%zu] %g %g -> %g %g\n",
129                      i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
130         }
131         REPORTER_ASSERT(reporter, valid && !memcmp(&gFull[i], dst, sizeof(dst)));
132     }
133 
134     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);
147         if (!valid || 0 != memcmp(&gPartial[i+2], dst, sizeof(dst))) {
148             SkDebugf("++++ [%zu] %g %g -> %g %g\n",
149                      i/2, dst[0].fX, dst[0].fY, dst[1].fX, dst[1].fY);
150         }
151         REPORTER_ASSERT(reporter, valid &&
152                                   !memcmp(&gPartial[i+2], dst, sizeof(dst)));
153     }
154 
155 }
156 
DEF_TEST(Clipper,reporter)157 DEF_TEST(Clipper, reporter) {
158     test_intersectline(reporter);
159     test_edgeclipper();
160     test_hairclipping(reporter);
161 }
162 
DEF_TEST(LineClipper_skbug_7981,r)163 DEF_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 
171