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 #ifndef SkDrawShadowInfo_DEFINED
9 #define SkDrawShadowInfo_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkPoint3.h"
14 #include "include/private/SkTPin.h"
15
16 class SkMatrix;
17 class SkPath;
18 struct SkRect;
19
20 struct SK_API SkDrawShadowRec {
21 SkPoint3 fZPlaneParams;
22 SkPoint3 fLightPos;
23 SkScalar fLightRadius;
24 SkColor fAmbientColor;
25 SkColor fSpotColor;
26 uint32_t fFlags;
27 bool isLimitElevation = false;
28
29 /** Writes text representation of SkDrawShadowRec to string.
30
31 @param desc the string storing a description of parameters.
32 @param depth the number of tabs preceding each line.
33 */
34 void dump(std::string& desc, int depth) const;
35 };
36
37 namespace SkDrawShadowMetrics {
38
39 static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f;
40 static constexpr auto kAmbientGeomFactor = 64.0f;
41 // Assuming that we have a light height of 600 for the spot shadow,
42 // the spot values will reach their maximum at a height of approximately 292.3077.
43 // We'll round up to 300 to keep it simple.
44 static constexpr auto kMaxAmbientRadius = 300*kAmbientHeightFactor*kAmbientGeomFactor;
45
divide_and_pin(float numer,float denom,float min,float max)46 static inline float divide_and_pin(float numer, float denom, float min, float max) {
47 float result = SkTPin(sk_ieee_float_divide(numer, denom), min, max);
48 // ensure that SkTPin handled non-finites correctly
49 SkASSERT(result >= min && result <= max);
50 return result;
51 }
52
AmbientBlurRadius(SkScalar height)53 inline SkScalar AmbientBlurRadius(SkScalar height) {
54 return std::min(height*kAmbientHeightFactor*kAmbientGeomFactor, kMaxAmbientRadius);
55 }
56
AmbientRecipAlpha(SkScalar height)57 inline SkScalar AmbientRecipAlpha(SkScalar height) {
58 return 1.0f + std::max(height*kAmbientHeightFactor, 0.0f);
59 }
60
SpotBlurRadius(SkScalar occluderZ,SkScalar lightZ,SkScalar lightRadius)61 inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) {
62 return lightRadius*divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
63 }
64
GetSpotParams(SkScalar occluderZ,SkScalar lightX,SkScalar lightY,SkScalar lightZ,SkScalar lightRadius,SkScalar * blurRadius,SkScalar * scale,SkVector * translate,bool isLimitElevation)65 inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ,
66 SkScalar lightRadius,
67 SkScalar* blurRadius, SkScalar* scale, SkVector* translate, bool isLimitElevation) {
68 SkScalar zRatio = divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
69 *blurRadius = lightRadius*zRatio;
70 *scale = divide_and_pin(lightZ, lightZ - occluderZ, 1.0f, 1.95f);
71
72 if (isLimitElevation) {
73 *scale = 1.0f;
74 zRatio = 0.0f;
75 }
76
77 *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
78 }
79
GetSpotParams(SkScalar occluderZ,SkScalar lightX,SkScalar lightY,SkScalar lightZ,SkScalar lightRadius,SkScalar * blurRadius,SkScalar * scale,SkVector * translate)80 inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ,
81 SkScalar lightRadius,
82 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
83
84 GetSpotParams(occluderZ, lightX, lightY, lightZ, lightRadius, blurRadius, scale, translate, false);
85 }
86
GetDirectionalParams(SkScalar occluderZ,SkScalar lightX,SkScalar lightY,SkScalar lightZ,SkScalar lightRadius,SkScalar * blurRadius,SkScalar * scale,SkVector * translate)87 inline void GetDirectionalParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY,
88 SkScalar lightZ, SkScalar lightRadius,
89 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
90 *blurRadius = lightRadius*occluderZ;
91 *scale = 1;
92 // Max z-ratio is "max expected elevation"/"min allowable z"
93 constexpr SkScalar kMaxZRatio = 64/SK_ScalarNearlyZero;
94 SkScalar zRatio = divide_and_pin(occluderZ, lightZ, 0.0f, kMaxZRatio);
95 *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
96 }
97
98 // Create the transformation to apply to a path to get its base shadow outline, given the light
99 // parameters and the path's 3D transformation (given by ctm and zPlaneParams).
100 // Also computes the blur radius to apply the transformed outline.
101 bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius,
102 const SkMatrix& ctm, const SkPoint3& zPlaneParams,
103 const SkRect& pathBounds, bool directional,
104 SkMatrix* shadowTransform, SkScalar* radius);
105
106 // get bounds prior to the ctm being applied
107 void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds);
108
109 } // namespace SkDrawShadowMetrics
110
111 #endif
112