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/SkPath.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSurface.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/SkFloatBits.h"
20 #include "src/core/SkCubicClipper.h"
21 #include "tests/Test.h"
22
23 // Currently the supersampler blitter uses int16_t for its index into an array
24 // the width of the clip. Test that we don't crash/assert if we try to draw
25 // with a device/clip that is larger.
test_giantClip()26 static void test_giantClip() {
27 SkBitmap bm;
28 bm.allocN32Pixels(64919, 1);
29 SkCanvas canvas(bm);
30 canvas.clear(SK_ColorTRANSPARENT);
31
32 SkPaint paint;
33 paint.setAntiAlias(true);
34 canvas.drawPath(SkPath::Polygon({{0,0}, {1,0}, {33,1}}, false), paint);
35 }
36
PrintCurve(const char * name,const SkPoint crv[4])37 static void PrintCurve(const char *name, const SkPoint crv[4]) {
38 SkDebugf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n",
39 name,
40 (float)crv[0].fX, (float)crv[0].fY,
41 (float)crv[1].fX, (float)crv[1].fY,
42 (float)crv[2].fX, (float)crv[2].fY,
43 (float)crv[3].fX, (float)crv[3].fY);
44
45 }
46
47
CurvesAreEqual(const SkPoint c0[4],const SkPoint c1[4],float tol)48 static bool CurvesAreEqual(const SkPoint c0[4],
49 const SkPoint c1[4],
50 float tol) {
51 for (int i = 0; i < 4; i++) {
52 if (SkScalarAbs(c0[i].fX - c1[i].fX) > tol ||
53 SkScalarAbs(c0[i].fY - c1[i].fY) > tol
54 ) {
55 PrintCurve("c0", c0);
56 PrintCurve("c1", c1);
57 return false;
58 }
59 }
60 return true;
61 }
62
63
SetCurve(float x0,float y0,float x1,float y1,float x2,float y2,float x3,float y3,SkPoint crv[4])64 static SkPoint* SetCurve(float x0, float y0,
65 float x1, float y1,
66 float x2, float y2,
67 float x3, float y3,
68 SkPoint crv[4]) {
69 crv[0].fX = x0; crv[0].fY = y0;
70 crv[1].fX = x1; crv[1].fY = y1;
71 crv[2].fX = x2; crv[2].fY = y2;
72 crv[3].fX = x3; crv[3].fY = y3;
73 return crv;
74 }
75
76
DEF_TEST(ClipCubic,reporter)77 DEF_TEST(ClipCubic, reporter) {
78 static SkPoint crv[4] = {
79 { SkIntToScalar(0), SkIntToScalar(0) },
80 { SkIntToScalar(2), SkIntToScalar(3) },
81 { SkIntToScalar(1), SkIntToScalar(10) },
82 { SkIntToScalar(4), SkIntToScalar(12) }
83 };
84
85 SkCubicClipper clipper;
86 SkPoint clipped[4], shouldbe[4];
87 SkIRect clipRect;
88 bool success;
89 const float tol = 1e-4f;
90
91 // Test no clip, with plenty of room.
92 clipRect.setLTRB(-2, -2, 6, 14);
93 clipper.setClip(clipRect);
94 success = clipper.clipCubic(crv, clipped);
95 REPORTER_ASSERT(reporter, success == true);
96 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
97 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
98
99 // Test no clip, touching first point.
100 clipRect.setLTRB(-2, 0, 6, 14);
101 clipper.setClip(clipRect);
102 success = clipper.clipCubic(crv, clipped);
103 REPORTER_ASSERT(reporter, success == true);
104 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
105 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
106
107 // Test no clip, touching last point.
108 clipRect.setLTRB(-2, -2, 6, 12);
109 clipper.setClip(clipRect);
110 success = clipper.clipCubic(crv, clipped);
111 REPORTER_ASSERT(reporter, success == true);
112 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
113 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
114
115 // Test all clip.
116 clipRect.setLTRB(-2, 14, 6, 20);
117 clipper.setClip(clipRect);
118 success = clipper.clipCubic(crv, clipped);
119 REPORTER_ASSERT(reporter, success == false);
120
121 // Test clip at 1.
122 clipRect.setLTRB(-2, 1, 6, 14);
123 clipper.setClip(clipRect);
124 success = clipper.clipCubic(crv, clipped);
125 REPORTER_ASSERT(reporter, success == true);
126 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
127 0.5126125216f, 1,
128 1.841195941f, 4.337081432f,
129 1.297019958f, 10.19801331f,
130 4, 12,
131 shouldbe), tol));
132
133 // Test clip at 2.
134 clipRect.setLTRB(-2, 2, 6, 14);
135 clipper.setClip(clipRect);
136 success = clipper.clipCubic(crv, clipped);
137 REPORTER_ASSERT(reporter, success == true);
138 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
139 00.8412352204f, 2,
140 1.767683744f, 5.400758266f,
141 1.55052948f, 10.36701965f,
142 4, 12,
143 shouldbe), tol));
144
145 // Test clip at 11.
146 clipRect.setLTRB(-2, -2, 6, 11);
147 clipper.setClip(clipRect);
148 success = clipper.clipCubic(crv, clipped);
149 REPORTER_ASSERT(reporter, success == true);
150 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
151 0, 0,
152 1.742904663f, 2.614356995f,
153 1.207521796f, 8.266430855f,
154 3.026495695f, 11,
155 shouldbe), tol));
156
157 // Test clip at 10.
158 clipRect.setLTRB(-2, -2, 6, 10);
159 clipper.setClip(clipRect);
160 success = clipper.clipCubic(crv, clipped);
161 REPORTER_ASSERT(reporter, success == true);
162 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
163 0, 0,
164 1.551193237f, 2.326789856f,
165 1.297736168f, 7.059780121f,
166 2.505550385f, 10,
167 shouldbe), tol));
168
169 test_giantClip();
170 }
171
DEF_TEST(test_fuzz_crbug_698714,reporter)172 DEF_TEST(test_fuzz_crbug_698714, reporter) {
173 auto surface(SkSurface::MakeRasterN32Premul(500, 500));
174 SkCanvas* canvas = surface->getCanvas();
175 SkPaint paint;
176 paint.setAntiAlias(true);
177 SkPath path;
178 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0,0
179 path.lineTo(SkBits2Float(0x43434343), SkBits2Float(0x43430143)); //195.263f, 195.005f
180 path.lineTo(SkBits2Float(0x43434343), SkBits2Float(0x43434343)); //195.263f, 195.263f
181 path.lineTo(SkBits2Float(0xb5434343), SkBits2Float(0x434300be)); //-7.2741e-07f, 195.003f
182 // 195.263f, 195.263f, -1.16387e-05f, 3.58641e-38f, 3.85088e-29f,1.86082e-39f
183 path.cubicTo(SkBits2Float(0x43434343), SkBits2Float(0x43434341),
184 SkBits2Float(0xb74343bd), SkBits2Float(0x01434343),
185 SkBits2Float(0x10434343), SkBits2Float(0x00144332));
186 // 4.11823e-38f, 195.263f, 195.263f, 195.263f, -7.2741e-07f, 195.263f
187 path.cubicTo(SkBits2Float(0x016037c0), SkBits2Float(0x43434343),
188 SkBits2Float(0x43434343), SkBits2Float(0x43434343),
189 SkBits2Float(0xb5434343), SkBits2Float(0x43434343));
190 // 195.263f, 195.263f, -1.16387e-05f, 3.58641e-38f, 195.263f, -2
191 path.cubicTo(SkBits2Float(0x43434344), SkBits2Float(0x43434341),
192 SkBits2Float(0xb74343bd), SkBits2Float(0x01434343),
193 SkBits2Float(0x43434343), SkBits2Float(0xc0000014));
194 // -5.87228e+06f, 3.7773e-07f, 3.60231e-13f, -6.64511e+06f,2.77692e-15f, 2.48803e-15f
195 path.cubicTo(SkBits2Float(0xcab33535), SkBits2Float(0x34cacaca),
196 SkBits2Float(0x2acacaca), SkBits2Float(0xcacacae3),
197 SkBits2Float(0x27481927), SkBits2Float(0x27334805));
198 path.lineTo(SkBits2Float(0xb5434343), SkBits2Float(0x43434343)); //-7.2741e-07f, 195.263f
199 // 195.263f, 195.263f, -1.16387e-05f, 195.212f, 195.263f, -2
200 path.cubicTo(SkBits2Float(0x43434343), SkBits2Float(0x43434341),
201 SkBits2Float(0xb74343b9), SkBits2Float(0x43433643),
202 SkBits2Float(0x43434343), SkBits2Float(0xc0000014));
203 path.lineTo(SkBits2Float(0xc7004343), SkBits2Float(0x27480527)); //-32835.3f, 2.77584e-15f
204 path.lineTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000)); // 0,0
205 path.close();
206 canvas->clipRect({0, 0, 65, 202});
207 canvas->drawPath(path, paint);
208 }
209
DEF_TEST(cubic_scan_error_crbug_844457_and_845489,reporter)210 DEF_TEST(cubic_scan_error_crbug_844457_and_845489, reporter) {
211 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
212 SkCanvas* canvas = surface->getCanvas();
213 SkPaint p;
214
215 SkPath path;
216 path.moveTo(-30/64.0, -31/64.0);
217 path.cubicTo(-31/64.0, -31/64,-31/64.0, -31/64,-31/64.0, 100);
218 path.lineTo(100, 100);
219 canvas->drawPath(path, p);
220
221 // May need to define SK_RASTERIZE_EVEN_ROUNDING to trigger the need for this test
222 path.reset();
223 path.moveTo(-30/64.0f, -31/64.0f + 1/256.0f);
224 path.cubicTo(-31/64.0f + 1/256.0f, -31/64.0f + 1/256.0f,
225 -31/64.0f + 1/256.0f, -31/64.0f + 1/256.0f,
226 -31/64.0f + 1/256.0f, 100);
227 path.lineTo(100, 100);
228 canvas->drawPath(path, p);
229 }
230