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