diff --git a/engine/src/flutter/impeller/aiks/canvas.cc b/engine/src/flutter/impeller/aiks/canvas.cc index 2e167659e6..b19e042c7d 100644 --- a/engine/src/flutter/impeller/aiks/canvas.cc +++ b/engine/src/flutter/impeller/aiks/canvas.cc @@ -83,6 +83,10 @@ void Canvas::Concat(const Matrix& xformation) { xformation_stack_.back().xformation = GetCurrentTransformation() * xformation; } +void Canvas::PreConcat(const Matrix& xformation) { + xformation_stack_.back().xformation = xformation * GetCurrentTransformation(); +} + void Canvas::ResetTransform() { xformation_stack_.back().xformation = {}; } @@ -227,8 +231,6 @@ void Canvas::RestoreClip() { GetCurrentPass().AddEntity(std::move(entity)); } -void Canvas::DrawShadow(Path path, Color color, Scalar elevation) {} - void Canvas::DrawPicture(Picture picture) { if (!picture.pass) { return; diff --git a/engine/src/flutter/impeller/aiks/canvas.h b/engine/src/flutter/impeller/aiks/canvas.h index 78f6fa9a0f..780fccefbc 100644 --- a/engine/src/flutter/impeller/aiks/canvas.h +++ b/engine/src/flutter/impeller/aiks/canvas.h @@ -54,6 +54,8 @@ class Canvas { void Concat(const Matrix& xformation); + void PreConcat(const Matrix& xformation); + void Translate(const Vector3& offset); void Scale(const Vector2& scale); @@ -89,8 +91,6 @@ class Canvas { Path path, Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect); - void DrawShadow(Path path, Color color, Scalar elevation); - void DrawPicture(Picture picture); void DrawTextFrame(TextFrame text_frame, Point position, Paint paint); diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 68739c5b98..cce7ee1f4a 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -4,6 +4,7 @@ #include "impeller/display_list/display_list_dispatcher.h" +#include #include #include @@ -1061,7 +1062,66 @@ void DisplayListDispatcher::drawShadow(const SkPath& path, const SkScalar elevation, bool transparent_occluder, SkScalar dpr) { - UNIMPLEMENTED; + Color spot_color = ToColor(color); + spot_color.alpha *= 0.25; + + // Compute the spot color -- ported from SkShadowUtils::ComputeTonalColors. + { + Scalar max = + std::max(std::max(spot_color.red, spot_color.green), spot_color.blue); + Scalar min = + std::min(std::min(spot_color.red, spot_color.green), spot_color.blue); + Scalar luminance = (min + max) * 0.5; + + Scalar alpha_adjust = + (2.6f + (-2.66667f + 1.06667f * spot_color.alpha) * spot_color.alpha) * + spot_color.alpha; + Scalar color_alpha = + (3.544762f + (-4.891428f + 2.3466f * luminance) * luminance) * + luminance; + color_alpha = std::clamp(alpha_adjust * color_alpha, 0.0f, 1.0f); + + Scalar greyscale_alpha = + std::clamp(spot_color.alpha * (1 - 0.4f * luminance), 0.0f, 1.0f); + + Scalar color_scale = color_alpha * (1 - greyscale_alpha); + Scalar tonal_alpha = color_scale + greyscale_alpha; + Scalar unpremul_scale = color_scale / tonal_alpha; + spot_color = Color(unpremul_scale * spot_color.red, + unpremul_scale * spot_color.green, + unpremul_scale * spot_color.blue, tonal_alpha); + } + + Vector3 light_position(0, -1, 1); + Scalar occluder_z = dpr * elevation; + + constexpr Scalar kLightRadius = 800 / 600; // Light radius / light height + + Paint paint; + paint.style = Paint::Style::kFill; + paint.color = spot_color; + paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{ + .style = FilterContents::BlurStyle::kNormal, + .sigma = Radius{kLightRadius * occluder_z / + canvas_.GetCurrentTransformation().GetScale().y}, + }; + + canvas_.Save(); + canvas_.PreConcat( + Matrix::MakeTranslation(Vector2(0, -occluder_z * light_position.y))); + + SkRect rect; + SkRRect rrect; + if (path.isRect(&rect)) { + canvas_.DrawRect(ToRect(rect), std::move(paint)); + } else if (path.isRRect(&rrect) && rrect.isSimple()) { + canvas_.DrawRRect(ToRect(rrect.rect()), rrect.getSimpleRadii().fX, + std::move(paint)); + } else { + canvas_.DrawPath(ToPath(path), std::move(paint)); + } + + canvas_.Restore(); } Picture DisplayListDispatcher::EndRecordingAsPicture() { diff --git a/engine/src/flutter/impeller/display_list/display_list_unittests.cc b/engine/src/flutter/impeller/display_list/display_list_unittests.cc index 08e4bb352d..b887c22e49 100644 --- a/engine/src/flutter/impeller/display_list/display_list_unittests.cc +++ b/engine/src/flutter/impeller/display_list/display_list_unittests.cc @@ -16,6 +16,7 @@ #include "impeller/display_list/display_list_playground.h" #include "impeller/geometry/point.h" #include "impeller/playground/widgets.h" +#include "include/core/SkRRect.h" #include "third_party/imgui/imgui.h" #include "third_party/skia/include/core/SkClipOp.h" #include "third_party/skia/include/core/SkColor.h" @@ -493,5 +494,32 @@ TEST_P(DisplayListTest, CanDrawZeroLengthLine) { ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); } +TEST_P(DisplayListTest, CanDrawShadow) { + flutter::DisplayListBuilder builder; + std::array paths = { + SkPath{}.addRect(SkRect::MakeXYWH(0, 0, 200, 100)), + SkPath{}.addRRect( + SkRRect::MakeRectXY(SkRect::MakeXYWH(0, 0, 200, 100), 30, 30)), + SkPath{}.addCircle(100, 50, 50), + }; + builder.setColor(flutter::DlColor::kWhite()); + builder.drawPaint(); + builder.setColor(flutter::DlColor::kCyan()); + builder.translate(100, 100); + for (size_t x = 0; x < paths.size(); x++) { + builder.save(); + for (size_t y = 0; y < 5; y++) { + builder.drawShadow(paths[x], flutter::DlColor::kBlack(), 3 + y * 5, false, + 1); + builder.drawPath(paths[x]); + builder.translate(0, 200); + } + builder.restore(); + builder.translate(300, 0); + } + + ASSERT_TRUE(OpenPlaygroundHere(builder.Build())); +} + } // namespace testing } // namespace impeller