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

Commit 495ed03

Browse files
authored
Create blanket backdrop filter mutator (#34408)
1 parent 9fb5d8c commit 495ed03

8 files changed

Lines changed: 97 additions & 62 deletions

File tree

flow/embedded_views.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ void MutatorsStack::PushOpacity(const int& alpha) {
3636
vector_.push_back(element);
3737
};
3838

39+
void MutatorsStack::PushBackdropFilter(const DlImageFilter& filter) {
40+
std::shared_ptr<Mutator> element = std::make_shared<Mutator>(filter);
41+
vector_.push_back(element);
42+
};
43+
3944
void MutatorsStack::Pop() {
4045
vector_.pop_back();
4146
};

flow/embedded_views.h

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,65 @@
2020

2121
namespace flutter {
2222

23-
// TODO(chinmaygarde): Make these enum names match the style guide.
24-
enum MutatorType { clip_rect, clip_rrect, clip_path, transform, opacity };
23+
enum MutatorType {
24+
kClipRect,
25+
kClipRRect,
26+
kClipPath,
27+
kTransform,
28+
kOpacity,
29+
kBackdropFilter
30+
};
2531

26-
// Stores mutation information like clipping or transform.
32+
// Stores mutation information like clipping or kTransform.
2733
//
28-
// The `type` indicates the type of the mutation: clip_rect, transform and etc.
34+
// The `type` indicates the type of the mutation: kClipRect, kTransform and etc.
2935
// Each `type` is paired with an object that supports the mutation. For example,
30-
// if the `type` is clip_rect, `rect()` is used the represent the rect to be
36+
// if the `type` is kClipRect, `rect()` is used the represent the rect to be
3137
// clipped. One mutation object must only contain one type of mutation.
3238
class Mutator {
3339
public:
3440
Mutator(const Mutator& other) {
3541
type_ = other.type_;
3642
switch (other.type_) {
37-
case clip_rect:
43+
case kClipRect:
3844
rect_ = other.rect_;
3945
break;
40-
case clip_rrect:
46+
case kClipRRect:
4147
rrect_ = other.rrect_;
4248
break;
43-
case clip_path:
49+
case kClipPath:
4450
path_ = new SkPath(*other.path_);
4551
break;
46-
case transform:
52+
case kTransform:
4753
matrix_ = other.matrix_;
4854
break;
49-
case opacity:
55+
case kOpacity:
5056
alpha_ = other.alpha_;
5157
break;
58+
case kBackdropFilter:
59+
filter_ = other.filter_;
60+
break;
5261
default:
5362
break;
5463
}
5564
}
5665

57-
explicit Mutator(const SkRect& rect) : type_(clip_rect), rect_(rect) {}
58-
explicit Mutator(const SkRRect& rrect) : type_(clip_rrect), rrect_(rrect) {}
66+
explicit Mutator(const SkRect& rect) : type_(kClipRect), rect_(rect) {}
67+
explicit Mutator(const SkRRect& rrect) : type_(kClipRRect), rrect_(rrect) {}
5968
explicit Mutator(const SkPath& path)
60-
: type_(clip_path), path_(new SkPath(path)) {}
69+
: type_(kClipPath), path_(new SkPath(path)) {}
6170
explicit Mutator(const SkMatrix& matrix)
62-
: type_(transform), matrix_(matrix) {}
63-
explicit Mutator(const int& alpha) : type_(opacity), alpha_(alpha) {}
71+
: type_(kTransform), matrix_(matrix) {}
72+
explicit Mutator(const int& alpha) : type_(kOpacity), alpha_(alpha) {}
73+
explicit Mutator(const DlImageFilter& filter)
74+
: type_(kBackdropFilter), filter_(&filter) {}
6475

6576
const MutatorType& GetType() const { return type_; }
6677
const SkRect& GetRect() const { return rect_; }
6778
const SkRRect& GetRRect() const { return rrect_; }
6879
const SkPath& GetPath() const { return *path_; }
6980
const SkMatrix& GetMatrix() const { return matrix_; }
81+
const DlImageFilter& GetFilter() const { return *filter_; }
7082
const int& GetAlpha() const { return alpha_; }
7183
float GetAlphaFloat() const { return (alpha_ / 255.0); }
7284

@@ -75,16 +87,18 @@ class Mutator {
7587
return false;
7688
}
7789
switch (type_) {
78-
case clip_rect:
90+
case kClipRect:
7991
return rect_ == other.rect_;
80-
case clip_rrect:
92+
case kClipRRect:
8193
return rrect_ == other.rrect_;
82-
case clip_path:
94+
case kClipPath:
8395
return *path_ == *other.path_;
84-
case transform:
96+
case kTransform:
8597
return matrix_ == other.matrix_;
86-
case opacity:
98+
case kOpacity:
8799
return alpha_ == other.alpha_;
100+
case kBackdropFilter:
101+
return *filter_ == *other.filter_;
88102
}
89103

90104
return false;
@@ -93,11 +107,11 @@ class Mutator {
93107
bool operator!=(const Mutator& other) const { return !operator==(other); }
94108

95109
bool IsClipType() {
96-
return type_ == clip_rect || type_ == clip_rrect || type_ == clip_path;
110+
return type_ == kClipRect || type_ == kClipRRect || type_ == kClipPath;
97111
}
98112

99113
~Mutator() {
100-
if (type_ == clip_path) {
114+
if (type_ == kClipPath) {
101115
delete path_;
102116
}
103117
};
@@ -111,6 +125,7 @@ class Mutator {
111125
SkMatrix matrix_;
112126
SkPath* path_;
113127
int alpha_;
128+
const DlImageFilter* filter_;
114129
};
115130

116131
}; // Mutator
@@ -133,6 +148,7 @@ class MutatorsStack {
133148
void PushClipPath(const SkPath& path);
134149
void PushTransform(const SkMatrix& matrix);
135150
void PushOpacity(const int& alpha);
151+
void PushBackdropFilter(const DlImageFilter& filter);
136152

137153
// Removes the `Mutator` on the top of the stack
138154
// and destroys it.

flow/mutators_stack_unittests.cc

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ TEST(MutatorsStack, CopyAndUpdateTheCopy) {
3636
ASSERT_TRUE(copy.is_empty());
3737
ASSERT_TRUE(!stack.is_empty());
3838
auto iter = stack.Bottom();
39-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rrect);
39+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRRect);
4040
ASSERT_TRUE(iter->get()->GetRRect() == rrect);
4141
++iter;
42-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rect);
42+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRect);
4343
ASSERT_TRUE(iter->get()->GetRect() == rect);
4444
}
4545

@@ -48,7 +48,7 @@ TEST(MutatorsStack, PushClipRect) {
4848
auto rect = SkRect::MakeEmpty();
4949
stack.PushClipRect(rect);
5050
auto iter = stack.Bottom();
51-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rect);
51+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRect);
5252
ASSERT_TRUE(iter->get()->GetRect() == rect);
5353
}
5454

@@ -57,7 +57,7 @@ TEST(MutatorsStack, PushClipRRect) {
5757
auto rrect = SkRRect::MakeEmpty();
5858
stack.PushClipRRect(rrect);
5959
auto iter = stack.Bottom();
60-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rrect);
60+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRRect);
6161
ASSERT_TRUE(iter->get()->GetRRect() == rrect);
6262
}
6363

@@ -66,7 +66,7 @@ TEST(MutatorsStack, PushClipPath) {
6666
SkPath path;
6767
stack.PushClipPath(path);
6868
auto iter = stack.Bottom();
69-
ASSERT_TRUE(iter->get()->GetType() == flutter::MutatorType::clip_path);
69+
ASSERT_TRUE(iter->get()->GetType() == flutter::MutatorType::kClipPath);
7070
ASSERT_TRUE(iter->get()->GetPath() == path);
7171
}
7272

@@ -76,7 +76,7 @@ TEST(MutatorsStack, PushTransform) {
7676
matrix.setIdentity();
7777
stack.PushTransform(matrix);
7878
auto iter = stack.Bottom();
79-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::transform);
79+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kTransform);
8080
ASSERT_TRUE(iter->get()->GetMatrix() == matrix);
8181
}
8282

@@ -85,10 +85,19 @@ TEST(MutatorsStack, PushOpacity) {
8585
int alpha = 240;
8686
stack.PushOpacity(alpha);
8787
auto iter = stack.Bottom();
88-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::opacity);
88+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kOpacity);
8989
ASSERT_TRUE(iter->get()->GetAlpha() == 240);
9090
}
9191

92+
TEST(MutatorsStack, PushBackdropFilter) {
93+
MutatorsStack stack;
94+
auto filter = DlBlurImageFilter(5, 5, DlTileMode::kClamp);
95+
stack.PushBackdropFilter(filter);
96+
auto iter = stack.Bottom();
97+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kBackdropFilter);
98+
ASSERT_TRUE(iter->get()->GetFilter() == filter);
99+
}
100+
92101
TEST(MutatorsStack, Pop) {
93102
MutatorsStack stack;
94103
SkMatrix matrix;
@@ -113,15 +122,15 @@ TEST(MutatorsStack, Traversal) {
113122
while (iter != stack.Top()) {
114123
switch (index) {
115124
case 0:
116-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rrect);
125+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRRect);
117126
ASSERT_TRUE(iter->get()->GetRRect() == rrect);
118127
break;
119128
case 1:
120-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::clip_rect);
129+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kClipRect);
121130
ASSERT_TRUE(iter->get()->GetRect() == rect);
122131
break;
123132
case 2:
124-
ASSERT_TRUE(iter->get()->GetType() == MutatorType::transform);
133+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::kTransform);
125134
ASSERT_TRUE(iter->get()->GetMatrix() == matrix);
126135
break;
127136
default:
@@ -163,28 +172,28 @@ TEST(MutatorsStack, Equality) {
163172
TEST(Mutator, Initialization) {
164173
SkRect rect = SkRect::MakeEmpty();
165174
Mutator mutator = Mutator(rect);
166-
ASSERT_TRUE(mutator.GetType() == MutatorType::clip_rect);
175+
ASSERT_TRUE(mutator.GetType() == MutatorType::kClipRect);
167176
ASSERT_TRUE(mutator.GetRect() == rect);
168177

169178
SkRRect rrect = SkRRect::MakeEmpty();
170179
Mutator mutator2 = Mutator(rrect);
171-
ASSERT_TRUE(mutator2.GetType() == MutatorType::clip_rrect);
180+
ASSERT_TRUE(mutator2.GetType() == MutatorType::kClipRRect);
172181
ASSERT_TRUE(mutator2.GetRRect() == rrect);
173182

174183
SkPath path;
175184
Mutator mutator3 = Mutator(path);
176-
ASSERT_TRUE(mutator3.GetType() == MutatorType::clip_path);
185+
ASSERT_TRUE(mutator3.GetType() == MutatorType::kClipPath);
177186
ASSERT_TRUE(mutator3.GetPath() == path);
178187

179188
SkMatrix matrix;
180189
matrix.setIdentity();
181190
Mutator mutator4 = Mutator(matrix);
182-
ASSERT_TRUE(mutator4.GetType() == MutatorType::transform);
191+
ASSERT_TRUE(mutator4.GetType() == MutatorType::kTransform);
183192
ASSERT_TRUE(mutator4.GetMatrix() == matrix);
184193

185194
int alpha = 240;
186195
Mutator mutator5 = Mutator(alpha);
187-
ASSERT_TRUE(mutator5.GetType() == MutatorType::opacity);
196+
ASSERT_TRUE(mutator5.GetType() == MutatorType::kOpacity);
188197
}
189198

190199
TEST(Mutator, CopyConstructor) {

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(
14091409
mutators_stack.Begin();
14101410
while (iter != mutators_stack.End()) {
14111411
switch ((*iter)->GetType()) {
1412-
case transform: {
1412+
case kTransform: {
14131413
const SkMatrix& matrix = (*iter)->GetMatrix();
14141414
SkScalar matrix_array[9];
14151415
matrix.get9(matrix_array);
@@ -1422,15 +1422,15 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(
14221422
transformMatrix.obj());
14231423
break;
14241424
}
1425-
case clip_rect: {
1425+
case kClipRect: {
14261426
const SkRect& rect = (*iter)->GetRect();
14271427
env->CallVoidMethod(
14281428
mutatorsStack, g_mutators_stack_push_cliprect_method,
14291429
static_cast<int>(rect.left()), static_cast<int>(rect.top()),
14301430
static_cast<int>(rect.right()), static_cast<int>(rect.bottom()));
14311431
break;
14321432
}
1433-
case clip_rrect: {
1433+
case kClipRRect: {
14341434
const SkRRect& rrect = (*iter)->GetRRect();
14351435
const SkRect& rect = rrect.rect();
14361436
const SkVector& upper_left = rrect.radii(SkRRect::kUpperLeft_Corner);
@@ -1452,8 +1452,9 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(
14521452
}
14531453
// TODO(cyanglaz): Implement other mutators.
14541454
// https://github.com/flutter/flutter/issues/58426
1455-
case clip_path:
1456-
case opacity:
1455+
case kClipPath:
1456+
case kOpacity:
1457+
case kBackdropFilter:
14571458
break;
14581459
}
14591460
++iter;

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,23 +405,25 @@ - (BOOL)flt_hasFirstResponderInViewHierarchySubtree {
405405
auto iter = mutators_stack.Begin();
406406
while (iter != mutators_stack.End()) {
407407
switch ((*iter)->GetType()) {
408-
case transform: {
408+
case kTransform: {
409409
CATransform3D transform = GetCATransform3DFromSkMatrix((*iter)->GetMatrix());
410410
finalTransform = CATransform3DConcat(transform, finalTransform);
411411
break;
412412
}
413-
case clip_rect:
413+
case kClipRect:
414414
[maskView clipRect:(*iter)->GetRect() matrix:finalTransform];
415415
break;
416-
case clip_rrect:
416+
case kClipRRect:
417417
[maskView clipRRect:(*iter)->GetRRect() matrix:finalTransform];
418418
break;
419-
case clip_path:
419+
case kClipPath:
420420
[maskView clipPath:(*iter)->GetPath() matrix:finalTransform];
421421
break;
422-
case opacity:
422+
case kOpacity:
423423
embedded_view.alpha = (*iter)->GetAlphaFloat() * embedded_view.alpha;
424424
break;
425+
case kBackdropFilter:
426+
break;
425427
}
426428
++iter;
427429
}

shell/platform/embedder/embedder_layers.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,30 +115,30 @@ void EmbedderLayers::PushPlatformViewLayer(
115115
for (auto i = mutators.Bottom(); i != mutators.Top(); ++i) {
116116
const auto& mutator = *i;
117117
switch (mutator->GetType()) {
118-
case MutatorType::clip_rect: {
118+
case MutatorType::kClipRect: {
119119
mutations_array.push_back(
120120
mutations_referenced_
121121
.emplace_back(ConvertMutation(mutator->GetRect()))
122122
.get());
123123
} break;
124-
case MutatorType::clip_rrect: {
124+
case MutatorType::kClipRRect: {
125125
mutations_array.push_back(
126126
mutations_referenced_
127127
.emplace_back(ConvertMutation(mutator->GetRRect()))
128128
.get());
129129
} break;
130-
case MutatorType::clip_path: {
130+
case MutatorType::kClipPath: {
131131
// Unsupported mutation.
132132
} break;
133-
case MutatorType::transform: {
133+
case MutatorType::kTransform: {
134134
const auto& matrix = mutator->GetMatrix();
135135
if (!matrix.isIdentity()) {
136136
mutations_array.push_back(
137137
mutations_referenced_.emplace_back(ConvertMutation(matrix))
138138
.get());
139139
}
140140
} break;
141-
case MutatorType::opacity: {
141+
case MutatorType::kOpacity: {
142142
const double opacity =
143143
std::clamp(mutator->GetAlphaFloat(), 0.0f, 1.0f);
144144
if (opacity < 1.0) {
@@ -147,6 +147,8 @@ void EmbedderLayers::PushPlatformViewLayer(
147147
.get());
148148
}
149149
} break;
150+
case MutatorType::kBackdropFilter:
151+
break;
150152
}
151153
}
152154

0 commit comments

Comments
 (0)