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