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/SkMatrix.h"
9 #include "include/core/SkPath.h"
10 #include "include/core/SkRect.h"
11 #include "include/private/SkShadowFlags.h"
12 #include "src/core/SkDrawShadowInfo.h"
13 #include "src/utils/SkPolyUtils.h"
14
15 namespace SkDrawShadowMetrics {
16
compute_z(SkScalar x,SkScalar y,const SkPoint3 & params)17 static SkScalar compute_z(SkScalar x, SkScalar y, const SkPoint3& params) {
18 return x*params.fX + y*params.fY + params.fZ;
19 }
20
GetSpotShadowTransform(const SkPoint3 & lightPos,SkScalar lightRadius,const SkMatrix & ctm,const SkPoint3 & zPlaneParams,const SkRect & pathBounds,bool directional,SkMatrix * shadowTransform,SkScalar * radius)21 bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius,
22 const SkMatrix& ctm, const SkPoint3& zPlaneParams,
23 const SkRect& pathBounds, bool directional,
24 SkMatrix* shadowTransform, SkScalar* radius) {
25 auto heightFunc = [zPlaneParams] (SkScalar x, SkScalar y) {
26 return zPlaneParams.fX*x + zPlaneParams.fY*y + zPlaneParams.fZ;
27 };
28 SkScalar occluderHeight = heightFunc(pathBounds.centerX(), pathBounds.centerY());
29
30 // TODO: have directional lights support tilt via the zPlaneParams
31 if (!ctm.hasPerspective() || directional) {
32 SkScalar scale;
33 SkVector translate;
34 if (directional) {
35 SkDrawShadowMetrics::GetDirectionalParams(occluderHeight, lightPos.fX, lightPos.fY,
36 lightPos.fZ, lightRadius, radius,
37 &scale, &translate);
38 } else {
39 SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY,
40 lightPos.fZ, lightRadius, radius,
41 &scale, &translate);
42 }
43 shadowTransform->setScaleTranslate(scale, scale, translate.fX, translate.fY);
44 shadowTransform->preConcat(ctm);
45 } else {
46 if (SkScalarNearlyZero(pathBounds.width()) || SkScalarNearlyZero(pathBounds.height())) {
47 return false;
48 }
49
50 // get rotated quad in 3D
51 SkPoint pts[4];
52 ctm.mapRectToQuad(pts, pathBounds);
53 // No shadows for bowties or other degenerate cases
54 if (!SkIsConvexPolygon(pts, 4)) {
55 return false;
56 }
57 SkPoint3 pts3D[4];
58 SkScalar z = heightFunc(pathBounds.fLeft, pathBounds.fTop);
59 pts3D[0].set(pts[0].fX, pts[0].fY, z);
60 z = heightFunc(pathBounds.fRight, pathBounds.fTop);
61 pts3D[1].set(pts[1].fX, pts[1].fY, z);
62 z = heightFunc(pathBounds.fRight, pathBounds.fBottom);
63 pts3D[2].set(pts[2].fX, pts[2].fY, z);
64 z = heightFunc(pathBounds.fLeft, pathBounds.fBottom);
65 pts3D[3].set(pts[3].fX, pts[3].fY, z);
66
67 // project from light through corners to z=0 plane
68 for (int i = 0; i < 4; ++i) {
69 SkScalar dz = lightPos.fZ - pts3D[i].fZ;
70 // light shouldn't be below or at a corner's z-location
71 if (dz <= SK_ScalarNearlyZero) {
72 return false;
73 }
74 SkScalar zRatio = pts3D[i].fZ / dz;
75 pts3D[i].fX -= (lightPos.fX - pts3D[i].fX)*zRatio;
76 pts3D[i].fY -= (lightPos.fY - pts3D[i].fY)*zRatio;
77 pts3D[i].fZ = SK_Scalar1;
78 }
79
80 // Generate matrix that projects from [-1,1]x[-1,1] square to projected quad
81 SkPoint3 h0, h1, h2;
82 // Compute homogenous crossing point between top and bottom edges (gives new x-axis).
83 h0 = (pts3D[1].cross(pts3D[0])).cross(pts3D[2].cross(pts3D[3]));
84 // Compute homogenous crossing point between left and right edges (gives new y-axis).
85 h1 = (pts3D[0].cross(pts3D[3])).cross(pts3D[1].cross(pts3D[2]));
86 // Compute homogenous crossing point between diagonals (gives new origin).
87 h2 = (pts3D[0].cross(pts3D[2])).cross(pts3D[1].cross(pts3D[3]));
88 // If h2 is a vector (z=0 in 2D homogeneous space), that means that at least
89 // two of the quad corners are coincident and we don't have a realistic projection
90 if (SkScalarNearlyZero(h2.fZ)) {
91 return false;
92 }
93 // In some cases the crossing points are in the wrong direction
94 // to map (-1,-1) to pts3D[0], so we need to correct for that.
95 // Want h0 to be to the right of the left edge.
96 SkVector3 v = pts3D[3] - pts3D[0];
97 SkVector3 w = h0 - pts3D[0];
98 SkScalar perpDot = v.fX*w.fY - v.fY*w.fX;
99 if (perpDot > 0) {
100 h0 = -h0;
101 }
102 // Want h1 to be above the bottom edge.
103 v = pts3D[1] - pts3D[0];
104 perpDot = v.fX*w.fY - v.fY*w.fX;
105 if (perpDot < 0) {
106 h1 = -h1;
107 }
108 shadowTransform->setAll(h0.fX / h2.fZ, h1.fX / h2.fZ, h2.fX / h2.fZ,
109 h0.fY / h2.fZ, h1.fY / h2.fZ, h2.fY / h2.fZ,
110 h0.fZ / h2.fZ, h1.fZ / h2.fZ, 1);
111 // generate matrix that transforms from bounds to [-1,1]x[-1,1] square
112 SkMatrix toHomogeneous;
113 SkScalar xScale = 2/(pathBounds.fRight - pathBounds.fLeft);
114 SkScalar yScale = 2/(pathBounds.fBottom - pathBounds.fTop);
115 toHomogeneous.setAll(xScale, 0, -xScale*pathBounds.fLeft - 1,
116 0, yScale, -yScale*pathBounds.fTop - 1,
117 0, 0, 1);
118 shadowTransform->preConcat(toHomogeneous);
119
120 *radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius);
121 }
122
123 return true;
124 }
125
GetLocalBounds(const SkPath & path,const SkDrawShadowRec & rec,const SkMatrix & ctm,SkRect * bounds)126 void GetLocalBounds(const SkPath& path, const SkDrawShadowRec& rec, const SkMatrix& ctm,
127 SkRect* bounds) {
128 SkRect ambientBounds = path.getBounds();
129 SkScalar occluderZ;
130 if (SkScalarNearlyZero(rec.fZPlaneParams.fX) && SkScalarNearlyZero(rec.fZPlaneParams.fY)) {
131 occluderZ = rec.fZPlaneParams.fZ;
132 } else {
133 occluderZ = compute_z(ambientBounds.fLeft, ambientBounds.fTop, rec.fZPlaneParams);
134 occluderZ = std::max(occluderZ, compute_z(ambientBounds.fRight, ambientBounds.fTop,
135 rec.fZPlaneParams));
136 occluderZ = std::max(occluderZ, compute_z(ambientBounds.fLeft, ambientBounds.fBottom,
137 rec.fZPlaneParams));
138 occluderZ = std::max(occluderZ, compute_z(ambientBounds.fRight, ambientBounds.fBottom,
139 rec.fZPlaneParams));
140 }
141 SkScalar ambientBlur;
142 SkScalar spotBlur;
143 SkScalar spotScale;
144 SkPoint spotOffset;
145 if (ctm.hasPerspective()) {
146 // transform ambient and spot bounds into device space
147 ctm.mapRect(&ambientBounds);
148
149 // get ambient blur (in device space)
150 ambientBlur = SkDrawShadowMetrics::AmbientBlurRadius(occluderZ);
151
152 // get spot params (in device space)
153 if (SkToBool(rec.fFlags & SkShadowFlags::kDirectionalLight_ShadowFlag)) {
154 SkDrawShadowMetrics::GetDirectionalParams(occluderZ, rec.fLightPos.fX, rec.fLightPos.fY,
155 rec.fLightPos.fZ, rec.fLightRadius,
156 &spotBlur, &spotScale, &spotOffset);
157 } else {
158 SkPoint devLightPos = SkPoint::Make(rec.fLightPos.fX, rec.fLightPos.fY);
159 ctm.mapPoints(&devLightPos, 1);
160 SkDrawShadowMetrics::GetSpotParams(occluderZ, devLightPos.fX, devLightPos.fY,
161 rec.fLightPos.fZ, rec.fLightRadius,
162 &spotBlur, &spotScale, &spotOffset);
163 }
164 } else {
165 SkScalar devToSrcScale = SkScalarInvert(ctm.getMinScale());
166
167 // get ambient blur (in local space)
168 SkScalar devSpaceAmbientBlur = SkDrawShadowMetrics::AmbientBlurRadius(occluderZ);
169 ambientBlur = devSpaceAmbientBlur*devToSrcScale;
170
171 // get spot params (in local space)
172 if (SkToBool(rec.fFlags & SkShadowFlags::kDirectionalLight_ShadowFlag)) {
173 SkDrawShadowMetrics::GetDirectionalParams(occluderZ, rec.fLightPos.fX, rec.fLightPos.fY,
174 rec.fLightPos.fZ, rec.fLightRadius,
175 &spotBlur, &spotScale, &spotOffset);
176 // light dir is in device space, so need to map spot offset back into local space
177 SkMatrix inverse;
178 if (ctm.invert(&inverse)) {
179 inverse.mapVectors(&spotOffset, 1);
180 }
181 } else {
182 SkDrawShadowMetrics::GetSpotParams(occluderZ, rec.fLightPos.fX, rec.fLightPos.fY,
183 rec.fLightPos.fZ, rec.fLightRadius,
184 &spotBlur, &spotScale, &spotOffset);
185 }
186
187 // convert spot blur to local space
188 spotBlur *= devToSrcScale;
189 }
190
191 // in both cases, adjust ambient and spot bounds
192 SkRect spotBounds = ambientBounds;
193 ambientBounds.outset(ambientBlur, ambientBlur);
194 spotBounds.fLeft *= spotScale;
195 spotBounds.fTop *= spotScale;
196 spotBounds.fRight *= spotScale;
197 spotBounds.fBottom *= spotScale;
198 spotBounds.offset(spotOffset.fX, spotOffset.fY);
199 spotBounds.outset(spotBlur, spotBlur);
200
201 // merge bounds
202 *bounds = ambientBounds;
203 bounds->join(spotBounds);
204 // outset a bit to account for floating point error
205 bounds->outset(1, 1);
206
207 // if perspective, transform back to src space
208 if (ctm.hasPerspective()) {
209 // TODO: create tighter mapping from dev rect back to src rect
210 SkMatrix inverse;
211 if (ctm.invert(&inverse)) {
212 inverse.mapRect(bounds);
213 }
214 }
215 }
216
217
218 } // namespace SkDrawShadowMetrics
219
220