Skip to content

Commit 0c52dee

Browse files
ahyangnbb-luk
andauthored
[Impeller] Add filterQuality support to ImageFilter.shader (flutter#188544)
Adds a `filterQuality` argument to `ImageFilter.shader` so the implicitly bound image-filter input sampler can be configured instead of always using nearest-neighbor sampling. This threads the requested sampling quality through `dart:ui`, DisplayList runtime-effect image filters, and Impeller’s runtime-effect texture input setup. The default remains `FilterQuality.none` to preserve existing behavior. This also adds regression coverage with a fractional-texel shader to verify that `BackdropFilter` + `ImageFilter.shader` honors `FilterQuality`, plus equality coverage for shader image filters that differ only by input sampling. Fixes flutter#188365 No changes were required in the `flutter/tests` repo. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: b-luk <[email protected]>
1 parent 7f24a41 commit 0c52dee

18 files changed

Lines changed: 230 additions & 94 deletions

‎engine/src/flutter/display_list/effects/dl_image_filter.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ std::shared_ptr<DlImageFilter> DlImageFilter::MakeMatrix(
3434
std::shared_ptr<DlImageFilter> DlImageFilter::MakeRuntimeEffect(
3535
sk_sp<DlRuntimeEffect> runtime_effect,
3636
std::vector<std::shared_ptr<DlColorSource>> samplers,
37-
std::shared_ptr<std::vector<uint8_t>> uniform_data) {
37+
std::shared_ptr<std::vector<uint8_t>> uniform_data,
38+
DlImageSampling input_sampling) {
3839
return DlRuntimeEffectImageFilter::Make(
39-
std::move(runtime_effect), std::move(samplers), std::move(uniform_data));
40+
std::move(runtime_effect), std::move(samplers), std::move(uniform_data),
41+
input_sampling);
4042
}
4143

4244
std::shared_ptr<DlImageFilter> DlImageFilter::MakeColorFilter(

‎engine/src/flutter/display_list/effects/dl_image_filter.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class DlImageFilter : public DlAttribute<DlImageFilter, DlImageFilterType> {
6767
static std::shared_ptr<DlImageFilter> MakeRuntimeEffect(
6868
sk_sp<DlRuntimeEffect> runtime_effect,
6969
std::vector<std::shared_ptr<DlColorSource>> samplers,
70-
std::shared_ptr<std::vector<uint8_t>> uniform_data);
70+
std::shared_ptr<std::vector<uint8_t>> uniform_data,
71+
DlImageSampling input_sampling = DlImageSampling::kNearestNeighbor);
7172

7273
static std::shared_ptr<DlImageFilter> MakeColorFilter(
7374
const std::shared_ptr<const DlColorFilter>& filter);

‎engine/src/flutter/display_list/effects/dl_image_filter_unittests.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,21 @@ TEST(DisplayListImageFilter, RuntimeEffectEquality) {
907907
EXPECT_NE(filter_a, filter_c);
908908
}
909909

910+
TEST(DisplayListImageFilter, RuntimeEffectEqualityWithInputSampling) {
911+
DlRuntimeEffectImageFilter filter_a(nullptr, {nullptr},
912+
std::make_shared<std::vector<uint8_t>>());
913+
DlRuntimeEffectImageFilter filter_b(nullptr, {nullptr},
914+
std::make_shared<std::vector<uint8_t>>());
915+
DlRuntimeEffectImageFilter filter_c(nullptr, {nullptr},
916+
std::make_shared<std::vector<uint8_t>>(),
917+
DlImageSampling::kLinear);
918+
919+
EXPECT_EQ(filter_a.input_sampling(), DlImageSampling::kNearestNeighbor);
920+
EXPECT_EQ(filter_c.input_sampling(), DlImageSampling::kLinear);
921+
EXPECT_EQ(filter_a, filter_b);
922+
EXPECT_NE(filter_a, filter_c);
923+
}
924+
910925
TEST(DisplayListImageFilter, RuntimeEffectEqualityWithSamplers) {
911926
auto image_a =
912927
DlColorSource::MakeImage(nullptr, DlTileMode::kClamp, DlTileMode::kDecal);

‎engine/src/flutter/display_list/effects/image_filters/dl_runtime_effect_image_filter.cc‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ namespace flutter {
99
std::shared_ptr<DlImageFilter> DlRuntimeEffectImageFilter::Make(
1010
sk_sp<DlRuntimeEffect> runtime_effect,
1111
std::vector<std::shared_ptr<DlColorSource>> samplers,
12-
std::shared_ptr<std::vector<uint8_t>> uniform_data) {
12+
std::shared_ptr<std::vector<uint8_t>> uniform_data,
13+
DlImageSampling input_sampling) {
1314
return std::make_shared<DlRuntimeEffectImageFilter>(
14-
std::move(runtime_effect), std::move(samplers), std::move(uniform_data));
15+
std::move(runtime_effect), std::move(samplers), std::move(uniform_data),
16+
input_sampling);
1517
}
1618

1719
DlRect* DlRuntimeEffectImageFilter::map_local_bounds(
@@ -42,7 +44,8 @@ bool DlRuntimeEffectImageFilter::equals_(const DlImageFilter& other) const {
4244
auto that = static_cast<const DlRuntimeEffectImageFilter*>(&other);
4345
if (runtime_effect_ != that->runtime_effect_ ||
4446
samplers_.size() != that->samplers().size() ||
45-
uniform_data_->size() != that->uniform_data()->size()) {
47+
uniform_data_->size() != that->uniform_data()->size() ||
48+
input_sampling_ != that->input_sampling()) {
4649
return false;
4750
}
4851
for (auto i = 0u; i < samplers_.size(); i++) {

‎engine/src/flutter/display_list/effects/image_filters/dl_runtime_effect_image_filter.h‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ class DlRuntimeEffectImageFilter final : public DlImageFilter {
1717
explicit DlRuntimeEffectImageFilter(
1818
sk_sp<DlRuntimeEffect> runtime_effect,
1919
std::vector<std::shared_ptr<DlColorSource>> samplers,
20-
std::shared_ptr<std::vector<uint8_t>> uniform_data)
20+
std::shared_ptr<std::vector<uint8_t>> uniform_data,
21+
DlImageSampling input_sampling = DlImageSampling::kNearestNeighbor)
2122
: runtime_effect_(std::move(runtime_effect)),
2223
samplers_(std::move(samplers)),
23-
uniform_data_(std::move(uniform_data)) {}
24+
uniform_data_(std::move(uniform_data)),
25+
input_sampling_(input_sampling) {}
2426

2527
std::shared_ptr<DlImageFilter> shared() const override {
2628
return std::make_shared<DlRuntimeEffectImageFilter>(
27-
this->runtime_effect_, this->samplers_, this->uniform_data_);
29+
this->runtime_effect_, this->samplers_, this->uniform_data_,
30+
this->input_sampling_);
2831
}
2932

3033
static std::shared_ptr<DlImageFilter> Make(
3134
sk_sp<DlRuntimeEffect> runtime_effect,
3235
std::vector<std::shared_ptr<DlColorSource>> samplers,
33-
std::shared_ptr<std::vector<uint8_t>> uniform_data);
36+
std::shared_ptr<std::vector<uint8_t>> uniform_data,
37+
DlImageSampling input_sampling = DlImageSampling::kNearestNeighbor);
3438

3539
DlImageFilterType type() const override {
3640
return DlImageFilterType::kRuntimeEffect;
@@ -66,13 +70,16 @@ class DlRuntimeEffectImageFilter final : public DlImageFilter {
6670
return uniform_data_;
6771
}
6872

73+
DlImageSampling input_sampling() const { return input_sampling_; }
74+
6975
protected:
7076
bool equals_(const DlImageFilter& other) const override;
7177

7278
private:
7379
sk_sp<DlRuntimeEffect> runtime_effect_;
7480
std::vector<std::shared_ptr<DlColorSource>> samplers_;
7581
std::shared_ptr<std::vector<uint8_t>> uniform_data_;
82+
DlImageSampling input_sampling_;
7683
};
7784

7885
} // namespace flutter

‎engine/src/flutter/impeller/display_list/image_filter.cc‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@ std::shared_ptr<FilterContents> WrapInput(const ContentContext& renderer,
117117
runtime_filter->runtime_effect()->runtime_stage();
118118

119119
std::vector<RuntimeEffectContents::TextureInput> texture_inputs;
120-
size_t index = 0;
120+
bool is_first = true;
121121
for (const std::shared_ptr<flutter::DlColorSource>& sampler :
122122
runtime_filter->samplers()) {
123-
if (index == 0 && sampler == nullptr) {
124-
// Insert placeholder for filter.
123+
if (is_first) {
124+
is_first = false;
125+
// The first sampler is always the image filter input.
125126
texture_inputs.push_back(
126-
{.sampler_descriptor = skia_conversions::ToSamplerDescriptor({}),
127+
{.sampler_descriptor = skia_conversions::ToSamplerDescriptor(
128+
runtime_filter->input_sampling()),
127129
.texture = nullptr});
128130
continue;
129131
}
@@ -137,7 +139,6 @@ std::shared_ptr<FilterContents> WrapInput(const ContentContext& renderer,
137139
std::shared_ptr<impeller::Texture> texture =
138140
image->image()->asImpellerImage()->GetCachedTexture(renderer);
139141
FML_DCHECK(texture);
140-
index++;
141142
texture_inputs.push_back({
142143
.sampler_descriptor =
143144
skia_conversions::ToSamplerDescriptor(image->sampling()),

‎engine/src/flutter/lib/ui/fixtures/shaders/general_shaders/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if (enable_unittests) {
1313
"circle_sdf.frag",
1414
"double_sampler_swapped.frag",
1515
"double_sampler.frag",
16+
"filter_shader_fractional_texel.frag",
1617
"filter_shader.frag",
1718
"functions.frag",
1819
"missing_size.frag",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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/runtime_effect.glsl>
6+
7+
uniform vec2 u_size;
8+
uniform sampler2D u_texture;
9+
10+
out vec4 frag_color;
11+
12+
void main() {
13+
vec2 uv = (FlutterFragCoord().xy + vec2(0.25, 0.0)) / u_size;
14+
frag_color = texture(u_texture, uv);
15+
}

‎engine/src/flutter/lib/ui/painting.dart‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4429,6 +4429,9 @@ abstract class ImageFilter {
44294429
/// also be at least one sampler2D uniform, the first of which will be set by
44304430
/// the engine to contain the filter input.
44314431
///
4432+
/// The optional [filterQuality] argument sets the quality level used to sample
4433+
/// the filter input. By default, it is set to [FilterQuality.none].
4434+
///
44324435
/// When Impeller uses the OpenGL(ES) backend, the y-axis direction is
44334436
/// reversed. Custom fragment shaders must invert the y-axis on
44344437
/// GLES or they will render upside-down.
@@ -4458,7 +4461,10 @@ abstract class ImageFilter {
44584461
/// }
44594462
///
44604463
/// ```
4461-
factory ImageFilter.shader(FragmentShader shader) {
4464+
factory ImageFilter.shader(
4465+
FragmentShader shader, {
4466+
FilterQuality filterQuality = FilterQuality.none,
4467+
}) {
44624468
if (!_impellerEnabled) {
44634469
throw UnsupportedError('ImageFilter.shader only supported with Impeller rendering engine.');
44644470
}
@@ -4477,7 +4483,7 @@ abstract class ImageFilter {
44774483
}
44784484
throw StateError(buffer.toString());
44794485
}
4480-
return _FragmentShaderImageFilter(shader);
4486+
return _FragmentShaderImageFilter(shader, filterQuality);
44814487
}
44824488

44834489
/// Whether [ImageFilter.shader] is supported on the current backend.
@@ -4673,9 +4679,10 @@ class _ComposeImageFilter implements ImageFilter {
46734679
}
46744680

46754681
class _FragmentShaderImageFilter implements ImageFilter {
4676-
_FragmentShaderImageFilter(this.shader);
4682+
_FragmentShaderImageFilter(this.shader, this.filterQuality);
46774683

46784684
final FragmentShader shader;
4685+
final FilterQuality filterQuality;
46794686

46804687
late final _ImageFilter nativeFilter = _ImageFilter.shader(this);
46814688

@@ -4686,7 +4693,7 @@ class _FragmentShaderImageFilter implements ImageFilter {
46864693
String get debugShortDescription => 'shader';
46874694

46884695
@override
4689-
String toString() => 'ImageFilter.shader(Shader#${shader.hashCode})';
4696+
String toString() => 'ImageFilter.shader(Shader#${shader.hashCode}, $filterQuality)';
46904697

46914698
@override
46924699
bool operator ==(Object other) {
@@ -4695,14 +4702,15 @@ class _FragmentShaderImageFilter implements ImageFilter {
46954702
}
46964703
return other is _FragmentShaderImageFilter &&
46974704
other.shader == shader &&
4705+
other.filterQuality == filterQuality &&
46984706
_equals(nativeFilter, other.nativeFilter);
46994707
}
47004708

47014709
@Native<Bool Function(Handle, Handle)>(symbol: 'ImageFilter::equals')
47024710
external static bool _equals(_ImageFilter a, _ImageFilter b);
47034711

47044712
@override
4705-
int get hashCode => shader.hashCode;
4713+
int get hashCode => Object.hash(shader, filterQuality);
47064714
}
47074715

47084716
/// An [ImageFilter] that is backed by a native DlImageFilter.
@@ -4770,7 +4778,7 @@ base class _ImageFilter extends NativeFieldWrapperClass1 {
47704778

47714779
_ImageFilter.shader(_FragmentShaderImageFilter filter) : creator = filter {
47724780
_constructor();
4773-
_initShader(filter.shader);
4781+
_initShader(filter.shader, filter.filterQuality.index);
47744782
}
47754783

47764784
@Native<Void Function(Handle)>(symbol: 'ImageFilter::Create')
@@ -4813,8 +4821,8 @@ base class _ImageFilter extends NativeFieldWrapperClass1 {
48134821
)
48144822
external void _initComposed(_ImageFilter outerFilter, _ImageFilter innerFilter);
48154823

4816-
@Native<Void Function(Pointer<Void>, Pointer<Void>)>(symbol: 'ImageFilter::initShader')
4817-
external void _initShader(FragmentShader shader);
4824+
@Native<Void Function(Pointer<Void>, Pointer<Void>, Int32)>(symbol: 'ImageFilter::initShader')
4825+
external void _initShader(FragmentShader shader, int filterQuality);
48184826

48194827
/// The original Dart object that created the native wrapper, which retains
48204828
/// the values used for the filter.

‎engine/src/flutter/lib/ui/painting/fragment_program.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,10 @@ std::shared_ptr<DlColorSource> FragmentProgram::MakeDlColorSource(
271271

272272
std::shared_ptr<DlImageFilter> FragmentProgram::MakeDlImageFilter(
273273
std::shared_ptr<std::vector<uint8_t>> float_uniforms,
274-
const std::vector<std::shared_ptr<DlColorSource>>& children) {
275-
return DlImageFilter::MakeRuntimeEffect(runtime_effect_, children,
276-
std::move(float_uniforms));
274+
const std::vector<std::shared_ptr<DlColorSource>>& children,
275+
DlImageSampling input_sampling) {
276+
return DlImageFilter::MakeRuntimeEffect(
277+
runtime_effect_, children, std::move(float_uniforms), input_sampling);
277278
}
278279

279280
void FragmentProgram::Create(Dart_Handle wrapper) {

0 commit comments

Comments
 (0)