| Flutter Impeller |

gaussian_blur_filter_contents.h
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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_GAUSSIAN_BLUR_FILTER_CONTENTS_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_GAUSSIAN_BLUR_FILTER_CONTENTS_H_
7
8 #include <optional>
9 #include "impeller/entity/contents/content_context.h"
10 #include "impeller/entity/contents/filters/filter_contents.h"
11 #include "impeller/entity/geometry/geometry.h"
12
13 namespace impeller {
14
15 // Comes from gaussian.frag.
16 static constexpr int32_t kGaussianBlurMaxKernelSize = 50;
17
18 struct BlurParameters {
19 Point blur_uv_offset;
20 Scalar blur_sigma;
21 int blur_radius;
22 int step_size;
23 };
24
25 /// A larger mirror of GaussianBlurPipeline::FragmentShader::KernelSamples.
26 ///
27 /// This is a mirror of GaussianBlurPipeline::FragmentShader::KernelSamples that
28 /// can hold 2x the max kernel size since it will get reduced with the lerp
29 /// hack.
30 struct KernelSamples {
31 static constexpr int kMaxKernelSize = kGaussianBlurMaxKernelSize * 2;
32 int sample_count;
33 GaussianBlurPipeline::FragmentShader::KernelSample samples[kMaxKernelSize];
34 };
35
36 KernelSamples GenerateBlurInfo(BlurParameters parameters);
37
38 /// This will shrink the size of a kernel by roughly half by sampling between
39 /// samples and relying on linear interpolation between the samples.
40 GaussianBlurPipeline::FragmentShader::KernelSamples LerpHackKernelSamples(
41 KernelSamples samples);
42
43 /// Performs a bidirectional Gaussian blur.
44 ///
45 /// This is accomplished by rendering multiple passes in multiple directions.
46 /// Note: This will replace `DirectionalGaussianBlurFilterContents`.
47 class GaussianBlurFilterContents final : public FilterContents {
48 public:
49 static std::string_view kNoMipsError;
50 static const int32_t kBlurFilterRequiredMipCount;
51
52 explicit GaussianBlurFilterContents(
53 Scalar sigma_x,
54 Scalar sigma_y,
55 Entity::TileMode tile_mode,
56 BlurStyle mask_blur_style,
57 const std::shared_ptr<Geometry>& mask_geometry);
58
59 Scalar GetSigmaX() const { return sigma_.x; }
60 Scalar GetSigmaY() const { return sigma_.y; }
61
62 // |FilterContents|
63 std::optional<Rect> GetFilterSourceCoverage(
64 const Matrix& effect_transform,
65 const Rect& output_limit) const override;
66
67 // |FilterContents|
68 std::optional<Rect> GetFilterCoverage(
69 const FilterInput::Vector& inputs,
70 const Entity& entity,
71 const Matrix& effect_transform) const override;
72
73 /// Given a sigma (standard deviation) calculate the blur radius (1/2 the
74 /// kernel size).
75 static Scalar CalculateBlurRadius(Scalar sigma);
76
77 /// Calculate the UV coordinates for rendering the filter_input.
78 /// @param filter_input The FilterInput that should be rendered.
79 /// @param entity The associated entity for the filter_input.
80 /// @param source_rect The rect in source coordinates to convert to uvs.
81 /// @param texture_size The rect to convert in source coordinates.
82 static Quad CalculateUVs(const std::shared_ptr<FilterInput>& filter_input,
83 const Entity& entity,
84 const Rect& source_rect,
85 const ISize& texture_size);
86
87 /// Calculate the scale factor for the downsample pass given a sigma value.
88 ///
89 /// Visible for testing.
90 static Scalar CalculateScale(Scalar sigma);
91
92 /// Scales down the sigma value to match Skia's behavior.
93 ///
94 /// effective_blur_radius = CalculateBlurRadius(ScaleSigma(sigma_));
95 ///
96 /// This function was calculated by observing Skia's behavior. Its blur at
97 /// 500 seemed to be 0.15. Since we clamp at 500 I solved the quadratic
98 /// equation that puts the minima there and a f(0)=1.
99 static Scalar ScaleSigma(Scalar sigma);
100
101 private:
102 // |FilterContents|
103 std::optional<Entity> RenderFilter(
104 const FilterInput::Vector& input_textures,
105 const ContentContext& renderer,
106 const Entity& entity,
107 const Matrix& effect_transform,
108 const Rect& coverage,
109 const std::optional<Rect>& coverage_hint) const override;
110
111 const Vector2 sigma_ = Vector2(0.0, 0.0);
112 const Entity::TileMode tile_mode_;
113 const BlurStyle mask_blur_style_;
114 std::shared_ptr<Geometry> mask_geometry_;
115 };
116
117 } // namespace impeller
118
119 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_GAUSSIAN_BLUR_FILTER_CONTENTS_H_
impeller::KernelSamples
Definition: gaussian_blur_filter_contents.h:30
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::KernelSamples::sample_count
int sample_count
Definition: gaussian_blur_filter_contents.h:32
impeller::BlurParameters::blur_uv_offset
Point blur_uv_offset
Definition: gaussian_blur_filter_contents.h:19
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:26
impeller::KernelSamples::samples
GaussianBlurPipeline::FragmentShader::KernelSample samples[kMaxKernelSize]
Definition: gaussian_blur_filter_contents.h:33
impeller::Vector2
Point Vector2
Definition: point.h:326
impeller::kGaussianBlurMaxKernelSize
static constexpr int32_t kGaussianBlurMaxKernelSize
Definition: gaussian_blur_filter_contents.h:16
impeller::GaussianBlurFilterContents::CalculateBlurRadius
static Scalar CalculateBlurRadius(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:787
impeller::GaussianBlurFilterContents::GetSigmaX
Scalar GetSigmaX() const
Definition: gaussian_blur_filter_contents.h:59
impeller::GaussianBlurFilterContents::GetFilterCoverage
std::optional< Rect > GetFilterCoverage(const FilterInput::Vector &inputs, const Entity &entity, const Matrix &effect_transform) const override
Internal utility method for |GetLocalCoverage| that computes the output coverage of this filter acros...
Definition: gaussian_blur_filter_contents.cc:598
impeller::BlurParameters::blur_radius
int blur_radius
Definition: gaussian_blur_filter_contents.h:21
impeller::Entity
Definition: entity.h:20
impeller::TSize
Definition: size.h:19
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:327
filter_contents.h
impeller::GaussianBlurFilterContents::GetFilterSourceCoverage
std::optional< Rect > GetFilterSourceCoverage(const Matrix &effect_transform, const Rect &output_limit) const override
Internal utility method for |GetSourceCoverage| that computes the inverse effect of this transform on...
Definition: gaussian_blur_filter_contents.cc:587
geometry.h
impeller::BlurParameters::step_size
int step_size
Definition: gaussian_blur_filter_contents.h:22
impeller::BlurParameters
Definition: gaussian_blur_filter_contents.h:18
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::GenerateBlurInfo
KernelSamples GenerateBlurInfo(BlurParameters parameters)
Definition: gaussian_blur_filter_contents.cc:817
impeller::GaussianBlurFilterContents::GetSigmaY
Scalar GetSigmaY() const
Definition: gaussian_blur_filter_contents.h:60
content_context.h
impeller::GaussianBlurFilterContents::kNoMipsError
static std::string_view kNoMipsError
Definition: gaussian_blur_filter_contents.h:49
impeller::TPoint< Scalar >
impeller::GaussianBlurFilterContents::CalculateScale
static Scalar CalculateScale(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:560
impeller::LerpHackKernelSamples
GaussianBlurPipeline::FragmentShader::KernelSamples LerpHackKernelSamples(KernelSamples parameters)
Definition: gaussian_blur_filter_contents.cc:864
impeller::GaussianBlurFilterContents::CalculateUVs
static Quad CalculateUVs(const std::shared_ptr< FilterInput > &filter_input, const Entity &entity, const Rect &source_rect, const ISize &texture_size)
Definition: gaussian_blur_filter_contents.cc:791
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::GaussianBlurFilterContents
Definition: gaussian_blur_filter_contents.h:47
impeller
Definition: aiks_blend_unittests.cc:18
impeller::BlurParameters::blur_sigma
Scalar blur_sigma
Definition: gaussian_blur_filter_contents.h:20
impeller::ContentContext
Definition: content_context.h:366
impeller::KernelSamples::kMaxKernelSize
static constexpr int kMaxKernelSize
Definition: gaussian_blur_filter_contents.h:31
impeller::TRect< Scalar >
impeller::GaussianBlurFilterContents::kBlurFilterRequiredMipCount
static const int32_t kBlurFilterRequiredMipCount
Definition: gaussian_blur_filter_contents.h:50
impeller::GaussianBlurFilterContents::GaussianBlurFilterContents
GaussianBlurFilterContents(Scalar sigma_x, Scalar sigma_y, Entity::TileMode tile_mode, BlurStyle mask_blur_style, const std::shared_ptr< Geometry > &mask_geometry)
Definition: gaussian_blur_filter_contents.cc:543
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::FilterContents
Definition: filter_contents.h:22
impeller::GaussianBlurFilterContents::ScaleSigma
static Scalar ScaleSigma(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:807