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