Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions flow/layers/physical_shape_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace flow {

const SkScalar kLightHeight = 600;
const SkScalar kLightRadius = 800;

PhysicalShapeLayer::PhysicalShapeLayer(Clip clip_behavior)
: isRect_(false), clip_behavior_(clip_behavior) {}

Expand Down Expand Up @@ -51,11 +54,36 @@ void PhysicalShapeLayer::Preroll(PrerollContext* context,
set_needs_system_composite(true);
#else
// Add some margin to the paint bounds to leave space for the shadow.
// The margin is hardcoded to an arbitrary maximum for now because Skia
// doesn't provide a way to calculate it. We fill this whole region
// and clip children to it so we don't need to join the child paint bounds.
// We fill this whole region and clip children to it so we don't need to
// join the child paint bounds.
// The offset is calculated as follows:

// .-- (kLightRadius = 800)
// ----- (light)
// | (kLightHeight = 600)
// ------------- (layer)
// |
// | (elevation)
// |
// ------------------------------------------------ (canvas)
// ----------- (extent of shadow)
//
// E = lx } x = (r + w/2)/h
// } =>
// r + w/2 = hx } E = (l/h)(r + w/2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! It would be better to add a few legends about what E, x, h, l mean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//
// Where: E = extent of shadow
// l = elevation of layer
// r = radius of the light source
// w = width of the layer
// h = light height
// x = multiplier for elevation to extent
SkRect bounds(path_.getBounds());
bounds.outset(20.0, 20.0);
double ex = (kLightRadius * device_pixel_ratio_ + bounds.width() * 0.5) /
kLightHeight;
double ey = (kLightRadius * device_pixel_ratio_ + bounds.height() * 0.5) /
kLightHeight;
bounds.outset(elevation_ * ex, elevation_ * ey);
set_paint_bounds(bounds);
#endif // defined(OS_FUCHSIA)
}
Expand Down Expand Up @@ -145,8 +173,6 @@ void PhysicalShapeLayer::DrawShadow(SkCanvas* canvas,
SkScalar dpr) {
const SkScalar kAmbientAlpha = 0.039f;
const SkScalar kSpotAlpha = 0.25f;
const SkScalar kLightHeight = 600;
const SkScalar kLightRadius = 800;

SkShadowFlags flags = transparentOccluder
? SkShadowFlags::kTransparentOccluder_ShadowFlag
Expand Down