Flutter Engine
image_filter.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/lib/ui/painting/image_filter.h"
6 
7 #include "flutter/lib/ui/painting/matrix.h"
8 #include "third_party/skia/include/effects/SkImageFilters.h"
13 
14 namespace flutter {
15 
16 static void ImageFilter_constructor(Dart_NativeArguments args) {
19 }
20 
22 
23 #define FOR_EACH_BINDING(V) \
24  V(ImageFilter, initImage) \
25  V(ImageFilter, initPicture) \
26  V(ImageFilter, initBlur) \
27  V(ImageFilter, initMatrix) \
28  V(ImageFilter, initColorFilter) \
29  V(ImageFilter, initComposeFilter)
30 
32 
33 void ImageFilter::RegisterNatives(tonic::DartLibraryNatives* natives) {
34  natives->Register(
35  {{"ImageFilter_constructor", ImageFilter_constructor, 1, true},
37 }
38 
40  return fml::MakeRefCounted<ImageFilter>();
41 }
42 
43 static const std::array<SkSamplingOptions, 4> filter_qualities = {
44  SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone),
45  SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone),
46  SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
47  SkSamplingOptions(SkCubicResampler{1 / 3.0f, 1 / 3.0f}),
48 };
49 
50 SkSamplingOptions ImageFilter::SamplingFromIndex(int filterQualityIndex) {
51  if (filterQualityIndex < 0) {
52  return filter_qualities.front();
53  } else if (((size_t)filterQualityIndex) >= filter_qualities.size()) {
54  return filter_qualities.back();
55  } else {
56  return filter_qualities[filterQualityIndex];
57  }
58 }
59 
60 SkFilterMode ImageFilter::FilterModeFromIndex(int filterQualityIndex) {
61  if (filterQualityIndex <= 0) {
62  return SkFilterMode::kNearest;
63  }
64  return SkFilterMode::kLinear;
65 }
66 
67 ImageFilter::ImageFilter() {}
68 
70 
72  filter_ = SkImageFilters::Image(image->image());
73 }
74 
76  filter_ = SkImageFilters::Picture(picture->picture());
77 }
78 
79 void ImageFilter::initBlur(double sigma_x,
80  double sigma_y,
81  SkTileMode tile_mode) {
82  filter_ = SkImageFilters::Blur(sigma_x, sigma_y, tile_mode, nullptr, nullptr);
83 }
84 
85 void ImageFilter::initMatrix(const tonic::Float64List& matrix4,
86  int filterQualityIndex) {
87  auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
88  filter_ =
89  SkImageFilters::MatrixTransform(ToSkMatrix(matrix4), sampling, nullptr);
90 }
91 
93  filter_ = SkImageFilters::ColorFilter(
94  colorFilter ? colorFilter->filter() : nullptr, nullptr);
95 }
96 
98  filter_ = SkImageFilters::Compose(outer ? outer->filter() : nullptr,
99  inner ? inner->filter() : nullptr);
100 }
101 
102 } // namespace flutter
G_BEGIN_DECLS FlValue * args
void DartCallConstructor(Sig func, Dart_NativeArguments args)
Definition: dart_args.h:218
CanvasImage Image
Definition: image.cc:15
void initImage(CanvasImage *image)
Definition: image_filter.cc:71
sk_sp< SkPicture > picture() const
Definition: picture.h:34
#define DART_NATIVE_CALLBACK(CLASS, METHOD)
~ImageFilter() override
Definition: image_filter.cc:69
static fml::RefPtr< ImageFilter > Create()
Definition: image_filter.cc:39
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
static SkFilterMode FilterModeFromIndex(int index)
Definition: image_filter.cc:60
static void ThrowIfUIOperationsProhibited()
void initColorFilter(ColorFilter *colorFilter)
Definition: image_filter.cc:92
void initComposeFilter(ImageFilter *outer, ImageFilter *inner)
Definition: image_filter.cc:97
void initBlur(double sigma_x, double sigma_y, SkTileMode tile_mode)
Definition: image_filter.cc:79
static SkSamplingOptions SamplingFromIndex(int filterQualityIndex)
Definition: image_filter.cc:50
const sk_sp< SkImageFilter > & filter() const
Definition: image_filter.h:35
static void ImageFilter_constructor(Dart_NativeArguments args)
Definition: image_filter.cc:16
void initMatrix(const tonic::Float64List &matrix4, int filter_quality_index)
Definition: image_filter.cc:85
void initPicture(Picture *)
Definition: image_filter.cc:75
#define DART_REGISTER_NATIVE(CLASS, METHOD)
#define FOR_EACH_BINDING(V)
Definition: image_filter.cc:23
SkMatrix ToSkMatrix(const tonic::Float64List &matrix4)
Definition: matrix.cc:20
sk_sp< SkImage > image() const
Definition: image.h:37
sk_sp< SkColorFilter > filter() const
Definition: color_filter.h:42
static const std::array< SkSamplingOptions, 4 > filter_qualities
Definition: image_filter.cc:43