| 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
7 #include "flutter/display_list/effects/dl_image_filters.h"
8 #include "fml/logging.h"
9 #include "impeller/display_list/color_filter.h"
10 #include "impeller/display_list/skia_conversions.h"
11 #include "impeller/entity/contents/filters/color_filter_contents.h"
12 #include "impeller/entity/contents/filters/filter_contents.h"
13 #include "impeller/entity/contents/filters/inputs/filter_input.h"
14
15 namespace impeller {
16
17 std::shared_ptr<FilterContents> WrapInput(const flutter::DlImageFilter* filter,
18 const FilterInput::Ref& input) {
19 FML_DCHECK(filter);
20
21 switch (filter->type()) {
22 case flutter::DlImageFilterType::kBlur: {
23 auto blur_filter = filter->asBlur();
24 FML_DCHECK(blur_filter);
25
26 return FilterContents::MakeGaussianBlur(
27 input, //
28 Sigma(blur_filter->sigma_x()), //
29 Sigma(blur_filter->sigma_y()), //
30 static_cast<Entity::TileMode>(blur_filter->tile_mode()), //
31 FilterContents::BlurStyle::kNormal //
32 );
33 }
34 case flutter::DlImageFilterType::kDilate: {
35 auto dilate_filter = filter->asDilate();
36 FML_DCHECK(dilate_filter);
37
38 return FilterContents::MakeMorphology(
39 input, //
40 Radius(dilate_filter->radius_x()), //
41 Radius(dilate_filter->radius_y()), //
42 FilterContents::MorphType::kDilate //
43 );
44 }
45 case flutter::DlImageFilterType::kErode: {
46 auto erode_filter = filter->asErode();
47 FML_DCHECK(erode_filter);
48
49 return FilterContents::MakeMorphology(
50 input, //
51 Radius(erode_filter->radius_x()), //
52 Radius(erode_filter->radius_y()), //
53 FilterContents::MorphType::kErode //
54 );
55 }
56 case flutter::DlImageFilterType::kMatrix: {
57 auto matrix_filter = filter->asMatrix();
58 FML_DCHECK(matrix_filter);
59
60 auto matrix = matrix_filter->matrix();
61 auto desc =
62 skia_conversions::ToSamplerDescriptor(matrix_filter->sampling());
63 return FilterContents::MakeMatrixFilter(input, matrix, desc);
64 }
65 case flutter::DlImageFilterType::kLocalMatrix: {
66 auto matrix_filter = filter->asLocalMatrix();
67 FML_DCHECK(matrix_filter);
68 FML_DCHECK(matrix_filter->image_filter());
69
70 auto matrix = matrix_filter->matrix();
71 return FilterContents::MakeLocalMatrixFilter(
72 FilterInput::Make(
73 WrapInput(matrix_filter->image_filter().get(), input)),
74 matrix);
75 }
76 case flutter::DlImageFilterType::kColorFilter: {
77 auto image_color_filter = filter->asColorFilter();
78 FML_DCHECK(image_color_filter);
79 auto color_filter = image_color_filter->color_filter();
80 FML_DCHECK(color_filter);
81
82 // When color filters are used as image filters, set the color filter's
83 // "absorb opacity" flag to false. For image filters, the snapshot
84 // opacity needs to be deferred until the result of the filter chain is
85 // being blended with the layer.
86 return WrapWithGPUColorFilter(color_filter.get(), input,
87 ColorFilterContents::AbsorbOpacity::kNo);
88 }
89 case flutter::DlImageFilterType::kCompose: {
90 auto compose = filter->asCompose();
91 FML_DCHECK(compose);
92
93 auto outer_dl_filter = compose->outer();
94 auto inner_dl_filter = compose->inner();
95 if (!outer_dl_filter) {
96 return WrapInput(inner_dl_filter.get(), input);
97 }
98 if (!inner_dl_filter) {
99 return WrapInput(outer_dl_filter.get(), input);
100 }
101 FML_DCHECK(outer_dl_filter && inner_dl_filter);
102
103 return WrapInput(
104 outer_dl_filter.get(),
105 FilterInput::Make(WrapInput(inner_dl_filter.get(), input)));
106 }
107 case flutter::DlImageFilterType::kRuntimeEffect: {
108 const flutter::DlRuntimeEffectImageFilter* runtime_filter =
109 filter->asRuntimeEffectFilter();
110 FML_DCHECK(runtime_filter);
111 std::shared_ptr<impeller::RuntimeStage> runtime_stage =
112 runtime_filter->runtime_effect()->runtime_stage();
113
114 std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
115 size_t index = 0;
116 for (const std::shared_ptr<flutter::DlColorSource>& sampler :
117 runtime_filter->samplers()) {
118 if (index == 0 && sampler == nullptr) {
119 // Insert placeholder for filter.
120 texture_inputs.push_back(
121 {.sampler_descriptor = skia_conversions::ToSamplerDescriptor({}),
122 .texture = nullptr});
123 continue;
124 }
125 if (sampler == nullptr) {
126 return nullptr;
127 }
128 auto* image = sampler->asImage();
129 if (!image) {
130 return nullptr;
131 }
132 FML_DCHECK(image->image()->impeller_texture());
133 index++;
134 texture_inputs.push_back({
135 .sampler_descriptor =
136 skia_conversions::ToSamplerDescriptor(image->sampling()),
137 .texture = image->image()->impeller_texture(),
138 });
139 }
140 return FilterContents::MakeRuntimeEffect(input, std::move(runtime_stage),
141 runtime_filter->uniform_data(),
142 std::move(texture_inputs));
143 }
144 }
145 FML_UNREACHABLE();
146 }
147
148 } // namespace impeller
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
impeller::FilterContents::MorphType::kErode
@ kErode
impeller::FilterContents::MorphType::kDilate
@ kDilate
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:76
impeller::FilterContents::MakeLocalMatrixFilter
static std::shared_ptr< FilterContents > MakeLocalMatrixFilter(FilterInput::Ref input, const Matrix &matrix)
Definition: filter_contents.cc:99
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:38
impeller::FilterContents::MakeMatrixFilter
static std::shared_ptr< FilterContents > MakeMatrixFilter(FilterInput::Ref input, const Matrix &matrix, const SamplerDescriptor &desc)
Definition: filter_contents.cc:88
impeller::FilterContents::MakeRuntimeEffect
static std::shared_ptr< FilterContents > MakeRuntimeEffect(FilterInput::Ref input, std::shared_ptr< RuntimeStage > runtime_stage, std::shared_ptr< std::vector< uint8_t >> uniforms, std::vector< RuntimeEffectContents::TextureInput > texture_inputs)
Definition: filter_contents.cc:119
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
color_filter_contents.h
color_filter.h
image_filter.h
filter_contents.h
filter_input.h
impeller::skia_conversions::ToSamplerDescriptor
impeller::SamplerDescriptor ToSamplerDescriptor(const flutter::DlImageSampling options)
Definition: skia_conversions.cc:141
impeller
Definition: allocation.cc:12
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
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:17
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::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32