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

Commit ca123d4

Browse files
committed
[Impeller] implement mask blur for textures
1 parent 0d8588b commit ca123d4

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

impeller/aiks/aiks_blur_unittests.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,5 +644,22 @@ TEST_P(AiksTest, GaussianBlurStyleOuter) {
644644
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
645645
}
646646

647+
TEST_P(AiksTest, GaussianBlurStyleInnerTexture) {
648+
Canvas canvas;
649+
canvas.Scale(GetContentScale());
650+
Paint paint;
651+
paint.color = Color::Green();
652+
paint.mask_blur_descriptor = Paint::MaskBlurDescriptor{
653+
.style = FilterContents::BlurStyle::kNormal,
654+
.sigma = Sigma(30),
655+
};
656+
std::shared_ptr<Texture> boston = CreateTextureForFixture("boston.jpg");
657+
canvas.DrawImage(std::make_shared<Image>(boston), {200, 200}, paint);
658+
Paint red;
659+
red.color = Color::Red();
660+
canvas.DrawRect(Rect::MakeXYWH(0, 0, 200, 200), red);
661+
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
662+
}
663+
647664
} // namespace testing
648665
} // namespace impeller

impeller/aiks/canvas.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,30 @@ void Canvas::DrawImageRect(const std::shared_ptr<Image>& image,
650650
return;
651651
}
652652

653-
auto contents = TextureContents::MakeRect(dest);
654-
contents->SetTexture(image->GetTexture());
655-
contents->SetSourceRect(source);
656-
contents->SetStrictSourceRect(src_rect_constraint ==
657-
SourceRectConstraint::kStrict);
658-
contents->SetSamplerDescriptor(std::move(sampler));
659-
contents->SetOpacity(paint.color.alpha);
660-
contents->SetDeferApplyingOpacity(paint.HasColorFilter());
653+
auto texture_contents = TextureContents::MakeRect(dest);
654+
texture_contents->SetTexture(image->GetTexture());
655+
texture_contents->SetSourceRect(source);
656+
texture_contents->SetStrictSourceRect(src_rect_constraint ==
657+
SourceRectConstraint::kStrict);
658+
texture_contents->SetSamplerDescriptor(std::move(sampler));
659+
texture_contents->SetOpacity(paint.color.alpha);
660+
texture_contents->SetDeferApplyingOpacity(paint.HasColorFilter());
661+
662+
bool needs_color_filter = paint.HasColorFilter();
663+
if (needs_color_filter) {
664+
auto color_filter = paint.GetColorFilter();
665+
if (texture_contents->ApplyColorFilter(
666+
color_filter->GetCPUColorFilterProc())) {
667+
needs_color_filter = false;
668+
}
669+
}
670+
671+
std::shared_ptr<Contents> contents = texture_contents;
672+
if (paint.mask_blur_descriptor.has_value()) {
673+
contents = paint.mask_blur_descriptor->CreateMaskBlur(
674+
texture_contents,
675+
needs_color_filter ? paint.GetColorFilter() : nullptr);
676+
}
661677

662678
Entity entity;
663679
entity.SetBlendMode(paint.blend_mode);

impeller/aiks/paint.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ std::shared_ptr<Contents> Paint::WithColorFilter(
121121
absorb_opacity);
122122
}
123123

124+
std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
125+
std::shared_ptr<TextureContents> texture_contents,
126+
const std::shared_ptr<ColorFilter>& color_filter) const {
127+
auto mask = std::make_shared<SolidColorContents>();
128+
mask->SetColor(Color::White());
129+
std::optional<Rect> coverage = texture_contents->GetCoverage({});
130+
std::shared_ptr<Geometry> geometry = Geometry::MakeRect(coverage.value());
131+
mask->SetGeometry(geometry);
132+
std::shared_ptr<FilterContents> blurred_mask =
133+
FilterContents::MakeGaussianBlur(FilterInput::Make(mask), sigma, sigma,
134+
Entity::TileMode::kDecal, style,
135+
geometry);
136+
137+
return ColorFilterContents::MakeBlend(
138+
BlendMode::kSourceIn,
139+
{FilterInput::Make(blurred_mask), FilterInput::Make(texture_contents)});
140+
}
141+
124142
std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
125143
std::shared_ptr<ColorSourceContents> color_source_contents,
126144
const std::shared_ptr<ColorFilter>& color_filter) const {

impeller/aiks/paint.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "impeller/entity/contents/contents.h"
1414
#include "impeller/entity/contents/filters/color_filter_contents.h"
1515
#include "impeller/entity/contents/filters/filter_contents.h"
16+
#include "impeller/entity/contents/texture_contents.h"
1617
#include "impeller/entity/entity.h"
1718
#include "impeller/entity/geometry/geometry.h"
1819
#include "impeller/geometry/color.h"
@@ -43,6 +44,10 @@ struct Paint {
4344
std::shared_ptr<ColorSourceContents> color_source_contents,
4445
const std::shared_ptr<ColorFilter>& color_filter) const;
4546

47+
std::shared_ptr<FilterContents> CreateMaskBlur(
48+
std::shared_ptr<TextureContents> texture_contents,
49+
const std::shared_ptr<ColorFilter>& color_filter) const;
50+
4651
std::shared_ptr<FilterContents> CreateMaskBlur(
4752
const FilterInput::Ref& input,
4853
bool is_solid_color) const;

0 commit comments

Comments
 (0)