blob: 31b38f2726286228f24d74e35c6ab7f6e57ad551 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.com5755a2a2009-03-03 02:56:05 +00007
reed@google.com5546ef22012-01-30 17:09:45 +00008#include "SkCanvas.h"
reed@android.com0650c6c2009-03-04 14:02:44 +00009#include "SkCubicClipper.h"
turk@google.com5755a2a2009-03-03 02:56:05 +000010#include "SkGeometry.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "SkPaint.h"
12#include "Test.h"
turk@google.com5755a2a2009-03-03 02:56:05 +000013
reed@google.com5546ef22012-01-30 17:09:45 +000014// Currently the supersampler blitter uses int16_t for its index into an array
15// the width of the clip. Test that we don't crash/assert if we try to draw
16// with a device/clip that is larger.
17static void test_giantClip() {
18 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000019 bm.allocN32Pixels(64919, 1);
reed@google.com5546ef22012-01-30 17:09:45 +000020 SkCanvas canvas(bm);
junov@google.comdbfac8a2012-12-06 21:47:40 +000021 canvas.clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000022
reed@google.com5546ef22012-01-30 17:09:45 +000023 SkPath path;
24 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(33, 1);
25 SkPaint paint;
26 paint.setAntiAlias(true);
27 canvas.drawPath(path, paint);
28}
turk@google.com5755a2a2009-03-03 02:56:05 +000029
30static void PrintCurve(const char *name, const SkPoint crv[4]) {
bungeman@google.comfab44db2013-10-11 18:50:45 +000031 SkDebugf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n",
turk@google.com5755a2a2009-03-03 02:56:05 +000032 name,
reed@android.comd4134452011-02-09 02:24:26 +000033 (float)crv[0].fX, (float)crv[0].fY,
34 (float)crv[1].fX, (float)crv[1].fY,
35 (float)crv[2].fX, (float)crv[2].fY,
36 (float)crv[3].fX, (float)crv[3].fY);
turk@google.com7d3a58a2009-03-04 01:33:35 +000037
turk@google.com5755a2a2009-03-03 02:56:05 +000038}
39
40
turk@google.com7d3a58a2009-03-04 01:33:35 +000041static bool CurvesAreEqual(const SkPoint c0[4],
42 const SkPoint c1[4],
43 float tol) {
44 for (int i = 0; i < 4; i++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000045 if (SkScalarAbs(c0[i].fX - c1[i].fX) > tol ||
46 SkScalarAbs(c0[i].fY - c1[i].fY) > tol
turk@google.com7d3a58a2009-03-04 01:33:35 +000047 ) {
48 PrintCurve("c0", c0);
49 PrintCurve("c1", c1);
50 return false;
51 }
52 }
53 return true;
turk@google.com5755a2a2009-03-03 02:56:05 +000054}
55
56
turk@google.com7d3a58a2009-03-04 01:33:35 +000057static SkPoint* SetCurve(float x0, float y0,
58 float x1, float y1,
59 float x2, float y2,
60 float x3, float y3,
61 SkPoint crv[4]) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000062 crv[0].fX = x0; crv[0].fY = y0;
63 crv[1].fX = x1; crv[1].fY = y1;
64 crv[2].fX = x2; crv[2].fY = y2;
65 crv[3].fX = x3; crv[3].fY = y3;
turk@google.com5755a2a2009-03-03 02:56:05 +000066 return crv;
67}
turk@google.com7d3a58a2009-03-04 01:33:35 +000068
turk@google.com5755a2a2009-03-03 02:56:05 +000069
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000070DEF_TEST(ClipCubic, reporter) {
turk@google.com7d3a58a2009-03-04 01:33:35 +000071 static SkPoint crv[4] = {
reed@android.comd4134452011-02-09 02:24:26 +000072 { SkIntToScalar(0), SkIntToScalar(0) },
73 { SkIntToScalar(2), SkIntToScalar(3) },
74 { SkIntToScalar(1), SkIntToScalar(10) },
75 { SkIntToScalar(4), SkIntToScalar(12) }
turk@google.com7d3a58a2009-03-04 01:33:35 +000076 };
turk@google.com5755a2a2009-03-03 02:56:05 +000077
turk@google.com7d3a58a2009-03-04 01:33:35 +000078 SkCubicClipper clipper;
79 SkPoint clipped[4], shouldbe[4];
80 SkIRect clipRect;
81 bool success;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 const float tol = 1e-4f;
turk@google.com5755a2a2009-03-03 02:56:05 +000083
turk@google.com7d3a58a2009-03-04 01:33:35 +000084 // Test no clip, with plenty of room.
85 clipRect.set(-2, -2, 6, 14);
86 clipper.setClip(clipRect);
87 success = clipper.clipCubic(crv, clipped);
88 REPORTER_ASSERT(reporter, success == true);
89 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
90 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +000091
turk@google.com7d3a58a2009-03-04 01:33:35 +000092 // Test no clip, touching first point.
93 clipRect.set(-2, 0, 6, 14);
94 clipper.setClip(clipRect);
95 success = clipper.clipCubic(crv, clipped);
96 REPORTER_ASSERT(reporter, success == true);
97 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
98 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +000099
turk@google.com7d3a58a2009-03-04 01:33:35 +0000100 // Test no clip, touching last point.
101 clipRect.set(-2, -2, 6, 12);
102 clipper.setClip(clipRect);
103 success = clipper.clipCubic(crv, clipped);
104 REPORTER_ASSERT(reporter, success == true);
105 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
106 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000107
turk@google.com7d3a58a2009-03-04 01:33:35 +0000108 // Test all clip.
109 clipRect.set(-2, 14, 6, 20);
110 clipper.setClip(clipRect);
111 success = clipper.clipCubic(crv, clipped);
112 REPORTER_ASSERT(reporter, success == false);
turk@google.com5755a2a2009-03-03 02:56:05 +0000113
turk@google.com7d3a58a2009-03-04 01:33:35 +0000114 // Test clip at 1.
115 clipRect.set(-2, 1, 6, 14);
116 clipper.setClip(clipRect);
117 success = clipper.clipCubic(crv, clipped);
118 REPORTER_ASSERT(reporter, success == true);
119 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
robertphillips@google.com6853e802012-04-16 15:50:18 +0000120 0.5126125216f, 1,
121 1.841195941f, 4.337081432f,
122 1.297019958f, 10.19801331f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000123 4, 12,
124 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000125
turk@google.com7d3a58a2009-03-04 01:33:35 +0000126 // Test clip at 2.
127 clipRect.set(-2, 2, 6, 14);
128 clipper.setClip(clipRect);
129 success = clipper.clipCubic(crv, clipped);
130 REPORTER_ASSERT(reporter, success == true);
131 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
robertphillips@google.com6853e802012-04-16 15:50:18 +0000132 00.8412352204f, 2,
133 1.767683744f, 5.400758266f,
134 1.55052948f, 10.36701965f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000135 4, 12,
136 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000137
turk@google.com7d3a58a2009-03-04 01:33:35 +0000138 // Test clip at 11.
139 clipRect.set(-2, -2, 6, 11);
140 clipper.setClip(clipRect);
141 success = clipper.clipCubic(crv, clipped);
142 REPORTER_ASSERT(reporter, success == true);
143 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
144 0, 0,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000145 1.742904663f, 2.614356995f,
146 1.207521796f, 8.266430855f,
147 3.026495695f, 11,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000148 shouldbe), tol));
149
150 // Test clip at 10.
151 clipRect.set(-2, -2, 6, 10);
152 clipper.setClip(clipRect);
153 success = clipper.clipCubic(crv, clipped);
154 REPORTER_ASSERT(reporter, success == true);
155 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
156 0, 0,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000157 1.551193237f, 2.326789856f,
158 1.297736168f, 7.059780121f,
159 2.505550385f, 10,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000160 shouldbe), tol));
reed@google.com5546ef22012-01-30 17:09:45 +0000161
162 test_giantClip();
turk@google.com5755a2a2009-03-03 02:56:05 +0000163}