Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 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 | SkScalar fAmbientAlpha; |
| 24 | SkScalar fSpotAlpha; |
| 25 | SkColor fColor; |
| 26 | uint32_t fFlags; |
| 27 | }; |
| 28 | |
| 29 | namespace SkDrawShadowMetrics { |
| 30 | |
| 31 | static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f; |
| 32 | static constexpr auto kAmbientGeomFactor = 64.0f; |
| 33 | |
| 34 | inline SkScalar AmbientBlurRadius(SkScalar height) { |
| 35 | return height*kAmbientHeightFactor*kAmbientGeomFactor; |
| 36 | } |
| 37 | |
| 38 | inline SkScalar AmbientRecipAlpha(SkScalar height) { |
| 39 | return 1.0f + SkTMax(height*kAmbientHeightFactor, 0.0f); |
| 40 | } |
| 41 | |
| 42 | inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) { |
| 43 | return lightRadius*SkTPin(occluderZ / (lightZ - occluderZ), 0.0f, 0.95f); |
| 44 | } |
| 45 | |
| 46 | inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ, |
| 47 | SkScalar lightRadius, |
| 48 | SkScalar* blurRadius, SkScalar* scale, SkVector* translate) { |
| 49 | SkScalar zRatio = SkTPin(occluderZ / (lightZ - occluderZ), 0.0f, 0.95f); |
| 50 | *blurRadius = lightRadius*zRatio; |
| 51 | *scale = SkTMax(lightZ / (lightZ - occluderZ), 1.0f); |
| 52 | *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY); |
| 53 | } |
| 54 | |
| 55 | // get bounds prior to the ctm being applied |
| 56 | void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds); |
| 57 | |
| 58 | } |
| 59 | |
| 60 | #endif |