blob: 0970b833d7778775cccb9d7136e85bdd52460898 [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"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
turk@google.com5755a2a2009-03-03 02:56:05 +000014
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;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000020 bm.allocN32Pixels(64919, 1);
reed@google.com5546ef22012-01-30 17:09:45 +000021 SkCanvas canvas(bm);
junov@google.comdbfac8a2012-12-06 21:47:40 +000022 canvas.clear(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
reed@google.com5546ef22012-01-30 17:09:45 +000024 SkPath path;
25 path.moveTo(0, 0); path.lineTo(1, 0); path.lineTo(33, 1);
26 SkPaint paint;
27 paint.setAntiAlias(true);
28 canvas.drawPath(path, paint);
29}
turk@google.com5755a2a2009-03-03 02:56:05 +000030
31static void PrintCurve(const char *name, const SkPoint crv[4]) {
bungeman@google.comfab44db2013-10-11 18:50:45 +000032 SkDebugf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n",
turk@google.com5755a2a2009-03-03 02:56:05 +000033 name,
reed@android.comd4134452011-02-09 02:24:26 +000034 (float)crv[0].fX, (float)crv[0].fY,
35 (float)crv[1].fX, (float)crv[1].fY,
36 (float)crv[2].fX, (float)crv[2].fY,
37 (float)crv[3].fX, (float)crv[3].fY);
turk@google.com7d3a58a2009-03-04 01:33:35 +000038
turk@google.com5755a2a2009-03-03 02:56:05 +000039}
40
41
turk@google.com7d3a58a2009-03-04 01:33:35 +000042static bool CurvesAreEqual(const SkPoint c0[4],
43 const SkPoint c1[4],
44 float tol) {
45 for (int i = 0; i < 4; i++) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000046 if (SkScalarAbs(c0[i].fX - c1[i].fX) > tol ||
47 SkScalarAbs(c0[i].fY - c1[i].fY) > tol
turk@google.com7d3a58a2009-03-04 01:33:35 +000048 ) {
49 PrintCurve("c0", c0);
50 PrintCurve("c1", c1);
51 return false;
52 }
53 }
54 return true;
turk@google.com5755a2a2009-03-03 02:56:05 +000055}
56
57
turk@google.com7d3a58a2009-03-04 01:33:35 +000058static SkPoint* SetCurve(float x0, float y0,
59 float x1, float y1,
60 float x2, float y2,
61 float x3, float y3,
62 SkPoint crv[4]) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000063 crv[0].fX = x0; crv[0].fY = y0;
64 crv[1].fX = x1; crv[1].fY = y1;
65 crv[2].fX = x2; crv[2].fY = y2;
66 crv[3].fX = x3; crv[3].fY = y3;
turk@google.com5755a2a2009-03-03 02:56:05 +000067 return crv;
68}
turk@google.com7d3a58a2009-03-04 01:33:35 +000069
turk@google.com5755a2a2009-03-03 02:56:05 +000070
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000071DEF_TEST(ClipCubic, reporter) {
turk@google.com7d3a58a2009-03-04 01:33:35 +000072 static SkPoint crv[4] = {
reed@android.comd4134452011-02-09 02:24:26 +000073 { SkIntToScalar(0), SkIntToScalar(0) },
74 { SkIntToScalar(2), SkIntToScalar(3) },
75 { SkIntToScalar(1), SkIntToScalar(10) },
76 { SkIntToScalar(4), SkIntToScalar(12) }
turk@google.com7d3a58a2009-03-04 01:33:35 +000077 };
turk@google.com5755a2a2009-03-03 02:56:05 +000078
turk@google.com7d3a58a2009-03-04 01:33:35 +000079 SkCubicClipper clipper;
80 SkPoint clipped[4], shouldbe[4];
81 SkIRect clipRect;
82 bool success;
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000083 const float tol = 1e-4f;
turk@google.com5755a2a2009-03-03 02:56:05 +000084
turk@google.com7d3a58a2009-03-04 01:33:35 +000085 // Test no clip, with plenty of room.
86 clipRect.set(-2, -2, 6, 14);
87 clipper.setClip(clipRect);
88 success = clipper.clipCubic(crv, clipped);
89 REPORTER_ASSERT(reporter, success == true);
90 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
91 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +000092
turk@google.com7d3a58a2009-03-04 01:33:35 +000093 // Test no clip, touching first point.
94 clipRect.set(-2, 0, 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.com5755a2a2009-03-03 02:56:05 +0000100
turk@google.com7d3a58a2009-03-04 01:33:35 +0000101 // Test no clip, touching last point.
102 clipRect.set(-2, -2, 6, 12);
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.com5755a2a2009-03-03 02:56:05 +0000108
turk@google.com7d3a58a2009-03-04 01:33:35 +0000109 // Test all clip.
110 clipRect.set(-2, 14, 6, 20);
111 clipper.setClip(clipRect);
112 success = clipper.clipCubic(crv, clipped);
113 REPORTER_ASSERT(reporter, success == false);
turk@google.com5755a2a2009-03-03 02:56:05 +0000114
turk@google.com7d3a58a2009-03-04 01:33:35 +0000115 // Test clip at 1.
116 clipRect.set(-2, 1, 6, 14);
117 clipper.setClip(clipRect);
118 success = clipper.clipCubic(crv, clipped);
119 REPORTER_ASSERT(reporter, success == true);
120 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
robertphillips@google.com6853e802012-04-16 15:50:18 +0000121 0.5126125216f, 1,
122 1.841195941f, 4.337081432f,
123 1.297019958f, 10.19801331f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000124 4, 12,
125 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000126
turk@google.com7d3a58a2009-03-04 01:33:35 +0000127 // Test clip at 2.
128 clipRect.set(-2, 2, 6, 14);
129 clipper.setClip(clipRect);
130 success = clipper.clipCubic(crv, clipped);
131 REPORTER_ASSERT(reporter, success == true);
132 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
robertphillips@google.com6853e802012-04-16 15:50:18 +0000133 00.8412352204f, 2,
134 1.767683744f, 5.400758266f,
135 1.55052948f, 10.36701965f,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000136 4, 12,
137 shouldbe), tol));
turk@google.com5755a2a2009-03-03 02:56:05 +0000138
turk@google.com7d3a58a2009-03-04 01:33:35 +0000139 // Test clip at 11.
140 clipRect.set(-2, -2, 6, 11);
141 clipper.setClip(clipRect);
142 success = clipper.clipCubic(crv, clipped);
143 REPORTER_ASSERT(reporter, success == true);
144 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
145 0, 0,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000146 1.742904663f, 2.614356995f,
147 1.207521796f, 8.266430855f,
148 3.026495695f, 11,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000149 shouldbe), tol));
150
151 // Test clip at 10.
152 clipRect.set(-2, -2, 6, 10);
153 clipper.setClip(clipRect);
154 success = clipper.clipCubic(crv, clipped);
155 REPORTER_ASSERT(reporter, success == true);
156 REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
157 0, 0,
robertphillips@google.com6853e802012-04-16 15:50:18 +0000158 1.551193237f, 2.326789856f,
159 1.297736168f, 7.059780121f,
160 2.505550385f, 10,
turk@google.com7d3a58a2009-03-04 01:33:35 +0000161 shouldbe), tol));
reed@google.com5546ef22012-01-30 17:09:45 +0000162
163 test_giantClip();
turk@google.com5755a2a2009-03-03 02:56:05 +0000164}