[Impeller] Fix accumulating translucency opacity peephole bug (flutter/engine#40639)

This commit is contained in:
Brandon DeRosier
2023-03-26 12:33:11 -07:00
committed by GitHub
parent fba3461d8c
commit 7340201478
22 changed files with 79 additions and 67 deletions

View File

@@ -31,7 +31,7 @@ std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
auto& source = color_source.value();
auto contents = source();
contents->SetGeometry(std::move(geometry));
contents->SetAlpha(color.alpha);
contents->SetOpacity(color.alpha);
return contents;
}
auto solid_color = std::make_shared<SolidColorContents>();
@@ -46,7 +46,7 @@ std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
auto& source = color_source.value();
auto contents = source();
contents->SetGeometry(geometry);
contents->SetAlpha(color.alpha);
contents->SetOpacity(color.alpha);
return contents;
}
auto solid_color = std::make_shared<SolidColorContents>();

View File

@@ -97,7 +97,7 @@ bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass(
auto had_subpass = entity_pass->IterateUntilSubpass(
[&all_coverages, &all_can_accept](Entity& entity) {
auto contents = entity.GetContents();
if (!contents->CanAcceptOpacity(entity)) {
if (!contents->CanInheritOpacity(entity)) {
all_can_accept = false;
return false;
}

View File

@@ -70,7 +70,7 @@ bool ClipContents::ShouldRender(
return true;
}
bool ClipContents::CanAcceptOpacity(const Entity& entity) const {
bool ClipContents::CanInheritOpacity(const Entity& entity) const {
return true;
}
@@ -172,7 +172,7 @@ bool ClipRestoreContents::ShouldRender(
return true;
}
bool ClipRestoreContents::CanAcceptOpacity(const Entity& entity) const {
bool ClipRestoreContents::CanInheritOpacity(const Entity& entity) const {
return true;
}

View File

@@ -42,7 +42,7 @@ class ClipContents final : public Contents {
const Entity& entity,
RenderPass& pass) const override;
// |Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
// |Contents|
void SetInheritedOpacity(Scalar opacity) override;
@@ -84,7 +84,7 @@ class ClipRestoreContents final : public Contents {
RenderPass& pass) const override;
// |Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
// |Contents|
void SetInheritedOpacity(Scalar opacity) override;

View File

@@ -21,12 +21,12 @@ const std::shared_ptr<Geometry>& ColorSourceContents::GetGeometry() const {
return geometry_;
}
void ColorSourceContents::SetAlpha(Scalar alpha) {
alpha_ = alpha;
void ColorSourceContents::SetOpacity(Scalar alpha) {
opacity_ = alpha;
}
Scalar ColorSourceContents::GetAlpha() const {
return alpha_;
Scalar ColorSourceContents::GetOpacity() const {
return opacity_ * inherited_opacity_;
}
void ColorSourceContents::SetEffectTransform(Matrix matrix) {
@@ -42,12 +42,12 @@ std::optional<Rect> ColorSourceContents::GetCoverage(
return geometry_->GetCoverage(entity.GetTransformation());
};
bool ColorSourceContents::CanAcceptOpacity(const Entity& entity) const {
bool ColorSourceContents::CanInheritOpacity(const Entity& entity) const {
return true;
}
void ColorSourceContents::SetInheritedOpacity(Scalar opacity) {
SetAlpha(GetAlpha() * opacity);
inherited_opacity_ = opacity;
}
bool ColorSourceContents::ShouldRender(

View File

@@ -24,7 +24,7 @@ class ColorSourceContents : public Contents {
const Matrix& GetInverseMatrix() const;
void SetAlpha(Scalar alpha);
void SetOpacity(Scalar opacity);
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
@@ -33,13 +33,13 @@ class ColorSourceContents : public Contents {
bool ShouldRender(const Entity& entity,
const std::optional<Rect>& stencil_coverage) const override;
// | Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
// |Contents|
bool CanInheritOpacity(const Entity& entity) const override;
// | Contents|
// |Contents|
void SetInheritedOpacity(Scalar opacity) override;
Scalar GetAlpha() const;
Scalar GetOpacity() const;
protected:
const std::shared_ptr<Geometry>& GetGeometry() const;
@@ -47,7 +47,8 @@ class ColorSourceContents : public Contents {
private:
std::shared_ptr<Geometry> geometry_;
Matrix inverse_matrix_;
Scalar alpha_ = 1.0;
Scalar opacity_ = 1.0;
Scalar inherited_opacity_ = 1.0;
FML_DISALLOW_COPY_AND_ASSIGN(ColorSourceContents);
};

View File

@@ -70,7 +70,7 @@ bool ConicalGradientContents::RenderSSBO(const ContentContext& renderer,
frag_info.center = center_;
frag_info.radius = radius_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
if (focus_) {
frag_info.focus = focus_.value();
frag_info.focus_radius = focus_radius_;
@@ -141,7 +141,7 @@ bool ConicalGradientContents::RenderTexture(const ContentContext& renderer,
frag_info.radius = radius_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
0.5 / gradient_texture->GetSize().height);
if (focus_) {

View File

@@ -95,7 +95,7 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
return snapshot;
}
bool Contents::CanAcceptOpacity(const Entity& entity) const {
bool Contents::CanInheritOpacity(const Entity& entity) const {
return false;
}

View File

@@ -94,7 +94,7 @@ class Contents {
/// a way that makes accepting opacity impossible. It is always safe
/// to return false, especially if computing overlap would be
/// computationally expensive.
virtual bool CanAcceptOpacity(const Entity& entity) const;
virtual bool CanInheritOpacity(const Entity& entity) const;
/// @brief Inherit the provided opacity.
///

View File

@@ -71,7 +71,7 @@ bool LinearGradientContents::RenderTexture(const ContentContext& renderer,
frag_info.end_point = end_point_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
0.5 / gradient_texture->GetSize().height);
@@ -126,7 +126,7 @@ bool LinearGradientContents::RenderSSBO(const ContentContext& renderer,
frag_info.start_point = start_point_;
frag_info.end_point = end_point_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
auto& host_buffer = pass.GetTransientsBuffer();
auto colors = CreateGradientColors(colors_, stops_);

View File

@@ -64,7 +64,7 @@ bool RadialGradientContents::RenderSSBO(const ContentContext& renderer,
frag_info.center = center_;
frag_info.radius = radius_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
auto& host_buffer = pass.GetTransientsBuffer();
auto colors = CreateGradientColors(colors_, stops_);
@@ -128,7 +128,7 @@ bool RadialGradientContents::RenderTexture(const ContentContext& renderer,
frag_info.radius = radius_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
0.5 / gradient_texture->GetSize().height);

View File

@@ -37,7 +37,7 @@ void RuntimeEffectContents::SetTextureInputs(
texture_inputs_ = std::move(texture_inputs);
}
bool RuntimeEffectContents::CanAcceptOpacity(const Entity& entity) const {
bool RuntimeEffectContents::CanInheritOpacity(const Entity& entity) const {
return false;
}

View File

@@ -26,7 +26,7 @@ class RuntimeEffectContents final : public ColorSourceContents {
void SetTextureInputs(std::vector<TextureInput> texture_inputs);
// | Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
// |Contents|
bool Render(const ContentContext& renderer,

View File

@@ -20,8 +20,8 @@ void SolidColorContents::SetColor(Color color) {
color_ = color;
}
const Color& SolidColorContents::GetColor() const {
return color_;
Color SolidColorContents::GetColor() const {
return color_.WithAlpha(color_.alpha * inherited_opacity_);
}
void SolidColorContents::SetGeometry(std::shared_ptr<Geometry> geometry) {
@@ -29,19 +29,18 @@ void SolidColorContents::SetGeometry(std::shared_ptr<Geometry> geometry) {
}
// | Contents|
bool SolidColorContents::CanAcceptOpacity(const Entity& entity) const {
bool SolidColorContents::CanInheritOpacity(const Entity& entity) const {
return true;
}
// | Contents|
void SolidColorContents::SetInheritedOpacity(Scalar opacity) {
auto color = color_;
color_ = color.WithAlpha(color.alpha * opacity);
inherited_opacity_ = opacity;
}
std::optional<Rect> SolidColorContents::GetCoverage(
const Entity& entity) const {
if (color_.IsTransparent()) {
if (GetColor().IsTransparent()) {
return std::nullopt;
}
if (geometry_ == nullptr) {
@@ -86,7 +85,7 @@ bool SolidColorContents::Render(const ContentContext& renderer,
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
FS::FragInfo frag_info;
frag_info.color = color_.Premultiply();
frag_info.color = GetColor().Premultiply();
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
if (!pass.AddCommand(std::move(cmd))) {

View File

@@ -33,10 +33,10 @@ class SolidColorContents final : public Contents {
void SetColor(Color color);
const Color& GetColor() const;
Color GetColor() const;
// | Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
// | Contents|
void SetInheritedOpacity(Scalar opacity) override;
@@ -57,6 +57,7 @@ class SolidColorContents final : public Contents {
std::shared_ptr<Geometry> geometry_;
Color color_;
Scalar inherited_opacity_ = 1.0;
FML_DISALLOW_COPY_AND_ASSIGN(SolidColorContents);
};

View File

@@ -70,7 +70,7 @@ bool SweepGradientContents::RenderSSBO(const ContentContext& renderer,
frag_info.bias = bias_;
frag_info.scale = scale_;
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
auto& host_buffer = pass.GetTransientsBuffer();
auto colors = CreateGradientColors(colors_, stops_);
@@ -135,7 +135,7 @@ bool SweepGradientContents::RenderTexture(const ContentContext& renderer,
frag_info.scale = scale_;
frag_info.texture_sampler_y_coord_scale = gradient_texture->GetYCoordScale();
frag_info.tile_mode = static_cast<Scalar>(tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
frag_info.half_texel = Vector2(0.5 / gradient_texture->GetSize().width,
0.5 / gradient_texture->GetSize().height);

View File

@@ -51,16 +51,15 @@ void TextContents::SetColor(Color color) {
}
Color TextContents::GetColor() const {
return color_;
return color_.WithAlpha(color_.alpha * inherited_opacity_);
}
bool TextContents::CanAcceptOpacity(const Entity& entity) const {
bool TextContents::CanInheritOpacity(const Entity& entity) const {
return !frame_.MaybeHasOverlapping();
}
void TextContents::SetInheritedOpacity(Scalar opacity) {
auto color = color_;
color_ = color.WithAlpha(color.alpha * opacity);
inherited_opacity_ = opacity;
}
void TextContents::SetInverseMatrix(Matrix matrix) {
@@ -230,13 +229,14 @@ bool TextContents::RenderSdf(const ContentContext& renderer,
cmd.stencil_reference = entity.GetStencilDepth();
return CommonRender<GlyphAtlasSdfPipeline>(
renderer, entity, pass, color_, frame_, inverse_matrix_, atlas, cmd);
renderer, entity, pass, GetColor(), frame_, inverse_matrix_, atlas, cmd);
}
bool TextContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
if (color_.IsTransparent()) {
auto color = GetColor();
if (color.IsTransparent()) {
return true;
}
@@ -264,8 +264,8 @@ bool TextContents::Render(const ContentContext& renderer,
cmd.pipeline = renderer.GetGlyphAtlasPipeline(opts);
cmd.stencil_reference = entity.GetStencilDepth();
return CommonRender<GlyphAtlasPipeline>(renderer, entity, pass, color_,
frame_, inverse_matrix_, atlas, cmd);
return CommonRender<GlyphAtlasPipeline>(renderer, entity, pass, color, frame_,
inverse_matrix_, atlas, cmd);
}
} // namespace impeller

View File

@@ -34,7 +34,7 @@ class TextContents final : public Contents {
Color GetColor() const;
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
void SetInheritedOpacity(Scalar opacity) override;
@@ -56,6 +56,7 @@ class TextContents final : public Contents {
private:
TextFrame frame_;
Color color_;
Scalar inherited_opacity_;
mutable std::shared_ptr<LazyGlyphAtlas> lazy_atlas_;
Matrix inverse_matrix_;

View File

@@ -54,20 +54,20 @@ void TextureContents::SetStencilEnabled(bool enabled) {
stencil_enabled_ = enabled;
}
bool TextureContents::CanAcceptOpacity(const Entity& entity) const {
bool TextureContents::CanInheritOpacity(const Entity& entity) const {
return true;
}
void TextureContents::SetInheritedOpacity(Scalar opacity) {
opacity_ = opacity_ * opacity;
inherited_opacity_ = opacity;
}
Scalar TextureContents::GetOpacity() const {
return opacity_;
return opacity_ * inherited_opacity_;
}
std::optional<Rect> TextureContents::GetCoverage(const Entity& entity) const {
if (opacity_ == 0) {
if (GetOpacity() == 0) {
return std::nullopt;
}
return rect_.TransformBounds(entity.GetTransformation());
@@ -81,8 +81,9 @@ std::optional<Snapshot> TextureContents::RenderToSnapshot(
// Passthrough textures that have simple rectangle paths and complete source
// rects.
auto bounds = rect_;
auto opacity = GetOpacity();
if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
(opacity_ >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
(opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
auto scale = Vector2(bounds.size / Size(texture_->GetSize()));
return Snapshot{
.texture = texture_,
@@ -90,7 +91,7 @@ std::optional<Snapshot> TextureContents::RenderToSnapshot(
Matrix::MakeTranslation(bounds.origin) *
Matrix::MakeScale(scale),
.sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
.opacity = opacity_};
.opacity = opacity};
}
return Contents::RenderToSnapshot(
renderer, entity, sampler_descriptor.value_or(sampler_descriptor_));
@@ -136,7 +137,7 @@ bool TextureContents::Render(const ContentContext& renderer,
frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
FS::FragInfo frag_info;
frag_info.alpha = opacity_;
frag_info.alpha = GetOpacity();
Command cmd;
cmd.label = "Texture Fill";

View File

@@ -66,7 +66,7 @@ class TextureContents final : public Contents {
RenderPass& pass) const override;
// |Contents|
bool CanAcceptOpacity(const Entity& entity) const override;
bool CanInheritOpacity(const Entity& entity) const override;
// |Contents|
void SetInheritedOpacity(Scalar opacity) override;
@@ -83,6 +83,7 @@ class TextureContents final : public Contents {
SamplerDescriptor sampler_descriptor_ = {};
Rect source_rect_;
Scalar opacity_ = 1.0f;
Scalar inherited_opacity_ = 1.0f;
bool defer_applying_opacity_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(TextureContents);

View File

@@ -109,7 +109,7 @@ bool TiledTextureContents::Render(const ContentContext& renderer,
FS::FragInfo frag_info;
frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
frag_info.alpha = GetAlpha();
frag_info.alpha = GetOpacity();
Command cmd;
cmd.label = "TiledTextureFill";

View File

@@ -2387,8 +2387,10 @@ TEST_P(EntityTest, InheritOpacityTest) {
// Texture contents can always accept opacity.
auto texture_contents = std::make_shared<TextureContents>();
texture_contents->SetOpacity(0.5);
ASSERT_TRUE(texture_contents->CanAcceptOpacity(entity));
ASSERT_TRUE(texture_contents->CanInheritOpacity(entity));
texture_contents->SetInheritedOpacity(0.5);
ASSERT_EQ(texture_contents->GetOpacity(), 0.25);
texture_contents->SetInheritedOpacity(0.5);
ASSERT_EQ(texture_contents->GetOpacity(), 0.25);
@@ -2399,8 +2401,10 @@ TEST_P(EntityTest, InheritOpacityTest) {
Geometry::MakeRect(Rect::MakeLTRB(100, 100, 200, 200)));
solid_color->SetColor(Color::Blue().WithAlpha(0.5));
ASSERT_TRUE(solid_color->CanAcceptOpacity(entity));
ASSERT_TRUE(solid_color->CanInheritOpacity(entity));
solid_color->SetInheritedOpacity(0.5);
ASSERT_EQ(solid_color->GetColor().alpha, 0.25);
solid_color->SetInheritedOpacity(0.5);
ASSERT_EQ(solid_color->GetColor().alpha, 0.25);
@@ -2409,12 +2413,14 @@ TEST_P(EntityTest, InheritOpacityTest) {
auto tiled_texture = std::make_shared<TiledTextureContents>();
tiled_texture->SetGeometry(
Geometry::MakeRect(Rect::MakeLTRB(100, 100, 200, 200)));
tiled_texture->SetAlpha(0.5);
tiled_texture->SetOpacity(0.5);
ASSERT_TRUE(tiled_texture->CanAcceptOpacity(entity));
ASSERT_TRUE(tiled_texture->CanInheritOpacity(entity));
tiled_texture->SetInheritedOpacity(0.5);
ASSERT_EQ(tiled_texture->GetAlpha(), 0.25);
ASSERT_EQ(tiled_texture->GetOpacity(), 0.25);
tiled_texture->SetInheritedOpacity(0.5);
ASSERT_EQ(tiled_texture->GetOpacity(), 0.25);
// Text contents can accept opacity if the text frames do not
// overlap
@@ -2429,18 +2435,20 @@ TEST_P(EntityTest, InheritOpacityTest) {
text_contents->SetTextFrame(frame);
text_contents->SetColor(Color::Blue().WithAlpha(0.5));
ASSERT_TRUE(text_contents->CanAcceptOpacity(entity));
ASSERT_TRUE(text_contents->CanInheritOpacity(entity));
text_contents->SetInheritedOpacity(0.5);
ASSERT_EQ(text_contents->GetColor().alpha, 0.25);
text_contents->SetInheritedOpacity(0.5);
ASSERT_EQ(text_contents->GetColor().alpha, 0.25);
// Clips and restores trivially accept opacity.
ASSERT_TRUE(ClipContents().CanAcceptOpacity(entity));
ASSERT_TRUE(ClipRestoreContents().CanAcceptOpacity(entity));
ASSERT_TRUE(ClipContents().CanInheritOpacity(entity));
ASSERT_TRUE(ClipRestoreContents().CanInheritOpacity(entity));
// Runtime effect contents can't accept opacity.
auto runtime_effect = std::make_shared<RuntimeEffectContents>();
ASSERT_FALSE(runtime_effect->CanAcceptOpacity(entity));
ASSERT_FALSE(runtime_effect->CanInheritOpacity(entity));
}
} // namespace testing