diff --git a/engine/src/flutter/display_list/display_list_builder.cc b/engine/src/flutter/display_list/display_list_builder.cc index 1d0574dd8c..fb95d205ac 100644 --- a/engine/src/flutter/display_list/display_list_builder.cc +++ b/engine/src/flutter/display_list/display_list_builder.cc @@ -429,6 +429,12 @@ void DisplayListBuilder::restore() { } } } +void DisplayListBuilder::restoreToCount(int restore_count) { + FML_DCHECK(restore_count <= getSaveCount()); + while (restore_count < getSaveCount()) { + restore(); + } +} void DisplayListBuilder::saveLayer(const SkRect* bounds, const SaveLayerOptions in_options) { SaveLayerOptions options = in_options.without_optimizations(); diff --git a/engine/src/flutter/display_list/display_list_builder.h b/engine/src/flutter/display_list/display_list_builder.h index 80ac26ee72..15992c0e9a 100644 --- a/engine/src/flutter/display_list/display_list_builder.h +++ b/engine/src/flutter/display_list/display_list_builder.h @@ -162,6 +162,7 @@ class DisplayListBuilder final : public virtual Dispatcher, void saveLayer(const SkRect* bounds, const DlPaint* paint); void restore() override; int getSaveCount() { return layer_stack_.size(); } + void restoreToCount(int restore_count); void translate(SkScalar tx, SkScalar ty) override; void scale(SkScalar sx, SkScalar sy) override; diff --git a/engine/src/flutter/flow/layers/clip_path_layer.cc b/engine/src/flutter/flow/layers/clip_path_layer.cc index 5a4ad30441..b04f011ad7 100644 --- a/engine/src/flutter/flow/layers/clip_path_layer.cc +++ b/engine/src/flutter/flow/layers/clip_path_layer.cc @@ -10,7 +10,6 @@ namespace flutter { ClipPathLayer::ClipPathLayer(const SkPath& clip_path, Clip clip_behavior) : clip_path_(clip_path), clip_behavior_(clip_behavior) { FML_DCHECK(clip_behavior != Clip::none); - set_layer_can_inherit_opacity(true); } void ClipPathLayer::Diff(DiffContext* context, const Layer* old_layer) { @@ -41,12 +40,22 @@ void ClipPathLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { Layer::AutoPrerollSaveLayerState::Create(context, UsesSaveLayer()); context->mutators_stack.PushClipPath(clip_path_); + // Collect inheritance information on our children in Preroll so that + // we can pass it along by default. + context->subtree_can_inherit_opacity = true; + SkRect child_paint_bounds = SkRect::MakeEmpty(); PrerollChildren(context, matrix, &child_paint_bounds); if (child_paint_bounds.intersect(clip_path_bounds)) { set_paint_bounds(child_paint_bounds); } + // If we use a SaveLayer then we can accept opacity on behalf + // of our children and apply it in the saveLayer. + if (UsesSaveLayer()) { + context->subtree_can_inherit_opacity = true; + } + context->mutators_stack.Pop(); context->cull_rect = previous_cull_rect; } diff --git a/engine/src/flutter/flow/layers/clip_path_layer_unittests.cc b/engine/src/flutter/flow/layers/clip_path_layer_unittests.cc index 90ecbc3d69..b71403707a 100644 --- a/engine/src/flutter/flow/layers/clip_path_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/clip_path_layer_unittests.cc @@ -4,6 +4,7 @@ #include "flutter/flow/layers/clip_path_layer.h" +#include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" #include "flutter/fml/macros.h" @@ -262,5 +263,236 @@ TEST_F(ClipPathLayerTest, Readback) { EXPECT_TRUE(ReadbackResult(context, save_layer, reader, true)); } +TEST_F(ClipPathLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto layer_clip = SkPath() + .addRect(SkRect::MakeLTRB(5, 5, 25, 25)) + .addOval(SkRect::MakeLTRB(20, 20, 40, 50)); + auto clip_path_layer = + std::make_shared(layer_clip, Clip::hardEdge); + clip_path_layer->Add(mock1); + + // ClipRectLayer will pass through compatibility from a compatible child + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_path_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + clip_path_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + context->subtree_can_inherit_opacity = false; + clip_path_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path3 = SkPath().addRect({20, 20, 40, 40}); + auto mock3 = MockLayer::MakeOpacityCompatible(path3); + clip_path_layer->Add(mock3); + + // ClipRectLayer will not pass through compatibility from multiple + // overlapping children even if they are individually compatible + context->subtree_can_inherit_opacity = false; + clip_path_layer->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_path_saveLayer = std::make_shared( + layer_clip, Clip::antiAliasWithSaveLayer); + clip_path_saveLayer->Add(mock1); + clip_path_saveLayer->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_path_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the overlapping child and test again, should still be compatible + clip_path_saveLayer->Add(mock3); + context->subtree_can_inherit_opacity = false; + clip_path_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } + + // An incompatible, but non-overlapping child for the following tests + auto path4 = SkPath().addRect({60, 60, 70, 70}); + auto mock4 = MockLayer::Make(path4); + + { + // ClipRectLayer with incompatible child will not be compatible + auto clip_path_bad_child = + std::make_shared(layer_clip, Clip::hardEdge); + clip_path_bad_child->Add(mock1); + clip_path_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_path_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + clip_path_bad_child->Add(mock4); + + // The third child is non-overlapping, but not compatible so the + // TransformLayer should end up incompatible + context->subtree_can_inherit_opacity = false; + clip_path_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + } + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_path_saveLayer_bad_child = std::make_shared( + layer_clip, Clip::antiAliasWithSaveLayer); + clip_path_saveLayer_bad_child->Add(mock1); + clip_path_saveLayer_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_path_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the incompatible child and test again, should still be compatible + clip_path_saveLayer_bad_child->Add(mock4); + context->subtree_can_inherit_opacity = false; + clip_path_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } +} + +TEST_F(ClipPathLayerTest, OpacityInheritancePainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + auto layer_clip = SkPath() + .addRect(SkRect::MakeLTRB(5, 5, 25, 25)) + .addOval(SkRect::MakeLTRB(20, 20, 40, 50)); + auto clip_path_layer = + std::make_shared(layer_clip, Clip::antiAlias); + clip_path_layer->Add(mock1); + clip_path_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_path_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_path_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipPath(layer_clip, SkClipOp::kIntersect, true); + /* child layer1 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path1.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + expected_builder.restore(); + } + /* child layer2 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path2.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + +TEST_F(ClipPathLayerTest, OpacityInheritanceSaveLayerPainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({20, 20, 40, 40}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + auto children_bounds = path1.getBounds(); + children_bounds.join(path2.getBounds()); + auto layer_clip = SkPath() + .addRect(SkRect::MakeLTRB(5, 5, 25, 25)) + .addOval(SkRect::MakeLTRB(20, 20, 40, 50)); + auto clip_path_layer = + std::make_shared(layer_clip, Clip::antiAliasWithSaveLayer); + clip_path_layer->Add(mock1); + clip_path_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_path_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_path_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipPath(layer_clip, SkClipOp::kIntersect, true); + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&children_bounds, true); + /* child layer1 paint */ { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + /* child layer2 paint */ { // + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/clip_rect_layer.cc b/engine/src/flutter/flow/layers/clip_rect_layer.cc index cdb5531283..d7697680f8 100644 --- a/engine/src/flutter/flow/layers/clip_rect_layer.cc +++ b/engine/src/flutter/flow/layers/clip_rect_layer.cc @@ -10,7 +10,6 @@ namespace flutter { ClipRectLayer::ClipRectLayer(const SkRect& clip_rect, Clip clip_behavior) : clip_rect_(clip_rect), clip_behavior_(clip_behavior) { FML_DCHECK(clip_behavior != Clip::none); - set_layer_can_inherit_opacity(true); } void ClipRectLayer::Diff(DiffContext* context, const Layer* old_layer) { @@ -40,12 +39,22 @@ void ClipRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { Layer::AutoPrerollSaveLayerState::Create(context, UsesSaveLayer()); context->mutators_stack.PushClipRect(clip_rect_); + // Collect inheritance information on our children in Preroll so that + // we can pass it along by default. + context->subtree_can_inherit_opacity = true; + SkRect child_paint_bounds = SkRect::MakeEmpty(); PrerollChildren(context, matrix, &child_paint_bounds); if (child_paint_bounds.intersect(clip_rect_)) { set_paint_bounds(child_paint_bounds); } + // If we use a SaveLayer then we can accept opacity on behalf + // of our children and apply it in the saveLayer. + if (UsesSaveLayer()) { + context->subtree_can_inherit_opacity = true; + } + context->mutators_stack.Pop(); context->cull_rect = previous_cull_rect; } diff --git a/engine/src/flutter/flow/layers/clip_rect_layer_unittests.cc b/engine/src/flutter/flow/layers/clip_rect_layer_unittests.cc index 098322f04a..bb8607a95b 100644 --- a/engine/src/flutter/flow/layers/clip_rect_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/clip_rect_layer_unittests.cc @@ -4,6 +4,7 @@ #include "flutter/flow/layers/clip_rect_layer.h" +#include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" #include "flutter/fml/macros.h" @@ -260,5 +261,228 @@ TEST_F(ClipRectLayerTest, Readback) { EXPECT_TRUE(ReadbackResult(context, save_layer, reader, true)); } +TEST_F(ClipRectLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + SkRect clip_rect = SkRect::MakeWH(500, 500); + auto clip_rect_layer = + std::make_shared(clip_rect, Clip::hardEdge); + clip_rect_layer->Add(mock1); + + // ClipRectLayer will pass through compatibility from a compatible child + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + clip_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path3 = SkPath().addRect({20, 20, 40, 40}); + auto mock3 = MockLayer::MakeOpacityCompatible(path3); + clip_rect_layer->Add(mock3); + + // ClipRectLayer will not pass through compatibility from multiple + // overlapping children even if they are individually compatible + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_rect_saveLayer = std::make_shared( + clip_rect, Clip::antiAliasWithSaveLayer); + clip_rect_saveLayer->Add(mock1); + clip_rect_saveLayer->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_rect_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the overlapping child and test again, should still be compatible + clip_rect_saveLayer->Add(mock3); + context->subtree_can_inherit_opacity = false; + clip_rect_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } + + // An incompatible, but non-overlapping child for the following tests + auto path4 = SkPath().addRect({60, 60, 70, 70}); + auto mock4 = MockLayer::Make(path4); + + { + // ClipRectLayer with incompatible child will not be compatible + auto clip_rect_bad_child = + std::make_shared(clip_rect, Clip::hardEdge); + clip_rect_bad_child->Add(mock1); + clip_rect_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_rect_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + clip_rect_bad_child->Add(mock4); + + // The third child is non-overlapping, but not compatible so the + // TransformLayer should end up incompatible + context->subtree_can_inherit_opacity = false; + clip_rect_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + } + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_rect_saveLayer_bad_child = std::make_shared( + clip_rect, Clip::antiAliasWithSaveLayer); + clip_rect_saveLayer_bad_child->Add(mock1); + clip_rect_saveLayer_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_rect_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the incompatible child and test again, should still be compatible + clip_rect_saveLayer_bad_child->Add(mock4); + context->subtree_can_inherit_opacity = false; + clip_rect_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } +} + +TEST_F(ClipRectLayerTest, OpacityInheritancePainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + SkRect clip_rect = SkRect::MakeWH(500, 500); + auto clip_rect_layer = + std::make_shared(clip_rect, Clip::antiAlias); + clip_rect_layer->Add(mock1); + clip_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_rect_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipRect(clip_rect, SkClipOp::kIntersect, true); + /* child layer1 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path1.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + expected_builder.restore(); + } + /* child layer2 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path2.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + +TEST_F(ClipRectLayerTest, OpacityInheritanceSaveLayerPainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({20, 20, 40, 40}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + SkRect clip_rect = SkRect::MakeWH(500, 500); + auto clip_rect_layer = + std::make_shared(clip_rect, Clip::antiAliasWithSaveLayer); + clip_rect_layer->Add(mock1); + clip_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_rect_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipRect(clip_rect, SkClipOp::kIntersect, true); + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&clip_rect, true); + /* child layer1 paint */ { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + /* child layer2 paint */ { // + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/clip_rrect_layer.cc b/engine/src/flutter/flow/layers/clip_rrect_layer.cc index 52377ae984..792acd33f5 100644 --- a/engine/src/flutter/flow/layers/clip_rrect_layer.cc +++ b/engine/src/flutter/flow/layers/clip_rrect_layer.cc @@ -10,7 +10,6 @@ namespace flutter { ClipRRectLayer::ClipRRectLayer(const SkRRect& clip_rrect, Clip clip_behavior) : clip_rrect_(clip_rrect), clip_behavior_(clip_behavior) { FML_DCHECK(clip_behavior != Clip::none); - set_layer_can_inherit_opacity(true); } void ClipRRectLayer::Diff(DiffContext* context, const Layer* old_layer) { @@ -41,12 +40,22 @@ void ClipRRectLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { Layer::AutoPrerollSaveLayerState::Create(context, UsesSaveLayer()); context->mutators_stack.PushClipRRect(clip_rrect_); + // Collect inheritance information on our children in Preroll so that + // we can pass it along by default. + context->subtree_can_inherit_opacity = true; + SkRect child_paint_bounds = SkRect::MakeEmpty(); PrerollChildren(context, matrix, &child_paint_bounds); if (child_paint_bounds.intersect(clip_rrect_bounds)) { set_paint_bounds(child_paint_bounds); } + // If we use a SaveLayer then we can accept opacity on behalf + // of our children and apply it in the saveLayer. + if (UsesSaveLayer()) { + context->subtree_can_inherit_opacity = true; + } + context->mutators_stack.Pop(); context->cull_rect = previous_cull_rect; } diff --git a/engine/src/flutter/flow/layers/clip_rrect_layer_unittests.cc b/engine/src/flutter/flow/layers/clip_rrect_layer_unittests.cc index 0391e0309b..69a7085174 100644 --- a/engine/src/flutter/flow/layers/clip_rrect_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/clip_rrect_layer_unittests.cc @@ -4,6 +4,7 @@ #include "flutter/flow/layers/clip_rrect_layer.h" +#include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" #include "flutter/fml/macros.h" @@ -265,5 +266,233 @@ TEST_F(ClipRRectLayerTest, Readback) { EXPECT_TRUE(ReadbackResult(context, save_layer, reader, true)); } +TEST_F(ClipRRectLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + SkRect clip_rect = SkRect::MakeWH(500, 500); + SkRRect clip_r_rect = SkRRect::MakeRectXY(clip_rect, 20, 20); + auto clip_r_rect_layer = + std::make_shared(clip_r_rect, Clip::hardEdge); + clip_r_rect_layer->Add(mock1); + + // ClipRectLayer will pass through compatibility from a compatible child + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_r_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + clip_r_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + context->subtree_can_inherit_opacity = false; + clip_r_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path3 = SkPath().addRect({20, 20, 40, 40}); + auto mock3 = MockLayer::MakeOpacityCompatible(path3); + clip_r_rect_layer->Add(mock3); + + // ClipRectLayer will not pass through compatibility from multiple + // overlapping children even if they are individually compatible + context->subtree_can_inherit_opacity = false; + clip_r_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_r_rect_saveLayer = std::make_shared( + clip_r_rect, Clip::antiAliasWithSaveLayer); + clip_r_rect_saveLayer->Add(mock1); + clip_r_rect_saveLayer->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_r_rect_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the overlapping child and test again, should still be compatible + clip_r_rect_saveLayer->Add(mock3); + context->subtree_can_inherit_opacity = false; + clip_r_rect_saveLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } + + // An incompatible, but non-overlapping child for the following tests + auto path4 = SkPath().addRect({60, 60, 70, 70}); + auto mock4 = MockLayer::Make(path4); + + { + // ClipRectLayer with incompatible child will not be compatible + auto clip_r_rect_bad_child = + std::make_shared(clip_r_rect, Clip::hardEdge); + clip_r_rect_bad_child->Add(mock1); + clip_r_rect_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_r_rect_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + clip_r_rect_bad_child->Add(mock4); + + // The third child is non-overlapping, but not compatible so the + // TransformLayer should end up incompatible + context->subtree_can_inherit_opacity = false; + clip_r_rect_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + } + + { + // ClipRectLayer(aa with saveLayer) will always be compatible + auto clip_r_rect_saveLayer_bad_child = std::make_shared( + clip_r_rect, Clip::antiAliasWithSaveLayer); + clip_r_rect_saveLayer_bad_child->Add(mock1); + clip_r_rect_saveLayer_bad_child->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + clip_r_rect_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // Now add the incompatible child and test again, should still be compatible + clip_r_rect_saveLayer_bad_child->Add(mock4); + context->subtree_can_inherit_opacity = false; + clip_r_rect_saveLayer_bad_child->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + } +} + +TEST_F(ClipRRectLayerTest, OpacityInheritancePainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + SkRect clip_rect = SkRect::MakeWH(500, 500); + SkRRect clip_r_rect = SkRRect::MakeRectXY(clip_rect, 20, 20); + auto clip_rect_layer = + std::make_shared(clip_r_rect, Clip::antiAlias); + clip_rect_layer->Add(mock1); + clip_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_rect_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipRRect(clip_r_rect, SkClipOp::kIntersect, true); + /* child layer1 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path1.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + expected_builder.restore(); + } + /* child layer2 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path2.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + +TEST_F(ClipRRectLayerTest, OpacityInheritanceSaveLayerPainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({20, 20, 40, 40}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + auto children_bounds = path1.getBounds(); + children_bounds.join(path2.getBounds()); + SkRect clip_rect = SkRect::MakeWH(500, 500); + SkRRect clip_r_rect = SkRRect::MakeRectXY(clip_rect, 20, 20); + auto clip_r_rect_layer = std::make_shared( + clip_r_rect, Clip::antiAliasWithSaveLayer); + clip_r_rect_layer->Add(mock1); + clip_r_rect_layer->Add(mock2); + + // ClipRectLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + clip_r_rect_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(clip_r_rect_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ClipRectLayer::Paint() */ { + expected_builder.save(); + expected_builder.clipRRect(clip_r_rect, SkClipOp::kIntersect, true); + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&children_bounds, true); + /* child layer1 paint */ { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + /* child layer2 paint */ { // + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/color_filter_layer.cc b/engine/src/flutter/flow/layers/color_filter_layer.cc index 6f33b70981..17de94b4bd 100644 --- a/engine/src/flutter/flow/layers/color_filter_layer.cc +++ b/engine/src/flutter/flow/layers/color_filter_layer.cc @@ -30,6 +30,10 @@ void ColorFilterLayer::Preroll(PrerollContext* context, Layer::AutoPrerollSaveLayerState::Create(context); ContainerLayer::Preroll(context, matrix); + // We always use a saveLayer (or a cached rendering), so we + // can always apply opacity in those cases. + context->subtree_can_inherit_opacity = true; + if (render_count_ >= kMinimumRendersBeforeCachingFilterLayer) { TryToPrepareRasterCache(context, this, matrix, RasterCacheLayerStrategy::kLayer); @@ -44,26 +48,27 @@ void ColorFilterLayer::Paint(PaintContext& context) const { TRACE_EVENT0("flutter", "ColorFilterLayer::Paint"); FML_DCHECK(needs_painting(context)); + AutoCachePaint cache_paint(context); + if (context.raster_cache) { if (context.raster_cache->Draw(this, *context.leaf_nodes_canvas, - RasterCacheLayerStrategy::kLayer)) { + RasterCacheLayerStrategy::kLayer, + cache_paint.paint())) { return; } - SkPaint paint; - paint.setColorFilter(filter_); + cache_paint.setColorFilter(filter_); if (context.raster_cache->Draw(this, *context.leaf_nodes_canvas, RasterCacheLayerStrategy::kLayerChildren, - &paint)) { + cache_paint.paint())) { return; } } - SkPaint paint; - paint.setColorFilter(filter_); + cache_paint.setColorFilter(filter_); - Layer::AutoSaveLayer save = - Layer::AutoSaveLayer::Create(context, paint_bounds(), &paint); + Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create( + context, paint_bounds(), cache_paint.paint()); PaintChildren(context); } diff --git a/engine/src/flutter/flow/layers/color_filter_layer_unittests.cc b/engine/src/flutter/flow/layers/color_filter_layer_unittests.cc index f721a5946a..6cc9c8cfac 100644 --- a/engine/src/flutter/flow/layers/color_filter_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/color_filter_layer_unittests.cc @@ -4,6 +4,8 @@ #include "flutter/flow/layers/color_filter_layer.h" +#include "flutter/display_list/display_list_color_filter.h" +#include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" #include "flutter/fml/macros.h" @@ -317,5 +319,67 @@ TEST_F(ColorFilterLayerTest, CacheChildren) { RasterCacheLayerStrategy::kLayerChildren)); } +TEST_F(ColorFilterLayerTest, OpacityInheritance) { + // clang-format off + float matrix[20] = { + 0, 1, 0, 0, 0, + 0, 0, 1, 0, 0, + 1, 0, 0, 0, 0, + 0, 0, 0, 1, 0, + }; + // clang-format on + auto layer_filter = DlMatrixColorFilter(matrix); + auto initial_transform = SkMatrix::Translate(50.0, 25.5); + const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f)); + auto mock_layer = std::make_shared(child_path); + auto color_filter_layer = + std::make_shared(layer_filter.skia_object()); + color_filter_layer->Add(mock_layer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + color_filter_layer->Preroll(preroll_context(), initial_transform); + // ImageFilterLayers can always inherit opacity whether or not their + // children are compatible. + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(color_filter_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = RasterCache::GetIntegralTransCTM( + SkMatrix::Translate(offset.fX, offset.fY)); + DisplayListBuilder expected_builder; + /* opacity_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* image_filter_layer::Paint() */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.setColorFilter(&layer_filter); + expected_builder.saveLayer(&child_path.getBounds(), true); + /* mock_layer::Paint() */ { + expected_builder.setColor(0xFF000000); + expected_builder.setColorFilter(nullptr); + expected_builder.drawPath(child_path); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/container_layer.cc b/engine/src/flutter/flow/layers/container_layer.cc index 36ea6ac883..7e9820e4aa 100644 --- a/engine/src/flutter/flow/layers/container_layer.cc +++ b/engine/src/flutter/flow/layers/container_layer.cc @@ -136,16 +136,16 @@ void ContainerLayer::PrerollChildren(PrerollContext* context, FML_DCHECK(!context->has_platform_view); bool child_has_platform_view = false; bool child_has_texture_layer = false; - bool subtree_can_inherit_opacity = layer_can_inherit_opacity(); + bool subtree_can_inherit_opacity = context->subtree_can_inherit_opacity; for (auto& layer : layers_) { // Reset context->has_platform_view to false so that layers aren't treated // as if they have a platform view based on one being previously found in a // sibling tree. context->has_platform_view = false; - // Initialize the "inherit opacity" flag to the value recorded in the layer - // and allow it to override the answer during its |Preroll| - context->subtree_can_inherit_opacity = layer->layer_can_inherit_opacity(); + // Initialize the "inherit opacity" flag to false and allow the layer to + // override the answer during its |Preroll| + context->subtree_can_inherit_opacity = false; layer->Preroll(context, child_matrix); diff --git a/engine/src/flutter/flow/layers/container_layer_unittests.cc b/engine/src/flutter/flow/layers/container_layer_unittests.cc index 7fd4b32bb6..8573c0ee95 100644 --- a/engine/src/flutter/flow/layers/container_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/container_layer_unittests.cc @@ -192,6 +192,61 @@ TEST_F(ContainerLayerTest, NeedsSystemComposite) { child_path2, child_paint2}}})); } +TEST_F(ContainerLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto container1 = std::make_shared(); + container1->Add(mock1); + + // ContainerLayer will not pass through compatibility on its own + // Subclasses must explicitly enable this in their own Preroll + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + container1->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + container1->Add(mock2); + + // ContainerLayer will pass through compatibility from multiple + // non-overlapping compatible children if the caller enables it + context->subtree_can_inherit_opacity = true; + container1->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path3 = SkPath().addRect({20, 20, 40, 40}); + auto mock3 = MockLayer::MakeOpacityCompatible(path3); + container1->Add(mock3); + + // ContainerLayer will not pass through compatibility from multiple + // overlapping children even if they are individually compatible + // and the caller requests it + context->subtree_can_inherit_opacity = true; + container1->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + auto container2 = std::make_shared(); + container2->Add(mock1); + container2->Add(mock2); + + // Double check first two children are compatible and non-overlapping + // if the caller requests it + context->subtree_can_inherit_opacity = true; + container2->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path4 = SkPath().addRect({60, 60, 70, 70}); + auto mock4 = MockLayer::Make(path4); + container2->Add(mock4); + + // The third child is non-overlapping, but not compatible so the + // TransformLayer should end up incompatible + context->subtree_can_inherit_opacity = true; + container2->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); +} + using ContainerLayerDiffTest = DiffContextTest; // Insert PictureLayer amongst container layers diff --git a/engine/src/flutter/flow/layers/display_list_layer.cc b/engine/src/flutter/flow/layers/display_list_layer.cc index e5fe2554c8..e44d57b608 100644 --- a/engine/src/flutter/flow/layers/display_list_layer.cc +++ b/engine/src/flutter/flow/layers/display_list_layer.cc @@ -5,6 +5,7 @@ #include "flutter/flow/layers/display_list_layer.h" #include "flutter/display_list/display_list_builder.h" +#include "flutter/display_list/display_list_flags.h" #include "flutter/flow/layers/offscreen_surface.h" namespace flutter { @@ -16,12 +17,7 @@ DisplayListLayer::DisplayListLayer(const SkPoint& offset, : offset_(offset), display_list_(std::move(display_list)), is_complex_(is_complex), - will_change_(will_change) { - if (display_list_.skia_object()) { - set_layer_can_inherit_opacity( - display_list_.skia_object()->can_apply_group_opacity()); - } -} + will_change_(will_change) {} bool DisplayListLayer::IsReplacing(DiffContext* context, const Layer* layer) const { @@ -97,6 +93,10 @@ void DisplayListLayer::Preroll(PrerollContext* context, SkRect bounds = disp_list->bounds().makeOffset(offset_.x(), offset_.y()); + if (disp_list->can_apply_group_opacity()) { + context->subtree_can_inherit_opacity = true; + } + if (auto* cache = context->raster_cache) { TRACE_EVENT0("flutter", "DisplayListLayer::RasterCache (Preroll)"); if (context->cull_rect.intersects(bounds)) { @@ -157,8 +157,16 @@ void DisplayListLayer::Paint(PaintContext& context) const { } if (context.leaf_nodes_builder) { + AutoCachePaint save_paint(context); + int restore_count = context.leaf_nodes_builder->getSaveCount(); + if (save_paint.paint() != nullptr) { + context.leaf_nodes_builder->setAttributesFromPaint( + *save_paint.paint(), DisplayListOpFlags::kSaveLayerWithPaintFlags); + context.leaf_nodes_builder->saveLayer(&paint_bounds(), true); + } display_list()->RenderTo(context.leaf_nodes_builder, context.inherited_opacity); + context.leaf_nodes_builder->restoreToCount(restore_count); } else { display_list()->RenderTo(context.leaf_nodes_canvas, context.inherited_opacity); diff --git a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc index 02f6f02b64..7796d586f6 100644 --- a/engine/src/flutter/flow/layers/display_list_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/display_list_layer_unittests.cc @@ -107,6 +107,232 @@ TEST_F(DisplayListLayerTest, SimpleDisplayList) { EXPECT_EQ(mock_canvas().draw_calls(), expected_draw_calls); } +TEST_F(DisplayListLayerTest, SimpleDisplayListOpacityInheritance) { + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + DisplayListBuilder builder; + builder.drawRect(picture_bounds); + auto display_list = builder.Build(); + auto display_list_layer = std::make_shared( + layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + EXPECT_TRUE(display_list->can_apply_group_opacity()); + + auto context = preroll_context(); + context->subtree_can_inherit_opacity = false; + display_list_layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint opacity_offset = SkPoint::Make(10, 10); + auto opacity_layer = + std::make_shared(opacity_alpha, opacity_offset); + opacity_layer->Add(display_list_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto save_layer_bounds = + picture_bounds.makeOffset(layer_offset.fX, layer_offset.fY); + auto opacity_integral_matrix = + RasterCache::GetIntegralTransCTM(SkMatrix::Translate(opacity_offset)); + SkMatrix layer_offset_matrix = opacity_integral_matrix; + layer_offset_matrix.postTranslate(layer_offset.fX, layer_offset.fY); + auto layer_offset_integral_matrix = + RasterCache::GetIntegralTransCTM(layer_offset_matrix); + DisplayListBuilder expected_builder; + /* opacity_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(opacity_offset.fX, opacity_offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integral_matrix); +#endif + /* display_list_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(layer_offset.fX, layer_offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(layer_offset_integral_matrix); +#endif + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&save_layer_bounds, true); + /* display_list contents */ { // + expected_builder.drawRect(picture_bounds); + } + expected_builder.restore(); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE( + DisplayListsEQ_Verbose(expected_builder.Build(), this->display_list())); +} + +TEST_F(DisplayListLayerTest, IncompatibleDisplayListOpacityInheritance) { + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture1_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + const SkRect picture2_bounds = SkRect::MakeLTRB(10.0f, 15.0f, 30.0f, 35.0f); + DisplayListBuilder builder; + builder.drawRect(picture1_bounds); + builder.drawRect(picture2_bounds); + auto display_list = builder.Build(); + auto display_list_layer = std::make_shared( + layer_offset, SkiaGPUObject(display_list, unref_queue()), false, false); + EXPECT_FALSE(display_list->can_apply_group_opacity()); + + auto context = preroll_context(); + context->subtree_can_inherit_opacity = false; + display_list_layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint opacity_offset = SkPoint::Make(10, 10); + auto opacity_layer = + std::make_shared(opacity_alpha, opacity_offset); + opacity_layer->Add(display_list_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(opacity_layer->children_can_accept_opacity()); + + auto display_list_bounds = picture1_bounds; + display_list_bounds.join(picture2_bounds); + auto save_layer_bounds = + display_list_bounds.makeOffset(layer_offset.fX, layer_offset.fY); + save_layer_bounds.roundOut(&save_layer_bounds); + auto opacity_integral_matrix = + RasterCache::GetIntegralTransCTM(SkMatrix::Translate(opacity_offset)); + SkMatrix layer_offset_matrix = opacity_integral_matrix; + layer_offset_matrix.postTranslate(layer_offset.fX, layer_offset.fY); + auto layer_offset_integral_matrix = + RasterCache::GetIntegralTransCTM(layer_offset_matrix); + DisplayListBuilder expected_builder; + /* opacity_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(opacity_offset.fX, opacity_offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integral_matrix); +#endif + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&save_layer_bounds, true); + { + /* display_list_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(layer_offset.fX, layer_offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(layer_offset_integral_matrix); +#endif + expected_builder.drawRect(picture1_bounds); + expected_builder.drawRect(picture2_bounds); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE( + DisplayListsEQ_Verbose(expected_builder.Build(), this->display_list())); +} + +TEST_F(DisplayListLayerTest, CachedIncompatibleDisplayListOpacityInheritance) { + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture1_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + const SkRect picture2_bounds = SkRect::MakeLTRB(10.0f, 15.0f, 30.0f, 35.0f); + DisplayListBuilder builder; + builder.drawRect(picture1_bounds); + builder.drawRect(picture2_bounds); + auto display_list = builder.Build(); + auto display_list_layer = std::make_shared( + layer_offset, SkiaGPUObject(display_list, unref_queue()), true, false); + EXPECT_FALSE(display_list->can_apply_group_opacity()); + + use_skia_raster_cache(); + + auto context = preroll_context(); + context->subtree_can_inherit_opacity = false; + display_list_layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + // Pump the DisplayListLayer until it is ready to cache its DL + display_list_layer->Paint(paint_context()); + display_list_layer->Paint(paint_context()); + display_list_layer->Paint(paint_context()); + + int opacity_alpha = 0x7F; + SkPoint opacity_offset = SkPoint::Make(10, 10); + auto opacity_layer = + std::make_shared(opacity_alpha, opacity_offset); + opacity_layer->Add(display_list_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + // The following would be a great test of the painting of the above + // setup, but for the fact that the raster cache stores raw pointers + // to sk_sp and the canvas recorder then wraps each of those + // in a unique DlImage - which means the DisplayList objects will not + // compare with the Equals method since the addresses of the two + // DlImage objects will not be equal even if they point to the same + // SkImage on each frame. + // See https://github.com/flutter/flutter/issues/102331 + // auto display_list_bounds = picture1_bounds; + // display_list_bounds.join(picture2_bounds); + // auto save_layer_bounds = + // display_list_bounds.makeOffset(layer_offset.fX, layer_offset.fY); + // save_layer_bounds.roundOut(&save_layer_bounds); + // auto opacity_integral_matrix = + // RasterCache::GetIntegralTransCTM(SkMatrix::Translate(opacity_offset)); + // SkMatrix layer_offset_matrix = opacity_integral_matrix; + // layer_offset_matrix.postTranslate(layer_offset.fX, layer_offset.fY); + // auto layer_offset_integral_matrix = + // RasterCache::GetIntegralTransCTM(layer_offset_matrix); + // // Using a recorder instead of a DisplayListBuilder so we can hand it + // // off to the RasterCache::Draw() method + // DisplayListCanvasRecorder recorder(SkRect::MakeWH(1000, 1000)); + // /* opacity_layer::Paint() */ { + // recorder.save(); + // { + // recorder.translate(opacity_offset.fX, opacity_offset.fY); + // #ifndef SUPPORT_FRACTIONAL_TRANSLATION + // recorder.resetMatrix(); + // recorder.concat(opacity_integral_matrix); + // #endif + // /* display_list_layer::Paint() */ { + // recorder.save(); + // { + // recorder.translate(layer_offset.fX, layer_offset.fY); + // #ifndef SUPPORT_FRACTIONAL_TRANSLATION + // recorder.resetMatrix(); + // recorder.concat(layer_offset_integral_matrix); + // #endif + // SkPaint p; + // p.setAlpha(opacity_alpha); + // context->raster_cache->Draw(*display_list, recorder, &p); + // } + // recorder.restore(); + // } + // } + // recorder.restore(); + // } + + // opacity_layer->Paint(display_list_paint_context()); + // EXPECT_TRUE( + // DisplayListsEQ_Verbose(recorder.Build(), this->display_list())); +} + using DisplayListLayerDiffTest = DiffContextTest; TEST_F(DisplayListLayerDiffTest, SimpleDisplayList) { diff --git a/engine/src/flutter/flow/layers/image_filter_layer.cc b/engine/src/flutter/flow/layers/image_filter_layer.cc index fbeff92307..e5158b73b5 100644 --- a/engine/src/flutter/flow/layers/image_filter_layer.cc +++ b/engine/src/flutter/flow/layers/image_filter_layer.cc @@ -44,6 +44,11 @@ void ImageFilterLayer::Preroll(PrerollContext* context, SkRect child_bounds = SkRect::MakeEmpty(); PrerollChildren(context, matrix, &child_bounds); + context->subtree_can_inherit_opacity = true; + + // We always paint with a saveLayer (or a cached rendering), + // so we can always apply opacity in any of those cases. + context->subtree_can_inherit_opacity = true; if (!filter_) { set_paint_bounds(child_bounds); @@ -95,31 +100,32 @@ void ImageFilterLayer::Paint(PaintContext& context) const { TRACE_EVENT0("flutter", "ImageFilterLayer::Paint"); FML_DCHECK(needs_painting(context)); + AutoCachePaint cache_paint(context); + if (context.raster_cache) { if (context.raster_cache->Draw(this, *context.leaf_nodes_canvas, - RasterCacheLayerStrategy::kLayer)) { + RasterCacheLayerStrategy::kLayer, + cache_paint.paint())) { return; } if (transformed_filter_) { - SkPaint paint; - paint.setImageFilter(transformed_filter_); + cache_paint.setImageFilter(transformed_filter_); if (context.raster_cache->Draw(this, *context.leaf_nodes_canvas, RasterCacheLayerStrategy::kLayerChildren, - &paint)) { + cache_paint.paint())) { return; } } } - SkPaint paint; - paint.setImageFilter(filter_); + cache_paint.setImageFilter(filter_); // Normally a save_layer is sized to the current layer bounds, but in this // case the bounds of the child may not be the same as the filtered version // so we use the bounds of the child container which do not include any // modifications that the filter might apply. - Layer::AutoSaveLayer save_layer = - Layer::AutoSaveLayer::Create(context, child_paint_bounds(), &paint); + Layer::AutoSaveLayer save_layer = Layer::AutoSaveLayer::Create( + context, child_paint_bounds(), cache_paint.paint()); PaintChildren(context); } diff --git a/engine/src/flutter/flow/layers/image_filter_layer_unittests.cc b/engine/src/flutter/flow/layers/image_filter_layer_unittests.cc index 0b5898efbc..acf1c825d3 100644 --- a/engine/src/flutter/flow/layers/image_filter_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/image_filter_layer_unittests.cc @@ -373,6 +373,65 @@ TEST_F(ImageFilterLayerTest, CacheChildren) { RasterCacheLayerStrategy::kLayerChildren)); } +TEST_F(ImageFilterLayerTest, OpacityInheritance) { + const SkMatrix initial_transform = SkMatrix::Translate(0.5f, 1.0f); + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + const SkPath child_path = SkPath().addRect(child_bounds); + const SkPaint child_paint = SkPaint(SkColors::kYellow); + auto layer_filter = SkImageFilters::MatrixTransform( + SkMatrix(), + SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear), nullptr); + // The mock_layer child will not be compatible with opacity + auto mock_layer = MockLayer::Make(child_path, child_paint); + auto image_filter_layer = std::make_shared(layer_filter); + image_filter_layer->Add(mock_layer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + image_filter_layer->Preroll(preroll_context(), initial_transform); + // ImageFilterLayers can always inherit opacity whether or not their + // children are compatible. + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(image_filter_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = RasterCache::GetIntegralTransCTM( + SkMatrix::Translate(offset.fX, offset.fY)); + auto dl_image_filter = DlImageFilter::From(layer_filter); + DisplayListBuilder expected_builder; + /* opacity_layer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* image_filter_layer::Paint() */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.setImageFilter(dl_image_filter.get()); + expected_builder.saveLayer(&child_path.getBounds(), true); + /* mock_layer::Paint() */ { + expected_builder.setColor(child_paint.getColor()); + expected_builder.setImageFilter(nullptr); + expected_builder.drawPath(child_path); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + using ImageFilterLayerDiffTest = DiffContextTest; TEST_F(ImageFilterLayerDiffTest, ImageFilterLayer) { diff --git a/engine/src/flutter/flow/layers/layer.cc b/engine/src/flutter/flow/layers/layer.cc index 621ae58570..8a34b775ef 100644 --- a/engine/src/flutter/flow/layers/layer.cc +++ b/engine/src/flutter/flow/layers/layer.cc @@ -13,8 +13,7 @@ Layer::Layer() : paint_bounds_(SkRect::MakeEmpty()), unique_id_(NextUniqueID()), original_layer_id_(unique_id_), - subtree_has_platform_view_(false), - layer_can_inherit_opacity_(false) {} + subtree_has_platform_view_(false) {} Layer::~Layer() = default; diff --git a/engine/src/flutter/flow/layers/layer.h b/engine/src/flutter/flow/layers/layer.h index f0ca47ed0d..9a7316d4c8 100644 --- a/engine/src/flutter/flow/layers/layer.h +++ b/engine/src/flutter/flow/layers/layer.h @@ -63,23 +63,41 @@ struct PrerollContext { // prescence of a texture layer during Preroll. bool has_texture_layer = false; - // This value indicates that the entire subtree below the layer can inherit - // an opacity value and modulate its own visibility accordingly. - // For Layers which cannot either apply such an inherited opacity nor pass - // it along to their children, they can ignore this value as its default - // behavior is "opt-in". - // For Layers that support this condition, it can be recorded in their - // constructor using the |set_layer_can_inherit_opacity| method and the - // value will be accumulated and recorded by the |PrerollChidren| method - // automatically. - // If the property is more dynamic then the Layer can dynamically set this - // flag before returning from the |Preroll| method. - // For ContainerLayers that need to know if their children can inherit - // the value, the |PrerollChildren| method will have set this value in - // the context before it returns. If the container can support it as long - // as the subtree can support it, no further work needs to be done other - // than to remember the value so that it can choose the right strategy - // for its |Paint| method. + // This field indicates whether the subtree rooted at this layer can + // inherit an opacity value and modulate its visibility accordingly. + // + // Any layer is free to ignore this flag. Its value will be false upon + // entry into its Preroll method, it will remain false if it calls + // PrerollChildren on any children it might have, and it will remain + // false upon exit from the Preroll method unless it takes specific + // action compute if it should be true. Thus, this property is "opt-in". + // + // If the value is false when the Preroll method exits, then the + // |PaintContext::inherited_opacity| value should always be set to + // 1.0 when its |Paint| method is called. + // + // Leaf layers need only be concerned with their own rendering and + // can set the value according to whether they can apply the opacity. + // + // For containers, there are 3 ways to interact with this field: + // + // 1. If you need to know whether your children are compatible, then + // set the field to true before you call PrerollChildren. That + // method will then reset the field to false if it detects any + // incompatible children. + // + // 2. If your decision on whether to inherit the opacity depends on + // the answer from the children, then remember the value of the + // field when PrerollChildren returns. (eg. OpacityLayer remembers + // this value to control whether to set the opacity value into the + // |PaintContext::inherited_opacity| field in |Paint| before + // recursing to its children in Paint) + // + // 3. If you want to indicate to your parents that you can accept + // inherited opacity regardless of whether your children were + // compatible then set this field to true before returning + // from your Preroll method. (eg. layers that always apply a + // saveLayer when rendering anyway can apply the opacity there) bool subtree_can_inherit_opacity = false; }; @@ -196,12 +214,28 @@ class Layer { ~AutoCachePaint() { context_.inherited_opacity = paint_.getAlphaf(); } + void setImageFilter(sk_sp filter) { + paint_.setImageFilter(filter); + update_needs_paint(); + } + + void setColorFilter(sk_sp filter) { + paint_.setColorFilter(filter); + update_needs_paint(); + } + const SkPaint* paint() { return needs_paint_ ? &paint_ : nullptr; } private: PaintContext& context_; SkPaint paint_; bool needs_paint_; + + void update_needs_paint() { + needs_paint_ = paint_.getImageFilter() != nullptr || + paint_.getColorFilter() != nullptr || + paint_.getAlphaf() < SK_Scalar1; + } }; // Calls SkCanvas::saveLayer and restores the layer upon destruction. Also @@ -271,18 +305,6 @@ class Layer { subtree_has_platform_view_ = value; } - // Returns true if the layer can render with an added opacity value inherited - // from an OpacityLayer ancestor and delivered to its |Paint| method through - // the |PaintContext.inherited_opacity| field. This flag can be set either - // in the Layer's constructor if it is a lifetime constant value, or during - // the |Preroll| method if it must determine the capability based on data - // only available when it is part of a tree. It must set this value before - // recursing to its children if it is a |ContainerLayer|. - bool layer_can_inherit_opacity() const { return layer_can_inherit_opacity_; } - void set_layer_can_inherit_opacity(bool value) { - layer_can_inherit_opacity_ = value; - } - // Returns the paint bounds in the layer's local coordinate system // as determined during Preroll(). The bounds should include any // transform, clip or distortions performed by the layer itself, @@ -353,7 +375,6 @@ class Layer { uint64_t unique_id_; uint64_t original_layer_id_; bool subtree_has_platform_view_; - bool layer_can_inherit_opacity_; static uint64_t NextUniqueID(); diff --git a/engine/src/flutter/flow/layers/opacity_layer.cc b/engine/src/flutter/flow/layers/opacity_layer.cc index 49fc890a63..b234428a80 100644 --- a/engine/src/flutter/flow/layers/opacity_layer.cc +++ b/engine/src/flutter/flow/layers/opacity_layer.cc @@ -10,12 +10,7 @@ namespace flutter { OpacityLayer::OpacityLayer(SkAlpha alpha, const SkPoint& offset) - : alpha_(alpha), offset_(offset), children_can_accept_opacity_(false) { - // We can always inhert opacity even if we cannot pass it along to - // our children as we can accumulate the inherited opacity into our - // own opacity value before we recurse. - set_layer_can_inherit_opacity(true); -} + : alpha_(alpha), offset_(offset), children_can_accept_opacity_(false) {} void OpacityLayer::Diff(DiffContext* context, const Layer* old_layer) { DiffContext::AutoSubtreeRestore subtree(context); @@ -51,12 +46,25 @@ void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { context->mutators_stack.PushOpacity(alpha_); Layer::AutoPrerollSaveLayerState save = Layer::AutoPrerollSaveLayerState::Create(context); - ContainerLayer::Preroll(context, child_matrix); - context->mutators_stack.Pop(); - context->mutators_stack.Pop(); + // Collect inheritance information on our children in Preroll so that + // we can decide whether or not to use a saveLayer in Paint. + context->subtree_can_inherit_opacity = true; + + // ContainerLayer will turn the flag off if any children are + // incompatible or if they overlap + ContainerLayer::Preroll(context, child_matrix); + + // We store the inheritance ability of our children for |Paint| set_children_can_accept_opacity(context->subtree_can_inherit_opacity); + // Now we let our parent layers know that we, too, can inherit opacity + // regardless of what our children are capable of + context->subtree_can_inherit_opacity = true; + + context->mutators_stack.Pop(); + context->mutators_stack.Pop(); + set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY)); if (!children_can_accept_opacity()) { diff --git a/engine/src/flutter/flow/layers/opacity_layer_unittests.cc b/engine/src/flutter/flow/layers/opacity_layer_unittests.cc index 320f60c5b0..03625756b4 100644 --- a/engine/src/flutter/flow/layers/opacity_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/opacity_layer_unittests.cc @@ -5,6 +5,8 @@ #include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/layers/clip_rect_layer.h" +#include "flutter/flow/layers/image_filter_layer.h" +#include "flutter/flow/layers/transform_layer.h" #include "flutter/flow/testing/diff_context_test.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" @@ -465,6 +467,142 @@ TEST_F(OpacityLayerTest, CullRectIsTransformed) { EXPECT_EQ(mockLayer->parent_cull_rect().fTop, -20); } +TEST_F(OpacityLayerTest, OpacityInheritanceCompatibleChild) { + auto opacityLayer = + std::make_shared(128, SkPoint::Make(20, 20)); + auto mockLayer = MockLayer::MakeOpacityCompatible(SkPath()); + opacityLayer->Add(mockLayer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + EXPECT_TRUE(opacityLayer->children_can_accept_opacity()); +} + +TEST_F(OpacityLayerTest, OpacityInheritanceIncompatibleChild) { + auto opacityLayer = + std::make_shared(128, SkPoint::Make(20, 20)); + auto mockLayer = MockLayer::Make(SkPath()); + opacityLayer->Add(mockLayer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + EXPECT_FALSE(opacityLayer->children_can_accept_opacity()); +} + +TEST_F(OpacityLayerTest, OpacityInheritanceThroughContainer) { + auto opacityLayer = + std::make_shared(128, SkPoint::Make(20, 20)); + auto containerLayer = std::make_shared(); + auto mockLayer = MockLayer::MakeOpacityCompatible(SkPath()); + containerLayer->Add(mockLayer); + opacityLayer->Add(containerLayer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + // By default a container layer will not pass opacity through to + // its children - specific subclasses will have to enable this + // pass through by setting the flag to true themselves before + // calling their super method ContainerLayer::Preroll(). + EXPECT_FALSE(opacityLayer->children_can_accept_opacity()); +} + +TEST_F(OpacityLayerTest, OpacityInheritanceThroughTransform) { + auto opacityLayer = + std::make_shared(128, SkPoint::Make(20, 20)); + auto transformLayer = std::make_shared(SkMatrix::Scale(2, 2)); + auto mockLayer = MockLayer::MakeOpacityCompatible(SkPath()); + transformLayer->Add(mockLayer); + opacityLayer->Add(transformLayer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + EXPECT_TRUE(opacityLayer->children_can_accept_opacity()); +} + +TEST_F(OpacityLayerTest, OpacityInheritanceThroughImageFilter) { + auto opacityLayer = + std::make_shared(128, SkPoint::Make(20, 20)); + auto filterLayer = std::make_shared( + SkImageFilters::Blur(5.0, 5.0, nullptr)); + auto mockLayer = MockLayer::MakeOpacityCompatible(SkPath()); + filterLayer->Add(mockLayer); + opacityLayer->Add(filterLayer); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + EXPECT_TRUE(opacityLayer->children_can_accept_opacity()); +} + +TEST_F(OpacityLayerTest, OpacityInheritanceNested) { + SkPoint offset1 = SkPoint::Make(10, 20); + SkPoint offset2 = SkPoint::Make(20, 10); + SkPath mockPath = SkPath::Rect({10, 10, 20, 20}); + auto opacityLayer1 = std::make_shared(128, offset1); + auto opacityLayer2 = std::make_shared(64, offset2); + auto mockLayer = MockLayer::MakeOpacityCompatible(mockPath); + opacityLayer2->Add(mockLayer); + opacityLayer1->Add(opacityLayer2); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + opacityLayer1->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + EXPECT_TRUE(opacityLayer1->children_can_accept_opacity()); + EXPECT_TRUE(opacityLayer2->children_can_accept_opacity()); + + SkPaint saveLayerPaint; + SkScalar inheritedOpacity = 128 * 1.0 / SK_AlphaOPAQUE; + inheritedOpacity *= 64 * 1.0 / SK_AlphaOPAQUE; + saveLayerPaint.setAlphaf(inheritedOpacity); + + DisplayListBuilder expected_builder; + /* opacityLayer1::Paint */ { + expected_builder.save(); + { + expected_builder.translate(offset1.fX, offset1.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(SkM44::Translate(offset1.fX, offset1.fY)); +#endif + /* opacityLayer2::Paint */ { + expected_builder.save(); + { + expected_builder.translate(offset2.fX, offset2.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(SkM44::Translate(offset1.fX + offset2.fX, + offset1.fY + offset2.fY)); +#endif + /* mockLayer::Paint */ { + expected_builder.setColor(saveLayerPaint.getAlpha() << 24); + expected_builder.saveLayer(&mockPath.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(mockPath); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacityLayer1->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + using OpacityLayerDiffTest = DiffContextTest; TEST_F(OpacityLayerDiffTest, FractionalTranslation) { diff --git a/engine/src/flutter/flow/layers/physical_shape_layer_unittests.cc b/engine/src/flutter/flow/layers/physical_shape_layer_unittests.cc index aa1b15c665..3e41506ea4 100644 --- a/engine/src/flutter/flow/layers/physical_shape_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/physical_shape_layer_unittests.cc @@ -416,6 +416,20 @@ TEST_F(PhysicalShapeLayerTest, Readback) { EXPECT_TRUE(ReadbackResult(context, save_layer, reader, true)); } +TEST_F(PhysicalShapeLayerTest, OpacityInheritance) { + SkPath layer_path; + layer_path.addRect(5.0f, 6.0f, 20.5f, 21.5f); + auto layer = + std::make_shared(SK_ColorGREEN, SK_ColorBLACK, + 0.0f, // elevation + layer_path, Clip::none); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + layer->Preroll(context, SkMatrix()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); +} + using PhysicalShapeLayerDiffTest = DiffContextTest; TEST_F(PhysicalShapeLayerDiffTest, NoClipPaintRegion) { diff --git a/engine/src/flutter/flow/layers/picture_layer_unittests.cc b/engine/src/flutter/flow/layers/picture_layer_unittests.cc index 1d619acb08..7838c84808 100644 --- a/engine/src/flutter/flow/layers/picture_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/picture_layer_unittests.cc @@ -97,6 +97,64 @@ TEST_F(PictureLayerTest, SimplePicture) { EXPECT_EQ(mock_canvas().draw_calls(), expected_draw_calls); } +TEST_F(PictureLayerTest, OpacityInheritanceCacheablePicture) { + use_mock_raster_cache(); + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + auto mock_picture = SkPicture::MakePlaceholder(picture_bounds); + auto layer = std::make_shared( + layer_offset, SkiaGPUObject(mock_picture, unref_queue()), true, false); + + // First try, no caching, cannot support opacity + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_EQ(raster_cache()->GetPictureCachedEntriesCount(), size_t(1)); + EXPECT_EQ(raster_cache()->EstimatePictureCacheByteSize(), size_t(0)); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + // Paint it enough times to reach its cache threshold + layer->Paint(paint_context()); + layer->Paint(paint_context()); + layer->Paint(paint_context()); + + // This time it will cache and can now support opacity + context->subtree_can_inherit_opacity = false; + layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_EQ(raster_cache()->GetPictureCachedEntriesCount(), size_t(1)); + EXPECT_NE(raster_cache()->EstimatePictureCacheByteSize(), size_t(0)); + EXPECT_TRUE(context->subtree_can_inherit_opacity); +} + +TEST_F(PictureLayerTest, OpacityInheritanceUncacheablePicture) { + use_mock_raster_cache(); + const SkPoint layer_offset = SkPoint::Make(1.5f, -0.5f); + const SkRect picture_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + auto mock_picture = SkPicture::MakePlaceholder(picture_bounds); + auto layer = std::make_shared( + layer_offset, SkiaGPUObject(mock_picture, unref_queue()), false, true); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_EQ(raster_cache()->GetPictureCachedEntriesCount(), size_t(0)); + EXPECT_EQ(raster_cache()->EstimatePictureCacheByteSize(), size_t(0)); + // First try, no caching, cannot support opacity + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + // Paint it enough times to reach its cache threshold + layer->Paint(paint_context()); + layer->Paint(paint_context()); + layer->Paint(paint_context()); + + context->subtree_can_inherit_opacity = false; + layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_EQ(raster_cache()->GetPictureCachedEntriesCount(), size_t(0)); + EXPECT_EQ(raster_cache()->EstimatePictureCacheByteSize(), size_t(0)); + // It still is not compatible because it cannot cache + EXPECT_FALSE(context->subtree_can_inherit_opacity); +} + using PictureLayerDiffTest = DiffContextTest; TEST_F(PictureLayerDiffTest, SimplePicture) { diff --git a/engine/src/flutter/flow/layers/platform_view_layer_unittests.cc b/engine/src/flutter/flow/layers/platform_view_layer_unittests.cc index 58a791d7ac..ecbbe3f39f 100644 --- a/engine/src/flutter/flow/layers/platform_view_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/platform_view_layer_unittests.cc @@ -83,5 +83,18 @@ TEST_F(PlatformViewLayerTest, ClippedPlatformViewPrerollsAndPaintsNothing) { MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}})); } +TEST_F(PlatformViewLayerTest, OpacityInheritance) { + const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f); + const SkSize layer_size = SkSize::Make(8.0f, 8.0f); + const int64_t view_id = 0; + auto layer = + std::make_shared(layer_offset, layer_size, view_id); + + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + layer->Preroll(preroll_context(), SkMatrix()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/shader_mask_layer.cc b/engine/src/flutter/flow/layers/shader_mask_layer.cc index e5b0fc84f3..c428987383 100644 --- a/engine/src/flutter/flow/layers/shader_mask_layer.cc +++ b/engine/src/flutter/flow/layers/shader_mask_layer.cc @@ -12,9 +12,7 @@ ShaderMaskLayer::ShaderMaskLayer(sk_sp shader, : shader_(shader), mask_rect_(mask_rect), blend_mode_(blend_mode), - render_count_(1) { - set_layer_can_inherit_opacity(true); -} + render_count_(1) {} void ShaderMaskLayer::Diff(DiffContext* context, const Layer* old_layer) { DiffContext::AutoSubtreeRestore subtree(context); @@ -37,6 +35,10 @@ void ShaderMaskLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { Layer::AutoPrerollSaveLayerState::Create(context); ContainerLayer::Preroll(context, matrix); + // We always paint with a saveLayer (or a cached rendering), + // so we can always apply opacity in any of those cases. + context->subtree_can_inherit_opacity = true; + if (render_count_ >= kMinimumRendersBeforeCachingFilterLayer) { TryToPrepareRasterCache(context, this, matrix, RasterCacheLayerStrategy::kLayer); @@ -49,13 +51,15 @@ void ShaderMaskLayer::Paint(PaintContext& context) const { TRACE_EVENT0("flutter", "ShaderMaskLayer::Paint"); FML_DCHECK(needs_painting(context)); + AutoCachePaint cache_paint(context); + if (context.raster_cache && context.raster_cache->Draw(this, *context.leaf_nodes_canvas, - RasterCacheLayerStrategy::kLayer)) { + RasterCacheLayerStrategy::kLayer, + cache_paint.paint())) { return; } - AutoCachePaint cache_paint(context); Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create( context, paint_bounds(), cache_paint.paint()); PaintChildren(context); diff --git a/engine/src/flutter/flow/layers/shader_mask_layer_unittests.cc b/engine/src/flutter/flow/layers/shader_mask_layer_unittests.cc index 76423b3b16..91219896ff 100644 --- a/engine/src/flutter/flow/layers/shader_mask_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/shader_mask_layer_unittests.cc @@ -4,6 +4,7 @@ #include "flutter/flow/layers/shader_mask_layer.h" +#include "flutter/flow/layers/opacity_layer.h" #include "flutter/flow/testing/layer_test.h" #include "flutter/flow/testing/mock_layer.h" #include "flutter/fml/macros.h" @@ -319,5 +320,61 @@ TEST_F(ShaderMaskLayerTest, LayerCached) { RasterCacheLayerStrategy::kLayer)); } +TEST_F(ShaderMaskLayerTest, OpacityInheritance) { + const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f); + const SkPath child_path = SkPath().addRect(child_bounds); + auto mock_layer = MockLayer::Make(child_path); + const SkRect mask_rect = SkRect::MakeLTRB(10, 10, 20, 20); + auto shader_mask_layer = + std::make_shared(nullptr, mask_rect, SkBlendMode::kSrc); + shader_mask_layer->Add(mock_layer); + + // ShaderMaskLayers can always support opacity despite incompatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + shader_mask_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(shader_mask_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* OpacityLayer::Paint() */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* ShaderMaskLayer::Paint() */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&child_path.getBounds(), true); + { + /* child layer paint */ { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(child_path); + } + expected_builder.translate(mask_rect.fLeft, mask_rect.fTop); + expected_builder.setBlendMode(DlBlendMode::kSrc); + expected_builder.drawRect( + SkRect::MakeWH(mask_rect.width(), mask_rect.height())); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/texture_layer.cc b/engine/src/flutter/flow/layers/texture_layer.cc index 63194b0cd5..6f5207da63 100644 --- a/engine/src/flutter/flow/layers/texture_layer.cc +++ b/engine/src/flutter/flow/layers/texture_layer.cc @@ -17,9 +17,7 @@ TextureLayer::TextureLayer(const SkPoint& offset, size_(size), texture_id_(texture_id), freeze_(freeze), - sampling_(sampling) { - set_layer_can_inherit_opacity(true); -} + sampling_(sampling) {} void TextureLayer::Diff(DiffContext* context, const Layer* old_layer) { DiffContext::AutoSubtreeRestore subtree(context); @@ -48,6 +46,7 @@ void TextureLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { set_paint_bounds(SkRect::MakeXYWH(offset_.x(), offset_.y(), size_.width(), size_.height())); context->has_texture_layer = true; + context->subtree_can_inherit_opacity = true; } void TextureLayer::Paint(PaintContext& context) const { diff --git a/engine/src/flutter/flow/layers/texture_layer_unittests.cc b/engine/src/flutter/flow/layers/texture_layer_unittests.cc index 4d251120ef..bd2c863c2d 100644 --- a/engine/src/flutter/flow/layers/texture_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/texture_layer_unittests.cc @@ -116,5 +116,29 @@ TEST_F(TextureLayerDiffTest, TextureInRetainedLayer) { EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(0, 0, 100, 100)); } +TEST_F(TextureLayerTest, OpacityInheritance) { + const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f); + const SkSize layer_size = SkSize::Make(8.0f, 8.0f); + const int64_t texture_id = 0; + auto mock_texture = std::make_shared(texture_id); + auto layer = std::make_shared( + layer_offset, layer_size, texture_id, false, + SkSamplingOptions(SkFilterMode::kLinear)); + + // Ensure the texture is located by the Layer. + preroll_context()->texture_registry.RegisterTexture(mock_texture); + + // The texture layer always reports opacity compatibility. + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + context->texture_registry.RegisterTexture(mock_texture); + layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + // MockTexture has no actual textur to render into the + // PaintContext canvas so we have no way to verify its + // rendering. +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/layers/transform_layer.cc b/engine/src/flutter/flow/layers/transform_layer.cc index 68cf4e8e31..f34566a915 100644 --- a/engine/src/flutter/flow/layers/transform_layer.cc +++ b/engine/src/flutter/flow/layers/transform_layer.cc @@ -24,7 +24,6 @@ TransformLayer::TransformLayer(const SkMatrix& transform) FML_LOG(ERROR) << "TransformLayer is constructed with an invalid matrix."; transform_.setIdentity(); } - set_layer_can_inherit_opacity(true); } void TransformLayer::Diff(DiffContext* context, const Layer* old_layer) { @@ -57,6 +56,10 @@ void TransformLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { context->cull_rect = kGiantRect; } + // Collect inheritance information on our children in Preroll so that + // we can pass it along by default. + context->subtree_can_inherit_opacity = true; + SkRect child_paint_bounds = SkRect::MakeEmpty(); PrerollChildren(context, child_matrix, &child_paint_bounds); diff --git a/engine/src/flutter/flow/layers/transform_layer_unittests.cc b/engine/src/flutter/flow/layers/transform_layer_unittests.cc index 174944c55a..6947c13ea4 100644 --- a/engine/src/flutter/flow/layers/transform_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/transform_layer_unittests.cc @@ -234,6 +234,124 @@ TEST_F(TransformLayerTest, NestedSeparated) { MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}})); } +TEST_F(TransformLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto transform1 = std::make_shared(SkMatrix::Scale(2, 2)); + transform1->Add(mock1); + + // TransformLayer will pass through compatibility from a compatible child + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + transform1->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + transform1->Add(mock2); + + // TransformLayer will pass through compatibility from multiple + // non-overlapping compatible children + context->subtree_can_inherit_opacity = false; + transform1->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path3 = SkPath().addRect({20, 20, 40, 40}); + auto mock3 = MockLayer::MakeOpacityCompatible(path3); + transform1->Add(mock3); + + // TransformLayer will not pass through compatibility from multiple + // overlapping children even if they are individually compatible + context->subtree_can_inherit_opacity = false; + transform1->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + auto transform2 = std::make_shared(SkMatrix::Scale(2, 2)); + transform2->Add(mock1); + transform2->Add(mock2); + + // Double check first two children are compatible and non-overlapping + context->subtree_can_inherit_opacity = false; + transform2->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + auto path4 = SkPath().addRect({60, 60, 70, 70}); + auto mock4 = MockLayer::Make(path4); + transform2->Add(mock4); + + // The third child is non-overlapping, but not compatible so the + // TransformLayer should end up incompatible + context->subtree_can_inherit_opacity = false; + transform2->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); +} + +TEST_F(TransformLayerTest, OpacityInheritancePainting) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + auto mock1 = MockLayer::MakeOpacityCompatible(path1); + auto path2 = SkPath().addRect({40, 40, 50, 50}); + auto mock2 = MockLayer::MakeOpacityCompatible(path2); + auto transform = SkMatrix::Scale(2, 2); + auto transform_layer = std::make_shared(transform); + transform_layer->Add(mock1); + transform_layer->Add(mock2); + + // TransformLayer will pass through compatibility from multiple + // non-overlapping compatible children + PrerollContext* context = preroll_context(); + context->subtree_can_inherit_opacity = false; + transform_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); + + int opacity_alpha = 0x7F; + SkPoint offset = SkPoint::Make(10, 10); + auto opacity_layer = std::make_shared(opacity_alpha, offset); + opacity_layer->Add(transform_layer); + context->subtree_can_inherit_opacity = false; + opacity_layer->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(opacity_layer->children_can_accept_opacity()); + + auto opacity_integer_transform = SkM44::Translate(offset.fX, offset.fY); + DisplayListBuilder expected_builder; + /* opacity_layer paint */ { + expected_builder.save(); + { + expected_builder.translate(offset.fX, offset.fY); +#ifndef SUPPORT_FRACTIONAL_TRANSLATION + expected_builder.transformReset(); + expected_builder.transform(opacity_integer_transform); +#endif + /* transform_layer paint */ { + expected_builder.save(); + expected_builder.transform(transform); + /* child layer1 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path1.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path1); + } + expected_builder.restore(); + } + /* child layer2 paint */ { + expected_builder.setColor(opacity_alpha << 24); + expected_builder.saveLayer(&path2.getBounds(), true); + { + expected_builder.setColor(0xFF000000); + expected_builder.drawPath(path2); + } + expected_builder.restore(); + } + expected_builder.restore(); + } + } + expected_builder.restore(); + } + + opacity_layer->Paint(display_list_paint_context()); + EXPECT_TRUE(DisplayListsEQ_Verbose(expected_builder.Build(), display_list())); +} + using TransformLayerLayerDiffTest = DiffContextTest; TEST_F(TransformLayerLayerDiffTest, Transform) { diff --git a/engine/src/flutter/flow/testing/layer_test.h b/engine/src/flutter/flow/testing/layer_test.h index e234c529ab..650f50ca96 100644 --- a/engine/src/flutter/flow/testing/layer_test.h +++ b/engine/src/flutter/flow/testing/layer_test.h @@ -39,6 +39,8 @@ template class LayerTestBase : public CanvasTestBase { using TestT = CanvasTestBase; + const SkRect kDlBounds = SkRect::MakeWH(500, 500); + public: LayerTestBase() : preroll_context_{ @@ -72,8 +74,8 @@ class LayerTestBase : public CanvasTestBase { .frame_device_pixel_ratio = 1.0f, // clang-format on }, - display_list_recorder_(kGiantRect), - internal_display_list_canvas_(kGiantRect.width(), kGiantRect.height()), + display_list_recorder_(kDlBounds), + internal_display_list_canvas_(kDlBounds.width(), kDlBounds.height()), display_list_paint_context_{ // clang-format off .internal_nodes_canvas = &internal_display_list_canvas_, @@ -193,6 +195,7 @@ class LayerTestBase : public CanvasTestBase { raster_cache_ = std::move(raster_cache); preroll_context_.raster_cache = raster_cache_.get(); paint_context_.raster_cache = raster_cache_.get(); + display_list_paint_context_.raster_cache = raster_cache_.get(); } FixedRefreshRateStopwatch raster_time_; diff --git a/engine/src/flutter/flow/testing/mock_layer.cc b/engine/src/flutter/flow/testing/mock_layer.cc index dff041d36c..09b7c20ccd 100644 --- a/engine/src/flutter/flow/testing/mock_layer.cc +++ b/engine/src/flutter/flow/testing/mock_layer.cc @@ -10,11 +10,13 @@ namespace testing { MockLayer::MockLayer(SkPath path, SkPaint paint, bool fake_has_platform_view, - bool fake_reads_surface) + bool fake_reads_surface, + bool fake_opacity_compatible) : fake_paint_path_(path), fake_paint_(paint), fake_has_platform_view_(fake_has_platform_view), - fake_reads_surface_(fake_reads_surface) {} + fake_reads_surface_(fake_reads_surface), + fake_opacity_compatible_(fake_opacity_compatible) {} bool MockLayer::IsReplacing(DiffContext* context, const Layer* layer) const { // Similar to PictureLayer, only return true for identical mock layers; @@ -42,12 +44,23 @@ void MockLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { if (fake_reads_surface_) { context->surface_needs_readback = true; } + if (fake_opacity_compatible_) { + context->subtree_can_inherit_opacity = true; + } } void MockLayer::Paint(PaintContext& context) const { FML_DCHECK(needs_painting(context)); + if (context.inherited_opacity < SK_Scalar1) { + SkPaint p; + p.setAlphaf(context.inherited_opacity); + context.leaf_nodes_canvas->saveLayer(fake_paint_path_.getBounds(), &p); + } context.leaf_nodes_canvas->drawPath(fake_paint_path_, fake_paint_); + if (context.inherited_opacity < SK_Scalar1) { + context.leaf_nodes_canvas->restore(); + } } } // namespace testing diff --git a/engine/src/flutter/flow/testing/mock_layer.h b/engine/src/flutter/flow/testing/mock_layer.h index 77a01854a0..54a555c8dc 100644 --- a/engine/src/flutter/flow/testing/mock_layer.h +++ b/engine/src/flutter/flow/testing/mock_layer.h @@ -19,7 +19,17 @@ class MockLayer : public Layer { explicit MockLayer(SkPath path, SkPaint paint = SkPaint(), bool fake_has_platform_view = false, - bool fake_reads_surface = false); + bool fake_reads_surface = false, + bool fake_opacity_compatible_ = false); + + static std::shared_ptr Make(SkPath path, + SkPaint paint = SkPaint()) { + return std::make_shared(path, paint, false, false, false); + } + + static std::shared_ptr MakeOpacityCompatible(SkPath path) { + return std::make_shared(path, SkPaint(), false, false, true); + } void Preroll(PrerollContext* context, const SkMatrix& matrix) override; void Paint(PaintContext& context) const override; @@ -42,6 +52,7 @@ class MockLayer : public Layer { bool parent_has_platform_view_ = false; bool fake_has_platform_view_ = false; bool fake_reads_surface_ = false; + bool fake_opacity_compatible_ = false; FML_DISALLOW_COPY_AND_ASSIGN(MockLayer); }; diff --git a/engine/src/flutter/flow/testing/mock_layer_unittests.cc b/engine/src/flutter/flow/testing/mock_layer_unittests.cc index 41bf971fe0..3323f76d07 100644 --- a/engine/src/flutter/flow/testing/mock_layer_unittests.cc +++ b/engine/src/flutter/flow/testing/mock_layer_unittests.cc @@ -78,5 +78,20 @@ TEST_F(MockLayerTest, SaveLayerOnLeafNodesCanvas) { EXPECT_EQ(preroll_context()->has_platform_view, true); } +TEST_F(MockLayerTest, OpacityInheritance) { + auto path1 = SkPath().addRect({10, 10, 30, 30}); + PrerollContext* context = preroll_context(); + + auto mock1 = std::make_shared(path1); + context->subtree_can_inherit_opacity = false; + mock1->Preroll(context, SkMatrix::I()); + EXPECT_FALSE(context->subtree_can_inherit_opacity); + + auto mock2 = MockLayer::MakeOpacityCompatible(path1); + context->subtree_can_inherit_opacity = false; + mock2->Preroll(context, SkMatrix::I()); + EXPECT_TRUE(context->subtree_can_inherit_opacity); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/flow/testing/mock_raster_cache.cc b/engine/src/flutter/flow/testing/mock_raster_cache.cc index c1669f7e51..dafa827461 100644 --- a/engine/src/flutter/flow/testing/mock_raster_cache.cc +++ b/engine/src/flutter/flow/testing/mock_raster_cache.cc @@ -25,6 +25,18 @@ std::unique_ptr MockRasterCache::RasterizePicture( return std::make_unique(cache_rect); } +std::unique_ptr MockRasterCache::RasterizeDisplayList( + DisplayList* display_list, + GrDirectContext* context, + const SkMatrix& ctm, + SkColorSpace* dst_color_space, + bool checkerboard) const { + SkRect logical_rect = display_list->bounds(); + SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm); + + return std::make_unique(cache_rect); +} + std::unique_ptr MockRasterCache::RasterizeLayer( PrerollContext* context, Layer* layer, diff --git a/engine/src/flutter/flow/testing/mock_raster_cache.h b/engine/src/flutter/flow/testing/mock_raster_cache.h index 47e884c1c1..1bd54ab62c 100644 --- a/engine/src/flutter/flow/testing/mock_raster_cache.h +++ b/engine/src/flutter/flow/testing/mock_raster_cache.h @@ -63,6 +63,13 @@ class MockRasterCache : public RasterCache { SkColorSpace* dst_color_space, bool checkerboard) const override; + std::unique_ptr RasterizeDisplayList( + DisplayList* display_list, + GrDirectContext* context, + const SkMatrix& ctm, + SkColorSpace* dst_color_space, + bool checkerboard) const override; + std::unique_ptr RasterizeLayer( PrerollContext* context, Layer* layer, diff --git a/engine/src/flutter/testing/display_list_testing.cc b/engine/src/flutter/testing/display_list_testing.cc index 39c2507533..3f026339cd 100644 --- a/engine/src/flutter/testing/display_list_testing.cc +++ b/engine/src/flutter/testing/display_list_testing.cc @@ -593,9 +593,9 @@ void DisplayListStreamDispatcher::saveLayer(const SkRect* bounds, indent(); } void DisplayListStreamDispatcher::restore() { - startl() << "restore();" << std::endl; outdent(); startl() << "}" << std::endl; + startl() << "restore();" << std::endl; } void DisplayListStreamDispatcher::translate(SkScalar tx, SkScalar ty) { @@ -651,7 +651,7 @@ void DisplayListStreamDispatcher::transformFullPerspective( startl() << ");" << std::endl; } void DisplayListStreamDispatcher::transformReset() { - startl() << "tranformReset();" << std::endl; + startl() << "transformReset();" << std::endl; } void DisplayListStreamDispatcher::clipRect(const SkRect& rect, SkClipOp clip_op, diff --git a/engine/src/flutter/testing/mock_canvas.cc b/engine/src/flutter/testing/mock_canvas.cc index 0132af7fe1..b53a01ca51 100644 --- a/engine/src/flutter/testing/mock_canvas.cc +++ b/engine/src/flutter/testing/mock_canvas.cc @@ -108,6 +108,21 @@ void MockCanvas::onDrawShadowRec(const SkPath& path, draw_calls_.emplace_back(DrawCall{current_layer_, DrawShadowData{path}}); } +void MockCanvas::onDrawImage2(const SkImage* image, + SkScalar x, + SkScalar y, + const SkSamplingOptions& options, + const SkPaint* paint) { + if (paint) { + draw_calls_.emplace_back( + DrawCall{current_layer_, + DrawImageData{sk_ref_sp(image), x, y, options, *paint}}); + } else { + draw_calls_.emplace_back(DrawCall{ + current_layer_, DrawImageDataNoPaint{sk_ref_sp(image), x, y, options}}); + } +} + void MockCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { @@ -220,14 +235,6 @@ void MockCanvas::onDrawRRect(const SkRRect&, const SkPaint&) { FML_DCHECK(false); } -void MockCanvas::onDrawImage2(const SkImage*, - SkScalar, - SkScalar, - const SkSamplingOptions&, - const SkPaint*) { - FML_DCHECK(false); -} - void MockCanvas::onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, @@ -357,6 +364,30 @@ std::ostream& operator<<(std::ostream& os, return os << data.serialized_text << " " << data.paint << " " << data.offset; } +bool operator==(const MockCanvas::DrawImageData& a, + const MockCanvas::DrawImageData& b) { + return a.image == b.image && a.x == b.x && a.y == b.y && + a.options == b.options && a.paint == b.paint; +} + +std::ostream& operator<<(std::ostream& os, + const MockCanvas::DrawImageData& data) { + return os << data.image << " " << data.x << " " << data.y << " " + << data.options << " " << data.paint; +} + +bool operator==(const MockCanvas::DrawImageDataNoPaint& a, + const MockCanvas::DrawImageDataNoPaint& b) { + return a.image == b.image && a.x == b.x && a.y == b.y && + a.options == b.options; +} + +std::ostream& operator<<(std::ostream& os, + const MockCanvas::DrawImageDataNoPaint& data) { + return os << data.image << " " << data.x << " " << data.y << " " + << data.options; +} + bool operator==(const MockCanvas::DrawPictureData& a, const MockCanvas::DrawPictureData& b) { return a.serialized_picture->equals(b.serialized_picture.get()) && diff --git a/engine/src/flutter/testing/mock_canvas.h b/engine/src/flutter/testing/mock_canvas.h index f31d5e385b..c09acf5d9e 100644 --- a/engine/src/flutter/testing/mock_canvas.h +++ b/engine/src/flutter/testing/mock_canvas.h @@ -15,6 +15,7 @@ #include "third_party/skia/include/core/SkCanvasVirtualEnforcer.h" #include "third_party/skia/include/core/SkClipOp.h" #include "third_party/skia/include/core/SkData.h" +#include "third_party/skia/include/core/SkImage.h" #include "third_party/skia/include/core/SkImageFilter.h" #include "third_party/skia/include/core/SkM44.h" #include "third_party/skia/include/core/SkPath.h" @@ -76,6 +77,21 @@ class MockCanvas : public SkCanvasVirtualEnforcer { SkPoint offset; }; + struct DrawImageDataNoPaint { + sk_sp image; + SkScalar x; + SkScalar y; + SkSamplingOptions options; + }; + + struct DrawImageData { + sk_sp image; + SkScalar x; + SkScalar y; + SkSamplingOptions options; + SkPaint paint; + }; + struct DrawPictureData { sk_sp serialized_picture; SkPaint paint; @@ -118,6 +134,8 @@ class MockCanvas : public SkCanvasVirtualEnforcer { DrawRectData, DrawPathData, DrawTextData, + DrawImageDataNoPaint, + DrawImageData, DrawPictureData, DrawShadowData, ClipRectData, @@ -268,6 +286,14 @@ extern bool operator==(const MockCanvas::DrawTextData& a, const MockCanvas::DrawTextData& b); extern std::ostream& operator<<(std::ostream& os, const MockCanvas::DrawTextData& data); +extern bool operator==(const MockCanvas::DrawImageData& a, + const MockCanvas::DrawImageData& b); +extern std::ostream& operator<<(std::ostream& os, + const MockCanvas::DrawImageData& data); +extern bool operator==(const MockCanvas::DrawImageDataNoPaint& a, + const MockCanvas::DrawImageDataNoPaint& b); +extern std::ostream& operator<<(std::ostream& os, + const MockCanvas::DrawImageDataNoPaint& data); extern bool operator==(const MockCanvas::DrawPictureData& a, const MockCanvas::DrawPictureData& b); extern std::ostream& operator<<(std::ostream& os,