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