Support image filter on paint (flutter/engine#9508)
This commit is contained in:
@@ -1087,7 +1087,8 @@ class Paint {
|
||||
List<dynamic> _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<dynamic>(_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(')');
|
||||
|
||||
@@ -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<Shader*>::FromDart(shader);
|
||||
paint_.setShader(decoded->shader());
|
||||
}
|
||||
|
||||
Dart_Handle image_filter = values[kImageFilterIndex];
|
||||
if (!Dart_IsNull(image_filter)) {
|
||||
ImageFilter* decoded =
|
||||
tonic::DartConverter<ImageFilter*>::FromDart(image_filter);
|
||||
paint_.setImageFilter(decoded->filter());
|
||||
}
|
||||
}
|
||||
|
||||
tonic::DartByteData byte_data(paint_data);
|
||||
|
||||
Reference in New Issue
Block a user