diff --git a/flow/layers/physical_model_layer.cc b/flow/layers/physical_model_layer.cc index 24050be7f91109bc1e672c8d1af6a602612129e9..516a9394160ddc642ed1a448e819b4aa829c570e 100644 --- a/flow/layers/physical_model_layer.cc +++ b/flow/layers/physical_model_layer.cc @@ -50,16 +50,8 @@ void PhysicalModelLayer::Paint(PaintContext& context) { path.addRRect(rrect_); if (elevation_ != 0) { - SkShadowFlags flags = SkColorGetA(color_) == 0xff ? - SkShadowFlags::kNone_ShadowFlag : - SkShadowFlags::kTransparentOccluder_ShadowFlag; - SkShadowUtils::DrawShadow(&context.canvas, path, - elevation_ * 4, - SkPoint3::Make(0.0f, -700.0f, 2800.0f), - 2800.0f, - 0.25f, 0.25f, - SK_ColorBLACK, - flags); + DrawShadow(&context.canvas, path, SK_ColorBLACK, elevation_, + SkColorGetA(color_) != 0xff); } if (needs_system_composite()) @@ -79,4 +71,19 @@ void PhysicalModelLayer::Paint(PaintContext& context) { PaintChildren(context); } +void PhysicalModelLayer::DrawShadow(SkCanvas* canvas, const SkPath& path, + SkColor color, int elevation, + bool transparentOccluder) { + SkShadowFlags flags = transparentOccluder ? + SkShadowFlags::kTransparentOccluder_ShadowFlag : + SkShadowFlags::kNone_ShadowFlag; + SkShadowUtils::DrawShadow(canvas, path, + elevation * 4, + SkPoint3::Make(0.0f, -700.0f, 2800.0f), + 2800.0f, + 0.25f, 0.25f, + color, + flags); +} + } // namespace flow diff --git a/flow/layers/physical_model_layer.h b/flow/layers/physical_model_layer.h index 3271f79081f4d90287fe6a86422738a9181ac466..bdd10971f6d5bb2987061dbf10669030e2739f26 100644 --- a/flow/layers/physical_model_layer.h +++ b/flow/layers/physical_model_layer.h @@ -18,6 +18,9 @@ class PhysicalModelLayer : public ContainerLayer { void set_elevation(int elevation) { elevation_ = elevation; } void set_color(SkColor color) { color_ = color; } + static void DrawShadow(SkCanvas* canvas, const SkPath& path, + SkColor color, int elevation, bool transparentOccluder); + protected: void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) override; diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index 0347e57b63a380a7592f096ef02cbfc3e81dcf8b..e69a712a6d4c2abd4d31f18babdc330fc71c8512 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -1548,6 +1548,18 @@ class Canvas extends NativeFieldWrapperClass2 { Int32List colors, int blendMode, Float32List cullRect) native "Canvas_drawAtlas"; + + /// Draws a shadow for a [Path] representing the given material elevation. + /// + /// transparentOccluder should be true if the occluding object is not opaque. + void drawShadow(Path path, Color color, int elevation, bool transparentOccluder) { + _drawShadow(path, color.value, elevation, transparentOccluder); + } + + void _drawShadow(Path path, + int color, + int elevation, + bool transparentOccluder) native "Canvas_drawShadow"; } /// An object representing a sequence of recorded graphical operations. diff --git a/lib/ui/painting/canvas.cc b/lib/ui/painting/canvas.cc index f6f03dbae86dd47b8fbb1f7e0c0fce75ab429cfd..c63f23e2b2e708303c0545ba0eb349f5d4f13b41 100644 --- a/lib/ui/painting/canvas.cc +++ b/lib/ui/painting/canvas.cc @@ -6,6 +6,7 @@ #include +#include "flutter/flow/layers/physical_model_layer.h" #include "flutter/lib/ui/painting/image.h" #include "flutter/lib/ui/painting/matrix.h" #include "lib/tonic/converter/dart_converter.h" @@ -53,7 +54,8 @@ IMPLEMENT_WRAPPERTYPEINFO(ui, Canvas); V(Canvas, drawPicture) \ V(Canvas, drawPoints) \ V(Canvas, drawVertices) \ - V(Canvas, drawAtlas) + V(Canvas, drawAtlas) \ + V(Canvas, drawShadow) FOR_EACH_BINDING(DART_NATIVE_CALLBACK) @@ -395,6 +397,17 @@ void Canvas::drawAtlas(const Paint& paint, paint.paint()); } +void Canvas::drawShadow(const CanvasPath* path, + SkColor color, + int elevation, + bool transparentOccluder) { + flow::PhysicalModelLayer::DrawShadow(canvas_, + path->path(), + color, + elevation, + transparentOccluder); +} + void Canvas::Clear() { canvas_ = nullptr; } diff --git a/lib/ui/painting/canvas.h b/lib/ui/painting/canvas.h index 591c0182694e143c3c6c7cf3d82f07e06f7d8ca9..1eeffb5e33435868b2a64067845f5d1fe514b445 100644 --- a/lib/ui/painting/canvas.h +++ b/lib/ui/painting/canvas.h @@ -15,6 +15,7 @@ #include "lib/tonic/typed_data/float64_list.h" #include "lib/tonic/typed_data/int32_list.h" #include "third_party/skia/include/core/SkCanvas.h" +#include "third_party/skia/include/utils/SkShadowUtils.h" namespace tonic { class DartLibraryNatives; @@ -159,6 +160,11 @@ class Canvas : public ftl::RefCountedThreadSafe, SkBlendMode blend_mode, const tonic::Float32List& cull_rect); + void drawShadow(const CanvasPath* path, + SkColor color, + int elevation, + bool transparentOccluder); + SkCanvas* canvas() const { return canvas_; } void Clear(); bool IsRecording() const;