Skip to content

Commit

Permalink
iOS platform view opacity (flutter#9667)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Yang authored Jul 8, 2019
1 parent 3b6265b commit 802bd15
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions flow/embedded_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void MutatorsStack::PushTransform(const SkMatrix& matrix) {
vector_.push_back(element);
};

void MutatorsStack::PushOpacity(const int& alpha) {
std::shared_ptr<Mutator> element = std::make_shared<Mutator>(alpha);
vector_.push_back(element);
};

void MutatorsStack::Pop() {
vector_.pop_back();
};
Expand Down
12 changes: 11 additions & 1 deletion flow/embedded_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace flutter {

enum MutatorType { clip_rect, clip_rrect, clip_path, transform };
enum MutatorType { clip_rect, clip_rrect, clip_path, transform, opacity };

// Stores mutation information like clipping or transform.
//
Expand All @@ -42,6 +42,9 @@ class Mutator {
case transform:
matrix_ = other.matrix_;
break;
case opacity:
alpha_ = other.alpha_;
break;
default:
break;
}
Expand All @@ -53,12 +56,15 @@ class Mutator {
: type_(clip_path), path_(new SkPath(path)) {}
explicit Mutator(const SkMatrix& matrix)
: type_(transform), matrix_(matrix) {}
explicit Mutator(const int& alpha) : type_(opacity), alpha_(alpha) {}

const MutatorType& GetType() const { return type_; }
const SkRect& GetRect() const { return rect_; }
const SkRRect& GetRRect() const { return rrect_; }
const SkPath& GetPath() const { return *path_; }
const SkMatrix& GetMatrix() const { return matrix_; }
const int& GetAlpha() const { return alpha_; }
float GetAlphaFloat() const { return (alpha_ / 255.0); }

bool operator==(const Mutator& other) const {
if (type_ != other.type_) {
Expand All @@ -73,6 +79,8 @@ class Mutator {
return *path_ == *other.path_;
case transform:
return matrix_ == other.matrix_;
case opacity:
return alpha_ == other.alpha_;
}

return false;
Expand All @@ -98,6 +106,7 @@ class Mutator {
SkRRect rrect_;
SkMatrix matrix_;
SkPath* path_;
int alpha_;
};

}; // Mutator
Expand All @@ -119,6 +128,7 @@ class MutatorsStack {
void PushClipRRect(const SkRRect& rrect);
void PushClipPath(const SkPath& path);
void PushTransform(const SkMatrix& matrix);
void PushOpacity(const int& alpha);

// Removes the `Mutator` on the top of the stack
// and destroys it.
Expand Down
5 changes: 5 additions & 0 deletions flow/layers/opacity_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
EnsureSingleChild();
SkMatrix child_matrix = matrix;
child_matrix.postTranslate(offset_.fX, offset_.fY);
context->mutators_stack.PushTransform(
SkMatrix::MakeTrans(offset_.fX, offset_.fY));
context->mutators_stack.PushOpacity(alpha_);
ContainerLayer::Preroll(context, child_matrix);
context->mutators_stack.Pop();
context->mutators_stack.Pop();
set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY));
// See |EnsureSingleChild|.
FML_DCHECK(layers().size() == 1);
Expand Down
34 changes: 33 additions & 1 deletion flow/mutators_stack_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST(MutatorsStack, PushClipRRect) {
}

TEST(MutatorsStack, PushClipPath) {
flutter::MutatorsStack stack;
MutatorsStack stack;
SkPath path;
stack.PushClipPath(path);
auto iter = stack.Bottom();
Expand All @@ -60,6 +60,15 @@ TEST(MutatorsStack, PushTransform) {
ASSERT_TRUE(iter->get()->GetMatrix() == matrix);
}

TEST(MutatorsStack, PushOpacity) {
MutatorsStack stack;
int alpha = 240;
stack.PushOpacity(alpha);
auto iter = stack.Bottom();
ASSERT_TRUE(iter->get()->GetType() == MutatorType::opacity);
ASSERT_TRUE(iter->get()->GetAlpha() == 240);
}

TEST(MutatorsStack, Pop) {
MutatorsStack stack;
SkMatrix matrix;
Expand Down Expand Up @@ -113,6 +122,8 @@ TEST(MutatorsStack, Equality) {
stack.PushClipRRect(rrect);
SkPath path;
stack.PushClipPath(path);
int alpha = 240;
stack.PushOpacity(alpha);

MutatorsStack stackOther;
SkMatrix matrixOther = SkMatrix::MakeScale(1, 1);
Expand All @@ -123,6 +134,8 @@ TEST(MutatorsStack, Equality) {
stackOther.PushClipRRect(rrectOther);
SkPath otherPath;
stackOther.PushClipPath(otherPath);
int otherAlpha = 240;
stackOther.PushOpacity(otherAlpha);

ASSERT_TRUE(stack == stackOther);
}
Expand All @@ -148,6 +161,10 @@ TEST(Mutator, Initialization) {
Mutator mutator4 = Mutator(matrix);
ASSERT_TRUE(mutator4.GetType() == MutatorType::transform);
ASSERT_TRUE(mutator4.GetMatrix() == matrix);

int alpha = 240;
Mutator mutator5 = Mutator(alpha);
ASSERT_TRUE(mutator5.GetType() == MutatorType::opacity);
}

TEST(Mutator, CopyConstructor) {
Expand All @@ -171,6 +188,11 @@ TEST(Mutator, CopyConstructor) {
Mutator mutator4 = Mutator(matrix);
Mutator copy4 = Mutator(mutator4);
ASSERT_TRUE(mutator4 == copy4);

int alpha = 240;
Mutator mutator5 = Mutator(alpha);
Mutator copy5 = Mutator(mutator5);
ASSERT_TRUE(mutator5 == copy5);
}

TEST(Mutator, Equality) {
Expand All @@ -195,6 +217,10 @@ TEST(Mutator, Equality) {
flutter::Mutator otherMutator4 = flutter::Mutator(path);
ASSERT_TRUE(mutator4 == otherMutator4);
ASSERT_FALSE(mutator2 == mutator);
int alpha = 240;
Mutator mutator5 = Mutator(alpha);
Mutator otherMutator5 = Mutator(alpha);
ASSERT_TRUE(mutator5 == otherMutator5);
}

TEST(Mutator, UnEquality) {
Expand All @@ -204,6 +230,12 @@ TEST(Mutator, UnEquality) {
matrix.setIdentity();
Mutator notEqualMutator = Mutator(matrix);
ASSERT_TRUE(notEqualMutator != mutator);

int alpha = 240;
int alpha2 = 241;
Mutator mutator2 = Mutator(alpha);
Mutator otherMutator2 = Mutator(alpha2);
ASSERT_TRUE(mutator2 != otherMutator2);
}

} // namespace testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@
head = clipView;
break;
}
case opacity:
embedded_view.alpha = (*iter)->GetAlphaFloat() * embedded_view.alpha;
break;
}
++iter;
}
Expand All @@ -299,6 +302,7 @@
UIView* touchInterceptor = touch_interceptors_[view_id].get();
touchInterceptor.layer.transform = CATransform3DIdentity;
touchInterceptor.frame = frame;
touchInterceptor.alpha = 1;

int currentClippingCount = CountClips(params.mutatorsStack);
int previousClippingCount = clip_count_[view_id];
Expand Down

0 comments on commit 802bd15

Please sign in to comment.