• 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("----- [%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     }
107 
108     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     }
131 
132     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     }
151 
152 }
153 
DEF_TEST(Clipper,reporter)154 DEF_TEST(Clipper, reporter) {
155     test_intersectline(reporter);
156     test_edgeclipper();
157     test_hairclipping(reporter);
158 }
159 
DEF_TEST(LineClipper_skbug_7981,r)160 DEF_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 
168