Flutter Engine
The Flutter Engine
SkDrawShadowInfo.h
Go to the documentation of this file.
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
18
19#include <algorithm>
20#include <cstdint>
21
22class SkMatrix;
23class SkPath;
24struct SkRect;
25
32 uint32_t fFlags;
33};
34
35namespace SkDrawShadowMetrics {
36
37static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f;
38static constexpr auto kAmbientGeomFactor = 64.0f;
39// Assuming that we have a light height of 600 for the spot shadow,
40// the spot values will reach their maximum at a height of approximately 292.3077.
41// We'll round up to 300 to keep it simple.
43
44static inline float divide_and_pin(float numer, float denom, float min, float max) {
45 float result = SkTPin(sk_ieee_float_divide(numer, denom), min, max);
46 // ensure that SkTPin handled non-finites correctly
47 SkASSERT(result >= min && result <= max);
48 return result;
49}
50
53}
54
56 return 1.0f + std::max(height*kAmbientHeightFactor, 0.0f);
57}
58
59inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) {
60 return lightRadius*divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
61}
62
63inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ,
64 SkScalar lightRadius,
65 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
66 SkScalar zRatio = divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
67 *blurRadius = lightRadius*zRatio;
68 *scale = divide_and_pin(lightZ, lightZ - occluderZ, 1.0f, 1.95f);
69 *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
70}
71
72inline void GetDirectionalParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY,
73 SkScalar lightZ, SkScalar lightRadius,
74 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
75 *blurRadius = lightRadius*occluderZ;
76 *scale = 1;
77 // Max z-ratio is "max expected elevation"/"min allowable z"
78 constexpr SkScalar kMaxZRatio = 64/SK_ScalarNearlyZero;
79 SkScalar zRatio = divide_and_pin(occluderZ, lightZ, 0.0f, kMaxZRatio);
80 *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
81}
82
83// Create the transformation to apply to a path to get its base shadow outline, given the light
84// parameters and the path's 3D transformation (given by ctm and zPlaneParams).
85// Also computes the blur radius to apply the transformed outline.
86bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius,
87 const SkMatrix& ctm, const SkPoint3& zPlaneParams,
88 const SkRect& pathBounds, bool directional,
89 SkMatrix* shadowTransform, SkScalar* radius);
90
91// get bounds prior to the ctm being applied
92void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds);
93
94} // namespace SkDrawShadowMetrics
95
96#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
static constexpr float sk_ieee_float_divide(float numer, float denom)
#define SK_ScalarNearlyZero
Definition: SkScalar.h:99
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
Definition: SkPath.h:59
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
bool GetSpotShadowTransform(const SkPoint3 &lightPos, SkScalar lightRadius, const SkMatrix &ctm, const SkPoint3 &zPlaneParams, const SkRect &pathBounds, bool directional, SkMatrix *shadowTransform, SkScalar *radius)
static constexpr auto kMaxAmbientRadius
static float divide_and_pin(float numer, float denom, float min, float max)
static constexpr auto kAmbientHeightFactor
void GetLocalBounds(const SkPath &path, const SkDrawShadowRec &rec, const SkMatrix &ctm, SkRect *bounds)
SkScalar AmbientRecipAlpha(SkScalar height)
static constexpr auto kAmbientGeomFactor
void GetDirectionalParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ, SkScalar lightRadius, SkScalar *blurRadius, SkScalar *scale, SkVector *translate)
SkScalar AmbientBlurRadius(SkScalar height)
void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ, SkScalar lightRadius, SkScalar *blurRadius, SkScalar *scale, SkVector *translate)
SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius)
Optional< SkRect > bounds
Definition: SkRecords.h:189
int32_t height
const Scalar scale
SkPoint3 fZPlaneParams
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173