Introduce bitset to manage bool flags in mock_layer unit test. (flutter/engine#35489)

This commit is contained in:
xiaomiao
2022-09-02 17:15:18 +08:00
committed by GitHub
parent e8c513c3e3
commit 9439f622fd
14 changed files with 138 additions and 72 deletions

View File

@@ -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<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(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);

View File

@@ -233,7 +233,8 @@ TEST_F(ClipPathLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;
std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);
// No children, no prior readback -> no readback after

View File

@@ -228,7 +228,8 @@ TEST_F(ClipRectLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;
std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);
// No children, no prior readback -> no readback after

View File

@@ -233,7 +233,8 @@ TEST_F(ClipRRectLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;
std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);
// No children, no prior readback -> no readback after

View File

@@ -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<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(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);

View File

@@ -71,9 +71,8 @@ TEST_F(ContainerLayerTest, LayerWithParentHasTextureLayerNeedsResetFlag) {
SkPaint child_paint1(SkColors::kGray);
SkPaint child_paint2(SkColors::kGreen);
auto mock_layer1 = std::make_shared<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */, false,
false, true /* fake_has_texture_layer */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_texture_layer(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto root = std::make_shared<ContainerLayer>();
@@ -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<MockLayer>(
child_path1, child_paint1, true /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
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<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(false);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);

View File

@@ -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<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(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);

View File

@@ -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<MockLayer>(
child_path1, child_paint1, true /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
mock_layer1->set_fake_has_platform_view(true);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
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<MockLayer>(
child_path1, child_paint1, false /* fake_has_platform_view */);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ContainerLayer>();
layer->Add(mock_layer1);

View File

@@ -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<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(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);

View File

@@ -382,7 +382,8 @@ TEST_F(PhysicalShapeLayerTest, Readback) {
const Clip save_layer = Clip::antiAliasWithSaveLayer;
std::shared_ptr<MockLayer> nochild;
auto reader = std::make_shared<MockLayer>(path, paint, false, true);
auto reader = std::make_shared<MockLayer>(path, paint);
reader->set_fake_reads_surface(true);
auto nonreader = std::make_shared<MockLayer>(path, paint);
// No children, no prior readback -> no readback after

View File

@@ -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<MockLayer>(SkPath(), SkPaint(), false, true);
auto mock_layer = std::make_shared<MockLayer>(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);

View File

@@ -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;
}
}

View File

@@ -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<MockLayer> Make(SkPath path,
SkPaint paint = SkPaint()) {
return std::make_shared<MockLayer>(path, paint, false, false, false);
return std::make_shared<MockLayer>(path, paint);
}
static std::shared_ptr<MockLayer> MakeOpacityCompatible(SkPath path) {
return std::make_shared<MockLayer>(path, SkPaint(), false, false, true);
auto mock_layer = std::make_shared<MockLayer>(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<MockLayerCacheableItem>(this, render_limit);
}

View File

@@ -61,8 +61,8 @@ TEST_F(MockLayerTest, SimpleParams) {
}
TEST_F(MockLayerTest, FakePlatformView) {
auto layer = std::make_shared<MockLayer>(SkPath(), SkPaint(),
true /* fake_has_platform_view */);
auto layer = std::make_shared<MockLayer>(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<MockLayer>(SkPath(), SkPaint(),
true /* fake_has_platform_view */);
auto layer = std::make_shared<MockLayer>(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<MockLayer>(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