1 /*
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
8 #include "include/core/SkCanvas.h"
9 #include "include/core/SkMatrix.h"
10 #include "include/core/SkPath.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkPoint3.h"
13 #include "include/core/SkRRect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTypes.h"
18 #include "include/core/SkVertices.h"
19 #include "include/private/base/SkTo.h"
20 #include "include/utils/SkShadowUtils.h"
21 #include "src/core/SkDrawShadowInfo.h"
22 #include "src/core/SkVerticesPriv.h"
23 #include "src/utils/SkShadowTessellator.h"
24 #include "tests/Test.h"
25
26 #if !defined(SK_ENABLE_OPTIMIZE_SIZE)
27
28 enum ExpectVerts {
29 kDont_ExpectVerts,
30 kDo_ExpectVerts
31 };
32
check_result(skiatest::Reporter * reporter,sk_sp<SkVertices> verts,ExpectVerts expectVerts,bool expectSuccess)33 void check_result(skiatest::Reporter* reporter, sk_sp<SkVertices> verts,
34 ExpectVerts expectVerts, bool expectSuccess) {
35 if (expectSuccess != SkToBool(verts)) {
36 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
37 expectSuccess ? "succeed" : "fail");
38 }
39 if (SkToBool(verts)) {
40 if (kDont_ExpectVerts == expectVerts && verts->priv().vertexCount()) {
41 ERRORF(reporter, "Expected shadow tessellation to generate no vertices but it did.");
42 } else if (kDo_ExpectVerts == expectVerts && !verts->priv().vertexCount()) {
43 ERRORF(reporter, "Expected shadow tessellation to generate vertices but it didn't.");
44 }
45 }
46 }
47
tessellate_shadow(skiatest::Reporter * reporter,const SkPath & path,const SkMatrix & ctm,const SkPoint3 & heightParams,ExpectVerts expectVerts,bool expectSuccess)48 void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
49 const SkPoint3& heightParams, ExpectVerts expectVerts, bool expectSuccess) {
50
51 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, true);
52 check_result(reporter, verts, expectVerts, expectSuccess);
53
54 verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, false);
55 check_result(reporter, verts, expectVerts, expectSuccess);
56
57 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, true, false);
58 check_result(reporter, verts, expectVerts, expectSuccess);
59
60 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false,
61 false);
62 check_result(reporter, verts, expectVerts, expectSuccess);
63
64 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, true, true);
65 check_result(reporter, verts, expectVerts, expectSuccess);
66
67 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false, true);
68 check_result(reporter, verts, expectVerts, expectSuccess);
69 }
70
DEF_TEST(ShadowUtils,reporter)71 DEF_TEST(ShadowUtils, reporter) {
72 SkCanvas canvas(100, 100);
73
74 SkPath path;
75 path.cubicTo(100, 50, 20, 100, 0, 0);
76 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDo_ExpectVerts, true);
77 // super high path
78 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4.0e+37f},
79 kDo_ExpectVerts, true);
80
81 // This line segment has no area and no shadow.
82 path.reset();
83 path.lineTo(10.f, 10.f);
84 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
85
86 // A series of collinear line segments
87 path.reset();
88 for (int i = 0; i < 10; ++i) {
89 path.lineTo((SkScalar)i, (SkScalar)i);
90 }
91 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
92
93 // ugly degenerate path
94 path.reset();
95 path.moveTo(-134217728, 2.22265153e+21f);
96 path.cubicTo(-2.33326106e+21f, 7.36298265e-41f, 3.72237738e-22f, 5.99502692e-36f,
97 1.13631943e+22f, 2.0890786e+33f);
98 path.cubicTo(1.03397626e-25f, 5.99502692e-36f, 9.18354962e-41f, 0, 4.6142745e-37f, -213558848);
99 path.lineTo(-134217728, 2.2226515e+21f);
100 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, true);
101
102 // simple concave path (star of David)
103 path.reset();
104 path.moveTo(0.0f, -50.0f);
105 path.lineTo(14.43f, -25.0f);
106 path.lineTo(43.30f, -25.0f);
107 path.lineTo(28.86f, 0.0f);
108 path.lineTo(43.30f, 25.0f);
109 path.lineTo(14.43f, 25.0f);
110 path.lineTo(0.0f, 50.0f);
111 path.lineTo(-14.43f, 25.0f);
112 path.lineTo(-43.30f, 25.0f);
113 path.lineTo(-28.86f, 0.0f);
114 path.lineTo(-43.30f, -25.0f);
115 path.lineTo(-14.43f, -25.0f);
116 // uncomment when transparent concave shadows are working
117 // tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDo_ExpectVerts, true);
118
119 // complex concave path (bowtie)
120 path.reset();
121 path.moveTo(-50, -50);
122 path.lineTo(-50, 50);
123 path.lineTo(50, -50);
124 path.lineTo(50, 50);
125 path.lineTo(-50, -50);
126 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
127
128 // multiple contour path
129 path.close();
130 path.moveTo(0, 0);
131 path.lineTo(1, 0);
132 path.lineTo(0, 1);
133 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
134 }
135
check_xformed_bounds(skiatest::Reporter * reporter,const SkPath & path,const SkMatrix & ctm)136 void check_xformed_bounds(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm) {
137 SkDrawShadowRec rec = {
138 SkPoint3::Make(0, 0, 4),
139 SkPoint3::Make(100, 0, 600),
140 800.f,
141 0x08000000,
142 0x40000000,
143 0
144 };
145 // point light
146 SkRect bounds;
147 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds);
148 ctm.mapRect(&bounds);
149
150 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true);
151 if (verts) {
152 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
153 }
154
155 SkPoint mapXY = ctm.mapXY(rec.fLightPos.fX, rec.fLightPos.fY);
156 SkPoint3 devLightPos = SkPoint3::Make(mapXY.fX, mapXY.fY, rec.fLightPos.fZ);
157 verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos,
158 rec.fLightRadius, false, false);
159 if (verts) {
160 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
161 }
162
163 // directional light
164 rec.fFlags |= SkShadowFlags::kDirectionalLight_ShadowFlag;
165 rec.fLightRadius = 2.0f;
166 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds);
167 ctm.mapRect(&bounds);
168
169 verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true);
170 if (verts) {
171 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
172 }
173
174 devLightPos = rec.fLightPos;
175 devLightPos.normalize();
176 verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos,
177 rec.fLightRadius, false, true);
178 if (verts) {
179 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
180 }
181 }
182
check_bounds(skiatest::Reporter * reporter,const SkPath & path)183 void check_bounds(skiatest::Reporter* reporter, const SkPath& path) {
184 const bool fixed_shadows_in_perspective = false; // skbug.com/9698
185
186 SkMatrix ctm;
187 ctm.setTranslate(100, 100);
188 check_xformed_bounds(reporter, path, ctm);
189 ctm.postScale(2, 2);
190 check_xformed_bounds(reporter, path, ctm);
191 ctm.preRotate(45);
192 check_xformed_bounds(reporter, path, ctm);
193 ctm.preSkew(40, -20);
194 check_xformed_bounds(reporter, path, ctm);
195 if (fixed_shadows_in_perspective) {
196 ctm[SkMatrix::kMPersp0] = 0.0001f;
197 ctm[SkMatrix::kMPersp1] = 12.f;
198 check_xformed_bounds(reporter, path, ctm);
199 ctm[SkMatrix::kMPersp0] = 0.0001f;
200 ctm[SkMatrix::kMPersp1] = -12.f;
201 check_xformed_bounds(reporter, path, ctm);
202 ctm[SkMatrix::kMPersp0] = 12.f;
203 ctm[SkMatrix::kMPersp1] = 0.0001f;
204 check_xformed_bounds(reporter, path, ctm);
205 }
206 }
207
DEF_TEST(ShadowBounds,reporter)208 DEF_TEST(ShadowBounds, reporter) {
209 SkPath path;
210 path.addRRect(SkRRect::MakeRectXY(SkRect::MakeLTRB(-50, -20, 40, 30), 4, 4));
211 check_bounds(reporter, path);
212
213 path.reset();
214 path.addOval(SkRect::MakeLTRB(300, 300, 900, 900));
215 check_bounds(reporter, path);
216
217 path.reset();
218 path.cubicTo(100, 50, 20, 100, 0, 0);
219 check_bounds(reporter, path);
220 }
221
222 #endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)
223