From 489e39e29de051cf6604f34a9035ded2706b2b25 Mon Sep 17 00:00:00 2001 From: Dan Field Date: Wed, 26 Jun 2019 23:02:36 -0700 Subject: [PATCH] Support image filter on paint (flutter/engine#9508) --- engine/src/flutter/lib/ui/painting.dart | 40 ++++++++++++++++++++- engine/src/flutter/lib/ui/painting/paint.cc | 12 ++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 21a9f8387c..9e8bc77fe6 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -1087,7 +1087,8 @@ class Paint { List _objects; static const int _kShaderIndex = 0; static const int _kColorFilterMatrixIndex = 1; - static const int _kObjectCount = 2; // Must be one larger than the largest index. + static const int _kImageFilterIndex = 2; + static const int _kObjectCount = 3; // Must be one larger than the largest index. /// Whether to apply anti-aliasing to lines and images drawn on the /// canvas. @@ -1387,6 +1388,39 @@ class Paint { } } + /// The [ImageFilter] to use when drawing raster images. + /// + /// For example, to blur an image using [Canvas.drawImage], apply an + /// [ImageFilter.blur]: + /// + /// ```dart + /// import 'dart:ui' as ui; + /// + /// ui.Image image; + /// + /// void paint(Canvas canvas, Size size) { + /// canvas.drawImage( + /// image, + /// Offset.zero, + /// Paint()..imageFilter = ui.ImageFilter.blur(sigmaX: .5, sigmaY: .5), + /// ); + /// } + /// ``` + /// + /// See also: + /// + /// * [MaskFilter], which is used for drawing geometry. + ImageFilter get imageFilter { + if (_objects == null) + return null; + return _objects[_kImageFilterIndex]; + } + set imageFilter(ImageFilter value) { + _objects ??= List(_kObjectCount); + _objects[_kImageFilterIndex] = value; + } + + /// Whether the colors of the image are inverted when drawn. /// /// Inverting the colors of an image applies a new color filter that will @@ -1451,6 +1485,10 @@ class Paint { result.write('${semicolon}shader: $shader'); semicolon = '; '; } + if (imageFilter != null) { + result.write('${semicolon}imageFilter: $imageFilter'); + semicolon = '; '; + } if (invertColors) result.write('${semicolon}invert: $invertColors'); result.write(')'); diff --git a/engine/src/flutter/lib/ui/painting/paint.cc b/engine/src/flutter/lib/ui/painting/paint.cc index d144b2b31f..718b9ae610 100644 --- a/engine/src/flutter/lib/ui/painting/paint.cc +++ b/engine/src/flutter/lib/ui/painting/paint.cc @@ -5,8 +5,10 @@ #include "flutter/lib/ui/painting/paint.h" #include "flutter/fml/logging.h" +#include "flutter/lib/ui/painting/image_filter.h" #include "flutter/lib/ui/painting/shader.h" #include "third_party/skia/include/core/SkColorFilter.h" +#include "third_party/skia/include/core/SkImageFilter.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" @@ -37,7 +39,8 @@ constexpr size_t kDataByteCount = 75; // 4 * (last index + 1) // Indices for objects. constexpr int kShaderIndex = 0; constexpr int kColorFilterMatrixIndex = 1; -constexpr int kObjectCount = 2; // One larger than largest object index. +constexpr int kImageFilterIndex = 2; +constexpr int kObjectCount = 3; // One larger than largest object index. // Must be kept in sync with the default in painting.dart. constexpr uint32_t kColorDefault = 0xFF000000; @@ -141,6 +144,13 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) { Shader* decoded = tonic::DartConverter::FromDart(shader); paint_.setShader(decoded->shader()); } + + Dart_Handle image_filter = values[kImageFilterIndex]; + if (!Dart_IsNull(image_filter)) { + ImageFilter* decoded = + tonic::DartConverter::FromDart(image_filter); + paint_.setImageFilter(decoded->filter()); + } } tonic::DartByteData byte_data(paint_data);