From f3d22aaf83bb1412992e86e43f8e2de45380b7d3 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 9 Feb 2017 10:25:33 -0800 Subject: [PATCH] Export the Skia ShadowMaskFilter API (flutter/engine#3401) See https://github.com/flutter/flutter/issues/6807 --- engine/src/flutter/lib/ui/painting.dart | 44 ++++++++++++++++++- .../flutter/lib/ui/painting/mask_filter.cc | 29 ++++++++++-- .../src/flutter/lib/ui/painting/mask_filter.h | 10 ++++- 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 0347e57b63..9f859981a1 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -861,9 +861,49 @@ class MaskFilter extends NativeFieldWrapperClass2 { /// /// A blur is an expensive operation and should therefore be used sparingly. MaskFilter.blur(BlurStyle style, double sigma) { - _constructor(style.index, sigma); + _constructorBlur(style.index, sigma); } - void _constructor(int style, double sigma) native "MaskFilter_constructor"; + void _constructorBlur(int style, double sigma) native "MaskFilter_constructorBlur"; + + /// Creates a mask filter that implements a pair of shadows for an occluding + /// object - one representing ambient occlusion, and one representing a + /// displaced shadow from a point light. + /// + /// `occluderHeight`: Height of occluding object off of ground plane. + /// + /// `lightPos`: Position of the light applied to this object. + /// + /// `lightRadius`: Radius of the light (light is assumed to be spherical). + /// + /// `ambientAlpha`: Base opacity of the ambient occlusion shadow. + /// + /// `spotAlpha`: Base opacity of the displaced spot shadow. + MaskFilter.shadow({ + double occluderHeight: 0.0, + double lightPosX: 0.0, + double lightPosY: 0.0, + double lightPosZ: 0.0, + double lightRadius: 0.0, + double ambientAlpha: 0.0, + double spotAlpha: 0.0, + }) { + if (!(occluderHeight > 0.0 && + lightPosZ > occluderHeight && + lightRadius > 0.0 && + ambientAlpha >= 0.0 && + spotAlpha >= 0.0)) + throw new ArgumentError("Invalid shadow mask filter parameters"); + + _constructorShadow(occluderHeight, lightPosX, lightPosY, lightPosZ, + lightRadius, ambientAlpha, spotAlpha); + } + void _constructorShadow(double occluderHeight, + double lightPosX, + double lightPosY, + double lightPosZ, + double lightRadius, + double ambientAlpha, + double spotAlpha) native "MaskFilter_constructorShadow"; } /// A description of a color filter to apply when drawing a shape or compositing diff --git a/engine/src/flutter/lib/ui/painting/mask_filter.cc b/engine/src/flutter/lib/ui/painting/mask_filter.cc index 0e6866ae44..26a7fc62ff 100644 --- a/engine/src/flutter/lib/ui/painting/mask_filter.cc +++ b/engine/src/flutter/lib/ui/painting/mask_filter.cc @@ -11,26 +11,47 @@ #include "lib/tonic/converter/dart_converter.h" #include "lib/tonic/dart_library_natives.h" #include "third_party/skia/include/effects/SkBlurMaskFilter.h" +#include "third_party/skia/include/effects/SkShadowMaskFilter.h" namespace blink { -static void MaskFilter_constructor(Dart_NativeArguments args) { - DartCallConstructor(&MaskFilter::Create, args); +static void MaskFilter_constructorBlur(Dart_NativeArguments args) { + DartCallConstructor(&MaskFilter::CreateBlur, args); +} + +static void MaskFilter_constructorShadow(Dart_NativeArguments args) { + DartCallConstructor(&MaskFilter::CreateShadow, args); } IMPLEMENT_WRAPPERTYPEINFO(ui, MaskFilter); void MaskFilter::RegisterNatives(tonic::DartLibraryNatives* natives) { natives->Register({ - {"MaskFilter_constructor", MaskFilter_constructor, 3, true}, + {"MaskFilter_constructorBlur", MaskFilter_constructorBlur, 3, true}, + {"MaskFilter_constructorShadow", MaskFilter_constructorShadow, 8, true}, }); } -ftl::RefPtr MaskFilter::Create(unsigned style, double sigma) { +ftl::RefPtr MaskFilter::CreateBlur(unsigned style, double sigma) { return ftl::MakeRefCounted( SkBlurMaskFilter::Make(static_cast(style), sigma)); } +ftl::RefPtr MaskFilter::CreateShadow(double occluderHeight, + double lightPosX, + double lightPosY, + double lightPosZ, + double lightRadius, + double ambientAlpha, + double spotAlpha) { + return ftl::MakeRefCounted( + SkShadowMaskFilter::Make(occluderHeight, + SkPoint3::Make(lightPosX, lightPosY, lightPosZ), + lightRadius, + ambientAlpha, + spotAlpha)); +} + MaskFilter::MaskFilter(sk_sp filter) : filter_(std::move(filter)) {} diff --git a/engine/src/flutter/lib/ui/painting/mask_filter.h b/engine/src/flutter/lib/ui/painting/mask_filter.h index 902be41c83..410217691f 100644 --- a/engine/src/flutter/lib/ui/painting/mask_filter.h +++ b/engine/src/flutter/lib/ui/painting/mask_filter.h @@ -23,7 +23,15 @@ class MaskFilter : public ftl::RefCountedThreadSafe, public: ~MaskFilter() override; - static ftl::RefPtr Create(unsigned style, double sigma); + static ftl::RefPtr CreateBlur(unsigned style, double sigma); + + static ftl::RefPtr CreateShadow(double occluderHeight, + double lightPosX, + double lightPosY, + double lightPosZ, + double lightRadius, + double ambientAlpha, + double spotAlpha); const sk_sp& filter() { return filter_; }