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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
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 | SkPaint paint; |
| 33 | paint.setAntiAlias(true); |
Mike Reed | 92f6eb1 | 2020-08-25 11:48:41 -0400 | [diff] [blame] | 34 | canvas.drawPath(SkPath::Polygon({{0,0}, {1,0}, {33,1}}, false), paint); |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 35 | } |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 36 | |
| 37 | static void PrintCurve(const char *name, const SkPoint crv[4]) { |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 38 | SkDebugf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n", |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 39 | name, |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 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); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 44 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 48 | static bool CurvesAreEqual(const SkPoint c0[4], |
| 49 | const SkPoint c1[4], |
| 50 | float tol) { |
| 51 | for (int i = 0; i < 4; i++) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 52 | if (SkScalarAbs(c0[i].fX - c1[i].fX) > tol || |
| 53 | SkScalarAbs(c0[i].fY - c1[i].fY) > tol |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 54 | ) { |
| 55 | PrintCurve("c0", c0); |
| 56 | PrintCurve("c1", c1); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | return true; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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]) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 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; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 73 | return crv; |
| 74 | } |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 75 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 76 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 77 | DEF_TEST(ClipCubic, reporter) { |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 78 | static SkPoint crv[4] = { |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 79 | { SkIntToScalar(0), SkIntToScalar(0) }, |
| 80 | { SkIntToScalar(2), SkIntToScalar(3) }, |
| 81 | { SkIntToScalar(1), SkIntToScalar(10) }, |
| 82 | { SkIntToScalar(4), SkIntToScalar(12) } |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 83 | }; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 84 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 85 | SkCubicClipper clipper; |
| 86 | SkPoint clipped[4], shouldbe[4]; |
| 87 | SkIRect clipRect; |
| 88 | bool success; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 89 | const float tol = 1e-4f; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 90 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 91 | // Test no clip, with plenty of room. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 92 | clipRect.setLTRB(-2, -2, 6, 14); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 98 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 99 | // Test no clip, touching first point. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 100 | clipRect.setLTRB(-2, 0, 6, 14); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 106 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 107 | // Test no clip, touching last point. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 108 | clipRect.setLTRB(-2, -2, 6, 12); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 114 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 115 | // Test all clip. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 116 | clipRect.setLTRB(-2, 14, 6, 20); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 117 | clipper.setClip(clipRect); |
| 118 | success = clipper.clipCubic(crv, clipped); |
| 119 | REPORTER_ASSERT(reporter, success == false); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 120 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 121 | // Test clip at 1. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 122 | clipRect.setLTRB(-2, 1, 6, 14); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 123 | clipper.setClip(clipRect); |
| 124 | success = clipper.clipCubic(crv, clipped); |
| 125 | REPORTER_ASSERT(reporter, success == true); |
| 126 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 127 | 0.5126125216f, 1, |
| 128 | 1.841195941f, 4.337081432f, |
| 129 | 1.297019958f, 10.19801331f, |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 130 | 4, 12, |
| 131 | shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 132 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 133 | // Test clip at 2. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 134 | clipRect.setLTRB(-2, 2, 6, 14); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 135 | clipper.setClip(clipRect); |
| 136 | success = clipper.clipCubic(crv, clipped); |
| 137 | REPORTER_ASSERT(reporter, success == true); |
| 138 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 139 | 00.8412352204f, 2, |
| 140 | 1.767683744f, 5.400758266f, |
| 141 | 1.55052948f, 10.36701965f, |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 142 | 4, 12, |
| 143 | shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 144 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 145 | // Test clip at 11. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 146 | clipRect.setLTRB(-2, -2, 6, 11); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 152 | 1.742904663f, 2.614356995f, |
| 153 | 1.207521796f, 8.266430855f, |
| 154 | 3.026495695f, 11, |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 155 | shouldbe), tol)); |
| 156 | |
| 157 | // Test clip at 10. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 158 | clipRect.setLTRB(-2, -2, 6, 10); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 164 | 1.551193237f, 2.326789856f, |
| 165 | 1.297736168f, 7.059780121f, |
| 166 | 2.505550385f, 10, |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 167 | shouldbe), tol)); |
reed@google.com | 5546ef2 | 2012-01-30 17:09:45 +0000 | [diff] [blame] | 168 | |
| 169 | test_giantClip(); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 170 | } |
Cary Clark | 44c1b11 | 2017-03-21 16:11:54 -0400 | [diff] [blame] | 171 | |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 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; |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 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 | } |
Mike Reed | 861b52e | 2018-05-18 14:07:57 -0400 | [diff] [blame] | 209 | |
Mike Reed | 3087c1f | 2018-05-23 08:30:04 -0400 | [diff] [blame] | 210 | DEF_TEST(cubic_scan_error_crbug_844457_and_845489, reporter) { |
Mike Reed | 861b52e | 2018-05-18 14:07:57 -0400 | [diff] [blame] | 211 | auto surface(SkSurface::MakeRasterN32Premul(100, 100)); |
Mike Reed | 3087c1f | 2018-05-23 08:30:04 -0400 | [diff] [blame] | 212 | SkCanvas* canvas = surface->getCanvas(); |
| 213 | SkPaint p; |
Mike Reed | 861b52e | 2018-05-18 14:07:57 -0400 | [diff] [blame] | 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); |
Mike Reed | 3087c1f | 2018-05-23 08:30:04 -0400 | [diff] [blame] | 218 | path.lineTo(100, 100); |
| 219 | canvas->drawPath(path, p); |
Mike Reed | 861b52e | 2018-05-18 14:07:57 -0400 | [diff] [blame] | 220 | |
Mike Reed | 3087c1f | 2018-05-23 08:30:04 -0400 | [diff] [blame] | 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); |
Mike Reed | 861b52e | 2018-05-18 14:07:57 -0400 | [diff] [blame] | 229 | } |