From 5bf3bf043308f4e15f55d375e904528df56b7f82 Mon Sep 17 00:00:00 2001 From: xiaomiao Date: Fri, 2 Sep 2022 17:15:18 +0800 Subject: [PATCH] Introduce bitset to manage bool flags in mock_layer unit test. (#35489) --- .../layers/backdrop_filter_layer_unittests.cc | 4 +- flow/layers/clip_path_layer_unittests.cc | 3 +- flow/layers/clip_rect_layer_unittests.cc | 3 +- flow/layers/clip_rrect_layer_unittests.cc | 3 +- flow/layers/color_filter_layer_unittests.cc | 4 +- flow/layers/container_layer_unittests.cc | 13 ++- flow/layers/image_filter_layer_unittests.cc | 4 +- flow/layers/layer_tree_unittests.cc | 7 +- flow/layers/opacity_layer_unittests.cc | 4 +- flow/layers/physical_shape_layer_unittests.cc | 3 +- flow/layers/shader_mask_layer_unittests.cc | 4 +- flow/testing/mock_layer.cc | 27 ++---- flow/testing/mock_layer.h | 95 ++++++++++++++----- flow/testing/mock_layer_unittests.cc | 36 ++++++- 14 files changed, 138 insertions(+), 72 deletions(-) diff --git a/flow/layers/backdrop_filter_layer_unittests.cc b/flow/layers/backdrop_filter_layer_unittests.cc index 16cf3bc242232..49a214bd76e73 100644 --- a/flow/layers/backdrop_filter_layer_unittests.cc +++ b/flow/layers/backdrop_filter_layer_unittests.cc @@ -269,8 +269,8 @@ TEST_F(BackdropFilterLayerTest, Readback) { EXPECT_TRUE(preroll_context()->surface_needs_readback); // BDF with no filter blocks child with readback - auto mock_layer = - std::make_shared(SkPath(), SkPaint(), false, true); + auto mock_layer = std::make_shared(SkPath(), SkPaint()); + mock_layer->set_fake_reads_surface(true); layer2->Add(mock_layer); preroll_context()->surface_needs_readback = false; layer2->Preroll(preroll_context(), initial_transform); diff --git a/flow/layers/clip_path_layer_unittests.cc b/flow/layers/clip_path_layer_unittests.cc index f65af652ce5f5..07ce12948a525 100644 --- a/flow/layers/clip_path_layer_unittests.cc +++ b/flow/layers/clip_path_layer_unittests.cc @@ -233,7 +233,8 @@ TEST_F(ClipPathLayerTest, Readback) { const Clip save_layer = Clip::antiAliasWithSaveLayer; std::shared_ptr nochild; - auto reader = std::make_shared(path, paint, false, true); + auto reader = std::make_shared(path, paint); + reader->set_fake_reads_surface(true); auto nonreader = std::make_shared(path, paint); // No children, no prior readback -> no readback after diff --git a/flow/layers/clip_rect_layer_unittests.cc b/flow/layers/clip_rect_layer_unittests.cc index 35ac1fb46f469..28acdaef63fac 100644 --- a/flow/layers/clip_rect_layer_unittests.cc +++ b/flow/layers/clip_rect_layer_unittests.cc @@ -228,7 +228,8 @@ TEST_F(ClipRectLayerTest, Readback) { const Clip save_layer = Clip::antiAliasWithSaveLayer; std::shared_ptr nochild; - auto reader = std::make_shared(path, paint, false, true); + auto reader = std::make_shared(path, paint); + reader->set_fake_reads_surface(true); auto nonreader = std::make_shared(path, paint); // No children, no prior readback -> no readback after diff --git a/flow/layers/clip_rrect_layer_unittests.cc b/flow/layers/clip_rrect_layer_unittests.cc index 3f47f130ac500..2c377ebedfd2c 100644 --- a/flow/layers/clip_rrect_layer_unittests.cc +++ b/flow/layers/clip_rrect_layer_unittests.cc @@ -233,7 +233,8 @@ TEST_F(ClipRRectLayerTest, Readback) { const Clip save_layer = Clip::antiAliasWithSaveLayer; std::shared_ptr nochild; - auto reader = std::make_shared(path, paint, false, true); + auto reader = std::make_shared(path, paint); + reader->set_fake_reads_surface(true); auto nonreader = std::make_shared(path, paint); // No children, no prior readback -> no readback after diff --git a/flow/layers/color_filter_layer_unittests.cc b/flow/layers/color_filter_layer_unittests.cc index 63ac582c5b527..e45604a45b939 100644 --- a/flow/layers/color_filter_layer_unittests.cc +++ b/flow/layers/color_filter_layer_unittests.cc @@ -248,8 +248,8 @@ TEST_F(ColorFilterLayerTest, Readback) { EXPECT_FALSE(preroll_context()->surface_needs_readback); // ColorFilterLayer blocks child with readback - auto mock_layer = - std::make_shared(SkPath(), SkPaint(), false, true); + auto mock_layer = std::make_shared(SkPath(), SkPaint()); + mock_layer->set_fake_reads_surface(true); layer->Add(mock_layer); preroll_context()->surface_needs_readback = false; layer->Preroll(preroll_context(), initial_transform); diff --git a/flow/layers/container_layer_unittests.cc b/flow/layers/container_layer_unittests.cc index f1c6df8cdf203..b2b4adc936ccd 100644 --- a/flow/layers/container_layer_unittests.cc +++ b/flow/layers/container_layer_unittests.cc @@ -71,9 +71,8 @@ TEST_F(ContainerLayerTest, LayerWithParentHasTextureLayerNeedsResetFlag) { SkPaint child_paint1(SkColors::kGray); SkPaint child_paint2(SkColors::kGreen); - auto mock_layer1 = std::make_shared( - child_path1, child_paint1, false /* fake_has_platform_view */, false, - false, true /* fake_has_texture_layer */); + auto mock_layer1 = std::make_shared(child_path1, child_paint1); + mock_layer1->set_fake_has_texture_layer(true); auto mock_layer2 = std::make_shared(child_path2, child_paint2); auto root = std::make_shared(); @@ -126,8 +125,8 @@ TEST_F(ContainerLayerTest, Multiple) { SkPaint child_paint2(SkColors::kGreen); SkMatrix initial_transform = SkMatrix::Translate(-0.5f, -0.5f); - auto mock_layer1 = std::make_shared( - child_path1, child_paint1, true /* fake_has_platform_view */); + auto mock_layer1 = std::make_shared(child_path1, child_paint1); + mock_layer1->set_fake_has_platform_view(true); auto mock_layer2 = std::make_shared(child_path2, child_paint2); auto layer = std::make_shared(); layer->Add(mock_layer1); @@ -201,8 +200,8 @@ TEST_F(ContainerLayerTest, NeedsSystemComposite) { SkPaint child_paint2(SkColors::kGreen); SkMatrix initial_transform = SkMatrix::Translate(-0.5f, -0.5f); - auto mock_layer1 = std::make_shared( - child_path1, child_paint1, false /* fake_has_platform_view */); + auto mock_layer1 = std::make_shared(child_path1, child_paint1); + mock_layer1->set_fake_has_platform_view(false); auto mock_layer2 = std::make_shared(child_path2, child_paint2); auto layer = std::make_shared(); layer->Add(mock_layer1); diff --git a/flow/layers/image_filter_layer_unittests.cc b/flow/layers/image_filter_layer_unittests.cc index 2ad4a7e3e86d6..dc855affc15fe 100644 --- a/flow/layers/image_filter_layer_unittests.cc +++ b/flow/layers/image_filter_layer_unittests.cc @@ -293,8 +293,8 @@ TEST_F(ImageFilterLayerTest, Readback) { EXPECT_FALSE(preroll_context()->surface_needs_readback); // ImageFilterLayer blocks child with readback - auto mock_layer = - std::make_shared(SkPath(), SkPaint(), false, true); + auto mock_layer = std::make_shared(SkPath(), SkPaint()); + mock_layer->set_fake_reads_surface(true); layer->Add(mock_layer); preroll_context()->surface_needs_readback = false; layer->Preroll(preroll_context(), initial_transform); diff --git a/flow/layers/layer_tree_unittests.cc b/flow/layers/layer_tree_unittests.cc index 9eeac5a1f77cd..950e542c3a685 100644 --- a/flow/layers/layer_tree_unittests.cc +++ b/flow/layers/layer_tree_unittests.cc @@ -97,8 +97,8 @@ TEST_F(LayerTreeTest, Multiple) { const SkPath child_path2 = SkPath().addRect(8.0f, 2.0f, 16.5f, 14.5f); const SkPaint child_paint1(SkColors::kGray); const SkPaint child_paint2(SkColors::kGreen); - auto mock_layer1 = std::make_shared( - child_path1, child_paint1, true /* fake_has_platform_view */); + auto mock_layer1 = std::make_shared(child_path1, child_paint1); + mock_layer1->set_fake_has_platform_view(true); auto mock_layer2 = std::make_shared(child_path2, child_paint2); auto layer = std::make_shared(); layer->Add(mock_layer1); @@ -163,8 +163,7 @@ TEST_F(LayerTreeTest, NeedsSystemComposite) { const SkPath child_path2 = SkPath().addRect(8.0f, 2.0f, 16.5f, 14.5f); const SkPaint child_paint1(SkColors::kGray); const SkPaint child_paint2(SkColors::kGreen); - auto mock_layer1 = std::make_shared( - child_path1, child_paint1, false /* fake_has_platform_view */); + auto mock_layer1 = std::make_shared(child_path1, child_paint1); auto mock_layer2 = std::make_shared(child_path2, child_paint2); auto layer = std::make_shared(); layer->Add(mock_layer1); diff --git a/flow/layers/opacity_layer_unittests.cc b/flow/layers/opacity_layer_unittests.cc index 6844b9bc50682..07da4e1717ab8 100644 --- a/flow/layers/opacity_layer_unittests.cc +++ b/flow/layers/opacity_layer_unittests.cc @@ -438,8 +438,8 @@ TEST_F(OpacityLayerTest, Readback) { EXPECT_FALSE(preroll_context()->surface_needs_readback); // OpacityLayer blocks child with readback - auto mock_layer = - std::make_shared(SkPath(), SkPaint(), false, true); + auto mock_layer = std::make_shared(SkPath(), SkPaint()); + mock_layer->set_fake_reads_surface(true); layer->Add(mock_layer); preroll_context()->surface_needs_readback = false; layer->Preroll(preroll_context(), initial_transform); diff --git a/flow/layers/physical_shape_layer_unittests.cc b/flow/layers/physical_shape_layer_unittests.cc index 3e41506ea4fad..ae9fb29d22cd3 100644 --- a/flow/layers/physical_shape_layer_unittests.cc +++ b/flow/layers/physical_shape_layer_unittests.cc @@ -382,7 +382,8 @@ TEST_F(PhysicalShapeLayerTest, Readback) { const Clip save_layer = Clip::antiAliasWithSaveLayer; std::shared_ptr nochild; - auto reader = std::make_shared(path, paint, false, true); + auto reader = std::make_shared(path, paint); + reader->set_fake_reads_surface(true); auto nonreader = std::make_shared(path, paint); // No children, no prior readback -> no readback after diff --git a/flow/layers/shader_mask_layer_unittests.cc b/flow/layers/shader_mask_layer_unittests.cc index b6412f2497217..86912c04902e1 100644 --- a/flow/layers/shader_mask_layer_unittests.cc +++ b/flow/layers/shader_mask_layer_unittests.cc @@ -283,8 +283,8 @@ TEST_F(ShaderMaskLayerTest, Readback) { EXPECT_FALSE(preroll_context()->surface_needs_readback); // ShaderMaskLayer blocks child with readback - auto mock_layer = - std::make_shared(SkPath(), SkPaint(), false, true); + auto mock_layer = std::make_shared(SkPath(), SkPaint()); + mock_layer->set_fake_reads_surface(true); layer->Add(mock_layer); preroll_context()->surface_needs_readback = false; layer->Preroll(preroll_context(), initial_transform); diff --git a/flow/testing/mock_layer.cc b/flow/testing/mock_layer.cc index 618a1d566798f..42eb85524fea3 100644 --- a/flow/testing/mock_layer.cc +++ b/flow/testing/mock_layer.cc @@ -10,18 +10,8 @@ namespace flutter { namespace testing { -MockLayer::MockLayer(SkPath path, - SkPaint paint, - bool fake_has_platform_view, - bool fake_reads_surface, - bool fake_opacity_compatible, - bool fake_has_texture_layer) - : fake_paint_path_(path), - fake_paint_(paint), - fake_has_platform_view_(fake_has_platform_view), - fake_reads_surface_(fake_reads_surface), - fake_opacity_compatible_(fake_opacity_compatible), - fake_has_texture_layer_(fake_has_texture_layer) {} +MockLayer::MockLayer(SkPath path, SkPaint paint) + : fake_paint_path_(path), fake_paint_(paint) {} bool MockLayer::IsReplacing(DiffContext* context, const Layer* layer) const { // Similar to PictureLayer, only return true for identical mock layers; @@ -42,16 +32,17 @@ void MockLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) { parent_mutators_ = context->mutators_stack; parent_matrix_ = matrix; parent_cull_rect_ = context->cull_rect; - parent_has_platform_view_ = context->has_platform_view; - parent_has_texture_layer_ = context->has_texture_layer; - context->has_platform_view = fake_has_platform_view_; - context->has_texture_layer = fake_has_texture_layer_; + set_parent_has_platform_view(context->has_platform_view); + set_parent_has_texture_layer(context->has_texture_layer); + + context->has_platform_view = fake_has_platform_view(); + context->has_texture_layer = fake_has_texture_layer(); set_paint_bounds(fake_paint_path_.getBounds()); - if (fake_reads_surface_) { + if (fake_reads_surface()) { context->surface_needs_readback = true; } - if (fake_opacity_compatible_) { + if (fake_opacity_compatible()) { context->subtree_can_inherit_opacity = true; } } diff --git a/flow/testing/mock_layer.h b/flow/testing/mock_layer.h index ad6bc2cc12b1e..20fc73e4e8341 100644 --- a/flow/testing/mock_layer.h +++ b/flow/testing/mock_layer.h @@ -24,20 +24,17 @@ namespace testing { // verify the data against expected values. class MockLayer : public Layer { public: - explicit MockLayer(SkPath path, - SkPaint paint = SkPaint(), - bool fake_has_platform_view = false, - bool fake_reads_surface = false, - bool fake_opacity_compatible_ = false, - bool fake_has_texture_layer = false); + explicit MockLayer(SkPath path, SkPaint paint = SkPaint()); static std::shared_ptr Make(SkPath path, SkPaint paint = SkPaint()) { - return std::make_shared(path, paint, false, false, false); + return std::make_shared(path, paint); } static std::shared_ptr MakeOpacityCompatible(SkPath path) { - return std::make_shared(path, SkPaint(), false, false, true); + auto mock_layer = std::make_shared(path, SkPaint()); + mock_layer->set_fake_opacity_compatible(true); + return mock_layer; } void Preroll(PrerollContext* context, const SkMatrix& matrix) override; @@ -46,25 +43,80 @@ class MockLayer : public Layer { const MutatorsStack& parent_mutators() { return parent_mutators_; } const SkMatrix& parent_matrix() { return parent_matrix_; } const SkRect& parent_cull_rect() { return parent_cull_rect_; } - bool parent_has_platform_view() { return parent_has_platform_view_; } - bool parent_has_texture_layer() { return parent_has_texture_layer_; } bool IsReplacing(DiffContext* context, const Layer* layer) const override; void Diff(DiffContext* context, const Layer* old_layer) override; const MockLayer* as_mock_layer() const override { return this; } + bool parent_has_platform_view() { + return mock_flags_ & kParentHasPlatformView; + } + + bool parent_has_texture_layer() { + return mock_flags_ & kParentHasTextureLayer; + } + + bool fake_has_platform_view() { return mock_flags_ & kFakeHasPlatformView; } + + bool fake_reads_surface() { return mock_flags_ & kFakeReadsSurface; } + + bool fake_opacity_compatible() { + return mock_flags_ & kFakeOpacityCompatible; + } + + bool fake_has_texture_layer() { return mock_flags_ & kFakeHasTextureLayer; } + + MockLayer& set_parent_has_platform_view(bool flag) { + flag ? (mock_flags_ |= kParentHasPlatformView) + : (mock_flags_ &= ~(kParentHasPlatformView)); + return *this; + } + + MockLayer& set_parent_has_texture_layer(bool flag) { + flag ? (mock_flags_ |= kParentHasTextureLayer) + : (mock_flags_ &= ~(kParentHasTextureLayer)); + return *this; + } + + MockLayer& set_fake_has_platform_view(bool flag) { + flag ? (mock_flags_ |= kFakeHasPlatformView) + : (mock_flags_ &= ~(kFakeHasPlatformView)); + return *this; + } + + MockLayer& set_fake_reads_surface(bool flag) { + flag ? (mock_flags_ |= kFakeReadsSurface) + : (mock_flags_ &= ~(kFakeReadsSurface)); + return *this; + } + + MockLayer& set_fake_opacity_compatible(bool flag) { + flag ? (mock_flags_ |= kFakeOpacityCompatible) + : (mock_flags_ &= ~(kFakeOpacityCompatible)); + return *this; + } + + MockLayer& set_fake_has_texture_layer(bool flag) { + flag ? (mock_flags_ |= kFakeHasTextureLayer) + : (mock_flags_ &= ~(kFakeHasTextureLayer)); + return *this; + } + private: MutatorsStack parent_mutators_; SkMatrix parent_matrix_; SkRect parent_cull_rect_ = SkRect::MakeEmpty(); SkPath fake_paint_path_; SkPaint fake_paint_; - bool parent_has_platform_view_ = false; - bool parent_has_texture_layer_ = false; - bool fake_has_platform_view_ = false; - bool fake_reads_surface_ = false; - bool fake_opacity_compatible_ = false; - bool fake_has_texture_layer_ = false; + + static constexpr int kParentHasPlatformView = 1 << 0; + static constexpr int kParentHasTextureLayer = 1 << 1; + static constexpr int kFakeHasPlatformView = 1 << 2; + static constexpr int kFakeReadsSurface = 1 << 3; + static constexpr int kFakeOpacityCompatible = 1 << 4; + static constexpr int kFakeHasTextureLayer = 1 << 5; + + int mock_flags_ = 0; FML_DISALLOW_COPY_AND_ASSIGN(MockLayer); }; @@ -97,15 +149,8 @@ class MockCacheableLayer : public MockLayer { public: explicit MockCacheableLayer(SkPath path, SkPaint paint = SkPaint(), - int render_limit = 3, - bool fake_has_platform_view = false, - bool fake_reads_surface = false, - bool fake_opacity_compatible = false) - : MockLayer(path, - paint, - fake_has_platform_view, - fake_reads_surface, - fake_opacity_compatible) { + int render_limit = 3) + : MockLayer(path, paint) { raster_cache_item_ = std::make_unique(this, render_limit); } diff --git a/flow/testing/mock_layer_unittests.cc b/flow/testing/mock_layer_unittests.cc index 3323f76d07618..d7f007eed25db 100644 --- a/flow/testing/mock_layer_unittests.cc +++ b/flow/testing/mock_layer_unittests.cc @@ -61,8 +61,8 @@ TEST_F(MockLayerTest, SimpleParams) { } TEST_F(MockLayerTest, FakePlatformView) { - auto layer = std::make_shared(SkPath(), SkPaint(), - true /* fake_has_platform_view */); + auto layer = std::make_shared(SkPath(), SkPaint()); + layer->set_fake_has_platform_view(true); EXPECT_EQ(preroll_context()->has_platform_view, false); layer->Preroll(preroll_context(), SkMatrix()); @@ -70,8 +70,8 @@ TEST_F(MockLayerTest, FakePlatformView) { } TEST_F(MockLayerTest, SaveLayerOnLeafNodesCanvas) { - auto layer = std::make_shared(SkPath(), SkPaint(), - true /* fake_has_platform_view */); + auto layer = std::make_shared(SkPath(), SkPaint()); + layer->set_fake_has_platform_view(true); EXPECT_EQ(preroll_context()->has_platform_view, false); layer->Preroll(preroll_context(), SkMatrix()); @@ -93,5 +93,33 @@ TEST_F(MockLayerTest, OpacityInheritance) { EXPECT_TRUE(context->subtree_can_inherit_opacity); } +TEST_F(MockLayerTest, FlagGetSet) { + auto mock_layer = std::make_shared(SkPath()); + + EXPECT_EQ(mock_layer->parent_has_platform_view(), false); + mock_layer->set_parent_has_platform_view(true); + EXPECT_EQ(mock_layer->parent_has_platform_view(), true); + + EXPECT_EQ(mock_layer->parent_has_texture_layer(), false); + mock_layer->set_parent_has_texture_layer(true); + EXPECT_EQ(mock_layer->parent_has_texture_layer(), true); + + EXPECT_EQ(mock_layer->fake_has_platform_view(), false); + mock_layer->set_fake_has_platform_view(true); + EXPECT_EQ(mock_layer->fake_has_platform_view(), true); + + EXPECT_EQ(mock_layer->fake_reads_surface(), false); + mock_layer->set_fake_reads_surface(true); + EXPECT_EQ(mock_layer->fake_reads_surface(), true); + + EXPECT_EQ(mock_layer->fake_opacity_compatible(), false); + mock_layer->set_fake_opacity_compatible(true); + EXPECT_EQ(mock_layer->fake_opacity_compatible(), true); + + EXPECT_EQ(mock_layer->fake_has_texture_layer(), false); + mock_layer->set_fake_has_texture_layer(true); + EXPECT_EQ(mock_layer->fake_has_texture_layer(), true); +} + } // namespace testing } // namespace flutter