blob: 859401021b9284c1b7e4db2923089cd0184233f2 [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"
Mike Reedba962562020-03-12 20:33:21 -040013#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/utils/SkShadowTessellator.h"
15#include "tests/Test.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040016
Jim Van Verth3645bb02018-06-26 14:58:58 -040017enum ExpectVerts {
18 kDont_ExpectVerts,
19 kDo_ExpectVerts
20};
Jim Van Verth1af03d42017-07-31 09:34:58 -040021
Jim Van Verth3645bb02018-06-26 14:58:58 -040022void check_result(skiatest::Reporter* reporter, sk_sp<SkVertices> verts,
23 ExpectVerts expectVerts, bool expectSuccess) {
24 if (expectSuccess != SkToBool(verts)) {
25 ERRORF(reporter, "Expected shadow tessellation to %s but it did not.",
26 expectSuccess ? "succeed" : "fail");
27 }
28 if (SkToBool(verts)) {
Brian Osman8cbedf92020-03-31 10:38:31 -040029 if (kDont_ExpectVerts == expectVerts && verts->priv().vertexCount()) {
Jim Van Verth3645bb02018-06-26 14:58:58 -040030 ERRORF(reporter, "Expected shadow tessellation to generate no vertices but it did.");
Brian Osman8cbedf92020-03-31 10:38:31 -040031 } else if (kDo_ExpectVerts == expectVerts && !verts->priv().vertexCount()) {
Jim Van Verth3645bb02018-06-26 14:58:58 -040032 ERRORF(reporter, "Expected shadow tessellation to generate vertices but it didn't.");
33 }
34 }
35}
36
37void tessellate_shadow(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm,
38 const SkPoint3& heightParams, ExpectVerts expectVerts, bool expectSuccess) {
Jim Van Verth1af03d42017-07-31 09:34:58 -040039
40 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, true);
Jim Van Verth3645bb02018-06-26 14:58:58 -040041 check_result(reporter, verts, expectVerts, expectSuccess);
42
Jim Van Verth1af03d42017-07-31 09:34:58 -040043 verts = SkShadowTessellator::MakeAmbient(path, ctm, heightParams, false);
Jim Van Verth3645bb02018-06-26 14:58:58 -040044 check_result(reporter, verts, expectVerts, expectSuccess);
45
Jim Van Vertha5ef3972019-05-01 13:28:07 -040046 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, true);
Jim Van Verth3645bb02018-06-26 14:58:58 -040047 check_result(reporter, verts, expectVerts, expectSuccess);
48
Jim Van Verth1af03d42017-07-31 09:34:58 -040049 verts = SkShadowTessellator::MakeSpot(path, ctm, heightParams, {0, 0, 128}, 128.f, false);
Jim Van Verth3645bb02018-06-26 14:58:58 -040050 check_result(reporter, verts, expectVerts, expectSuccess);
Jim Van Verth1af03d42017-07-31 09:34:58 -040051}
52
53DEF_TEST(ShadowUtils, reporter) {
54 SkCanvas canvas(100, 100);
55
56 SkPath path;
57 path.cubicTo(100, 50, 20, 100, 0, 0);
Jim Van Verth3645bb02018-06-26 14:58:58 -040058 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDo_ExpectVerts, true);
59 // super high path
60 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4.0e+37f},
61 kDo_ExpectVerts, true);
Jim Van Verth1af03d42017-07-31 09:34:58 -040062
63 // This line segment has no area and no shadow.
64 path.reset();
65 path.lineTo(10.f, 10.f);
Jim Van Verth3645bb02018-06-26 14:58:58 -040066 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
Jim Van Verth1af03d42017-07-31 09:34:58 -040067
Jim Van Verth3645bb02018-06-26 14:58:58 -040068 // A series of collinear line segments
Jim Van Verth1af03d42017-07-31 09:34:58 -040069 path.reset();
70 for (int i = 0; i < 10; ++i) {
71 path.lineTo((SkScalar)i, (SkScalar)i);
72 }
Jim Van Verth3645bb02018-06-26 14:58:58 -040073 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 4}, kDont_ExpectVerts, true);
74
75 // ugly degenerate path
76 path.reset();
77 path.moveTo(-134217728, 2.22265153e+21f);
78 path.cubicTo(-2.33326106e+21f, 7.36298265e-41f, 3.72237738e-22f, 5.99502692e-36f,
79 1.13631943e+22f, 2.0890786e+33f);
80 path.cubicTo(1.03397626e-25f, 5.99502692e-36f, 9.18354962e-41f, 0, 4.6142745e-37f, -213558848);
81 path.lineTo(-134217728, 2.2226515e+21f);
82 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, true);
83
84 // simple concave path (star of David)
85 path.reset();
86 path.moveTo(0.0f, -50.0f);
87 path.lineTo(14.43f, -25.0f);
88 path.lineTo(43.30f, -25.0f);
89 path.lineTo(28.86f, 0.0f);
90 path.lineTo(43.30f, 25.0f);
91 path.lineTo(14.43f, 25.0f);
92 path.lineTo(0.0f, 50.0f);
93 path.lineTo(-14.43f, 25.0f);
94 path.lineTo(-43.30f, 25.0f);
95 path.lineTo(-28.86f, 0.0f);
96 path.lineTo(-43.30f, -25.0f);
97 path.lineTo(-14.43f, -25.0f);
98// uncomment when transparent concave shadows are working
99// tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDo_ExpectVerts, true);
100
101 // complex concave path (bowtie)
102 path.reset();
103 path.moveTo(-50, -50);
104 path.lineTo(-50, 50);
105 path.lineTo(50, -50);
106 path.lineTo(50, 50);
107 path.lineTo(-50, -50);
108 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
109
110 // multiple contour path
111 path.close();
112 path.moveTo(0, 0);
113 path.lineTo(1, 0);
114 path.lineTo(0, 1);
115 tessellate_shadow(reporter, path, canvas.getTotalMatrix(), {0, 0, 9}, kDont_ExpectVerts, false);
Jim Van Verth1af03d42017-07-31 09:34:58 -0400116}
117
118void check_xformed_bounds(skiatest::Reporter* reporter, const SkPath& path, const SkMatrix& ctm) {
119 const SkDrawShadowRec rec = {
120 SkPoint3::Make(0, 0, 4),
121 SkPoint3::Make(100, 0, 600),
122 800.f,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500123 0x08000000,
124 0x40000000,
Jim Van Verth1af03d42017-07-31 09:34:58 -0400125 0
126 };
127 SkRect bounds;
128 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, &bounds);
129 ctm.mapRect(&bounds);
130
131 auto verts = SkShadowTessellator::MakeAmbient(path, ctm, rec.fZPlaneParams, true);
132 if (verts) {
133 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
134 }
135
136 SkPoint mapXY = ctm.mapXY(rec.fLightPos.fX, rec.fLightPos.fY);
137 SkPoint3 devLightPos = SkPoint3::Make(mapXY.fX, mapXY.fY, rec.fLightPos.fZ);
138 verts = SkShadowTessellator::MakeSpot(path, ctm, rec.fZPlaneParams, devLightPos,
139 rec.fLightRadius, false);
140 if (verts) {
141 REPORTER_ASSERT(reporter, bounds.contains(verts->bounds()));
142 }
143}
144
145void check_bounds(skiatest::Reporter* reporter, const SkPath& path) {
Mike Reedbb59dfa2019-12-12 14:48:28 -0500146 const bool fixed_shadows_in_perspective = false; // skbug.com/9698
147
Jim Van Verth1af03d42017-07-31 09:34:58 -0400148 SkMatrix ctm;
149 ctm.setTranslate(100, 100);
150 check_xformed_bounds(reporter, path, ctm);
151 ctm.postScale(2, 2);
152 check_xformed_bounds(reporter, path, ctm);
153 ctm.preRotate(45);
154 check_xformed_bounds(reporter, path, ctm);
155 ctm.preSkew(40, -20);
156 check_xformed_bounds(reporter, path, ctm);
Mike Reedbb59dfa2019-12-12 14:48:28 -0500157 if (fixed_shadows_in_perspective) {
158 ctm[SkMatrix::kMPersp0] = 0.0001f;
159 ctm[SkMatrix::kMPersp1] = 12.f;
160 check_xformed_bounds(reporter, path, ctm);
161 ctm[SkMatrix::kMPersp0] = 0.0001f;
162 ctm[SkMatrix::kMPersp1] = -12.f;
163 check_xformed_bounds(reporter, path, ctm);
164 ctm[SkMatrix::kMPersp0] = 12.f;
165 ctm[SkMatrix::kMPersp1] = 0.0001f;
166 check_xformed_bounds(reporter, path, ctm);
167 }
Jim Van Verth1af03d42017-07-31 09:34:58 -0400168}
169
170DEF_TEST(ShadowBounds, reporter) {
171 SkPath path;
172 path.addRRect(SkRRect::MakeRectXY(SkRect::MakeLTRB(-50, -20, 40, 30), 4, 4));
173 check_bounds(reporter, path);
174
175 path.reset();
176 path.addOval(SkRect::MakeLTRB(300, 300, 900, 900));
177 check_bounds(reporter, path);
178
179 path.reset();
180 path.cubicTo(100, 50, 20, 100, 0, 0);
181 check_bounds(reporter, path);
182}