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