Skip to content

Commit

Permalink
Make the filter_ in mutator shared_ptr (flutter#34944)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Yang authored Jul 27, 2022
1 parent ca4d66a commit bbd1997
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion flow/embedded_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ void MutatorsStack::PushOpacity(const int& alpha) {
vector_.push_back(element);
};

void MutatorsStack::PushBackdropFilter(const DlImageFilter& filter) {
void MutatorsStack::PushBackdropFilter(
std::shared_ptr<const DlImageFilter> filter) {
std::shared_ptr<Mutator> element = std::make_shared<Mutator>(filter);
vector_.push_back(element);
};
Expand Down
11 changes: 7 additions & 4 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class Mutator {
explicit Mutator(const SkMatrix& matrix)
: type_(kTransform), matrix_(matrix) {}
explicit Mutator(const int& alpha) : type_(kOpacity), alpha_(alpha) {}
explicit Mutator(const DlImageFilter& filter)
: type_(kBackdropFilter), filter_(&filter) {}
explicit Mutator(std::shared_ptr<const DlImageFilter> filter)
: type_(kBackdropFilter), filter_(filter) {}

const MutatorType& GetType() const { return type_; }
const SkRect& GetRect() const { return rect_; }
Expand Down Expand Up @@ -119,15 +119,18 @@ class Mutator {
private:
MutatorType type_;

// TODO(cyanglaz): Remove union.
// https://github.com/flutter/flutter/issues/108470
union {
SkRect rect_;
SkRRect rrect_;
SkMatrix matrix_;
SkPath* path_;
int alpha_;
const DlImageFilter* filter_;
};

std::shared_ptr<const DlImageFilter> filter_;

}; // Mutator

// A stack of mutators that can be applied to an embedded platform view.
Expand All @@ -148,7 +151,7 @@ class MutatorsStack {
void PushClipPath(const SkPath& path);
void PushTransform(const SkMatrix& matrix);
void PushOpacity(const int& alpha);
void PushBackdropFilter(const DlImageFilter& filter);
void PushBackdropFilter(std::shared_ptr<const DlImageFilter> filter);

// Removes the `Mutator` on the top of the stack
// and destroys it.
Expand Down
20 changes: 15 additions & 5 deletions flow/mutators_stack_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,21 @@ TEST(MutatorsStack, PushOpacity) {

TEST(MutatorsStack, PushBackdropFilter) {
MutatorsStack stack;
auto filter = DlBlurImageFilter(5, 5, DlTileMode::kClamp);
stack.PushBackdropFilter(filter);
auto iter = stack.Bottom();
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kBackdropFilter);
ASSERT_TRUE(iter->get()->GetFilter() == filter);
const int num_of_mutators = 10;
for (int i = 0; i < num_of_mutators; i++) {
auto filter = std::make_shared<DlBlurImageFilter>(i, 5, DlTileMode::kClamp);
stack.PushBackdropFilter(filter);
}

auto iter = stack.Begin();
int i = 0;
while (iter != stack.End()) {
ASSERT_EQ(iter->get()->GetType(), MutatorType::kBackdropFilter);
ASSERT_EQ(iter->get()->GetFilter().asBlur()->sigma_x(), i);
++iter;
++i;
}
ASSERT_EQ(i, num_of_mutators);
}

TEST(MutatorsStack, Pop) {
Expand Down

0 comments on commit bbd1997

Please sign in to comment.