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