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

Commit 4757a7c

Browse files
committed
Fix opacity peephole bugs
1 parent fc8b10a commit 4757a7c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

impeller/aiks/paint_pass_delegate.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "impeller/entity/contents/contents.h"
88
#include "impeller/entity/contents/texture_contents.h"
99
#include "impeller/entity/entity_pass.h"
10+
#include "impeller/geometry/color.h"
1011
#include "impeller/geometry/path_builder.h"
1112

1213
namespace impeller {
@@ -72,7 +73,8 @@ bool OpacityPeepholePassDelegate::CanElide() {
7273
// |EntityPassDelgate|
7374
bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass(
7475
EntityPass* entity_pass) {
75-
if (paint_.color.alpha <= 0.0 || paint_.color.alpha >= 1.0 ||
76+
if (paint_.blend_mode != BlendMode::kSourceOver ||
77+
paint_.color.alpha <= 0.0 || paint_.color.alpha >= 1.0 ||
7678
paint_.image_filter.has_value() || paint_.color_filter.has_value()) {
7779
return false;
7880
}
@@ -97,7 +99,7 @@ bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass(
9799
auto had_subpass = entity_pass->IterateUntilSubpass(
98100
[&all_coverages, &all_can_accept](Entity& entity) {
99101
auto contents = entity.GetContents();
100-
if (!contents->CanInheritOpacity(entity)) {
102+
if (!entity.CanInheritOpacity()) {
101103
all_can_accept = false;
102104
return false;
103105
}
@@ -119,7 +121,7 @@ bool OpacityPeepholePassDelegate::CanCollapseIntoParentPass(
119121
}
120122
auto alpha = paint_.color.alpha;
121123
entity_pass->IterateUntilSubpass([&alpha](Entity& entity) {
122-
entity.GetContents()->SetInheritedOpacity(alpha);
124+
entity.SetInheritedOpacity(alpha);
123125
return true;
124126
});
125127
return true;

impeller/entity/entity.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "impeller/entity/contents/filters/filter_contents.h"
1313
#include "impeller/entity/contents/texture_contents.h"
1414
#include "impeller/entity/entity_pass.h"
15+
#include "impeller/geometry/color.h"
1516
#include "impeller/geometry/vector.h"
1617
#include "impeller/renderer/render_pass.h"
1718

@@ -101,6 +102,28 @@ BlendMode Entity::GetBlendMode() const {
101102
return blend_mode_;
102103
}
103104

105+
bool Entity::CanInheritOpacity() const {
106+
if (!contents_) {
107+
return false;
108+
}
109+
if (!((blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) ||
110+
blend_mode_ == BlendMode::kSourceOver)) {
111+
return false;
112+
}
113+
return contents_->CanInheritOpacity(*this);
114+
}
115+
116+
bool Entity::SetInheritedOpacity(Scalar alpha) {
117+
if (!CanInheritOpacity()) {
118+
return false;
119+
}
120+
if (blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) {
121+
blend_mode_ = BlendMode::kSourceOver;
122+
}
123+
contents_->SetInheritedOpacity(alpha);
124+
return true;
125+
}
126+
104127
/// @brief Returns true if the blend mode is "destrictive", meaning that even
105128
/// fully transparent source colors would result in the destination
106129
/// getting changed.

impeller/entity/entity.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class Entity {
8888

8989
static bool IsBlendModeDestructive(BlendMode blend_mode);
9090

91+
bool CanInheritOpacity() const;
92+
93+
bool SetInheritedOpacity(Scalar alpha);
94+
9195
private:
9296
Matrix transformation_;
9397
std::shared_ptr<Contents> contents_;

0 commit comments

Comments
 (0)