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