| 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 struct BlurParameters {
16 Point blur_uv_offset;
17 Scalar blur_sigma;
18 int blur_radius;
19 int step_size;
20 };
21
22 KernelPipeline::FragmentShader::KernelSamples GenerateBlurInfo(
23 BlurParameters parameters);
24
25 /// This will shrink the size of a kernel by roughly half by sampling between
26 /// samples and relying on linear interpolation between the samples.
27 KernelPipeline::FragmentShader::KernelSamples LerpHackKernelSamples(
28 KernelPipeline::FragmentShader::KernelSamples samples);
29
30 /// Performs a bidirectional Gaussian blur.
31 ///
32 /// This is accomplished by rendering multiple passes in multiple directions.
33 /// Note: This will replace `DirectionalGaussianBlurFilterContents`.
34 class GaussianBlurFilterContents final : public FilterContents {
35 public:
36 static std::string_view kNoMipsError;
37 static const int32_t kBlurFilterRequiredMipCount;
38
39 explicit GaussianBlurFilterContents(
40 Scalar sigma_x,
41 Scalar sigma_y,
42 Entity::TileMode tile_mode,
43 BlurStyle mask_blur_style,
44 const std::shared_ptr<Geometry>& mask_geometry);
45
46 Scalar GetSigmaX() const { return sigma_x_; }
47 Scalar GetSigmaY() const { return sigma_y_; }
48
49 // |FilterContents|
50 std::optional<Rect> GetFilterSourceCoverage(
51 const Matrix& effect_transform,
52 const Rect& output_limit) const override;
53
54 // |FilterContents|
55 std::optional<Rect> GetFilterCoverage(
56 const FilterInput::Vector& inputs,
57 const Entity& entity,
58 const Matrix& effect_transform) const override;
59
60 /// Given a sigma (standard deviation) calculate the blur radius (1/2 the
61 /// kernel size).
62 static Scalar CalculateBlurRadius(Scalar sigma);
63
64 /// Calculate the UV coordinates for rendering the filter_input.
65 /// @param filter_input The FilterInput that should be rendered.
66 /// @param entity The associated entity for the filter_input.
67 /// @param source_rect The rect in source coordinates to convert to uvs.
68 /// @param texture_size The rect to convert in source coordinates.
69 static Quad CalculateUVs(const std::shared_ptr<FilterInput>& filter_input,
70 const Entity& entity,
71 const Rect& source_rect,
72 const ISize& texture_size);
73
74 /// Calculate the scale factor for the downsample pass given a sigma value.
75 ///
76 /// Visible for testing.
77 static Scalar CalculateScale(Scalar sigma);
78
79 /// Scales down the sigma value to match Skia's behavior.
80 ///
81 /// effective_blur_radius = CalculateBlurRadius(ScaleSigma(sigma_));
82 ///
83 /// This function was calculated by observing Skia's behavior. Its blur at
84 /// 500 seemed to be 0.15. Since we clamp at 500 I solved the quadratic
85 /// equation that puts the minima there and a f(0)=1.
86 static Scalar ScaleSigma(Scalar sigma);
87
88 private:
89 // |FilterContents|
90 std::optional<Entity> RenderFilter(
91 const FilterInput::Vector& input_textures,
92 const ContentContext& renderer,
93 const Entity& entity,
94 const Matrix& effect_transform,
95 const Rect& coverage,
96 const std::optional<Rect>& coverage_hint) const override;
97
98 const Scalar sigma_x_ = 0.0;
99 const Scalar sigma_y_ = 0.0;
100 const Entity::TileMode tile_mode_;
101 const BlurStyle mask_blur_style_;
102 std::shared_ptr<Geometry> mask_geometry_;
103 };
104
105 } // namespace impeller
106
107 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_GAUSSIAN_BLUR_FILTER_CONTENTS_H_
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::BlurParameters::blur_uv_offset
Point blur_uv_offset
Definition: gaussian_blur_filter_contents.h:16
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:26
impeller::GaussianBlurFilterContents::CalculateBlurRadius
static Scalar CalculateBlurRadius(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:556
impeller::GenerateBlurInfo
KernelPipeline::FragmentShader::KernelSamples GenerateBlurInfo(BlurParameters parameters)
Definition: gaussian_blur_filter_contents.cc:586
impeller::GaussianBlurFilterContents::GetSigmaX
Scalar GetSigmaX() const
Definition: gaussian_blur_filter_contents.h:46
impeller::LerpHackKernelSamples
KernelPipeline::FragmentShader::KernelSamples LerpHackKernelSamples(KernelPipeline::FragmentShader::KernelSamples parameters)
Definition: gaussian_blur_filter_contents.cc:622
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:359
impeller::BlurParameters::blur_radius
int blur_radius
Definition: gaussian_blur_filter_contents.h:18
impeller::Entity
Definition: entity.h:21
impeller::TSize< int64_t >
impeller::Quad
std::array< Point, 4 > Quad
Definition: point.h:321
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:348
geometry.h
impeller::BlurParameters::step_size
int step_size
Definition: gaussian_blur_filter_contents.h:19
impeller::BlurParameters
Definition: gaussian_blur_filter_contents.h:15
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::GaussianBlurFilterContents::GetSigmaY
Scalar GetSigmaY() const
Definition: gaussian_blur_filter_contents.h:47
content_context.h
impeller::GaussianBlurFilterContents::kNoMipsError
static std::string_view kNoMipsError
Definition: gaussian_blur_filter_contents.h:36
impeller::TPoint< Scalar >
impeller::GaussianBlurFilterContents::CalculateScale
static Scalar CalculateScale(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:321
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:560
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::GaussianBlurFilterContents
Definition: gaussian_blur_filter_contents.h:34
impeller
Definition: aiks_blur_unittests.cc:20
impeller::BlurParameters::blur_sigma
Scalar blur_sigma
Definition: gaussian_blur_filter_contents.h:17
impeller::ContentContext
Definition: content_context.h:392
impeller::TRect
Definition: rect.h:122
impeller::GaussianBlurFilterContents::kBlurFilterRequiredMipCount
static const int32_t kBlurFilterRequiredMipCount
Definition: gaussian_blur_filter_contents.h:37
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:303
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:576