From 73634e73bc44511aea417fb5867a96196c4f1e2b Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Thu, 11 Jan 2018 23:25:18 -0800 Subject: [PATCH] Convert MaskFilter to pure-Dart. (flutter/engine#4534) --- engine/src/flutter/lib/ui/BUILD.gn | 2 - engine/src/flutter/lib/ui/dart_ui.cc | 2 - engine/src/flutter/lib/ui/painting.dart | 84 +++++++++++++++---- .../flutter/lib/ui/painting/mask_filter.cc | 43 ---------- .../src/flutter/lib/ui/painting/mask_filter.h | 40 --------- engine/src/flutter/lib/ui/painting/paint.cc | 34 +++++--- engine/src/flutter/testing/run_tests.sh | 6 ++ 7 files changed, 98 insertions(+), 113 deletions(-) delete mode 100644 engine/src/flutter/lib/ui/painting/mask_filter.cc delete mode 100644 engine/src/flutter/lib/ui/painting/mask_filter.h diff --git a/engine/src/flutter/lib/ui/BUILD.gn b/engine/src/flutter/lib/ui/BUILD.gn index b5d4d4dc94..2862a9b68c 100644 --- a/engine/src/flutter/lib/ui/BUILD.gn +++ b/engine/src/flutter/lib/ui/BUILD.gn @@ -28,8 +28,6 @@ source_set("ui") { "painting/image_filter.h", "painting/image_shader.cc", "painting/image_shader.h", - "painting/mask_filter.cc", - "painting/mask_filter.h", "painting/matrix.cc", "painting/matrix.h", "painting/paint.cc", diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index 6853bfdec8..59d083a191 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -14,7 +14,6 @@ #include "flutter/lib/ui/painting/image.h" #include "flutter/lib/ui/painting/image_filter.h" #include "flutter/lib/ui/painting/image_shader.h" -#include "flutter/lib/ui/painting/mask_filter.h" #include "flutter/lib/ui/painting/path.h" #include "flutter/lib/ui/painting/picture.h" #include "flutter/lib/ui/painting/picture_recorder.h" @@ -59,7 +58,6 @@ void DartUI::InitForGlobal() { FrameInfo::RegisterNatives(g_natives); ImageFilter::RegisterNatives(g_natives); ImageShader::RegisterNatives(g_natives); - MaskFilter::RegisterNatives(g_natives); Paragraph::RegisterNatives(g_natives); ParagraphBuilder::RegisterNatives(g_natives); Picture::RegisterNatives(g_natives); diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 8a2129d381..cd191c3b90 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -562,6 +562,9 @@ class Paint { static const int _kColorFilterIndex = 9; static const int _kColorFilterColorIndex = 10; static const int _kColorFilterBlendModeIndex = 11; + static const int _kMaskFilterIndex = 12; + static const int _kMaskFilterBlurStyleIndex = 13; + static const int _kMaskFilterSigmaIndex = 14; static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2; static const int _kColorOffset = _kColorIndex << 2; @@ -575,14 +578,16 @@ class Paint { static const int _kColorFilterOffset = _kColorFilterIndex << 2; static const int _kColorFilterColorOffset = _kColorFilterColorIndex << 2; static const int _kColorFilterBlendModeOffset = _kColorFilterBlendModeIndex << 2; + static const int _kMaskFilterOffset = _kMaskFilterIndex << 2; + static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2; + static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2; // If you add more fields, remember to update _kDataByteCount. - static const int _kDataByteCount = 48; + static const int _kDataByteCount = 75; // Binary format must match the deserialization code in paint.cc. List _objects; - static const int _kMaskFilterIndex = 0; - static const int _kShaderIndex = 1; - static const int _kObjectCount = 2; // Must be one larger than the largest index + static const int _kShaderIndex = 0; + static const int _kObjectCount = 1; // Must be one larger than the largest index. /// Whether to apply anti-aliasing to lines and images drawn on the /// canvas. @@ -735,13 +740,29 @@ class Paint { /// /// See [MaskFilter] for details. MaskFilter get maskFilter { - if (_objects == null) - return null; - return _objects[_kMaskFilterIndex]; + switch (_data.getInt32(_kMaskFilterOffset, _kFakeHostEndian)) { + case MaskFilter._TypeNone: + return null; + case MaskFilter._TypeBlur: + return new MaskFilter.blur( + BlurStyle.values[_data.getInt32(_kMaskFilterBlurStyleOffset, _kFakeHostEndian)], + _data.getFloat32(_kMaskFilterSigmaOffset, _kFakeHostEndian), + ); + } + return null; } set maskFilter(MaskFilter value) { - _objects ??= new List(_kObjectCount); - _objects[_kMaskFilterIndex] = value; + if (value == null) { + _data.setInt32(_kMaskFilterOffset, MaskFilter._TypeNone, _kFakeHostEndian); + _data.setInt32(_kMaskFilterBlurStyleOffset, 0, _kFakeHostEndian); + _data.setFloat32(_kMaskFilterSigmaOffset, 0.0, _kFakeHostEndian); + } else { + // For now we only support one kind of MaskFilter, so we don't need to + // check what the type is if it's not null. + _data.setInt32(_kMaskFilterOffset, MaskFilter._TypeBlur, _kFakeHostEndian); + _data.setInt32(_kMaskFilterBlurStyleOffset, value._style.index, _kFakeHostEndian); + _data.setFloat32(_kMaskFilterSigmaOffset, value._sigma, _kFakeHostEndian); + } } /// Controls the performance vs quality trade-off to use when applying @@ -1291,7 +1312,7 @@ enum BlurStyle { /// color pixels. /// /// Instances of this class are used with [Paint.maskFilter] on [Paint] objects. -class MaskFilter extends NativeFieldWrapperClass2 { +class MaskFilter { /// Creates a mask filter that takes the shape being drawn and blurs it. /// /// This is commonly used to approximate shadows. @@ -1304,11 +1325,40 @@ class MaskFilter extends NativeFieldWrapperClass2 { /// in pixels. /// /// A blur is an expensive operation and should therefore be used sparingly. - MaskFilter.blur(BlurStyle style, double sigma) { - assert(style != null); - _constructor(style.index, sigma); + /// + /// The arguments must not be null. + /// + /// See also: + /// + /// * [Canvas.drawShadow], which is a more efficient way to draw shadows. + const MaskFilter.blur( + this._style, + this._sigma, + ) : assert(_style != null), + assert(_sigma != null); + + final BlurStyle _style; + final double _sigma; + + // The type of MaskFilter class to create for Skia. + // These constants must be kept in sync with MaskFilterType in paint.cc. + static const int _TypeNone = 0; // null + static const int _TypeBlur = 1; // SkBlurMaskFilter + + @override + bool operator ==(dynamic other) { + if (other is! MaskFilter) + return false; + final MaskFilter typedOther = other; + return _style == typedOther._style && + _sigma == typedOther._sigma; } - void _constructor(int style, double sigma) native "MaskFilter_constructor"; + + @override + int get hashCode => hashValues(_style, _sigma); + + @override + String toString() => "MaskFilter.blur($_style, ${_sigma.toStringAsFixed(1)})"; } /// A description of a color filter to apply when drawing a shape or compositing @@ -2406,10 +2456,14 @@ class Canvas extends NativeFieldWrapperClass2 { /// Draws a shadow for a [Path] representing the given material elevation. /// - /// transparentOccluder should be true if the occluding object is not opaque. + /// The `transparentOccluder` argument should be true if the occluding object + /// is not opaque. + /// + /// The arguments must not be null. void drawShadow(Path path, Color color, double elevation, bool transparentOccluder) { assert(path != null); // path is checked on the engine side assert(color != null); + assert(transparentOccluder != null); _drawShadow(path, color.value, elevation, transparentOccluder); } void _drawShadow(Path path, diff --git a/engine/src/flutter/lib/ui/painting/mask_filter.cc b/engine/src/flutter/lib/ui/painting/mask_filter.cc deleted file mode 100644 index 1dfcfaa4a1..0000000000 --- a/engine/src/flutter/lib/ui/painting/mask_filter.cc +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "flutter/lib/ui/painting/mask_filter.h" - -#include "flutter/common/threads.h" -#include "flutter/lib/ui/painting/utils.h" -#include "lib/tonic/converter/dart_converter.h" -#include "lib/tonic/dart_args.h" -#include "lib/tonic/dart_binding_macros.h" -#include "lib/tonic/dart_library_natives.h" -#include "third_party/skia/include/effects/SkBlurMaskFilter.h" - -namespace blink { - -static void MaskFilter_constructor(Dart_NativeArguments args) { - DartCallConstructor(&MaskFilter::Create, args); -} - -IMPLEMENT_WRAPPERTYPEINFO(ui, MaskFilter); - -void MaskFilter::RegisterNatives(tonic::DartLibraryNatives* natives) { - natives->Register({ - {"MaskFilter_constructor", MaskFilter_constructor, 3, true}, - }); -} - -fxl::RefPtr MaskFilter::Create(unsigned style, double sigma) { - return fxl::MakeRefCounted( - SkBlurMaskFilter::Make(static_cast(style), sigma)); -} - -MaskFilter::MaskFilter(sk_sp filter) - : filter_(std::move(filter)) {} - -MaskFilter::~MaskFilter() { - // Skia objects must be deleted on the IO thread so that any associated GL - // objects will be cleaned up through the IO thread's GL context. - SkiaUnrefOnIOThread(&filter_); -} - -} // namespace blink diff --git a/engine/src/flutter/lib/ui/painting/mask_filter.h b/engine/src/flutter/lib/ui/painting/mask_filter.h deleted file mode 100644 index 31cacf68eb..0000000000 --- a/engine/src/flutter/lib/ui/painting/mask_filter.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_LIB_UI_PAINTING_MASK_FILTER_H_ -#define FLUTTER_LIB_UI_PAINTING_MASK_FILTER_H_ - -#include "lib/tonic/dart_wrappable.h" -#include "third_party/skia/include/core/SkMaskFilter.h" - -class SkMaskFilter; - -namespace tonic { -class DartLibraryNatives; -} // namespace tonic - -namespace blink { - -class MaskFilter : public fxl::RefCountedThreadSafe, - public tonic::DartWrappable { - DEFINE_WRAPPERTYPEINFO(); - FRIEND_MAKE_REF_COUNTED(MaskFilter); - - public: - ~MaskFilter() override; - static fxl::RefPtr Create(unsigned style, double sigma); - - const sk_sp& filter() { return filter_; } - - static void RegisterNatives(tonic::DartLibraryNatives* natives); - - private: - MaskFilter(sk_sp filter); - - sk_sp filter_; -}; - -} // namespace blink - -#endif // FLUTTER_LIB_UI_PAINTING_MASK_FILTER_H_ diff --git a/engine/src/flutter/lib/ui/painting/paint.cc b/engine/src/flutter/lib/ui/painting/paint.cc index 8150695683..6e3acd8f41 100644 --- a/engine/src/flutter/lib/ui/painting/paint.cc +++ b/engine/src/flutter/lib/ui/painting/paint.cc @@ -4,7 +4,6 @@ #include "flutter/lib/ui/painting/paint.h" -#include "flutter/lib/ui/painting/mask_filter.h" #include "flutter/lib/ui/painting/shader.h" #include "lib/fxl/logging.h" #include "lib/tonic/typed_data/dart_byte_data.h" @@ -12,11 +11,13 @@ #include "third_party/skia/include/core/SkMaskFilter.h" #include "third_party/skia/include/core/SkShader.h" #include "third_party/skia/include/core/SkString.h" +#include "third_party/skia/include/effects/SkBlurMaskFilter.h" using namespace blink; namespace tonic { +// Indices for 32bit values. constexpr int kIsAntiAliasIndex = 0; constexpr int kColorIndex = 1; constexpr int kBlendModeIndex = 2; @@ -29,11 +30,14 @@ constexpr int kFilterQualityIndex = 8; constexpr int kColorFilterIndex = 9; constexpr int kColorFilterColorIndex = 10; constexpr int kColorFilterBlendModeIndex = 11; -constexpr size_t kDataByteCount = 48; +constexpr int kMaskFilterIndex = 12; +constexpr int kMaskFilterBlurStyleIndex = 13; +constexpr int kMaskFilterSigmaIndex = 14; +constexpr size_t kDataByteCount = 75; // 4 * (last index + 1) -constexpr int kMaskFilterIndex = 0; -constexpr int kShaderIndex = 1; -constexpr int kObjectCount = 2; // Must be one larger than the largest index +// Indices for objects. +constexpr int kShaderIndex = 0; +constexpr int kObjectCount = 1; // One larger than largest object index. // Must be kept in sync with the default in painting.dart. constexpr uint32_t kColorDefault = 0xFF000000; @@ -46,6 +50,9 @@ constexpr uint32_t kBlendModeDefault = // default SkPaintDefaults_MiterLimit in Skia (which is not in a public header). constexpr double kStrokeMiterLimitDefault = 4.0; +// Must be kept in sync with the MaskFilter private constants in painting.dart. +enum MaskFilterType { Null, Blur }; + Paint DartConverter::FromArguments(Dart_NativeArguments args, int index, Dart_Handle& exception) { @@ -68,12 +75,6 @@ Paint DartConverter::FromArguments(Dart_NativeArguments args, if (Dart_IsError(Dart_ListGetRange(paint_objects, 0, kObjectCount, values))) return result; - Dart_Handle mask_filter = values[kMaskFilterIndex]; - if (!Dart_IsNull(mask_filter)) { - MaskFilter* decoded = DartConverter::FromDart(mask_filter); - paint.setMaskFilter(decoded->filter()); - } - Dart_Handle shader = values[kShaderIndex]; if (!Dart_IsNull(shader)) { Shader* decoded = DartConverter::FromDart(shader); @@ -132,6 +133,17 @@ Paint DartConverter::FromArguments(Dart_NativeArguments args, paint.setColorFilter(SkColorFilter::MakeModeFilter(color, blend_mode)); } + switch (uint_data[kMaskFilterIndex]) { + case Null: + break; + case Blur: + SkBlurStyle blur_style = + static_cast(uint_data[kMaskFilterBlurStyleIndex]); + double sigma = float_data[kMaskFilterSigmaIndex]; + paint.setMaskFilter(SkBlurMaskFilter::Make(blur_style, sigma)); + break; + } + result.is_null_ = false; return result; } diff --git a/engine/src/flutter/testing/run_tests.sh b/engine/src/flutter/testing/run_tests.sh index 16b7428622..e84ce1c550 100755 --- a/engine/src/flutter/testing/run_tests.sh +++ b/engine/src/flutter/testing/run_tests.sh @@ -6,6 +6,8 @@ out/host_debug_unopt/fxl_unittests out/host_debug_unopt/synchronization_unittests out/host_debug_unopt/wtf_unittests +flutter/travis/analyze.sh + pushd flutter/testing/dart pub get popd @@ -17,3 +19,7 @@ for TEST_SCRIPT in flutter/testing/dart/*.dart; do out/host_debug_unopt/flutter_tester --disable-observatory --disable-diagnostic --non-interactive --enable-checked-mode $TEST_SCRIPT out/host_debug_unopt/flutter_tester --disable-observatory --disable-diagnostic --non-interactive $TEST_SCRIPT done + +pushd flutter +travis/test.sh +popd