SkDrawShadowInfo.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef SkDrawShadowInfo_DEFINED
  8. #define SkDrawShadowInfo_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkPoint.h"
  11. #include "include/core/SkPoint3.h"
  12. class SkMatrix;
  13. class SkPath;
  14. struct SkRect;
  15. struct SkDrawShadowRec {
  16. SkPoint3 fZPlaneParams;
  17. SkPoint3 fLightPos;
  18. SkScalar fLightRadius;
  19. SkColor fAmbientColor;
  20. SkColor fSpotColor;
  21. uint32_t fFlags;
  22. };
  23. namespace SkDrawShadowMetrics {
  24. static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f;
  25. static constexpr auto kAmbientGeomFactor = 64.0f;
  26. // Assuming that we have a light height of 600 for the spot shadow,
  27. // the spot values will reach their maximum at a height of approximately 292.3077.
  28. // We'll round up to 300 to keep it simple.
  29. static constexpr auto kMaxAmbientRadius = 300*kAmbientHeightFactor*kAmbientGeomFactor;
  30. static inline float divide_and_pin(float numer, float denom, float min, float max) {
  31. float result = SkTPin(sk_ieee_float_divide(numer, denom), min, max);
  32. // ensure that SkTPin handled non-finites correctly
  33. SkASSERT(result >= min && result <= max);
  34. return result;
  35. }
  36. inline SkScalar AmbientBlurRadius(SkScalar height) {
  37. return SkTMin(height*kAmbientHeightFactor*kAmbientGeomFactor, kMaxAmbientRadius);
  38. }
  39. inline SkScalar AmbientRecipAlpha(SkScalar height) {
  40. return 1.0f + SkTMax(height*kAmbientHeightFactor, 0.0f);
  41. }
  42. inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) {
  43. return lightRadius*divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
  44. }
  45. inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ,
  46. SkScalar lightRadius,
  47. SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
  48. SkScalar zRatio = divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
  49. *blurRadius = lightRadius*zRatio;
  50. *scale = divide_and_pin(lightZ, lightZ - occluderZ, 1.0f, 1.95f);
  51. *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
  52. }
  53. // Create the transformation to apply to a path to get its base shadow outline, given the light
  54. // parameters and the path's 3D transformation (given by ctm and zPlaneParams).
  55. // Also computes the blur radius to apply the transformed outline.
  56. bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius,
  57. const SkMatrix& ctm, const SkPoint3& zPlaneParams,
  58. const SkRect& pathBounds, SkMatrix* shadowTransform, SkScalar* radius);
  59. // get bounds prior to the ctm being applied
  60. void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds);
  61. }
  62. #endif