Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 992ee04

Browse files
committed
cleanup
1 parent daf47d1 commit 992ee04

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

impeller/entity/contents/filters/gaussian_blur_filter_contents.cc

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ void BindVertices(Command& cmd,
3737
cmd.BindVertices(vtx_buffer);
3838
}
3939

40+
Matrix MakeAnchorScale(const Point& anchor, Vector2 scale) {
41+
return Matrix::MakeTranslation({anchor.x, anchor.y, 0}) *
42+
Matrix::MakeScale(scale) *
43+
Matrix::MakeTranslation({-anchor.x, -anchor.y, 0});
44+
}
45+
4046
std::shared_ptr<Texture> MakeDownsampleSubpass(
4147
const ContentContext& renderer,
4248
std::shared_ptr<Texture> input_texture,
@@ -59,19 +65,14 @@ std::shared_ptr<Texture> MakeDownsampleSubpass(
5965
frame_info.texture_sampler_y_coord_scale = 1.0;
6066
frame_info.alpha = 1.0;
6167

62-
Quad vertices = {Point(0, 0), Point(1, 0), Point(0, 1), Point(1, 1)};
63-
6468
// Insert transparent gutter around the downsampled image so the blur
6569
// creates a halo effect.
66-
ISize texture_size = input_texture->GetSize();
67-
vertices =
68-
(Matrix::MakeTranslation({0.5, 0.5, 0}) *
69-
Matrix::MakeScale(
70-
{texture_size.width / (texture_size.width + padding.x * 2),
71-
texture_size.height / (texture_size.height + padding.y * 2),
72-
1.0}) *
73-
Matrix::MakeTranslation({-0.5, -0.5, 0}))
74-
.Transform(vertices);
70+
Vector2 texture_size = Vector2(input_texture->GetSize());
71+
Quad vertices =
72+
MakeAnchorScale({0.5, 0.5},
73+
texture_size / (texture_size + padding * 2))
74+
.Transform(
75+
{Point(0, 0), Point(1, 0), Point(0, 1), Point(1, 1)});
7576

7677
BindVertices<TextureFillVertexShader>(cmd, host_buffer,
7778
{
@@ -153,12 +154,6 @@ Scalar CalculateScale(Scalar radius) {
153154
return (curve - 1) * limit + 1;
154155
};
155156

156-
template <typename T, typename U>
157-
Vector2 CalculateSizeRatio(const T& x, const U& y) {
158-
return Vector2{x.width / static_cast<Scalar>(y.width),
159-
x.height / static_cast<Scalar>(y.height)};
160-
}
161-
162157
} // namespace
163158

164159
GaussianBlurFilterContents::GaussianBlurFilterContents(Scalar sigma)
@@ -220,13 +215,13 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
220215
Vector2 downsample_scalar(desired_scalar, desired_scalar);
221216
Vector2 padding(ceil(blur_radius), ceil(blur_radius));
222217

223-
Size expanded_size(
224-
input_snapshot->texture->GetSize().width + 2.0 * padding.x,
225-
input_snapshot->texture->GetSize().height + 2.0 * padding.y);
218+
Vector2 padded_size =
219+
Vector2(input_snapshot->texture->GetSize()) + 2.0 * padding;
226220
// TODO(gaaclarke): I don't think we are correctly handling this fractional
227221
// amount we are throwing away.
228-
ISize subpass_size = ISize(round(expanded_size.width * downsample_scalar.x),
229-
round(expanded_size.height * downsample_scalar.y));
222+
Vector2 downsampled_size = padded_size * downsample_scalar;
223+
ISize subpass_size =
224+
ISize(round(downsampled_size.x), round(downsampled_size.y));
230225

231226
Quad uvs =
232227
CalculateUVs(inputs[0], entity, input_snapshot->texture->GetSize());
@@ -235,13 +230,12 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
235230
renderer, input_snapshot->texture, input_snapshot->sampler_descriptor,
236231
uvs, subpass_size, padding);
237232

238-
Size pass1_pixel_size(1.0 / pass1_out_texture->GetSize().width,
239-
1.0 / pass1_out_texture->GetSize().height);
233+
Vector2 pass1_pixel_size = 1.0 / Vector2(pass1_out_texture->GetSize());
240234

241235
std::shared_ptr<Texture> pass2_out_texture = MakeBlurSubpass(
242236
renderer, pass1_out_texture, input_snapshot->sampler_descriptor,
243237
GaussianBlurFragmentShader::BlurInfo{
244-
.blur_uv_offset = Point(0.0, pass1_pixel_size.height),
238+
.blur_uv_offset = Point(0.0, pass1_pixel_size.y),
245239
.blur_sigma = sigma_ * downsample_scalar.y,
246240
.blur_radius = blur_radius * downsample_scalar.y,
247241
.step_size = 1.0,
@@ -251,7 +245,7 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
251245
std::shared_ptr<Texture> pass3_out_texture = MakeBlurSubpass(
252246
renderer, pass2_out_texture, input_snapshot->sampler_descriptor,
253247
GaussianBlurFragmentShader::BlurInfo{
254-
.blur_uv_offset = Point(pass1_pixel_size.width, 0.0),
248+
.blur_uv_offset = Point(pass1_pixel_size.x, 0.0),
255249
.blur_sigma = sigma_ * downsample_scalar.x,
256250
.blur_radius = blur_radius * downsample_scalar.x,
257251
.step_size = 1.0,
@@ -260,14 +254,13 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
260254
SamplerDescriptor sampler_desc = MakeSamplerDescriptor(
261255
MinMagFilter::kLinear, SamplerAddressMode::kClampToEdge);
262256

263-
Vector2 final_scale =
264-
CalculateSizeRatio(expanded_size, pass1_out_texture->GetSize());
265257
return Entity::FromSnapshot(
266258
Snapshot{
267259
.texture = pass3_out_texture,
268260
.transform = entity.GetTransformation() *
269261
Matrix::MakeTranslation({-padding.x, -padding.y, 0}) *
270-
Matrix::MakeScale(final_scale),
262+
Matrix::MakeScale(padded_size /
263+
Vector2(pass1_out_texture->GetSize())),
271264
.sampler_descriptor = sampler_desc,
272265
.opacity = input_snapshot->opacity},
273266
entity.GetBlendMode(), entity.GetClipDepth());

0 commit comments

Comments
 (0)