Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added more tests
  • Loading branch information
gaaclarke committed May 16, 2024
commit a63c263473039f721b643a7150a4fc62337544a6
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,43 @@
#include "flutter/testing/testing.h"
#include "gmock/gmock.h"
#include "impeller/entity/contents/filters/matrix_filter_contents.h"
#include "impeller/entity/entity_playground.h"
#include "impeller/geometry/geometry_asserts.h"

namespace impeller {
namespace testing {

class MatrixFilterContentsTest : public EntityPlayground {
public:
/// Create a texture that has been cleared to transparent black.
std::shared_ptr<Texture> MakeTexture(ISize size) {
std::shared_ptr<CommandBuffer> command_buffer =
GetContentContext()->GetContext()->CreateCommandBuffer();
if (!command_buffer) {
return nullptr;
}

auto render_target = GetContentContext()->MakeSubpass(
"Clear Subpass", size, command_buffer,
[](const ContentContext&, RenderPass&) { return true; });

if (!GetContentContext()
->GetContext()
->GetCommandQueue()
->Submit(/*buffers=*/{command_buffer})
.ok()) {
return nullptr;
}

if (render_target.ok()) {
return render_target.value().GetRenderTargetTexture();
}
return nullptr;
}
};

INSTANTIATE_PLAYGROUND_SUITE(MatrixFilterContentsTest);

TEST(MatrixFilterContentsTest, Create) {
MatrixFilterContents contents;
EXPECT_TRUE(contents.IsTranslationOnly());
Expand Down Expand Up @@ -56,5 +90,60 @@ TEST(MatrixFilterContentsTest, Coverage2xEffect) {
ASSERT_EQ(coverage, Rect::MakeXYWH(10, 10, 100, 100));
}

TEST_P(MatrixFilterContentsTest, RenderCoverageMatchesGetCoverageIdentity) {
std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
MatrixFilterContents contents;
contents.SetInputs({FilterInput::Make(texture)});

Entity entity;
entity.SetTransform(Matrix::MakeTranslation({100, 200, 0}));

std::shared_ptr<ContentContext> renderer = GetContentContext();
std::optional<Entity> result =
contents.GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents.GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_TRUE(RectNear(contents_coverage.value(),
Rect::MakeXYWH(100, 200, 100, 100)));
EXPECT_TRUE(RectNear(result_coverage.value(),
Rect::MakeXYWH(100, 200, 100, 100)));
}
}
}

TEST_P(MatrixFilterContentsTest, RenderCoverageMatchesGetCoverageTranslate) {
std::shared_ptr<Texture> texture = MakeTexture(ISize(100, 100));
MatrixFilterContents contents;
contents.SetInputs({FilterInput::Make(texture)});
contents.SetMatrix(Matrix::MakeTranslation({50, 100, 0}));

Entity entity;
entity.SetTransform(Matrix::MakeTranslation({100, 200, 0}));

std::shared_ptr<ContentContext> renderer = GetContentContext();
std::optional<Entity> result =
contents.GetEntity(*renderer, entity, /*coverage_hint=*/{});
EXPECT_TRUE(result.has_value());
if (result.has_value()) {
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
std::optional<Rect> result_coverage = result.value().GetCoverage();
std::optional<Rect> contents_coverage = contents.GetCoverage(entity);
EXPECT_TRUE(result_coverage.has_value());
EXPECT_TRUE(contents_coverage.has_value());
if (result_coverage.has_value() && contents_coverage.has_value()) {
EXPECT_TRUE(RectNear(contents_coverage.value(),
Rect::MakeXYWH(150, 300, 100, 100)));
EXPECT_TRUE(RectNear(result_coverage.value(),
Rect::MakeXYWH(150, 300, 100, 100)));
}
}
}

} // namespace testing
} // namespace impeller