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