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