blob: 0dc16e7f50b95638da9094d466a8281f94289c81 [file] [log] [blame]
Jim Van Verth1af03d42017-07-31 09:34:58 -04001/*
2 * Copyright 2017 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
10#include "include/core/SkVertices.h"
11#include "include/utils/SkShadowUtils.h"
12#include "src/core/SkDrawShadowInfo.h"
13#include "src/utils/SkShadowTessellator.h"
14#include "tests/Test.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040015
Jim Van Verth3645bb02018-06-26 14:58:58 -040016enum ExpectVerts {
17 kDont_ExpectVerts,
18 kDo_ExpectVerts
19};
Jim Van Verth1af03d42017-07-31 09:34:58 -040020
Jim Van Verth3645bb02018-06-26 14:58:58 -040021void check_result(skiatest::Reporter* reporter, sk_sp<SkVertices> verts,
22 ExpectVerts expectVerts, bool expectSuccess) {
23 if (expectSuccess != SkToBool(verts)) {
24 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
25 expectSuccess ? "succeed" : "fail");
26 }
27 if (SkToBool(verts)) {
28 if (kDont_ExpectVerts == expectVerts && verts->vertexCount()) {
29 ERRORF(reporter, "Expected shadow tessellation to generate no vertices but it did.");
30 } else if (kDo_ExpectVerts == expectVerts && !verts->vertexCount()) {
31 ERRORF(reporter, "Expected shadow tessellation to generate vertices but it didn't.");
32 }
33 }
34}
35
36void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
37 const SkPoint3& heightParams, ExpectVerts expectVerts, bool expectSuccess) {
Jim Van Verth1af03d42017-07-31 09:34:58 -040038
39 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, true);
Jim Van Verth3645bb02018-06-26 14:58:58 -040040 check_result(reporter, verts, expectVerts, expectSuccess);
41
Jim Van Verth1af03d42017-07-31 09:34:58 -040042 verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, false);
Jim Van Verth3645bb02018-06-26 14:58:58 -040043 check_result(reporter, verts, expectVerts, expectSuccess);
44
Jim Van Vertha5ef3972019-05-01 13:28:07 -040045 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, true);
Jim Van Verth3645bb02018-06-26 14:58:58 -040046 check_result(reporter, verts, expectVerts, expectSuccess);
47
Jim Van Verth1af03d42017-07-31 09:34:58 -040048 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false);
Jim Van Verth3645bb02018-06-26 14:58:58 -040049 check_result(reporter, verts, expectVerts, expectSuccess);
Jim Van Verth1af03d42017-07-31 09:34:58 -040050}
51
52DEF_TEST(ShadowUtils, reporter) {
53 SkCanvas canvas(100, 100);
54
55 SkPath path;
56 path.cubicTo(100, 50, 20, 100, 0, 0);
Jim Van Verth3645bb02018-06-26 14:58:58 -040057 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDo_ExpectVerts, true);
58 // super high path
59 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4.0e+37f},
60 kDo_ExpectVerts, true);
Jim Van Verth1af03d42017-07-31 09:34:58 -040061
62 // This line segment has no area and no shadow.
63 path.reset();
64 path.lineTo(10.f, 10.f);
Jim Van Verth3645bb02018-06-26 14:58:58 -040065 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
Jim Van Verth1af03d42017-07-31 09:34:58 -040066
Jim Van Verth3645bb02018-06-26 14:58:58 -040067 // A series of collinear line segments
Jim Van Verth1af03d42017-07-31 09:34:58 -040068 path.reset();
69 for (int i = 0; i < 10; ++i) {
70 path.lineTo((SkScalar)i, (SkScalar)i);
71 }
Jim Van Verth3645bb02018-06-26 14:58:58 -040072 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
73
74 // ugly degenerate path
75 path.reset();
76 path.moveTo(-134217728, 2.22265153e+21f);
77 path.cubicTo(-2.33326106e+21f, 7.36298265e-41f, 3.72237738e-22f, 5.99502692e-36f,
78 1.13631943e+22f, 2.0890786e+33f);
79 path.cubicTo(1.03397626e-25f, 5.99502692e-36f, 9.18354962e-41f, 0, 4.6142745e-37f, -213558848);
80 path.lineTo(-134217728, 2.2226515e+21f);
81 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, true);
82
83 // simple concave path (star of David)
84 path.reset();
85 path.moveTo(0.0f, -50.0f);
86 path.lineTo(14.43f, -25.0f);
87 path.lineTo(43.30f, -25.0f);
88 path.lineTo(28.86f, 0.0f);
89 path.lineTo(43.30f, 25.0f);
90 path.lineTo(14.43f, 25.0f);
91 path.lineTo(0.0f, 50.0f);
92 path.lineTo(-14.43f, 25.0f);
93 path.lineTo(-43.30f, 25.0f);
94 path.lineTo(-28.86f, 0.0f);
95 path.lineTo(-43.30f, -25.0f);
96 path.lineTo(-14.43f, -25.0f);
97// uncomment when transparent concave shadows are working
98// tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDo_ExpectVerts, true);
99
100 // complex concave path (bowtie)
101 path.reset();
102 path.moveTo(-50, -50);
103 path.lineTo(-50, 50);
104 path.lineTo(50, -50);
105 path.lineTo(50, 50);
106 path.lineTo(-50, -50);
107 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
108
109 // multiple contour path
110 path.close();
111 path.moveTo(0, 0);
112 path.lineTo(1, 0);
113 path.lineTo(0, 1);
114 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
Jim Van Verth1af03d42017-07-31 09:34:58 -0400115}
116
117void check_xformed_bounds(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm) {
118 const SkDrawShadowRec rec = {
119 SkPoint3::Make(0, 0, 4),
120 SkPoint3::Make(100, 0, 600),
121 800.f,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500122 0x08000000,
123 0x40000000,
Jim Van Verth1af03d42017-07-31 09:34:58 -0400124 0
125 };
126 SkRect bounds;
127 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds);
128 ctm.mapRect(&bounds);
129
130 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true);
131 if (verts) {
132 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
133 }
134
135 SkPoint mapXY = ctm.mapXY(rec.fLightPos.fX, rec.fLightPos.fY);
136 SkPoint3 devLightPos = SkPoint3::Make(mapXY.fX, mapXY.fY, rec.fLightPos.fZ);
137 verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos,
138 rec.fLightRadius, false);
139 if (verts) {
140 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
141 }
142}
143
144void check_bounds(skiatest::Reporter* reporter, const SkPath& path) {
Mike Reedbb59dfa2019-12-12 14:48:28 -0500145 const bool fixed_shadows_in_perspective = false; // skbug.com/9698
146
Jim Van Verth1af03d42017-07-31 09:34:58 -0400147 SkMatrix ctm;
148 ctm.setTranslate(100, 100);
149 check_xformed_bounds(reporter, path, ctm);
150 ctm.postScale(2, 2);
151 check_xformed_bounds(reporter, path, ctm);
152 ctm.preRotate(45);
153 check_xformed_bounds(reporter, path, ctm);
154 ctm.preSkew(40, -20);
155 check_xformed_bounds(reporter, path, ctm);
Mike Reedbb59dfa2019-12-12 14:48:28 -0500156 if (fixed_shadows_in_perspective) {
157 ctm[SkMatrix::kMPersp0] = 0.0001f;
158 ctm[SkMatrix::kMPersp1] = 12.f;
159 check_xformed_bounds(reporter, path, ctm);
160 ctm[SkMatrix::kMPersp0] = 0.0001f;
161 ctm[SkMatrix::kMPersp1] = -12.f;
162 check_xformed_bounds(reporter, path, ctm);
163 ctm[SkMatrix::kMPersp0] = 12.f;
164 ctm[SkMatrix::kMPersp1] = 0.0001f;
165 check_xformed_bounds(reporter, path, ctm);
166 }
Jim Van Verth1af03d42017-07-31 09:34:58 -0400167}
168
169DEF_TEST(ShadowBounds, reporter) {
170 SkPath path;
171 path.addRRect(SkRRect::MakeRectXY(SkRect::MakeLTRB(-50, -20, 40, 30), 4, 4));
172 check_bounds(reporter, path);
173
174 path.reset();
175 path.addOval(SkRect::MakeLTRB(300, 300, 900, 900));
176 check_bounds(reporter, path);
177
178 path.reset();
179 path.cubicTo(100, 50, 20, 100, 0, 0);
180 check_bounds(reporter, path);
181}