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