[flow] Switch to directional shadows (flutter/engine#27124)
This commit is contained in:
@@ -340,22 +340,8 @@ void DisplayListBoundsCalculator::drawShadow(const SkPath& path,
|
||||
const SkScalar elevation,
|
||||
bool occludes,
|
||||
SkScalar dpr) {
|
||||
// Constants from physical_shape_layer.cc
|
||||
const SkScalar kLightHeight = 600;
|
||||
const SkScalar kLightRadius = 800;
|
||||
|
||||
SkShadowFlags flags = occludes
|
||||
? SkShadowFlags::kTransparentOccluder_ShadowFlag
|
||||
: SkShadowFlags::kNone_ShadowFlag;
|
||||
const SkRect& bounds = path.getBounds();
|
||||
SkScalar shadow_x = (bounds.left() + bounds.right()) / 2;
|
||||
SkScalar shadow_y = bounds.top() - 600.0f;
|
||||
SkRect shadow_bounds;
|
||||
SkShadowUtils::GetLocalBounds(
|
||||
matrix(), path, SkPoint3::Make(0, 0, dpr * elevation),
|
||||
SkPoint3::Make(shadow_x, shadow_y, dpr * kLightHeight),
|
||||
dpr * kLightRadius, flags, &shadow_bounds);
|
||||
accumulateRect(shadow_bounds);
|
||||
accumulateRect(
|
||||
PhysicalShapeLayer::ComputeShadowBounds(path, elevation, dpr, matrix()));
|
||||
}
|
||||
|
||||
void DisplayListBoundsCalculator::accumulateRect(const SkRect& rect,
|
||||
|
||||
@@ -294,8 +294,8 @@ TEST_F(CheckerBoardLayerTest, PhysicalSaveLayerNotCheckBoard) {
|
||||
// The Fuchsia system compositor handles all elevated PhysicalShapeLayers and
|
||||
// their shadows , so we do not do any painting there.
|
||||
EXPECT_EQ(layer->paint_bounds(),
|
||||
PhysicalShapeLayer::ComputeShadowBounds(layer_path.getBounds(),
|
||||
initial_elevation, 1.0f));
|
||||
PhysicalShapeLayer::ComputeShadowBounds(
|
||||
layer_path, initial_elevation, 1.0f, SkMatrix()));
|
||||
EXPECT_TRUE(layer->needs_painting(paint_context()));
|
||||
EXPECT_EQ(layer->elevation(), initial_elevation);
|
||||
|
||||
@@ -333,8 +333,8 @@ TEST_F(CheckerBoardLayerTest, PhysicalSaveLayerCheckBoard) {
|
||||
// The Fuchsia system compositor handles all elevated PhysicalShapeLayers and
|
||||
// their shadows , so we do not do any painting there.
|
||||
EXPECT_EQ(layer->paint_bounds(),
|
||||
PhysicalShapeLayer::ComputeShadowBounds(layer_path.getBounds(),
|
||||
initial_elevation, 1.0f));
|
||||
PhysicalShapeLayer::ComputeShadowBounds(
|
||||
layer_path, initial_elevation, 1.0f, SkMatrix()));
|
||||
EXPECT_TRUE(layer->needs_painting(paint_context()));
|
||||
EXPECT_EQ(layer->elevation(), initial_elevation);
|
||||
|
||||
|
||||
@@ -41,8 +41,9 @@ void PhysicalShapeLayer::Diff(DiffContext* context, const Layer* old_layer) {
|
||||
if (elevation_ == 0) {
|
||||
bounds = path_.getBounds();
|
||||
} else {
|
||||
bounds = ComputeShadowBounds(path_.getBounds(), elevation_,
|
||||
context->frame_device_pixel_ratio());
|
||||
bounds = ComputeShadowBounds(path_, elevation_,
|
||||
context->frame_device_pixel_ratio(),
|
||||
context->GetTransform());
|
||||
}
|
||||
|
||||
context->AddLayerBounds(bounds);
|
||||
@@ -70,8 +71,8 @@ void PhysicalShapeLayer::Preroll(PrerollContext* context,
|
||||
// We will draw the shadow in Paint(), so add some margin to the paint
|
||||
// bounds to leave space for the shadow. We fill this whole region and clip
|
||||
// children to it so we don't need to join the child paint bounds.
|
||||
set_paint_bounds(ComputeShadowBounds(path_.getBounds(), elevation_,
|
||||
context->frame_device_pixel_ratio));
|
||||
set_paint_bounds(ComputeShadowBounds(
|
||||
path_, elevation_, context->frame_device_pixel_ratio, matrix));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,47 +128,15 @@ void PhysicalShapeLayer::Paint(PaintContext& context) const {
|
||||
}
|
||||
}
|
||||
|
||||
SkRect PhysicalShapeLayer::ComputeShadowBounds(const SkRect& bounds,
|
||||
SkRect PhysicalShapeLayer::ComputeShadowBounds(const SkPath& path,
|
||||
float elevation,
|
||||
float pixel_ratio) {
|
||||
// The shadow offset is calculated as follows:
|
||||
// .--- (kLightRadius)
|
||||
// -------/ (light)
|
||||
// | /
|
||||
// | /
|
||||
// |/
|
||||
// |O
|
||||
// /| (kLightHeight)
|
||||
// / |
|
||||
// / |
|
||||
// / |
|
||||
// / |
|
||||
// ------------- (layer)
|
||||
// /| |
|
||||
// / | | (elevation)
|
||||
// A / | |B
|
||||
// ------------------------------------------------ (canvas)
|
||||
// --- (extent of shadow)
|
||||
//
|
||||
// E = lt } t = (r + w/2)/h
|
||||
// } =>
|
||||
// r + w/2 = ht } E = (l/h)(r + w/2)
|
||||
//
|
||||
// Where: E = extent of shadow
|
||||
// l = elevation of layer
|
||||
// r = radius of the light source
|
||||
// w = width of the layer
|
||||
// h = light height
|
||||
// t = tangent of AOB, i.e., multiplier for elevation to extent
|
||||
// tangent for x
|
||||
double tx =
|
||||
(kLightRadius * pixel_ratio + bounds.width() * 0.5) / kLightHeight;
|
||||
// tangent for y
|
||||
double ty =
|
||||
(kLightRadius * pixel_ratio + bounds.height() * 0.5) / kLightHeight;
|
||||
SkRect shadow_bounds(bounds);
|
||||
shadow_bounds.outset(elevation * tx, elevation * ty);
|
||||
|
||||
SkScalar dpr,
|
||||
const SkMatrix& ctm) {
|
||||
SkRect shadow_bounds(path.getBounds());
|
||||
SkShadowUtils::GetLocalBounds(
|
||||
ctm, path, SkPoint3::Make(0, 0, dpr * elevation),
|
||||
SkPoint3::Make(0, -1, 1), kLightRadius / kLightHeight,
|
||||
SkShadowFlags::kDirectionalLight_ShadowFlag, &shadow_bounds);
|
||||
return shadow_bounds;
|
||||
}
|
||||
|
||||
@@ -180,21 +149,19 @@ void PhysicalShapeLayer::DrawShadow(SkCanvas* canvas,
|
||||
const SkScalar kAmbientAlpha = 0.039f;
|
||||
const SkScalar kSpotAlpha = 0.25f;
|
||||
|
||||
SkShadowFlags flags = transparentOccluder
|
||||
? SkShadowFlags::kTransparentOccluder_ShadowFlag
|
||||
: SkShadowFlags::kNone_ShadowFlag;
|
||||
const SkRect& bounds = path.getBounds();
|
||||
SkScalar shadow_x = (bounds.left() + bounds.right()) / 2;
|
||||
SkScalar shadow_y = bounds.top() - 600.0f;
|
||||
uint32_t flags = transparentOccluder
|
||||
? SkShadowFlags::kTransparentOccluder_ShadowFlag
|
||||
: SkShadowFlags::kNone_ShadowFlag;
|
||||
flags |= SkShadowFlags::kDirectionalLight_ShadowFlag;
|
||||
SkColor inAmbient = SkColorSetA(color, kAmbientAlpha * SkColorGetA(color));
|
||||
SkColor inSpot = SkColorSetA(color, kSpotAlpha * SkColorGetA(color));
|
||||
SkColor ambientColor, spotColor;
|
||||
SkShadowUtils::ComputeTonalColors(inAmbient, inSpot, &ambientColor,
|
||||
&spotColor);
|
||||
SkShadowUtils::DrawShadow(
|
||||
canvas, path, SkPoint3::Make(0, 0, dpr * elevation),
|
||||
SkPoint3::Make(shadow_x, shadow_y, dpr * kLightHeight),
|
||||
dpr * kLightRadius, ambientColor, spotColor, flags);
|
||||
SkShadowUtils::DrawShadow(canvas, path, SkPoint3::Make(0, 0, dpr * elevation),
|
||||
SkPoint3::Make(0, -1, 1),
|
||||
kLightRadius / kLightHeight, ambientColor,
|
||||
spotColor, flags);
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -17,9 +17,10 @@ class PhysicalShapeLayer : public ContainerLayer {
|
||||
const SkPath& path,
|
||||
Clip clip_behavior);
|
||||
|
||||
static SkRect ComputeShadowBounds(const SkRect& bounds,
|
||||
static SkRect ComputeShadowBounds(const SkPath& path,
|
||||
float elevation,
|
||||
float pixel_ratio);
|
||||
SkScalar dpr,
|
||||
const SkMatrix& ctm);
|
||||
static void DrawShadow(SkCanvas* canvas,
|
||||
const SkPath& path,
|
||||
SkColor color,
|
||||
|
||||
@@ -124,8 +124,8 @@ TEST_F(PhysicalShapeLayerTest, ElevationSimple) {
|
||||
// The Fuchsia system compositor handles all elevated PhysicalShapeLayers and
|
||||
// their shadows , so we do not do any painting there.
|
||||
EXPECT_EQ(layer->paint_bounds(),
|
||||
PhysicalShapeLayer::ComputeShadowBounds(layer_path.getBounds(),
|
||||
initial_elevation, 1.0f));
|
||||
PhysicalShapeLayer::ComputeShadowBounds(
|
||||
layer_path, initial_elevation, 1.0f, SkMatrix()));
|
||||
EXPECT_TRUE(layer->needs_painting(paint_context()));
|
||||
EXPECT_EQ(layer->elevation(), initial_elevation);
|
||||
|
||||
@@ -174,8 +174,8 @@ TEST_F(PhysicalShapeLayerTest, ElevationComplex) {
|
||||
// there.
|
||||
EXPECT_EQ(layers[i]->paint_bounds(),
|
||||
(PhysicalShapeLayer::ComputeShadowBounds(
|
||||
layer_path.getBounds(), initial_elevations[i],
|
||||
1.0f /* pixel_ratio */)));
|
||||
layer_path, initial_elevations[i], 1.0f /* pixel_ratio */,
|
||||
SkMatrix())));
|
||||
EXPECT_TRUE(layers[i]->needs_painting(paint_context()));
|
||||
}
|
||||
|
||||
@@ -200,6 +200,93 @@ TEST_F(PhysicalShapeLayerTest, ElevationComplex) {
|
||||
0, MockCanvas::DrawPathData{layer_path, layer_paint}}}));
|
||||
}
|
||||
|
||||
TEST_F(PhysicalShapeLayerTest, ShadowNotDependsCtm) {
|
||||
constexpr SkScalar elevations[] = {1, 2, 3, 4, 5, 10};
|
||||
constexpr SkScalar scales[] = {0.5, 1, 1.5, 2, 3, 5};
|
||||
constexpr SkScalar translates[] = {0, 1, -1, 0.5, 2, 10};
|
||||
|
||||
SkPath path;
|
||||
path.addRect(0, 0, 8, 8).close();
|
||||
|
||||
for (SkScalar elevation : elevations) {
|
||||
SkRect baseline_bounds = PhysicalShapeLayer::ComputeShadowBounds(
|
||||
path, elevation, 1.0f, SkMatrix());
|
||||
for (SkScalar scale : scales) {
|
||||
for (SkScalar translateX : translates) {
|
||||
for (SkScalar translateY : translates) {
|
||||
SkMatrix ctm;
|
||||
ctm.setScaleTranslate(scale, scale, translateX, translateY);
|
||||
SkRect bounds = PhysicalShapeLayer::ComputeShadowBounds(
|
||||
path, elevation, scale, ctm);
|
||||
EXPECT_FLOAT_EQ(bounds.fLeft, baseline_bounds.fLeft);
|
||||
EXPECT_FLOAT_EQ(bounds.fTop, baseline_bounds.fTop);
|
||||
EXPECT_FLOAT_EQ(bounds.fRight, baseline_bounds.fRight);
|
||||
EXPECT_FLOAT_EQ(bounds.fBottom, baseline_bounds.fBottom);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int RasterizedDifferenceInPixels(
|
||||
const std::function<void(SkCanvas*)>& actual_draw_function,
|
||||
const std::function<void(SkCanvas*)>& expected_draw_function,
|
||||
const SkSize& canvas_size) {
|
||||
sk_sp<SkSurface> actual_surface =
|
||||
SkSurface::MakeRasterN32Premul(canvas_size.width(), canvas_size.height());
|
||||
sk_sp<SkSurface> expected_surface =
|
||||
SkSurface::MakeRasterN32Premul(canvas_size.width(), canvas_size.height());
|
||||
|
||||
actual_surface->getCanvas()->drawColor(SK_ColorWHITE);
|
||||
expected_surface->getCanvas()->drawColor(SK_ColorWHITE);
|
||||
|
||||
actual_draw_function(actual_surface->getCanvas());
|
||||
expected_draw_function(expected_surface->getCanvas());
|
||||
|
||||
SkPixmap actual_pixels;
|
||||
EXPECT_TRUE(actual_surface->peekPixels(&actual_pixels));
|
||||
|
||||
SkPixmap expected_pixels;
|
||||
EXPECT_TRUE(expected_surface->peekPixels(&expected_pixels));
|
||||
|
||||
int different_pixels = 0;
|
||||
for (int y = 0; y < canvas_size.height(); y++) {
|
||||
const uint32_t* actual_row = actual_pixels.addr32(0, y);
|
||||
const uint32_t* expected_row = expected_pixels.addr32(0, y);
|
||||
for (int x = 0; x < canvas_size.width(); x++) {
|
||||
if (actual_row[x] != expected_row[x]) {
|
||||
different_pixels++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return different_pixels;
|
||||
}
|
||||
|
||||
TEST_F(PhysicalShapeLayerTest, ShadowNotDependsPathSize) {
|
||||
constexpr SkRect test_cases[][2] = {
|
||||
{{20, -100, 80, 80}, {20, -1000, 80, 80}},
|
||||
{{20, 20, 80, 200}, {20, 20, 80, 2000}},
|
||||
};
|
||||
|
||||
for (const SkRect* test_case : test_cases) {
|
||||
EXPECT_EQ(RasterizedDifferenceInPixels(
|
||||
[=](SkCanvas* canvas) {
|
||||
SkPath path;
|
||||
path.addRect(test_case[0]).close();
|
||||
PhysicalShapeLayer::DrawShadow(canvas, path, SK_ColorBLACK,
|
||||
1.0f, false, 1.0f);
|
||||
},
|
||||
[=](SkCanvas* canvas) {
|
||||
SkPath path;
|
||||
path.addRect(test_case[1]).close();
|
||||
PhysicalShapeLayer::DrawShadow(canvas, path, SK_ColorBLACK,
|
||||
1.0f, false, 1.0f);
|
||||
},
|
||||
SkSize::Make(100, 100)),
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
||||
static bool ReadbackResult(PrerollContext* context,
|
||||
Clip clip_behavior,
|
||||
std::shared_ptr<Layer> child,
|
||||
|
||||
Reference in New Issue
Block a user