blob: e980781d28f8522db860fedb133220f1ea1f5828 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
turk@google.com5755a2a2009-03-03 02:56:05 +00008#include "Test.h"
9
reed@google.com5546ef22012-01-30 17:09:45 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
reed@android.com0650c6c2009-03-04 14:02:44 +000012#include "SkCubicClipper.h"
turk@google.com5755a2a2009-03-03 02:56:05 +000013#include "SkGeometry.h"
14
reed@google.com5546ef22012-01-30 17:09:45 +000015// Currently the supersampler blitter uses int16_t for its index into an array
16// the width of the clip. Test that we don't crash/assert if we try to draw
17// with a device/clip that is larger.
18static void test_giantClip() {
19 SkBitmap bm;
20 bm.setConfig(SkBitmap::kARGB_8888_Config, 64919, 1);
21 bm.allocPixels();
22 SkCanvas canvas(bm);
junov@google.comdbfac8a2012-12-06 21:47:40 +000023 canvas.clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.com5546ef22012-01-30 17:09:45 +000025 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.com5755a2a2009-03-03 02:56:05 +000031
32static void PrintCurve(const char *name, const SkPoint crv[4]) {
33 printf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n",
34 name,
reed@android.comd4134452011-02-09 02:24:26 +000035 (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.com7d3a58a2009-03-04 01:33:35 +000039
turk@google.com5755a2a2009-03-03 02:56:05 +000040}
41
42
turk@google.com7d3a58a2009-03-04 01:33:35 +000043static bool CurvesAreEqual(const SkPoint c0[4],
44 const SkPoint c1[4],
45 float tol) {
46 for (int i = 0; i < 4; i++) {
47 if (SkScalarAbs(c0[i].fX - c1[i].fX) > SkFloatToScalar(tol) ||
48 SkScalarAbs(c0[i].fY - c1[i].fY) > SkFloatToScalar(tol)
49 ) {
50 PrintCurve("c0", c0);
51 PrintCurve("c1", c1);
52 return false;
53 }
54 }
55 return true;
turk@google.com5755a2a2009-03-03 02:56:05 +000056}
57
58
turk@google.com7d3a58a2009-03-04 01:33:35 +000059static 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]) {
turk@google.com5755a2a2009-03-03 02:56:05 +000064 crv[0].fX = SkFloatToScalar(x0); crv[0].fY = SkFloatToScalar(y0);
65 crv[1].fX = SkFloatToScalar(x1); crv[1].fY = SkFloatToScalar(y1);
66 crv[2].fX = SkFloatToScalar(x2); crv[2].fY = SkFloatToScalar(y2);
67 crv[3].fX = SkFloatToScalar(x3); crv[3].fY = SkFloatToScalar(y3);
68 return crv;
69}
turk@google.com7d3a58a2009-03-04 01:33:35 +000070
turk@google.com5755a2a2009-03-03 02:56:05 +000071
72static void TestCubicClipping(skiatest::Reporter* reporter) {
turk@google.com7d3a58a2009-03-04 01:33:35 +000073 static SkPoint crv[4] = {
reed@android.comd4134452011-02-09 02:24:26 +000074 { SkIntToScalar(0), SkIntToScalar(0) },
75 { SkIntToScalar(2), SkIntToScalar(3) },
76 { SkIntToScalar(1), SkIntToScalar(10) },
77 { SkIntToScalar(4), SkIntToScalar(12) }
turk@google.com7d3a58a2009-03-04 01:33:35 +000078 };
turk@google.com5755a2a2009-03-03 02:56:05 +000079
turk@google.com7d3a58a2009-03-04 01:33:35 +000080 SkCubicClipper clipper;
81 SkPoint clipped[4], shouldbe[4];
82 SkIRect clipRect;
83 bool success;
robertphillips@google.com6853e802012-04-16 15:50:18 +000084 const float tol = SkFloatToScalar(1e-4f);
turk@google.com5755a2a2009-03-03 02:56:05 +000085
turk@google.com7d3a58a2009-03-04 01:33:35 +000086 // 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.com5755a2a2009-03-03 02:56:05 +000093
turk@google.com7d3a58a2009-03-04 01:33:35 +000094 // 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.com5755a2a2009-03-03 02:56:05 +0000101
turk@google.com7d3a58a2009-03-04 01:33:35 +0000102 // 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.com5755a2a2009-03-03 02:56:05 +0000109
turk@google.com7d3a58a2009-03-04 01:33:35 +0000110 // 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.com5755a2a2009-03-03 02:56:05 +0000115
turk@google.com7d3a58a2009-03-04 01:33:35 +0000116 // 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.com6853e802012-04-16 15:50:18 +0000122 0.5126125216f, 1,
123 1.841195941f, 4.337081432f,
124 1.297019958f, 10.19801331f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000125 4, 12,
126 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000127
turk@google.com7d3a58a2009-03-04 01:33:35 +0000128 // 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.com6853e802012-04-16 15:50:18 +0000134 00.8412352204f, 2,
135 1.767683744f, 5.400758266f,
136 1.55052948f, 10.36701965f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000137 4, 12,
138 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000139
turk@google.com7d3a58a2009-03-04 01:33:35 +0000140 // 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.com6853e802012-04-16 15:50:18 +0000147 1.742904663f, 2.614356995f,
148 1.207521796f, 8.266430855f,
149 3.026495695f, 11,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000150 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.com6853e802012-04-16 15:50:18 +0000159 1.551193237f, 2.326789856f,
160 1.297736168f, 7.059780121f,
161 2.505550385f, 10,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000162 shouldbe), tol));
reed@google.com5546ef22012-01-30 17:09:45 +0000163
164 test_giantClip();
turk@google.com5755a2a2009-03-03 02:56:05 +0000165}
166
167
168
169
170#include "TestClassDef.h"
171DEFINE_TESTCLASS("CubicClipper", CubicClippingTestClass, TestCubicClipping)