| Flutter Impeller |

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 "impeller/display_list/image_filter.h"
6 #include "fml/logging.h"
7 #include "impeller/display_list/color_filter.h"
8 #include "impeller/display_list/skia_conversions.h"
9 #include "impeller/entity/contents/filters/color_filter_contents.h"
10 #include "impeller/entity/contents/filters/filter_contents.h"
11 #include "impeller/entity/contents/filters/inputs/filter_input.h"
12
13 namespace impeller {
14
15 std::shared_ptr<FilterContents> WrapInput(const flutter::DlImageFilter* filter,
16 const FilterInput::Ref& input) {
17 FML_DCHECK(filter);
18
19 switch (filter->type()) {
20 case flutter::DlImageFilterType::kBlur: {
21 auto blur_filter = filter->asBlur();
22 FML_DCHECK(blur_filter);
23
24 return FilterContents::MakeGaussianBlur(
25 input, //
26 Sigma(blur_filter->sigma_x()), //
27 Sigma(blur_filter->sigma_y()), //
28 static_cast<Entity::TileMode>(blur_filter->tile_mode()), //
29 FilterContents::BlurStyle::kNormal //
30 );
31 }
32 case flutter::DlImageFilterType::kDilate: {
33 auto dilate_filter = filter->asDilate();
34 FML_DCHECK(dilate_filter);
35
36 return FilterContents::MakeMorphology(
37 input, //
38 Radius(dilate_filter->radius_x()), //
39 Radius(dilate_filter->radius_y()), //
40 FilterContents::MorphType::kDilate //
41 );
42 }
43 case flutter::DlImageFilterType::kErode: {
44 auto erode_filter = filter->asErode();
45 FML_DCHECK(erode_filter);
46
47 return FilterContents::MakeMorphology(
48 input, //
49 Radius(erode_filter->radius_x()), //
50 Radius(erode_filter->radius_y()), //
51 FilterContents::MorphType::kErode //
52 );
53 }
54 case flutter::DlImageFilterType::kMatrix: {
55 auto matrix_filter = filter->asMatrix();
56 FML_DCHECK(matrix_filter);
57
58 auto matrix = skia_conversions::ToMatrix(matrix_filter->matrix());
59 auto desc =
60 skia_conversions::ToSamplerDescriptor(matrix_filter->sampling());
61 return FilterContents::MakeMatrixFilter(input, matrix, desc);
62 }
63 case flutter::DlImageFilterType::kLocalMatrix: {
64 auto matrix_filter = filter->asLocalMatrix();
65 FML_DCHECK(matrix_filter);
66 FML_DCHECK(matrix_filter->image_filter());
67
68 auto matrix = skia_conversions::ToMatrix(matrix_filter->matrix());
69 return FilterContents::MakeLocalMatrixFilter(
70 FilterInput::Make(
71 WrapInput(matrix_filter->image_filter().get(), input)),
72 matrix);
73 }
74 case flutter::DlImageFilterType::kColorFilter: {
75 auto image_color_filter = filter->asColorFilter();
76 FML_DCHECK(image_color_filter);
77 auto color_filter = image_color_filter->color_filter();
78 FML_DCHECK(color_filter);
79
80 // When color filters are used as image filters, set the color filter's
81 // "absorb opacity" flag to false. For image filters, the snapshot
82 // opacity needs to be deferred until the result of the filter chain is
83 // being blended with the layer.
84 return WrapWithGPUColorFilter(color_filter.get(), input,
85 ColorFilterContents::AbsorbOpacity::kNo);
86 }
87 case flutter::DlImageFilterType::kCompose: {
88 auto compose = filter->asCompose();
89 FML_DCHECK(compose);
90
91 auto outer_dl_filter = compose->outer();
92 auto inner_dl_filter = compose->inner();
93 if (!outer_dl_filter) {
94 return WrapInput(inner_dl_filter.get(), input);
95 }
96 if (!inner_dl_filter) {
97 return WrapInput(outer_dl_filter.get(), input);
98 }
99 FML_DCHECK(outer_dl_filter && inner_dl_filter);
100
101 return WrapInput(
102 outer_dl_filter.get(),
103 FilterInput::Make(WrapInput(inner_dl_filter.get(), input)));
104 }
105 }
106 FML_UNREACHABLE();
107 }
108
109 } // namespace impeller
impeller::FilterContents::MorphType::kErode
@ kErode
impeller::skia_conversions::ToSamplerDescriptor
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
Definition: skia_conversions.cc:169
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
impeller::skia_conversions::ToMatrix
Matrix ToMatrix(const SkMatrix &m)
Definition: skia_conversions.cc:193
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
filter_contents.h
color_filter.h
skia_conversions.h
impeller::Radius
For convolution filters, the "radius" is the size of the convolution kernel to use on the local space...
Definition: sigma.h:48
impeller::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:24
image_filter.h
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
filter_input.h
color_filter_contents.h
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::WrapInput
std::shared_ptr< FilterContents > WrapInput(const flutter::DlImageFilter *filter, const FilterInput::Ref &input)
Generate a new FilterContents using this filter's configuration.
Definition: image_filter.cc:15
impeller::FilterContents::MakeMorphology
static std::shared_ptr< FilterContents > MakeMorphology(FilterInput::Ref input, Radius radius_x, Radius radius_y, MorphType morph_type)
Definition: filter_contents.cc:74
impeller::FilterContents::MorphType::kDilate
@ kDilate
impeller::FilterContents::MakeMatrixFilter
static std::shared_ptr< FilterContents > MakeMatrixFilter(FilterInput::Ref input, const Matrix &matrix, const SamplerDescriptor &desc)
Definition: filter_contents.cc:86
impeller::FilterContents::MakeLocalMatrixFilter
static std::shared_ptr< FilterContents > MakeLocalMatrixFilter(FilterInput::Ref input, const Matrix &matrix)
Definition: filter_contents.cc:97
impeller::FilterContents::MakeGaussianBlur
static std::shared_ptr< FilterContents > MakeGaussianBlur(const FilterInput::Ref &input, Sigma sigma_x, Sigma sigma_y, Entity::TileMode tile_mode=Entity::TileMode::kDecal, BlurStyle mask_blur_style=BlurStyle::kNormal, const Geometry *mask_geometry=nullptr)
Definition: filter_contents.cc:36
impeller
Definition: allocation.cc:12