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 "SkColor.h" 12 #include "SkPoint.h" 13 #include "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 }; 27 28 namespace SkDrawShadowMetrics { 29 30 static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f; 31 static constexpr auto kAmbientGeomFactor = 64.0f; 32 // Assuming that we have a light height of 600 for the spot shadow, 33 // the spot values will reach their maximum at a height of approximately 292.3077. 34 // We'll round up to 300 to keep it simple. 35 static constexpr auto kMaxAmbientRadius = 300*kAmbientHeightFactor*kAmbientGeomFactor; 36 37 static inline float divide_and_pin(float numer, float denom, float min, float max) { 38 float result = SkTPin(sk_ieee_float_divide(numer, denom), min, max); 39 // ensure that SkTPin handled non-finites correctly 40 SkASSERT(result >= min && result <= max); 41 return result; 42 } 43 44 inline SkScalar AmbientBlurRadius(SkScalar height) { 45 return SkTMin(height*kAmbientHeightFactor*kAmbientGeomFactor, kMaxAmbientRadius); 46 } 47 48 inline SkScalar AmbientRecipAlpha(SkScalar height) { 49 return 1.0f + SkTMax(height*kAmbientHeightFactor, 0.0f); 50 } 51 52 inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) { 53 return lightRadius*divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f); 54 } 55 56 inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ, 57 SkScalar lightRadius, 58 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) { 59 SkScalar zRatio = divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f); 60 *blurRadius = lightRadius*zRatio; 61 *scale = divide_and_pin(lightZ, lightZ - occluderZ, 1.0f, 1.95f); 62 *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY); 63 } 64 65 // Create the transformation to apply to a path to get its base shadow outline, given the light 66 // parameters and the path's 3D transformation (given by ctm and zPlaneParams). 67 // Also computes the blur radius to apply the transformed outline. 68 bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius, 69 const SkMatrix& ctm, const SkPoint3& zPlaneParams, 70 const SkRect& pathBounds, SkMatrix* shadowTransform, SkScalar* radius); 71 72 // get bounds prior to the ctm being applied 73 void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds); 74 75 } 76 77 #endif 78