Skip to content

Commit

Permalink
Convert MaskFilter to pure-Dart. (flutter#4534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hixie authored Jan 12, 2018
1 parent 05fe72d commit 26c3ab0
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 115 deletions.
2 changes: 0 additions & 2 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions lib/ui/dart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
84 changes: 69 additions & 15 deletions lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<dynamic> _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.
Expand Down Expand Up @@ -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<dynamic>(_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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 0 additions & 43 deletions lib/ui/painting/mask_filter.cc

This file was deleted.

40 changes: 0 additions & 40 deletions lib/ui/painting/mask_filter.h

This file was deleted.

34 changes: 23 additions & 11 deletions lib/ui/painting/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

#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"
#include "third_party/skia/include/core/SkColorFilter.h"
#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;
Expand All @@ -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;
Expand All @@ -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<Paint>::FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
Expand All @@ -68,12 +75,6 @@ Paint DartConverter<Paint>::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<MaskFilter*>::FromDart(mask_filter);
paint.setMaskFilter(decoded->filter());
}

Dart_Handle shader = values[kShaderIndex];
if (!Dart_IsNull(shader)) {
Shader* decoded = DartConverter<Shader*>::FromDart(shader);
Expand Down Expand Up @@ -132,6 +133,17 @@ Paint DartConverter<Paint>::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<SkBlurStyle>(uint_data[kMaskFilterBlurStyleIndex]);
double sigma = float_data[kMaskFilterSigmaIndex];
paint.setMaskFilter(SkBlurMaskFilter::Make(blur_style, sigma));
break;
}

result.is_null_ = false;
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions testing/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 0 additions & 2 deletions travis/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -9509,8 +9509,6 @@ FILE: ../../../flutter/lib/ui/painting/gradient.cc
FILE: ../../../flutter/lib/ui/painting/gradient.h
FILE: ../../../flutter/lib/ui/painting/image_shader.cc
FILE: ../../../flutter/lib/ui/painting/image_shader.h
FILE: ../../../flutter/lib/ui/painting/mask_filter.cc
FILE: ../../../flutter/lib/ui/painting/mask_filter.h
FILE: ../../../flutter/lib/ui/painting/matrix.cc
FILE: ../../../flutter/lib/ui/painting/matrix.h
FILE: ../../../flutter/lib/ui/painting/paint.cc
Expand Down

0 comments on commit 26c3ab0

Please sign in to comment.