From 9fef46c3c8d9f569daaf0436203b24d7d51d77cb Mon Sep 17 00:00:00 2001
From: gaaclarke <30870216+gaaclarke@users.noreply.github.com>
Date: Thu, 8 Feb 2024 08:39:05 -0800
Subject: [PATCH] [Impeller] blur: drop eighth downsample barrier
(flutter/engine#50405)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
issue: https://github.com/flutter/flutter/issues/142990
I pushed this value as low as I dared to go and ended up with a kernel
size of 41 (radius 20). This means the range of sigma ~60 to ~70 will
now be 1/16th scalar instead of 1/8th.
Here is the new downsample rate versus sigma on a 2x display.
## before
## after
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
---
.../contents/filters/gaussian_blur_filter_contents.cc | 10 ++++++++--
.../filters/gaussian_blur_filter_contents_unittests.cc | 3 ++-
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
index 36bc0c8606..49f201413b 100644
--- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
+++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
@@ -221,12 +221,18 @@ Scalar GaussianBlurFilterContents::CalculateScale(Scalar sigma) {
exponent = std::max(-4.0f, exponent);
Scalar rounded = powf(2.0f, exponent);
Scalar result = rounded;
- // Only drop below 1/8 if 1/8 would overflow our kernel.
+ // Extend the range of the 1/8th downsample based on the effective kernel size
+ // for the blur.
if (rounded < 0.125f) {
Scalar rounded_plus = powf(2.0f, exponent + 1);
Scalar blur_radius = CalculateBlurRadius(sigma);
int kernel_size_plus = (ScaleBlurRadius(blur_radius, rounded_plus) * 2) + 1;
- result = kernel_size_plus < kMaxKernelSize ? rounded_plus : rounded;
+ // This constant was picked by looking at the results to make sure no
+ // shimmering was introduced at the highest sigma values that downscale to
+ // 1/16th.
+ static constexpr int32_t kEighthDownsampleKernalWidthMax = 41;
+ result = kernel_size_plus <= kEighthDownsampleKernalWidthMax ? rounded_plus
+ : rounded;
}
return result;
};
diff --git a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
index d84da440a4..369f9cb914 100644
--- a/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
+++ b/engine/src/flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
@@ -198,7 +198,8 @@ TEST(GaussianBlurFilterContentsTest, CalculateSigmaValues) {
EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(4.0f), 1);
EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(16.0f), 0.25);
// Hang on to 1/8 as long as possible.
- EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(100.0f), 0.125);
+ EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(95.0f), 0.125);
+ EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(96.0f), 0.0625);
// Downsample clamped to 1/16th.
EXPECT_EQ(GaussianBlurFilterContents::CalculateScale(1024.0f), 0.0625);
}